Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
appendNode(ast, "hello");
// $ExpectType Node[] || AST
appendNode(getNodesByName(ast, "div"), "test");
// $ExpectType Node[] || AST
appendNodes(ast, [["strong", [], ["div", ["pre", [], []]]], "test"]);
// $ExpectType Node[] || AST
prependNode(getNodesByName(ast, "div"), "test");
// $ExpectType Node[] || AST
prependNodes(ast, [["strong", [], ["div", ["pre", [], []]]], "test"]);
// $ExpectType TreeNode
createNode("div", { prop1: "prop1", prop2: ["expression", "x=2"] }, []);
// $ExpectType Node[]
getChildren(ast[0]);
// $ExpectType void
walkNodes(ast, (n: Node) => {
(n as TreeNode)[0] = "funky-name";
});
// $ExpectType Node[]
findNodes(ast, n => n instanceof Array);
// $ExpectType Node
modifyChildren(ast[0], (n: Node) => {
if (typeof n === "object") {
n[0] = "somename";
function seperateTextAndHyperLink(textnode, hyperlinks) {
let match;
let hyperLinkIndex = 0;
let substringIndex = 0;
let newChildNodes = [];
while(hyperLinkIndex < hyperlinks.length) {
let regexURL = new RegExp(hyperlinks[hyperLinkIndex], "g");
match = regexURL.exec(textnode.substring(substringIndex));
if(match) {
let linkEndIndex = regexURL.lastIndex;
let linkStartIndex = linkEndIndex - hyperlinks[hyperLinkIndex].length;
let textNodeValue = textnode.substring(substringIndex, linkStartIndex);
if(textNodeValue !== "") {
newChildNodes.push(createTextNode(textnode.substring(substringIndex, linkStartIndex)));
}
let anchorElement = createNode("a", [], [hyperlinks[hyperLinkIndex]]);
setProperty(anchorElement, "href", hyperlinks[hyperLinkIndex]);
newChildNodes.push(anchorElement);
textnode = textnode.substring(linkEndIndex);
substringIndex = 0;
}
hyperLinkIndex++;
}
if(textnode != "") {
newChildNodes.push(createTextNode(textnode));
}
return createNode("span", [], newChildNodes);
}
let textNodeValue = textnode.substring(substringIndex, linkStartIndex);
if(textNodeValue !== "") {
newChildNodes.push(createTextNode(textnode.substring(substringIndex, linkStartIndex)));
}
let anchorElement = createNode("a", [], [hyperlinks[hyperLinkIndex]]);
setProperty(anchorElement, "href", hyperlinks[hyperLinkIndex]);
newChildNodes.push(anchorElement);
textnode = textnode.substring(linkEndIndex);
substringIndex = 0;
}
hyperLinkIndex++;
}
if(textnode != "") {
newChildNodes.push(createTextNode(textnode));
}
return createNode("span", [], newChildNodes);
}