How to use the commonmark.Node function in commonmark

To help you get started, we’ve selected a few commonmark examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mattermost / mattermost-mobile / app / components / markdown / transform.js View on Github external
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;
}
github substance / substance / doc / generator / markdownConverter.js View on Github external
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);
      }
    }
  }
github sourcecred / sourcecred / src / plugins / github / parseMarkdown.js View on Github external
}
      }
      // 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);
          }
github mattermost / mattermost-mobile / app / components / markdown / transform.js View on Github external
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
github mattermost / mattermost-mobile / app / components / markdown / transform.js View on Github external
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;
}
github substance / substance / doc / generator / markdownConverter.js View on Github external
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);
      }
github substance / substance / doc / generator / markdownConverter.js View on Github external
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();
    }
  }
}
github substance / substance / doc / generator / markdownConverter.js View on Github external
function makeLinksExternal(parsed) {
  var walker = parsed.walker();
  var event, node;
  while ((event = walker.next())) {
    node = event.node;
    if (event.entering &amp;&amp; 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();
    }
  }
}

commonmark

a strongly specified, highly compatible variant of Markdown

BSD-2-Clause
Latest version published 2 months ago

Package Health Score

80 / 100
Full package analysis