Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function highlightTextNode(node, start, end, type) {
const literal = node.literal;
node.literal = literal.substring(start, end);
// Start by wrapping the node and then re-insert any non-highlighted code around it
const highlighted = new Node(type);
wrapNode(highlighted, node);
if (start !== 0) {
const before = new Node('text');
before.literal = literal.substring(0, start);
highlighted.insertBefore(before);
}
if (end !== literal.length) {
const after = new Node('text');
after.literal = literal.substring(end);
highlighted.insertAfter(after);
}
return highlighted;
}
sourceposPre = undefined;
sourceposLink = undefined;
sourceposPost = undefined;
if (node.sourcepos) {
var startLine = node.sourcepos[0][0];
var startCol = node.sourcepos[0][1];
var matchStart = startCol+match.index;
var matchEnd = matchStart+match[0].length;
sourceposPre = [node.sourcepos[0], [startLine, matchStart]];
sourceposLink = [[startLine, matchStart], [startLine, matchEnd]];
sourceposPost = [[startLine, matchEnd], node.sourcepos[1]];
}
var id = match[2].trim();
var pre = new commonmark.Node('Text', sourceposPre);
pre.literal = node.literal.slice(0, match.index);
var link = new commonmark.Node('Html', sourceposLink);
// Note: using server-side rendering here
var crossLink = CrossLinkComponent.render({ node: { id: id }, children: [id]});
link.literal = crossLink.outerHTML;
var post = new commonmark.Node('Text', sourceposPost);
post.literal = node.literal.slice(match.index+match[0].length);
node.insertBefore(pre);
node.insertBefore(link);
node.insertBefore(post);
node.unlink();
// iterating to find all matches
node = post;
match = re.exec(post.literal);
}
}
}
}
}
// The AST walker gets into a broken state if you unlink a node
// that has children before those children have been visited. We
// only unlink when leaving a node, or when entering a node that
// has no children.
if (!step.entering || node.firstChild == null) {
node.unlink();
continue;
}
}
switch (type) {
case "text":
break;
case "softbreak": {
const space = new Node("text", node.sourcepos);
space.literal = " ";
node.insertBefore(space);
node.unlink();
break;
}
case "linebreak":
break;
case "emph":
case "strong":
case "link":
case "image":
if (!step.entering) {
// Splice out the node.
while (node.firstChild) {
node.insertBefore(node.firstChild);
}
function copyNodeWithoutNeighbors(node) {
// commonmark uses classes so it takes a bit of work to copy them
const copy = new Node();
for (const key in node) {
if (!node.hasOwnProperty(key)) {
continue;
}
copy[key] = node[key];
}
copy._parent = null;
copy._firstChild = null;
copy._lastChild = null;
copy._prev = null;
copy._next = null;
// Deep copy list data since it's an object
const literal = node.literal;
node.literal = literal.substring(start, end);
// Start by wrapping the node and then re-insert any non-highlighted code around it
const highlighted = new Node(type);
wrapNode(highlighted, node);
if (start !== 0) {
const before = new Node('text');
before.literal = literal.substring(0, start);
highlighted.insertBefore(before);
}
if (end !== literal.length) {
const after = new Node('text');
after.literal = literal.substring(end);
highlighted.insertAfter(after);
}
return highlighted;
}
var match = re.exec(node.literal);
while (match) {
sourceposPre = undefined;
sourceposLink = undefined;
sourceposPost = undefined;
if (node.sourcepos) {
var startLine = node.sourcepos[0][0];
var startCol = node.sourcepos[0][1];
var matchStart = startCol+match.index;
var matchEnd = matchStart+match[0].length;
sourceposPre = [node.sourcepos[0], [startLine, matchStart]];
sourceposLink = [[startLine, matchStart], [startLine, matchEnd]];
sourceposPost = [[startLine, matchEnd], node.sourcepos[1]];
}
var id = match[2].trim();
var pre = new commonmark.Node('Text', sourceposPre);
pre.literal = node.literal.slice(0, match.index);
var link = new commonmark.Node('Html', sourceposLink);
// Note: using server-side rendering here
var crossLink = CrossLinkComponent.render({ node: { id: id }, children: [id]});
link.literal = crossLink.outerHTML;
var post = new commonmark.Node('Text', sourceposPost);
post.literal = node.literal.slice(match.index+match[0].length);
node.insertBefore(pre);
node.insertBefore(link);
node.insertBefore(post);
node.unlink();
// iterating to find all matches
node = post;
match = re.exec(post.literal);
}
while ((event = walker.next())) {
node = event.node;
if (node.type === 'CodeBlock') {
var info = node.info ? node.info.split(/\s+/) : [];
var lang = info[0];
var highlighted;
var classes = ['hljs'];
if (lang) {
highlighted = highlightjs.highlight(lang, node.literal).value;
classes.push('lang-'+lang);
} else {
highlighted = highlightjs.highlightAuto(node.literal).value;
}
var htmlBlock = new commonmark.Node('HtmlBlock', node.sourcepos);
htmlBlock.literal = ['<pre>', '<code class="'+classes.join(' ')+'">', highlighted, '</code>', '</pre>'].join('');
node.insertBefore(htmlBlock);
node.unlink();
}
}
}
function makeLinksExternal(parsed) {
var walker = parsed.walker();
var event, node;
while ((event = walker.next())) {
node = event.node;
if (event.entering && node.type === 'Link') {
var href = node.destination;
var text = href;
if (node.firstChild) {
text = node.firstChild.literal;
}
var el = new commonmark.Node('Html');
el.literal = ['<a href="', href, '">', text, '</a>'].join('');
node.insertBefore(el);
node.unlink();
}
}
}