Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 {
// 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>(.*)<\/p>\r?\n?$/i;
function markdownTag(strings) {
var input = strings[0];
for (var i = 1, length = arguments.length; i < 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>
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)
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);
}
/*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';