How to use commonmark - 10 common examples

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 accordproject / markdown-editor / src / MarkdownToSlateConverter.js View on Github external
convert(markdownText) {
    this.root = null;
    this.nodeStack = [];

    const reader = new commonmark.Parser();
    const parsed = reader.parse(markdownText);

    const walker = parsed.walker();
    let event;
    event = walker.next();

    while (event) {
      const type = event.node.type;
      // console.log(`Converting: ${type}`);

      if (this[type]) {
        // console.log(event.node);

        this[type](event.node, event.entering);
        this.dump();
      } else {
github deanlandolt / markdown-string / index.js View on Github external
// stupid simple markdown template strings
// dervied from https://github.com/brianleroux/micromark

var commonmark = require('commonmark');
var deindent = require('deindent');

var reader = new commonmark.DocParser();
var writer = new commonmark.HtmlRenderer();
var LINE_BREAK = /\n/;
var P_TAG_WRAP = /^<p>(.*)&lt;\/p&gt;\r?\n?$/i;

function markdownTag(strings) {
  var input = strings[0];
  for (var i = 1, length = arguments.length; i &lt; length; ++i) {
    input += arguments[i] + strings[i];
  }
  var parsed = writer.render(reader.parse(input));

  // if string is all on one line (contains no line breaks) strip </p><p> wrapper
  if (!LINE_BREAK.test(input)) {
    var wrapped = parsed.match(P_TAG_WRAP);
    if (wrapped) {
      parsed = wrapped[1];</p>
github witheve / Eve / src / editor.ts View on Github external
import {Parser} from "commonmark";
import * as CodeMirror from "codemirror";
import {sendSwap, sendSave, sendParse} from "./client";
import {setActiveIds, renderer, renderEve} from "./renderer";
let codeEditor: MarkdownEditor;
let lineMarks = {"item": true, "heading": true, "heading1": true, "heading2": true, "heading3": true, "heading4": true};
let parser = new Parser();


type Pos = CodeMirror.Position;
type Range = CodeMirror.Range;

type Elem = Element&any;

function isRange(loc:any): loc is Range {
  return loc.from !== undefined || loc.to !== undefined;
}
function isPosition(loc:any): loc is Position {
  return loc.line !== undefined || loc.ch !== undefined;
}

//---------------------------------------------------------
// Spans (TextMarkers)
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 textlint / textlint / lib / parse / markdown / markdown-parser.js View on Github external
/*eslint-disable */

// LICENSE : MIT
// Base:
// Copyright (c) 2014, John MacFarlane
// https://github.com/jgm/CommonMark/blob/master/js/lib/html-renderer.js

"use strict";
var traverse = require('traverse');
var positionNode = require("./markdown-position-node");
var StructuredSource = require('structured-source');
var Syntax = require("./markdown-syntax");
var commonmark = require("commonmark");
var normalize = require("./markdown-normalizer");
var objectAssign = require("object-assign");
var reader = new commonmark.DocParser();

// Helper function to produce content in a pair of HTML tags.
var toPlainText = function (tag, attribs, contents, selfclosing) {
    return contents;
};

// Render an inline element as HTML.
var renderInline = function (inline, parent) {
    var attrs;
    switch (inline.t) {
        case 'Str':
            return this.escape(inline.c);
        case 'Softbreak':
            return this.softbreak;
        case 'Hardbreak':
            return toPlainText('br', [], "", true) + '\n';

commonmark

a strongly specified, highly compatible variant of Markdown

BSD-2-Clause
Latest version published 6 months ago

Package Health Score

75 / 100
Full package analysis