diff --git a/.eslintrc.json b/.eslintrc.json index 7915ab9bd9..73a8c11117 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -17,6 +17,7 @@ "one-var": "off", "no-control-regex": "off", "no-prototype-builtins": "off", + "no-extra-semi": "error", "prefer-const": "error", "no-var": "error" diff --git a/docs/USING_PRO.md b/docs/USING_PRO.md index 5e9451bee1..23fb7b3158 100644 --- a/docs/USING_PRO.md +++ b/docs/USING_PRO.md @@ -4,7 +4,7 @@ To champion the single-responsibility and open/closed principles, we have tried

The renderer

-The renderer is... +The renderer defines the output of the parser. **Example:** Overriding default heading token by adding an embedded anchor tag like on GitHub. @@ -29,7 +29,7 @@ renderer.heading = function (text, level) { }; // Run marked -console.log(marked('# heading+', { renderer: renderer })); +console.log(marked('# heading+', { renderer })); ``` **Output:** @@ -91,12 +91,11 @@ slugger.slug('foo-1') // foo-1-2

The lexer

-The lexer is... - +The lexer turns a markdown string into tokens.

The parser

-The parser is... +The parser takes tokens as input and calls the renderer functions. *** @@ -105,30 +104,46 @@ The parser is... You also have direct access to the lexer and parser if you so desire. ``` js -const tokens = marked.lexer(text, options); +const tokens = marked.lexer(markdown, options); console.log(marked.parser(tokens, options)); ``` ``` js const lexer = new marked.Lexer(options); -const tokens = lexer.lex(text); +const tokens = lexer.lex(markdown); console.log(tokens); -console.log(lexer.rules); +console.log(lexer.rules.block); // block level rules +console.log(lexer.rules.inline); // inline level rules ``` ``` bash $ node -> require('marked').lexer('> i am using marked.') -[ { type: 'blockquote_start' }, - { type: 'paragraph', - text: 'i am using marked.' }, - { type: 'blockquote_end' }, - links: {} ] +> require('marked').lexer('> I am using marked.') +[ + { + type: "blockquote", + raw: "> I am using marked.", + tokens: [ + { + type: "paragraph", + raw: "I am using marked.", + text: "I am using marked.", + tokens: [ + { + type: "text", + raw: "I am using marked.", + text: "I am using marked." + } + ] + } + ] + }, + links: {} +] ``` -The Lexers build an array of tokens, which will be passed to their respective -Parsers. The Parsers process each token in the token arrays, -which are removed from the array of tokens: +The Lexer builds an array of tokens, which will be passed to the Parser. +The Parser processes each token in the token array: ``` js const marked = require('marked'); @@ -146,18 +161,60 @@ console.log(tokens); const html = marked.parser(tokens); console.log(html); - -console.log(tokens); ``` ``` bash -[ { type: 'heading', depth: 1, text: 'heading' }, - { type: 'paragraph', text: ' [link][1]' }, - { type: 'space' }, - links: { '1': { href: '#heading', title: 'heading' } } ] - +[ + { + type: "heading", + raw: " # heading\n\n", + depth: 1, + text: "heading", + tokens: [ + { + type: "text", + raw: "heading", + text: "heading" + } + ] + }, + { + type: "paragraph", + raw: " [link][1]", + text: " [link][1]", + tokens: [ + { + type: "text", + raw: " ", + text: " " + }, + { + type: "link", + raw: "[link][1]", + text: "link", + href: "#heading", + title: "heading", + tokens: [ + { + type: "text", + raw: "link", + text: "link" + } + ] + } + ] + }, + { + type: "space", + raw: "\n\n" + }, + links: { + "1": { + href: "#heading", + title: "heading" + } + } +]

heading

link

- -[ links: { '1': { href: '#heading', title: 'heading' } } ] ``` diff --git a/docs/demo/demo.js b/docs/demo/demo.js index 0564958302..142d127a30 100644 --- a/docs/demo/demo.js +++ b/docs/demo/demo.js @@ -183,7 +183,7 @@ function handleIframeLoad() { function handleInput() { inputDirty = true; -}; +} function handleVersionChange() { if ($markedVerElem.value === 'commit' || $markedVerElem.value === 'pr') { @@ -256,7 +256,7 @@ function handleChange(panes, visiblePane) { } } return active; -}; +} function addCommitVersion(value, text, commit) { if (markedVersions[value]) { @@ -331,13 +331,13 @@ function jsonString(input) { .replace(/[\\"']/g, '\\$&') .replace(/\u0000/g, '\\0'); return '"' + output + '"'; -}; +} function getScrollSize() { var e = $activeOutputElem; return e.scrollHeight - e.clientHeight; -}; +} function getScrollPercent() { var size = getScrollSize(); @@ -347,11 +347,11 @@ function getScrollPercent() { } return $activeOutputElem.scrollTop / size; -}; +} function setScrollPercent(percent) { $activeOutputElem.scrollTop = percent * getScrollSize(); -}; +} function updateLink() { var outputType = ''; @@ -446,7 +446,7 @@ function checkForChanges() { } } checkChangeTimeout = window.setTimeout(checkForChanges, delayTime); -}; +} function setResponseTime(ms) { var amount = ms; diff --git a/docs/demo/worker.js b/docs/demo/worker.js index 6a0fa03238..df69e9204c 100644 --- a/docs/demo/worker.js +++ b/docs/demo/worker.js @@ -1,4 +1,5 @@ /* globals marked, unfetch, ES6Promise, Promise */ // eslint-disable-line no-redeclare + if (!self.Promise) { self.importScripts('https://cdn.jsdelivr.net/npm/es6-promise/dist/es6-promise.js'); self.Promise = ES6Promise; @@ -48,28 +49,44 @@ function parse(e) { case 'parse': var startTime = new Date(); var lexed = marked.lexer(e.data.markdown, e.data.options); - var lexedList = []; - for (var i = 0; i < lexed.length; i++) { - var lexedLine = []; - for (var j in lexed[i]) { - lexedLine.push(j + ':' + jsonString(lexed[i][j])); - } - lexedList.push('{' + lexedLine.join(', ') + '}'); - } + var lexedList = getLexedList(lexed); var parsed = marked.parser(lexed, e.data.options); var endTime = new Date(); - // setTimeout(function () { postMessage({ task: e.data.task, - lexed: lexedList.join('\n'), + lexed: lexedList, parsed: parsed, time: endTime - startTime }); - // }, 10000); break; } } +function getLexedList(lexed, level) { + level = level || 0; + var lexedList = []; + for (var i = 0; i < lexed.length; i++) { + var lexedLine = []; + for (var j in lexed[i]) { + if (j === 'tokens' || j === 'items') { + lexedLine.push(j + ': [\n' + getLexedList(lexed[i][j], level + 1) + '\n]'); + } else { + lexedLine.push(j + ':' + jsonString(lexed[i][j])); + } + } + lexedList.push(stringRepeat(' ', 2 * level) + '{' + lexedLine.join(', ') + '}'); + } + return lexedList.join('\n'); +} + +function stringRepeat(char, times) { + var s = ''; + for (var i = 0; i < times; i++) { + s += char; + } + return s; +} + function jsonString(input) { var output = (input + '') .replace(/\n/g, '\\n') @@ -79,7 +96,7 @@ function jsonString(input) { .replace(/[\\"']/g, '\\$&') .replace(/\u0000/g, '\\0'); return '"' + output + '"'; -}; +} function loadVersion(ver) { var promise; diff --git a/lib/marked.esm.js b/lib/marked.esm.js index 6543d55699..b48c13368d 100644 --- a/lib/marked.esm.js +++ b/lib/marked.esm.js @@ -561,11 +561,12 @@ var rules = { }; const { defaults: defaults$1 } = defaults; -const { block: block$1 } = rules; +const { block: block$1, inline: inline$1 } = rules; const { rtrim: rtrim$1, splitCells: splitCells$1, - escape: escape$1 + escape: escape$1, + findClosingBracket: findClosingBracket$1 } = helpers; /** @@ -576,12 +577,21 @@ var Lexer_1 = class Lexer { this.tokens = []; this.tokens.links = Object.create(null); this.options = options || defaults$1; - this.rules = block$1.normal; + this.rules = { + block: block$1.normal, + inline: inline$1.normal + }; if (this.options.pedantic) { - this.rules = block$1.pedantic; + this.rules.block = block$1.pedantic; + this.rules.inline = inline$1.pedantic; } else if (this.options.gfm) { - this.rules = block$1.gfm; + this.rules.block = block$1.gfm; + if (this.options.breaks) { + this.rules.inline = inline$1.breaks; + } else { + this.rules.inline = inline$1.gfm; + } } } @@ -589,7 +599,10 @@ var Lexer_1 = class Lexer { * Expose Block Rules */ static get rules() { - return block$1; + return { + block: block$1, + inline: inline$1 + }; } /** @@ -598,7 +611,7 @@ var Lexer_1 = class Lexer { static lex(src, options) { const lexer = new Lexer(options); return lexer.lex(src); - }; + } /** * Preprocessing @@ -608,13 +621,17 @@ var Lexer_1 = class Lexer { .replace(/\r\n|\r/g, '\n') .replace(/\t/g, ' '); - return this.token(src, true); - }; + this.blockTokens(this.tokens, src); + + this.inlineTokens(this.tokens); + + return this.tokens; + } /** * Lexing */ - token(src, top) { + blockTokens(tokens, src, top = true) { src = src.replace(/^ +$/gm, ''); let next, loose, @@ -622,39 +639,45 @@ var Lexer_1 = class Lexer { bull, b, item, - listStart, - listItems, - t, + list, space, i, tag, l, isordered, istask, - ischecked; + ischecked, + lastToken, + addBack, + raw; while (src) { // newline - if (cap = this.rules.newline.exec(src)) { + if (cap = this.rules.block.newline.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; if (cap[0].length > 1) { - this.tokens.push({ - type: 'space' + tokens.push({ + type: 'space', + raw }); } } // code - if (cap = this.rules.code.exec(src)) { - const lastToken = this.tokens[this.tokens.length - 1]; + if (cap = this.rules.block.code.exec(src)) { + lastToken = tokens[tokens.length - 1]; src = src.substring(cap[0].length); + raw = cap[0]; // An indented code block cannot interrupt a paragraph. if (lastToken && lastToken.type === 'paragraph') { lastToken.text += '\n' + cap[0].trimRight(); + lastToken.raw += '\n' + raw; } else { cap = cap[0].replace(/^ {4}/gm, ''); - this.tokens.push({ + tokens.push({ type: 'code', + raw, codeBlockStyle: 'indented', text: !this.options.pedantic ? rtrim$1(cap, '\n') @@ -665,10 +688,12 @@ var Lexer_1 = class Lexer { } // fences - if (cap = this.rules.fences.exec(src)) { + if (cap = this.rules.block.fences.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'code', + raw, lang: cap[2] ? cap[2].trim() : cap[2], text: cap[3] || '' }); @@ -676,10 +701,12 @@ var Lexer_1 = class Lexer { } // heading - if (cap = this.rules.heading.exec(src)) { + if (cap = this.rules.block.heading.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'heading', + raw, depth: cap[1].length, text: cap[2] }); @@ -687,7 +714,7 @@ var Lexer_1 = class Lexer { } // table no leading pipe (gfm) - if (cap = this.rules.nptable.exec(src)) { + if (cap = this.rules.block.nptable.exec(src)) { item = { type: 'table', header: splitCells$1(cap[1].replace(/^ *| *\| *$/g, '')), @@ -697,8 +724,11 @@ var Lexer_1 = class Lexer { if (item.header.length === item.align.length) { src = src.substring(cap[0].length); + raw = cap[0]; + item.raw = raw; - for (i = 0; i < item.align.length; i++) { + l = item.align.length; + for (i = 0; i < l; i++) { if (/^ *-+: *$/.test(item.align[i])) { item.align[i] = 'right'; } else if (/^ *:-+: *$/.test(item.align[i])) { @@ -710,72 +740,71 @@ var Lexer_1 = class Lexer { } } - for (i = 0; i < item.cells.length; i++) { + l = item.cells.length; + for (i = 0; i < l; i++) { item.cells[i] = splitCells$1(item.cells[i], item.header.length); } - this.tokens.push(item); + tokens.push(item); continue; } } // hr - if (cap = this.rules.hr.exec(src)) { + if (cap = this.rules.block.hr.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ - type: 'hr' + raw = cap[0]; + tokens.push({ + type: 'hr', + raw }); continue; } // blockquote - if (cap = this.rules.blockquote.exec(src)) { + if (cap = this.rules.block.blockquote.exec(src)) { src = src.substring(cap[0].length); - - this.tokens.push({ - type: 'blockquote_start' - }); + raw = cap[0]; cap = cap[0].replace(/^ *> ?/gm, ''); - // Pass `top` to keep the current - // "toplevel" state. This is exactly - // how markdown.pl works. - this.token(cap, top); - - this.tokens.push({ - type: 'blockquote_end' + tokens.push({ + type: 'blockquote', + raw, + tokens: this.blockTokens([], cap, top) }); continue; } // list - if (cap = this.rules.list.exec(src)) { + if (cap = this.rules.block.list.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; bull = cap[2]; isordered = bull.length > 1; - listStart = { - type: 'list_start', + list = { + type: 'list', + raw, ordered: isordered, start: isordered ? +bull : '', - loose: false + loose: false, + items: [] }; - this.tokens.push(listStart); + tokens.push(list); // Get each top-level item. - cap = cap[0].match(this.rules.item); + cap = cap[0].match(this.rules.block.item); - listItems = []; next = false; - l = cap.length; - i = 0; - for (; i < l; i++) { + l = cap.length; + for (i = 0; i < l; i++) { item = cap[i]; + raw = item.trim(); // Remove the list item's bullet // so it is seen as the next token. @@ -797,7 +826,9 @@ var Lexer_1 = class Lexer { b = block$1.bullet.exec(cap[i + 1])[0]; if (bull.length > 1 ? b.length === 1 : (b.length > 1 || (this.options.smartLists && b !== bull))) { - src = cap.slice(i + 1).join('\n') + src; + addBack = cap.slice(i + 1).join('\n'); + src = addBack + src; + list.raw = list.raw.substring(list.raw.length - addBack.length); i = l - 1; } } @@ -812,7 +843,7 @@ var Lexer_1 = class Lexer { } if (loose) { - listStart.loose = true; + list.loose = true; } // Check for task list items @@ -823,46 +854,27 @@ var Lexer_1 = class Lexer { item = item.replace(/^\[[ xX]\] +/, ''); } - t = { - type: 'list_item_start', + list.items.push({ + raw, task: istask, checked: ischecked, - loose: loose - }; - - listItems.push(t); - this.tokens.push(t); - - // Recurse. - this.token(item, false); - - this.tokens.push({ - type: 'list_item_end' + loose: loose, + tokens: this.blockTokens([], item, false) }); } - if (listStart.loose) { - l = listItems.length; - i = 0; - for (; i < l; i++) { - listItems[i].loose = true; - } - } - - this.tokens.push({ - type: 'list_end' - }); - continue; } // html - if (cap = this.rules.html.exec(src)) { + if (cap = this.rules.block.html.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: this.options.sanitize ? 'paragraph' : 'html', + raw, pre: !this.options.sanitizer && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'), text: this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$1(cap[0])) : cap[0] @@ -871,7 +883,7 @@ var Lexer_1 = class Lexer { } // def - if (top && (cap = this.rules.def.exec(src))) { + if (top && (cap = this.rules.block.def.exec(src))) { src = src.substring(cap[0].length); if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1); tag = cap[1].toLowerCase().replace(/\s+/g, ' '); @@ -885,7 +897,7 @@ var Lexer_1 = class Lexer { } // table (gfm) - if (cap = this.rules.table.exec(src)) { + if (cap = this.rules.block.table.exec(src)) { item = { type: 'table', header: splitCells$1(cap[1].replace(/^ *| *\| *$/g, '')), @@ -895,8 +907,10 @@ var Lexer_1 = class Lexer { if (item.header.length === item.align.length) { src = src.substring(cap[0].length); + item.raw = cap[0]; - for (i = 0; i < item.align.length; i++) { + l = item.align.length; + for (i = 0; i < l; i++) { if (/^ *-+: *$/.test(item.align[i])) { item.align[i] = 'right'; } else if (/^ *:-+: *$/.test(item.align[i])) { @@ -908,23 +922,26 @@ var Lexer_1 = class Lexer { } } - for (i = 0; i < item.cells.length; i++) { + l = item.cells.length; + for (i = 0; i < l; i++) { item.cells[i] = splitCells$1( item.cells[i].replace(/^ *\| *| *\| *$/g, ''), item.header.length); } - this.tokens.push(item); + tokens.push(item); continue; } } // lheading - if (cap = this.rules.lheading.exec(src)) { + if (cap = this.rules.block.lheading.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'heading', + raw, depth: cap[2].charAt(0) === '=' ? 1 : 2, text: cap[1] }); @@ -932,10 +949,12 @@ var Lexer_1 = class Lexer { } // top-level paragraph - if (top && (cap = this.rules.paragraph.exec(src))) { + if (top && (cap = this.rules.block.paragraph.exec(src))) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'paragraph', + raw, text: cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1] @@ -944,427 +963,330 @@ var Lexer_1 = class Lexer { } // text - if (cap = this.rules.text.exec(src)) { + if (cap = this.rules.block.text.exec(src)) { // Top-level should never reach here. src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'text', + raw, text: cap[0] }); continue; } if (src) { - throw new Error('Infinite loop on byte: ' + src.charCodeAt(0)); + const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0); + if (this.options.silent) { + console.error(errMsg); + } else { + throw new Error(errMsg); + } } } - return this.tokens; - }; -}; - -const { defaults: defaults$2 } = defaults; -const { - cleanUrl: cleanUrl$1, - escape: escape$2 -} = helpers; - -/** - * Renderer - */ -var Renderer_1 = class Renderer { - constructor(options) { - this.options = options || defaults$2; + return tokens; } - code(code, infostring, escaped) { - const lang = (infostring || '').match(/\S*/)[0]; - if (this.options.highlight) { - const out = this.options.highlight(code, lang); - if (out != null && out !== code) { - escaped = true; - code = out; - } - } - - if (!lang) { - return '
'
-        + (escaped ? code : escape$2(code, true))
-        + '
'; - } - - return '
'
-      + (escaped ? code : escape$2(code, true))
-      + '
\n'; - }; + inlineTokens(tokens) { + let i, + j, + k, + l2, + row, + token; + + const l = tokens.length; + for (i = 0; i < l; i++) { + token = tokens[i]; + switch (token.type) { + case 'paragraph': + case 'text': + case 'heading': { + token.tokens = []; + this.inlineOutput(token.text, token.tokens); + break; + } + case 'table': { + token.tokens = { + header: [], + cells: [] + }; - blockquote(quote) { - return '
\n' + quote + '
\n'; - }; + // header + l2 = token.header.length; + for (j = 0; j < l2; j++) { + token.tokens.header[j] = []; + this.inlineOutput(token.header[j], token.tokens.header[j]); + } - html(html) { - return html; - }; + // cells + l2 = token.cells.length; + for (j = 0; j < l2; j++) { + row = token.cells[j]; + token.tokens.cells[j] = []; + for (k = 0; k < row.length; k++) { + token.tokens.cells[j][k] = []; + this.inlineOutput(row[k], token.tokens.cells[j][k]); + } + } - heading(text, level, raw, slugger) { - if (this.options.headerIds) { - return '' - + text - + '\n'; + break; + } + case 'blockquote': { + this.inlineTokens(token.tokens); + break; + } + case 'list': { + l2 = token.items.length; + for (j = 0; j < l2; j++) { + this.inlineTokens(token.items[j].tokens); + } + break; + } + } } - // ignore IDs - return '' + text + '\n'; - }; - - hr() { - return this.options.xhtml ? '
\n' : '
\n'; - }; - list(body, ordered, start) { - const type = ordered ? 'ol' : 'ul', - startatt = (ordered && start !== 1) ? (' start="' + start + '"') : ''; - return '<' + type + startatt + '>\n' + body + '\n'; - }; + return tokens; + } - listitem(text) { - return '
  • ' + text + '
  • \n'; - }; + /** + * Lexing/Compiling + */ + inlineOutput(src, tokens) { + let out = '', + link, + text, + newTokens, + href, + title, + cap, + prevCapZero, + lastParenIndex, + start, + linkLen, + raw; - checkbox(checked) { - return ' '; - }; + while (src) { + // escape + if (cap = this.rules.inline.escape.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + text = escape$1(cap[1]); + out += text; + tokens.push({ + type: 'escape', + raw, + text + }); + continue; + } - paragraph(text) { - return '

    ' + text + '

    \n'; - }; + // tag + if (cap = this.rules.inline.tag.exec(src)) { + if (!this.inLink && /^/i.test(cap[0])) { + this.inLink = false; + } + if (!this.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) { + this.inRawBlock = true; + } else if (this.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) { + this.inRawBlock = false; + } - table(header, body) { - if (body) body = '' + body + ''; + src = src.substring(cap[0].length); + raw = cap[0]; + text = this.options.sanitize + ? (this.options.sanitizer + ? this.options.sanitizer(cap[0]) + : escape$1(cap[0])) + : cap[0]; + tokens.push({ + type: this.options.sanitize + ? 'text' + : 'html', + raw, + text + }); + out += text; + continue; + } - return '\n' - + '\n' - + header - + '\n' - + body - + '
    \n'; - }; + // link + if (cap = this.rules.inline.link.exec(src)) { + lastParenIndex = findClosingBracket$1(cap[2], '()'); + if (lastParenIndex > -1) { + start = cap[0].indexOf('!') === 0 ? 5 : 4; + linkLen = start + cap[1].length + lastParenIndex; + cap[2] = cap[2].substring(0, lastParenIndex); + cap[0] = cap[0].substring(0, linkLen).trim(); + cap[3] = ''; + } + src = src.substring(cap[0].length); + raw = cap[0]; + this.inLink = true; + href = cap[2]; + if (this.options.pedantic) { + link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href); - tablerow(content) { - return '\n' + content + '\n'; - }; + if (link) { + href = link[1]; + title = link[3]; + } else { + title = ''; + } + } else { + title = cap[3] ? cap[3].slice(1, -1) : ''; + } + href = href.trim().replace(/^<([\s\S]*)>$/, '$1'); + out += this.outputLink(cap, { + href: this.escapes(href), + title: this.escapes(title) + }, tokens, raw); + this.inLink = false; + continue; + } - tablecell(content, flags) { - const type = flags.header ? 'th' : 'td'; - const tag = flags.align - ? '<' + type + ' align="' + flags.align + '">' - : '<' + type + '>'; - return tag + content + '\n'; - }; + // reflink, nolink + if ((cap = this.rules.inline.reflink.exec(src)) + || (cap = this.rules.inline.nolink.exec(src))) { + src = src.substring(cap[0].length); + raw = cap[0]; + link = (cap[2] || cap[1]).replace(/\s+/g, ' '); + link = this.tokens.links[link.toLowerCase()]; + if (!link || !link.href) { + text = cap[0].charAt(0); + out += text; + tokens.push({ + type: 'text', + raw: text, + text + }); + src = cap[0].substring(1) + src; + continue; + } + this.inLink = true; + out += this.outputLink(cap, link, tokens, raw); + this.inLink = false; + continue; + } - // span level renderer - strong(text) { - return '' + text + ''; - }; + // strong + if (cap = this.rules.inline.strong.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + newTokens = tokens ? [] : null; + text = this.inlineOutput(cap[4] || cap[3] || cap[2] || cap[1], newTokens); + + tokens.push({ + type: 'strong', + raw, + text, + tokens: newTokens + }); + out += text; + continue; + } - em(text) { - return '' + text + ''; - }; + // em + if (cap = this.rules.inline.em.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + newTokens = tokens ? [] : null; + text = this.inlineOutput(cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1], newTokens); + tokens.push({ + type: 'em', + raw, + text, + tokens: newTokens + }); + out += text; + continue; + } - codespan(text) { - return '' + text + ''; - }; + // code + if (cap = this.rules.inline.code.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + text = escape$1(cap[2].trim(), true); + tokens.push({ + type: 'codespan', + raw, + text + }); + out += text; + continue; + } - br() { - return this.options.xhtml ? '
    ' : '
    '; - }; + // br + if (cap = this.rules.inline.br.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + tokens.push({ + type: 'br', + raw + }); + out += '\n'; + continue; + } - del(text) { - return '' + text + ''; - }; - - link(href, title, text) { - href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); - if (href === null) { - return text; - } - let out = '
    '; - return out; - }; - - image(href, title, text) { - href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); - if (href === null) { - return text; - } - - let out = '' + text + '' : '>'; - return out; - }; - - text(text) { - return text; - }; -}; - -/** - * Slugger generates header id - */ -var Slugger_1 = class Slugger { - constructor() { - this.seen = {}; - } - - /** - * Convert string to unique id - */ - slug(value) { - let slug = value - .toLowerCase() - .trim() - // remove html tags - .replace(/<[!\/a-z].*?>/ig, '') - // remove unwanted chars - .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '') - .replace(/\s/g, '-'); - - if (this.seen.hasOwnProperty(slug)) { - const originalSlug = slug; - do { - this.seen[originalSlug]++; - slug = originalSlug + '-' + this.seen[originalSlug]; - } while (this.seen.hasOwnProperty(slug)); - } - this.seen[slug] = 0; - - return slug; - }; -}; - -const { defaults: defaults$3 } = defaults; -const { inline: inline$1 } = rules; -const { - findClosingBracket: findClosingBracket$1, - escape: escape$3 -} = helpers; - -/** - * Inline Lexer & Compiler - */ -var InlineLexer_1 = class InlineLexer { - constructor(links, options) { - this.options = options || defaults$3; - this.links = links; - this.rules = inline$1.normal; - this.options.renderer = this.options.renderer || new Renderer_1(); - this.renderer = this.options.renderer; - this.renderer.options = this.options; - - if (!this.links) { - throw new Error('Tokens array requires a `links` property.'); - } - - if (this.options.pedantic) { - this.rules = inline$1.pedantic; - } else if (this.options.gfm) { - if (this.options.breaks) { - this.rules = inline$1.breaks; - } else { - this.rules = inline$1.gfm; - } - } - } - - /** - * Expose Inline Rules - */ - static get rules() { - return inline$1; - } - - /** - * Static Lexing/Compiling Method - */ - static output(src, links, options) { - const inline = new InlineLexer(links, options); - return inline.output(src); - } - - /** - * Lexing/Compiling - */ - output(src) { - let out = '', - link, - text, - href, - title, - cap, - prevCapZero; - - while (src) { - // escape - if (cap = this.rules.escape.exec(src)) { - src = src.substring(cap[0].length); - out += escape$3(cap[1]); - continue; - } - - // tag - if (cap = this.rules.tag.exec(src)) { - if (!this.inLink && /^/i.test(cap[0])) { - this.inLink = false; - } - if (!this.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) { - this.inRawBlock = true; - } else if (this.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) { - this.inRawBlock = false; - } - - src = src.substring(cap[0].length); - out += this.renderer.html(this.options.sanitize - ? (this.options.sanitizer - ? this.options.sanitizer(cap[0]) - : escape$3(cap[0])) - : cap[0]); - continue; - } - - // link - if (cap = this.rules.link.exec(src)) { - const lastParenIndex = findClosingBracket$1(cap[2], '()'); - if (lastParenIndex > -1) { - const start = cap[0].indexOf('!') === 0 ? 5 : 4; - const linkLen = start + cap[1].length + lastParenIndex; - cap[2] = cap[2].substring(0, lastParenIndex); - cap[0] = cap[0].substring(0, linkLen).trim(); - cap[3] = ''; - } - src = src.substring(cap[0].length); - this.inLink = true; - href = cap[2]; - if (this.options.pedantic) { - link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href); - - if (link) { - href = link[1]; - title = link[3]; - } else { - title = ''; - } - } else { - title = cap[3] ? cap[3].slice(1, -1) : ''; - } - href = href.trim().replace(/^<([\s\S]*)>$/, '$1'); - out += this.outputLink(cap, { - href: InlineLexer.escapes(href), - title: InlineLexer.escapes(title) - }); - this.inLink = false; - continue; - } - - // reflink, nolink - if ((cap = this.rules.reflink.exec(src)) - || (cap = this.rules.nolink.exec(src))) { - src = src.substring(cap[0].length); - link = (cap[2] || cap[1]).replace(/\s+/g, ' '); - link = this.links[link.toLowerCase()]; - if (!link || !link.href) { - out += cap[0].charAt(0); - src = cap[0].substring(1) + src; - continue; - } - this.inLink = true; - out += this.outputLink(cap, link); - this.inLink = false; - continue; - } - - // strong - if (cap = this.rules.strong.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.strong(this.output(cap[4] || cap[3] || cap[2] || cap[1])); - continue; - } - - // em - if (cap = this.rules.em.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.em(this.output(cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1])); - continue; - } - - // code - if (cap = this.rules.code.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.codespan(escape$3(cap[2].trim(), true)); - continue; - } - - // br - if (cap = this.rules.br.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.br(); - continue; - } - - // del (gfm) - if (cap = this.rules.del.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.del(this.output(cap[1])); - continue; - } + // del (gfm) + if (cap = this.rules.inline.del.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + newTokens = tokens ? [] : null; + text = this.inlineOutput(cap[1], newTokens); + tokens.push({ + type: 'del', + raw, + text, + tokens: newTokens + }); + out += text; + continue; + } // autolink - if (cap = this.rules.autolink.exec(src)) { + if (cap = this.rules.inline.autolink.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; if (cap[2] === '@') { - text = escape$3(this.mangle(cap[1])); + text = escape$1(this.options.mangle ? this.mangle(cap[1]) : cap[1]); href = 'mailto:' + text; } else { - text = escape$3(cap[1]); + text = escape$1(cap[1]); href = text; } - out += this.renderer.link(href, null, text); + tokens.push({ + type: 'link', + raw, + text, + href, + tokens: [ + { + type: 'text', + raw: text, + text + } + ] + }); + out += text; continue; } // url (gfm) - if (!this.inLink && (cap = this.rules.url.exec(src))) { + if (!this.inLink && (cap = this.rules.inline.url.exec(src))) { if (cap[2] === '@') { - text = escape$3(cap[0]); + text = escape$1(this.options.mangle ? this.mangle(cap[0]) : cap[0]); href = 'mailto:' + text; } else { // do extended autolink path validation do { prevCapZero = cap[0]; - cap[0] = this.rules._backpedal.exec(cap[0])[0]; + cap[0] = this.rules.inline._backpedal.exec(cap[0])[0]; } while (prevCapZero !== cap[0]); - text = escape$3(cap[0]); + text = escape$1(cap[0]); if (cap[1] === 'www.') { href = 'http://' + text; } else { @@ -1372,50 +1294,95 @@ var InlineLexer_1 = class InlineLexer { } } src = src.substring(cap[0].length); - out += this.renderer.link(href, null, text); + raw = cap[0]; + tokens.push({ + type: 'link', + raw, + text, + href, + tokens: [ + { + type: 'text', + raw: text, + text + } + ] + }); + out += text; continue; } // text - if (cap = this.rules.text.exec(src)) { + if (cap = this.rules.inline.text.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; if (this.inRawBlock) { - out += this.renderer.text(this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$3(cap[0])) : cap[0]); + text = this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$1(cap[0])) : cap[0]; } else { - out += this.renderer.text(escape$3(this.smartypants(cap[0]))); + text = escape$1(this.options.smartypants ? this.smartypants(cap[0]) : cap[0]); } + tokens.push({ + type: 'text', + raw, + text + }); + out += text; continue; } if (src) { - throw new Error('Infinite loop on byte: ' + src.charCodeAt(0)); + const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0); + if (this.options.silent) { + console.error(errMsg); + } else { + throw new Error(errMsg); + } } } return out; } - static escapes(text) { - return text ? text.replace(InlineLexer.rules._escapes, '$1') : text; + escapes(text) { + return text ? text.replace(inline$1._escapes, '$1') : text; } /** - * Compile Link + * tokenize Link */ - outputLink(cap, link) { - const href = link.href, - title = link.title ? escape$3(link.title) : null; - - return cap[0].charAt(0) !== '!' - ? this.renderer.link(href, title, this.output(cap[1])) - : this.renderer.image(href, title, escape$3(cap[1])); + outputLink(cap, link, tokens, raw) { + const href = link.href; + const title = link.title ? escape$1(link.title) : null; + const newTokens = tokens ? [] : null; + + if (cap[0].charAt(0) !== '!') { + const text = this.inlineOutput(cap[1], newTokens); + tokens.push({ + type: 'link', + raw, + text, + href, + title, + tokens: newTokens + }); + return text; + } else { + const text = escape$1(cap[1]); + tokens.push({ + type: 'image', + raw, + text, + href, + title + }); + return text; + } } /** * Smartypants Transformations */ smartypants(text) { - if (!this.options.smartypants) return text; return text // em-dashes .replace(/---/g, '\u2014') @@ -1437,13 +1404,12 @@ var InlineLexer_1 = class InlineLexer { * Mangle Links */ mangle(text) { - if (!this.options.mangle) return text; - const l = text.length; let out = '', - i = 0, + i, ch; - for (; i < l; i++) { + const l = text.length; + for (i = 0; i < l; i++) { ch = text.charCodeAt(i); if (Math.random() > 0.5) { ch = 'x' + ch.toString(16); @@ -1455,6 +1421,171 @@ var InlineLexer_1 = class InlineLexer { } }; +const { defaults: defaults$2 } = defaults; +const { + cleanUrl: cleanUrl$1, + escape: escape$2 +} = helpers; + +/** + * Renderer + */ +var Renderer_1 = class Renderer { + constructor(options) { + this.options = options || defaults$2; + } + + code(code, infostring, escaped) { + const lang = (infostring || '').match(/\S*/)[0]; + if (this.options.highlight) { + const out = this.options.highlight(code, lang); + if (out != null && out !== code) { + escaped = true; + code = out; + } + } + + if (!lang) { + return '
    '
    +        + (escaped ? code : escape$2(code, true))
    +        + '
    '; + } + + return '
    '
    +      + (escaped ? code : escape$2(code, true))
    +      + '
    \n'; + } + + blockquote(quote) { + return '
    \n' + quote + '
    \n'; + } + + html(html) { + return html; + } + + heading(text, level, raw, slugger) { + if (this.options.headerIds) { + return '' + + text + + '\n'; + } + // ignore IDs + return '' + text + '\n'; + } + + hr() { + return this.options.xhtml ? '
    \n' : '
    \n'; + } + + list(body, ordered, start) { + const type = ordered ? 'ol' : 'ul', + startatt = (ordered && start !== 1) ? (' start="' + start + '"') : ''; + return '<' + type + startatt + '>\n' + body + '\n'; + } + + listitem(text) { + return '
  • ' + text + '
  • \n'; + } + + checkbox(checked) { + return ' '; + } + + paragraph(text) { + return '

    ' + text + '

    \n'; + } + + table(header, body) { + if (body) body = '' + body + ''; + + return '\n' + + '\n' + + header + + '\n' + + body + + '
    \n'; + } + + tablerow(content) { + return '\n' + content + '\n'; + } + + tablecell(content, flags) { + const type = flags.header ? 'th' : 'td'; + const tag = flags.align + ? '<' + type + ' align="' + flags.align + '">' + : '<' + type + '>'; + return tag + content + '\n'; + } + + // span level renderer + strong(text) { + return '' + text + ''; + } + + em(text) { + return '' + text + ''; + } + + codespan(text) { + return '' + text + ''; + } + + br() { + return this.options.xhtml ? '
    ' : '
    '; + } + + del(text) { + return '' + text + ''; + } + + link(href, title, text) { + href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); + if (href === null) { + return text; + } + let out = '
    '; + return out; + } + + image(href, title, text) { + href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); + if (href === null) { + return text; + } + + let out = '' + text + '' : '>'; + return out; + } + + text(text) { + return text; + } +}; + /** * TextRenderer * returns only the textual part of the token @@ -1498,9 +1629,42 @@ var TextRenderer_1 = class TextRenderer { } }; -const { defaults: defaults$4 } = defaults; +/** + * Slugger generates header id + */ +var Slugger_1 = class Slugger { + constructor() { + this.seen = {}; + } + + /** + * Convert string to unique id + */ + slug(value) { + let slug = value + .toLowerCase() + .trim() + // remove html tags + .replace(/<[!\/a-z].*?>/ig, '') + // remove unwanted chars + .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '') + .replace(/\s/g, '-'); + + if (this.seen.hasOwnProperty(slug)) { + const originalSlug = slug; + do { + this.seen[originalSlug]++; + slug = originalSlug + '-' + this.seen[originalSlug]; + } while (this.seen.hasOwnProperty(slug)); + } + this.seen[slug] = 0; + + return slug; + } +}; + +const { defaults: defaults$3 } = defaults; const { - merge: merge$2, unescape: unescape$1 } = helpers; @@ -1509,11 +1673,10 @@ const { */ var Parser_1 = class Parser { constructor(options) { - this.tokens = []; - this.token = null; - this.options = options || defaults$4; + this.options = options || defaults$3; this.options.renderer = this.options.renderer || new Renderer_1(); this.renderer = this.options.renderer; + this.textRenderer = new TextRenderer_1(); this.renderer.options = this.options; this.slugger = new Slugger_1(); } @@ -1524,192 +1687,242 @@ var Parser_1 = class Parser { static parse(tokens, options) { const parser = new Parser(options); return parser.parse(tokens); - }; + } /** * Parse Loop */ - parse(tokens) { - this.inline = new InlineLexer_1(tokens.links, this.options); - // use an InlineLexer with a TextRenderer to extract pure text - this.inlineText = new InlineLexer_1( - tokens.links, - merge$2({}, this.options, { renderer: new TextRenderer_1() }) - ); - this.tokens = tokens.reverse(); - - let out = ''; - while (this.next()) { - out += this.tok(); - } - - return out; - }; - - /** - * Next Token - */ - next() { - this.token = this.tokens.pop(); - return this.token; - }; - - /** - * Preview Next Token - */ - peek() { - return this.tokens[this.tokens.length - 1] || 0; - }; - - /** - * Parse Text Tokens - */ - parseText() { - let body = this.token.text; - - while (this.peek().type === 'text') { - body += '\n' + this.next().text; - } - - return this.inline.output(body); - }; - - /** - * Parse Current Token - */ - tok() { - let body = ''; - switch (this.token.type) { - case 'space': { - return ''; - } - case 'hr': { - return this.renderer.hr(); - } - case 'heading': { - return this.renderer.heading( - this.inline.output(this.token.text), - this.token.depth, - unescape$1(this.inlineText.output(this.token.text)), - this.slugger); - } - case 'code': { - return this.renderer.code(this.token.text, - this.token.lang, - this.token.escaped); - } - case 'table': { - let header = '', - i, - row, - cell, - j; - - // header - cell = ''; - for (i = 0; i < this.token.header.length; i++) { - cell += this.renderer.tablecell( - this.inline.output(this.token.header[i]), - { header: true, align: this.token.align[i] } - ); + parse(tokens, top = true) { + let out = '', + i, + j, + k, + l2, + l3, + row, + cell, + header, + body, + token, + ordered, + start, + loose, + itemBody, + item, + checked, + task, + checkbox; + + const l = tokens.length; + for (i = 0; i < l; i++) { + token = tokens[i]; + switch (token.type) { + case 'space': { + continue; } - header += this.renderer.tablerow(cell); - - for (i = 0; i < this.token.cells.length; i++) { - row = this.token.cells[i]; + case 'hr': { + out += this.renderer.hr(); + continue; + } + case 'heading': { + out += this.renderer.heading( + this.parseInline(token.tokens), + token.depth, + unescape$1(this.parseInline(token.tokens, this.textRenderer)), + this.slugger); + continue; + } + case 'code': { + out += this.renderer.code(token.text, + token.lang, + token.escaped); + continue; + } + case 'table': { + header = ''; + // header cell = ''; - for (j = 0; j < row.length; j++) { + l2 = token.header.length; + for (j = 0; j < l2; j++) { cell += this.renderer.tablecell( - this.inline.output(row[j]), - { header: false, align: this.token.align[j] } + this.parseInline(token.tokens.header[j]), + { header: true, align: token.align[j] } ); } + header += this.renderer.tablerow(cell); + + body = ''; + l2 = token.cells.length; + for (j = 0; j < l2; j++) { + row = token.tokens.cells[j]; + + cell = ''; + l3 = row.length; + for (k = 0; k < l3; k++) { + cell += this.renderer.tablecell( + this.parseInline(row[k]), + { header: false, align: token.align[k] } + ); + } - body += this.renderer.tablerow(cell); + body += this.renderer.tablerow(cell); + } + out += this.renderer.table(header, body); + continue; } - return this.renderer.table(header, body); - } - case 'blockquote_start': { - body = ''; - - while (this.next().type !== 'blockquote_end') { - body += this.tok(); + case 'blockquote': { + body = this.parse(token.tokens); + out += this.renderer.blockquote(body); + continue; } + case 'list': { + ordered = token.ordered; + start = token.start; + loose = token.loose; + l2 = token.items.length; + + body = ''; + for (j = 0; j < l2; j++) { + item = token.items[j]; + checked = item.checked; + task = item.task; + + itemBody = ''; + if (item.task) { + checkbox = this.renderer.checkbox(checked); + if (loose) { + if (item.tokens[0].type === 'text') { + item.tokens[0].text = checkbox + ' ' + item.tokens[0].text; + if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') { + item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text; + } + } else { + item.tokens.unshift({ + type: 'text', + text: checkbox + }); + } + } else { + itemBody += checkbox; + } + } - return this.renderer.blockquote(body); - } - case 'list_start': { - body = ''; - const ordered = this.token.ordered, - start = this.token.start; + itemBody += this.parse(item.tokens, loose); + body += this.renderer.listitem(itemBody, task, checked); + } - while (this.next().type !== 'list_end') { - body += this.tok(); + out += this.renderer.list(body, ordered, start); + continue; } - - return this.renderer.list(body, ordered, start); - } - case 'list_item_start': { - body = ''; - const loose = this.token.loose; - const checked = this.token.checked; - const task = this.token.task; - - if (this.token.task) { - if (loose) { - if (this.peek().type === 'text') { - const nextToken = this.peek(); - nextToken.text = this.renderer.checkbox(checked) + ' ' + nextToken.text; - } else { - this.tokens.push({ - type: 'text', - text: this.renderer.checkbox(checked) - }); - } + case 'html': { + // TODO parse inline content if parameter markdown=1 + out += this.renderer.html(token.text); + continue; + } + case 'paragraph': { + out += this.renderer.paragraph(this.parseInline(token.tokens)); + continue; + } + case 'text': { + body = token.tokens ? this.parseInline(token.tokens) : token.text; + while (i + 1 < l && tokens[i + 1].type === 'text') { + token = tokens[++i]; + body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text); + } + out += top ? this.renderer.paragraph(body) : body; + continue; + } + default: { + const errMsg = 'Token with "' + token.type + '" type was not found.'; + if (this.options.silent) { + console.error(errMsg); } else { - body += this.renderer.checkbox(checked); + throw new Error(errMsg); } } + } + } - while (this.next().type !== 'list_item_end') { - body += !loose && this.token.type === 'text' - ? this.parseText() - : this.tok(); + return out; + } + + /** + * Parse Inline Tokens + */ + parseInline(tokens, renderer) { + renderer = renderer || this.renderer; + let out = '', + i, + token; + + const l = tokens.length; + for (i = 0; i < l; i++) { + token = tokens[i]; + switch (token.type) { + case 'escape': { + out += token.text; + break; } - return this.renderer.listitem(body, task, checked); - } - case 'html': { - // TODO parse inline content if parameter markdown=1 - return this.renderer.html(this.token.text); - } - case 'paragraph': { - return this.renderer.paragraph(this.inline.output(this.token.text)); - } - case 'text': { - return this.renderer.paragraph(this.parseText()); - } - default: { - const errMsg = 'Token with "' + this.token.type + '" type was not found.'; - if (this.options.silent) { - console.log(errMsg); - } else { - throw new Error(errMsg); + case 'html': { + out += renderer.html(token.text); + break; + } + case 'link': { + out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer)); + break; + } + case 'image': { + out += renderer.image(token.href, token.title, token.text); + break; + } + case 'strong': { + out += renderer.strong(this.parseInline(token.tokens, renderer)); + break; + } + case 'em': { + out += renderer.em(this.parseInline(token.tokens, renderer)); + break; + } + case 'codespan': { + out += renderer.codespan(token.text); + break; + } + case 'br': { + out += renderer.br(); + break; + } + case 'del': { + out += renderer.del(this.parseInline(token.tokens, renderer)); + break; + } + case 'text': { + out += renderer.text(token.text); + break; + } + default: { + const errMsg = 'Token with "' + this.token.type + '" type was not found.'; + if (this.options.silent) { + console.error(errMsg); + } else { + throw new Error(errMsg); + } } } } - }; + return out; + } }; const { - merge: merge$3, + merge: merge$2, checkSanitizeDeprecation: checkSanitizeDeprecation$1, - escape: escape$4 + escape: escape$3 } = helpers; const { getDefaults, changeDefaults, - defaults: defaults$5 + defaults: defaults$4 } = defaults; /** @@ -1731,7 +1944,7 @@ function marked(src, opt, callback) { opt = null; } - opt = merge$3({}, marked.defaults, opt || {}); + opt = merge$2({}, marked.defaults, opt || {}); checkSanitizeDeprecation$1(opt); const highlight = opt.highlight; let tokens, @@ -1795,14 +2008,14 @@ function marked(src, opt, callback) { return; } try { - opt = merge$3({}, marked.defaults, opt || {}); + opt = merge$2({}, marked.defaults, opt || {}); checkSanitizeDeprecation$1(opt); return Parser_1.parse(Lexer_1.lex(src, opt), opt); } catch (e) { e.message += '\nPlease report this to https://github.com/markedjs/marked.'; if ((opt || marked.defaults).silent) { return '

    An error occurred:

    '
    -        + escape$4(e.message + '', true)
    +        + escape$3(e.message + '', true)
             + '
    '; } throw e; @@ -1815,14 +2028,14 @@ function marked(src, opt, callback) { marked.options = marked.setOptions = function(opt) { - merge$3(marked.defaults, opt); + merge$2(marked.defaults, opt); changeDefaults(marked.defaults); return marked; }; marked.getDefaults = getDefaults; -marked.defaults = defaults$5; +marked.defaults = defaults$4; /** * Expose @@ -1837,9 +2050,6 @@ marked.TextRenderer = TextRenderer_1; marked.Lexer = Lexer_1; marked.lexer = Lexer_1.lex; -marked.InlineLexer = InlineLexer_1; -marked.inlineLexer = InlineLexer_1.output; - marked.Slugger = Slugger_1; marked.parse = marked; diff --git a/lib/marked.js b/lib/marked.js index af3f912356..d25ef2836c 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -510,10 +510,12 @@ }; var defaults$1 = defaults.defaults; - var block$1 = rules.block; + var block$1 = rules.block, + inline$1 = rules.inline; var rtrim$1 = helpers.rtrim, splitCells$1 = helpers.splitCells, - escape$1 = helpers.escape; + escape$1 = helpers.escape, + findClosingBracket$1 = helpers.findClosingBracket; /** * Block Lexer */ @@ -523,12 +525,22 @@ this.tokens = []; this.tokens.links = Object.create(null); this.options = options || defaults$1; - this.rules = block$1.normal; + this.rules = { + block: block$1.normal, + inline: inline$1.normal + }; if (this.options.pedantic) { - this.rules = block$1.pedantic; + this.rules.block = block$1.pedantic; + this.rules.inline = inline$1.pedantic; } else if (this.options.gfm) { - this.rules = block$1.gfm; + this.rules.block = block$1.gfm; + + if (this.options.breaks) { + this.rules.inline = inline$1.breaks; + } else { + this.rules.inline = inline$1.gfm; + } } } /** @@ -542,48 +554,61 @@ Lexer.lex = function lex(src, options) { var lexer = new Lexer(options); return lexer.lex(src); - }; - - var _proto = Lexer.prototype; - + } /** * Preprocessing */ + ; + + var _proto = Lexer.prototype; + _proto.lex = function lex(src) { src = src.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' '); - return this.token(src, true); - }; - + this.blockTokens(this.tokens, src); + this.inlineTokens(this.tokens); + return this.tokens; + } /** * Lexing */ - _proto.token = function token(src, top) { + ; + + _proto.blockTokens = function blockTokens(tokens, src, top) { + if (top === void 0) { + top = true; + } + src = src.replace(/^ +$/gm, ''); - var next, loose, cap, bull, b, item, listStart, listItems, t, space, i, tag, l, isordered, istask, ischecked; + var next, loose, cap, bull, b, item, list, space, i, tag, l, isordered, istask, ischecked, lastToken, addBack, raw; while (src) { // newline - if (cap = this.rules.newline.exec(src)) { + if (cap = this.rules.block.newline.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; if (cap[0].length > 1) { - this.tokens.push({ - type: 'space' + tokens.push({ + type: 'space', + raw: raw }); } } // code - if (cap = this.rules.code.exec(src)) { - var lastToken = this.tokens[this.tokens.length - 1]; - src = src.substring(cap[0].length); // An indented code block cannot interrupt a paragraph. + if (cap = this.rules.block.code.exec(src)) { + lastToken = tokens[tokens.length - 1]; + src = src.substring(cap[0].length); + raw = cap[0]; // An indented code block cannot interrupt a paragraph. if (lastToken && lastToken.type === 'paragraph') { lastToken.text += '\n' + cap[0].trimRight(); + lastToken.raw += '\n' + raw; } else { cap = cap[0].replace(/^ {4}/gm, ''); - this.tokens.push({ + tokens.push({ type: 'code', + raw: raw, codeBlockStyle: 'indented', text: !this.options.pedantic ? rtrim$1(cap, '\n') : cap }); @@ -593,10 +618,12 @@ } // fences - if (cap = this.rules.fences.exec(src)) { + if (cap = this.rules.block.fences.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'code', + raw: raw, lang: cap[2] ? cap[2].trim() : cap[2], text: cap[3] || '' }); @@ -604,10 +631,12 @@ } // heading - if (cap = this.rules.heading.exec(src)) { + if (cap = this.rules.block.heading.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'heading', + raw: raw, depth: cap[1].length, text: cap[2] }); @@ -615,7 +644,7 @@ } // table no leading pipe (gfm) - if (cap = this.rules.nptable.exec(src)) { + if (cap = this.rules.block.nptable.exec(src)) { item = { type: 'table', header: splitCells$1(cap[1].replace(/^ *| *\| *$/g, '')), @@ -625,8 +654,11 @@ if (item.header.length === item.align.length) { src = src.substring(cap[0].length); + raw = cap[0]; + item.raw = raw; + l = item.align.length; - for (i = 0; i < item.align.length; i++) { + for (i = 0; i < l; i++) { if (/^ *-+: *$/.test(item.align[i])) { item.align[i] = 'right'; } else if (/^ *:-+: *$/.test(item.align[i])) { @@ -638,62 +670,64 @@ } } - for (i = 0; i < item.cells.length; i++) { + l = item.cells.length; + + for (i = 0; i < l; i++) { item.cells[i] = splitCells$1(item.cells[i], item.header.length); } - this.tokens.push(item); + tokens.push(item); continue; } } // hr - if (cap = this.rules.hr.exec(src)) { + if (cap = this.rules.block.hr.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ - type: 'hr' + raw = cap[0]; + tokens.push({ + type: 'hr', + raw: raw }); continue; } // blockquote - if (cap = this.rules.blockquote.exec(src)) { + if (cap = this.rules.block.blockquote.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ - type: 'blockquote_start' - }); - cap = cap[0].replace(/^ *> ?/gm, ''); // Pass `top` to keep the current - // "toplevel" state. This is exactly - // how markdown.pl works. - - this.token(cap, top); - this.tokens.push({ - type: 'blockquote_end' + raw = cap[0]; + cap = cap[0].replace(/^ *> ?/gm, ''); + tokens.push({ + type: 'blockquote', + raw: raw, + tokens: this.blockTokens([], cap, top) }); continue; } // list - if (cap = this.rules.list.exec(src)) { + if (cap = this.rules.block.list.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; bull = cap[2]; isordered = bull.length > 1; - listStart = { - type: 'list_start', + list = { + type: 'list', + raw: raw, ordered: isordered, start: isordered ? +bull : '', - loose: false + loose: false, + items: [] }; - this.tokens.push(listStart); // Get each top-level item. + tokens.push(list); // Get each top-level item. - cap = cap[0].match(this.rules.item); - listItems = []; + cap = cap[0].match(this.rules.block.item); next = false; l = cap.length; - i = 0; - for (; i < l; i++) { - item = cap[i]; // Remove the list item's bullet + for (i = 0; i < l; i++) { + item = cap[i]; + raw = item.trim(); // Remove the list item's bullet // so it is seen as the next token. space = item.length; @@ -711,7 +745,9 @@ b = block$1.bullet.exec(cap[i + 1])[0]; if (bull.length > 1 ? b.length === 1 : b.length > 1 || this.options.smartLists && b !== bull) { - src = cap.slice(i + 1).join('\n') + src; + addBack = cap.slice(i + 1).join('\n'); + src = addBack + src; + list.raw = list.raw.substring(list.raw.length - addBack.length); i = l - 1; } } // Determine whether item is loose or not. @@ -727,7 +763,7 @@ } if (loose) { - listStart.loose = true; + list.loose = true; } // Check for task list items @@ -739,41 +775,25 @@ item = item.replace(/^\[[ xX]\] +/, ''); } - t = { - type: 'list_item_start', + list.items.push({ + raw: raw, task: istask, checked: ischecked, - loose: loose - }; - listItems.push(t); - this.tokens.push(t); // Recurse. - - this.token(item, false); - this.tokens.push({ - type: 'list_item_end' + loose: loose, + tokens: this.blockTokens([], item, false) }); } - if (listStart.loose) { - l = listItems.length; - i = 0; - - for (; i < l; i++) { - listItems[i].loose = true; - } - } - - this.tokens.push({ - type: 'list_end' - }); continue; } // html - if (cap = this.rules.html.exec(src)) { + if (cap = this.rules.block.html.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: this.options.sanitize ? 'paragraph' : 'html', + raw: raw, pre: !this.options.sanitizer && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'), text: this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$1(cap[0]) : cap[0] }); @@ -781,7 +801,7 @@ } // def - if (top && (cap = this.rules.def.exec(src))) { + if (top && (cap = this.rules.block.def.exec(src))) { src = src.substring(cap[0].length); if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1); tag = cap[1].toLowerCase().replace(/\s+/g, ' '); @@ -797,7 +817,7 @@ } // table (gfm) - if (cap = this.rules.table.exec(src)) { + if (cap = this.rules.block.table.exec(src)) { item = { type: 'table', header: splitCells$1(cap[1].replace(/^ *| *\| *$/g, '')), @@ -807,8 +827,10 @@ if (item.header.length === item.align.length) { src = src.substring(cap[0].length); + item.raw = cap[0]; + l = item.align.length; - for (i = 0; i < item.align.length; i++) { + for (i = 0; i < l; i++) { if (/^ *-+: *$/.test(item.align[i])) { item.align[i] = 'right'; } else if (/^ *:-+: *$/.test(item.align[i])) { @@ -820,20 +842,24 @@ } } - for (i = 0; i < item.cells.length; i++) { + l = item.cells.length; + + for (i = 0; i < l; i++) { item.cells[i] = splitCells$1(item.cells[i].replace(/^ *\| *| *\| *$/g, ''), item.header.length); } - this.tokens.push(item); + tokens.push(item); continue; } } // lheading - if (cap = this.rules.lheading.exec(src)) { + if (cap = this.rules.block.lheading.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'heading', + raw: raw, depth: cap[2].charAt(0) === '=' ? 1 : 2, text: cap[1] }); @@ -841,297 +867,148 @@ } // top-level paragraph - if (top && (cap = this.rules.paragraph.exec(src))) { + if (top && (cap = this.rules.block.paragraph.exec(src))) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'paragraph', + raw: raw, text: cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1] }); continue; } // text - if (cap = this.rules.text.exec(src)) { + if (cap = this.rules.block.text.exec(src)) { // Top-level should never reach here. src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'text', + raw: raw, text: cap[0] }); continue; } if (src) { - throw new Error('Infinite loop on byte: ' + src.charCodeAt(0)); - } - } - - return this.tokens; - }; - - _createClass(Lexer, null, [{ - key: "rules", - get: function get() { - return block$1; - } - }]); - - return Lexer; - }(); - - var defaults$2 = defaults.defaults; - var cleanUrl$1 = helpers.cleanUrl, - escape$2 = helpers.escape; - /** - * Renderer - */ - - var Renderer_1 = /*#__PURE__*/function () { - function Renderer(options) { - this.options = options || defaults$2; - } - - var _proto = Renderer.prototype; - - _proto.code = function code(_code, infostring, escaped) { - var lang = (infostring || '').match(/\S*/)[0]; - - if (this.options.highlight) { - var out = this.options.highlight(_code, lang); + var errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0); - if (out != null && out !== _code) { - escaped = true; - _code = out; + if (this.options.silent) { + console.error(errMsg); + } else { + throw new Error(errMsg); + } } } - if (!lang) { - return '
    ' + (escaped ? _code : escape$2(_code, true)) + '
    '; - } - - return '
    ' + (escaped ? _code : escape$2(_code, true)) + '
    \n'; - }; - - _proto.blockquote = function blockquote(quote) { - return '
    \n' + quote + '
    \n'; - }; - - _proto.html = function html(_html) { - return _html; - }; - - _proto.heading = function heading(text, level, raw, slugger) { - if (this.options.headerIds) { - return '' + text + '\n'; - } // ignore IDs - - - return '' + text + '\n'; - }; - - _proto.hr = function hr() { - return this.options.xhtml ? '
    \n' : '
    \n'; - }; - - _proto.list = function list(body, ordered, start) { - var type = ordered ? 'ol' : 'ul', - startatt = ordered && start !== 1 ? ' start="' + start + '"' : ''; - return '<' + type + startatt + '>\n' + body + '\n'; - }; - - _proto.listitem = function listitem(text) { - return '
  • ' + text + '
  • \n'; - }; - - _proto.checkbox = function checkbox(checked) { - return ' '; - }; - - _proto.paragraph = function paragraph(text) { - return '

    ' + text + '

    \n'; - }; - - _proto.table = function table(header, body) { - if (body) body = '' + body + ''; - return '\n' + '\n' + header + '\n' + body + '
    \n'; - }; - - _proto.tablerow = function tablerow(content) { - return '\n' + content + '\n'; - }; - - _proto.tablecell = function tablecell(content, flags) { - var type = flags.header ? 'th' : 'td'; - var tag = flags.align ? '<' + type + ' align="' + flags.align + '">' : '<' + type + '>'; - return tag + content + '\n'; - }; - - // span level renderer - _proto.strong = function strong(text) { - return '' + text + ''; - }; - - _proto.em = function em(text) { - return '' + text + ''; - }; - - _proto.codespan = function codespan(text) { - return '' + text + ''; - }; - - _proto.br = function br() { - return this.options.xhtml ? '
    ' : '
    '; - }; - - _proto.del = function del(text) { - return '' + text + ''; - }; - - _proto.link = function link(href, title, text) { - href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); - - if (href === null) { - return text; - } - - var out = '
    '; - return out; - }; - - _proto.image = function image(href, title, text) { - href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); - - if (href === null) { - return text; - } - - var out = '' + text + '' : '>'; - return out; - }; - - _proto.text = function text(_text) { - return _text; + return tokens; }; - return Renderer; - }(); + _proto.inlineTokens = function inlineTokens(tokens) { + var i, j, k, l2, row, token; + var l = tokens.length; + + for (i = 0; i < l; i++) { + token = tokens[i]; + + switch (token.type) { + case 'paragraph': + case 'text': + case 'heading': + { + token.tokens = []; + this.inlineOutput(token.text, token.tokens); + break; + } - /** - * Slugger generates header id - */ - var Slugger_1 = /*#__PURE__*/function () { - function Slugger() { - this.seen = {}; - } - /** - * Convert string to unique id - */ + case 'table': + { + token.tokens = { + header: [], + cells: [] + }; // header + l2 = token.header.length; - var _proto = Slugger.prototype; + for (j = 0; j < l2; j++) { + token.tokens.header[j] = []; + this.inlineOutput(token.header[j], token.tokens.header[j]); + } // cells - _proto.slug = function slug(value) { - var slug = value.toLowerCase().trim() // remove html tags - .replace(/<[!\/a-z].*?>/ig, '') // remove unwanted chars - .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '').replace(/\s/g, '-'); - if (this.seen.hasOwnProperty(slug)) { - var originalSlug = slug; + l2 = token.cells.length; - do { - this.seen[originalSlug]++; - slug = originalSlug + '-' + this.seen[originalSlug]; - } while (this.seen.hasOwnProperty(slug)); - } + for (j = 0; j < l2; j++) { + row = token.cells[j]; + token.tokens.cells[j] = []; - this.seen[slug] = 0; - return slug; - }; + for (k = 0; k < row.length; k++) { + token.tokens.cells[j][k] = []; + this.inlineOutput(row[k], token.tokens.cells[j][k]); + } + } - return Slugger; - }(); + break; + } - var defaults$3 = defaults.defaults; - var inline$1 = rules.inline; - var findClosingBracket$1 = helpers.findClosingBracket, - escape$3 = helpers.escape; - /** - * Inline Lexer & Compiler - */ + case 'blockquote': + { + this.inlineTokens(token.tokens); + break; + } - var InlineLexer_1 = /*#__PURE__*/function () { - function InlineLexer(links, options) { - this.options = options || defaults$3; - this.links = links; - this.rules = inline$1.normal; - this.options.renderer = this.options.renderer || new Renderer_1(); - this.renderer = this.options.renderer; - this.renderer.options = this.options; + case 'list': + { + l2 = token.items.length; - if (!this.links) { - throw new Error('Tokens array requires a `links` property.'); - } + for (j = 0; j < l2; j++) { + this.inlineTokens(token.items[j].tokens); + } - if (this.options.pedantic) { - this.rules = inline$1.pedantic; - } else if (this.options.gfm) { - if (this.options.breaks) { - this.rules = inline$1.breaks; - } else { - this.rules = inline$1.gfm; + break; + } } } - } - /** - * Expose Inline Rules - */ - - /** - * Static Lexing/Compiling Method - */ - InlineLexer.output = function output(src, links, options) { - var inline = new InlineLexer(links, options); - return inline.output(src); + return tokens; } /** * Lexing/Compiling */ ; - var _proto = InlineLexer.prototype; - - _proto.output = function output(src) { + _proto.inlineOutput = function inlineOutput(src, tokens) { var out = '', link, text, + newTokens, href, title, cap, - prevCapZero; + prevCapZero, + lastParenIndex, + start, + linkLen, + raw; while (src) { // escape - if (cap = this.rules.escape.exec(src)) { + if (cap = this.rules.inline.escape.exec(src)) { src = src.substring(cap[0].length); - out += escape$3(cap[1]); + raw = cap[0]; + text = escape$1(cap[1]); + out += text; + tokens.push({ + type: 'escape', + raw: raw, + text: text + }); continue; } // tag - if (cap = this.rules.tag.exec(src)) { + if (cap = this.rules.inline.tag.exec(src)) { if (!this.inLink && /^/i.test(cap[0])) { @@ -1145,23 +1022,31 @@ } src = src.substring(cap[0].length); - out += this.renderer.html(this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$3(cap[0]) : cap[0]); + raw = cap[0]; + text = this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$1(cap[0]) : cap[0]; + tokens.push({ + type: this.options.sanitize ? 'text' : 'html', + raw: raw, + text: text + }); + out += text; continue; } // link - if (cap = this.rules.link.exec(src)) { - var lastParenIndex = findClosingBracket$1(cap[2], '()'); + if (cap = this.rules.inline.link.exec(src)) { + lastParenIndex = findClosingBracket$1(cap[2], '()'); if (lastParenIndex > -1) { - var start = cap[0].indexOf('!') === 0 ? 5 : 4; - var linkLen = start + cap[1].length + lastParenIndex; + start = cap[0].indexOf('!') === 0 ? 5 : 4; + linkLen = start + cap[1].length + lastParenIndex; cap[2] = cap[2].substring(0, lastParenIndex); cap[0] = cap[0].substring(0, linkLen).trim(); cap[3] = ''; } src = src.substring(cap[0].length); + raw = cap[0]; this.inLink = true; href = cap[2]; @@ -1180,95 +1065,153 @@ href = href.trim().replace(/^<([\s\S]*)>$/, '$1'); out += this.outputLink(cap, { - href: InlineLexer.escapes(href), - title: InlineLexer.escapes(title) - }); + href: this.escapes(href), + title: this.escapes(title) + }, tokens, raw); this.inLink = false; continue; } // reflink, nolink - if ((cap = this.rules.reflink.exec(src)) || (cap = this.rules.nolink.exec(src))) { + if ((cap = this.rules.inline.reflink.exec(src)) || (cap = this.rules.inline.nolink.exec(src))) { src = src.substring(cap[0].length); + raw = cap[0]; link = (cap[2] || cap[1]).replace(/\s+/g, ' '); - link = this.links[link.toLowerCase()]; + link = this.tokens.links[link.toLowerCase()]; if (!link || !link.href) { - out += cap[0].charAt(0); + text = cap[0].charAt(0); + out += text; + tokens.push({ + type: 'text', + raw: text, + text: text + }); src = cap[0].substring(1) + src; continue; } this.inLink = true; - out += this.outputLink(cap, link); + out += this.outputLink(cap, link, tokens, raw); this.inLink = false; continue; } // strong - if (cap = this.rules.strong.exec(src)) { + if (cap = this.rules.inline.strong.exec(src)) { src = src.substring(cap[0].length); - out += this.renderer.strong(this.output(cap[4] || cap[3] || cap[2] || cap[1])); + raw = cap[0]; + newTokens = tokens ? [] : null; + text = this.inlineOutput(cap[4] || cap[3] || cap[2] || cap[1], newTokens); + tokens.push({ + type: 'strong', + raw: raw, + text: text, + tokens: newTokens + }); + out += text; continue; } // em - if (cap = this.rules.em.exec(src)) { + if (cap = this.rules.inline.em.exec(src)) { src = src.substring(cap[0].length); - out += this.renderer.em(this.output(cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1])); + raw = cap[0]; + newTokens = tokens ? [] : null; + text = this.inlineOutput(cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1], newTokens); + tokens.push({ + type: 'em', + raw: raw, + text: text, + tokens: newTokens + }); + out += text; continue; } // code - if (cap = this.rules.code.exec(src)) { + if (cap = this.rules.inline.code.exec(src)) { src = src.substring(cap[0].length); - out += this.renderer.codespan(escape$3(cap[2].trim(), true)); + raw = cap[0]; + text = escape$1(cap[2].trim(), true); + tokens.push({ + type: 'codespan', + raw: raw, + text: text + }); + out += text; continue; } // br - if (cap = this.rules.br.exec(src)) { + if (cap = this.rules.inline.br.exec(src)) { src = src.substring(cap[0].length); - out += this.renderer.br(); + raw = cap[0]; + tokens.push({ + type: 'br', + raw: raw + }); + out += '\n'; continue; } // del (gfm) - if (cap = this.rules.del.exec(src)) { + if (cap = this.rules.inline.del.exec(src)) { src = src.substring(cap[0].length); - out += this.renderer.del(this.output(cap[1])); + raw = cap[0]; + newTokens = tokens ? [] : null; + text = this.inlineOutput(cap[1], newTokens); + tokens.push({ + type: 'del', + raw: raw, + text: text, + tokens: newTokens + }); + out += text; continue; } // autolink - if (cap = this.rules.autolink.exec(src)) { + if (cap = this.rules.inline.autolink.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; if (cap[2] === '@') { - text = escape$3(this.mangle(cap[1])); + text = escape$1(this.options.mangle ? this.mangle(cap[1]) : cap[1]); href = 'mailto:' + text; } else { - text = escape$3(cap[1]); + text = escape$1(cap[1]); href = text; } - out += this.renderer.link(href, null, text); + tokens.push({ + type: 'link', + raw: raw, + text: text, + href: href, + tokens: [{ + type: 'text', + raw: text, + text: text + }] + }); + out += text; continue; } // url (gfm) - if (!this.inLink && (cap = this.rules.url.exec(src))) { + if (!this.inLink && (cap = this.rules.inline.url.exec(src))) { if (cap[2] === '@') { - text = escape$3(cap[0]); + text = escape$1(this.options.mangle ? this.mangle(cap[0]) : cap[0]); href = 'mailto:' + text; } else { // do extended autolink path validation do { prevCapZero = cap[0]; - cap[0] = this.rules._backpedal.exec(cap[0])[0]; + cap[0] = this.rules.inline._backpedal.exec(cap[0])[0]; } while (prevCapZero !== cap[0]); - text = escape$3(cap[0]); + text = escape$1(cap[0]); if (cap[1] === 'www.') { href = 'http://' + text; @@ -1278,43 +1221,92 @@ } src = src.substring(cap[0].length); - out += this.renderer.link(href, null, text); + raw = cap[0]; + tokens.push({ + type: 'link', + raw: raw, + text: text, + href: href, + tokens: [{ + type: 'text', + raw: text, + text: text + }] + }); + out += text; continue; } // text - if (cap = this.rules.text.exec(src)) { + if (cap = this.rules.inline.text.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; if (this.inRawBlock) { - out += this.renderer.text(this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$3(cap[0]) : cap[0]); + text = this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape$1(cap[0]) : cap[0]; } else { - out += this.renderer.text(escape$3(this.smartypants(cap[0]))); + text = escape$1(this.options.smartypants ? this.smartypants(cap[0]) : cap[0]); } + tokens.push({ + type: 'text', + raw: raw, + text: text + }); + out += text; continue; } if (src) { - throw new Error('Infinite loop on byte: ' + src.charCodeAt(0)); + var errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0); + + if (this.options.silent) { + console.error(errMsg); + } else { + throw new Error(errMsg); + } } } return out; }; - InlineLexer.escapes = function escapes(text) { - return text ? text.replace(InlineLexer.rules._escapes, '$1') : text; + _proto.escapes = function escapes(text) { + return text ? text.replace(inline$1._escapes, '$1') : text; } /** - * Compile Link + * tokenize Link */ ; - _proto.outputLink = function outputLink(cap, link) { - var href = link.href, - title = link.title ? escape$3(link.title) : null; - return cap[0].charAt(0) !== '!' ? this.renderer.link(href, title, this.output(cap[1])) : this.renderer.image(href, title, escape$3(cap[1])); + _proto.outputLink = function outputLink(cap, link, tokens, raw) { + var href = link.href; + var title = link.title ? escape$1(link.title) : null; + var newTokens = tokens ? [] : null; + + if (cap[0].charAt(0) !== '!') { + var text = this.inlineOutput(cap[1], newTokens); + tokens.push({ + type: 'link', + raw: raw, + text: text, + href: href, + title: title, + tokens: newTokens + }); + return text; + } else { + var _text = escape$1(cap[1]); + + tokens.push({ + type: 'image', + raw: raw, + text: _text, + href: href, + title: title + }); + return _text; + } } /** * Smartypants Transformations @@ -1322,7 +1314,6 @@ ; _proto.smartypants = function smartypants(text) { - if (!this.options.smartypants) return text; return text // em-dashes .replace(/---/g, "\u2014") // en-dashes .replace(/--/g, "\u2013") // opening singles @@ -1338,13 +1329,12 @@ ; _proto.mangle = function mangle(text) { - if (!this.options.mangle) return text; - var l = text.length; var out = '', - i = 0, + i, ch; + var l = text.length; - for (; i < l; i++) { + for (i = 0; i < l; i++) { ch = text.charCodeAt(i); if (Math.random() > 0.5) { @@ -1357,14 +1347,166 @@ return out; }; - _createClass(InlineLexer, null, [{ + _createClass(Lexer, null, [{ key: "rules", get: function get() { - return inline$1; + return { + block: block$1, + inline: inline$1 + }; } }]); - return InlineLexer; + return Lexer; + }(); + + var defaults$2 = defaults.defaults; + var cleanUrl$1 = helpers.cleanUrl, + escape$2 = helpers.escape; + /** + * Renderer + */ + + var Renderer_1 = /*#__PURE__*/function () { + function Renderer(options) { + this.options = options || defaults$2; + } + + var _proto = Renderer.prototype; + + _proto.code = function code(_code, infostring, escaped) { + var lang = (infostring || '').match(/\S*/)[0]; + + if (this.options.highlight) { + var out = this.options.highlight(_code, lang); + + if (out != null && out !== _code) { + escaped = true; + _code = out; + } + } + + if (!lang) { + return '
    ' + (escaped ? _code : escape$2(_code, true)) + '
    '; + } + + return '
    ' + (escaped ? _code : escape$2(_code, true)) + '
    \n'; + }; + + _proto.blockquote = function blockquote(quote) { + return '
    \n' + quote + '
    \n'; + }; + + _proto.html = function html(_html) { + return _html; + }; + + _proto.heading = function heading(text, level, raw, slugger) { + if (this.options.headerIds) { + return '' + text + '\n'; + } // ignore IDs + + + return '' + text + '\n'; + }; + + _proto.hr = function hr() { + return this.options.xhtml ? '
    \n' : '
    \n'; + }; + + _proto.list = function list(body, ordered, start) { + var type = ordered ? 'ol' : 'ul', + startatt = ordered && start !== 1 ? ' start="' + start + '"' : ''; + return '<' + type + startatt + '>\n' + body + '\n'; + }; + + _proto.listitem = function listitem(text) { + return '
  • ' + text + '
  • \n'; + }; + + _proto.checkbox = function checkbox(checked) { + return ' '; + }; + + _proto.paragraph = function paragraph(text) { + return '

    ' + text + '

    \n'; + }; + + _proto.table = function table(header, body) { + if (body) body = '' + body + ''; + return '\n' + '\n' + header + '\n' + body + '
    \n'; + }; + + _proto.tablerow = function tablerow(content) { + return '\n' + content + '\n'; + }; + + _proto.tablecell = function tablecell(content, flags) { + var type = flags.header ? 'th' : 'td'; + var tag = flags.align ? '<' + type + ' align="' + flags.align + '">' : '<' + type + '>'; + return tag + content + '\n'; + } // span level renderer + ; + + _proto.strong = function strong(text) { + return '' + text + ''; + }; + + _proto.em = function em(text) { + return '' + text + ''; + }; + + _proto.codespan = function codespan(text) { + return '' + text + ''; + }; + + _proto.br = function br() { + return this.options.xhtml ? '
    ' : '
    '; + }; + + _proto.del = function del(text) { + return '' + text + ''; + }; + + _proto.link = function link(href, title, text) { + href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); + + if (href === null) { + return text; + } + + var out = '
    '; + return out; + }; + + _proto.image = function image(href, title, text) { + href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); + + if (href === null) { + return text; + } + + var out = '' + text + '' : '>'; + return out; + }; + + _proto.text = function text(_text) { + return _text; + }; + + return Renderer; }(); /** @@ -1416,20 +1558,53 @@ return TextRenderer; }(); - var defaults$4 = defaults.defaults; - var merge$2 = helpers.merge, - unescape$1 = helpers.unescape; + /** + * Slugger generates header id + */ + var Slugger_1 = /*#__PURE__*/function () { + function Slugger() { + this.seen = {}; + } + /** + * Convert string to unique id + */ + + + var _proto = Slugger.prototype; + + _proto.slug = function slug(value) { + var slug = value.toLowerCase().trim() // remove html tags + .replace(/<[!\/a-z].*?>/ig, '') // remove unwanted chars + .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '').replace(/\s/g, '-'); + + if (this.seen.hasOwnProperty(slug)) { + var originalSlug = slug; + + do { + this.seen[originalSlug]++; + slug = originalSlug + '-' + this.seen[originalSlug]; + } while (this.seen.hasOwnProperty(slug)); + } + + this.seen[slug] = 0; + return slug; + }; + + return Slugger; + }(); + + var defaults$3 = defaults.defaults; + var unescape$1 = helpers.unescape; /** * Parsing & Compiling */ var Parser_1 = /*#__PURE__*/function () { function Parser(options) { - this.tokens = []; - this.token = null; - this.options = options || defaults$4; + this.options = options || defaults$3; this.options.renderer = this.options.renderer || new Renderer_1(); this.renderer = this.options.renderer; + this.textRenderer = new TextRenderer_1(); this.renderer.options = this.options; this.slugger = new Slugger_1(); } @@ -1441,212 +1616,296 @@ Parser.parse = function parse(tokens, options) { var parser = new Parser(options); return parser.parse(tokens); - }; - - var _proto = Parser.prototype; - + } /** * Parse Loop */ - _proto.parse = function parse(tokens) { - this.inline = new InlineLexer_1(tokens.links, this.options); // use an InlineLexer with a TextRenderer to extract pure text + ; - this.inlineText = new InlineLexer_1(tokens.links, merge$2({}, this.options, { - renderer: new TextRenderer_1() - })); - this.tokens = tokens.reverse(); - var out = ''; + var _proto = Parser.prototype; - while (this.next()) { - out += this.tok(); + _proto.parse = function parse(tokens, top) { + if (top === void 0) { + top = true; } - return out; - }; + var out = '', + i, + j, + k, + l2, + l3, + row, + cell, + header, + body, + token, + ordered, + start, + loose, + itemBody, + item, + checked, + task, + checkbox; + var l = tokens.length; + + for (i = 0; i < l; i++) { + token = tokens[i]; + + switch (token.type) { + case 'space': + { + continue; + } - /** - * Next Token - */ - _proto.next = function next() { - this.token = this.tokens.pop(); - return this.token; - }; + case 'hr': + { + out += this.renderer.hr(); + continue; + } - /** - * Preview Next Token - */ - _proto.peek = function peek() { - return this.tokens[this.tokens.length - 1] || 0; - }; + case 'heading': + { + out += this.renderer.heading(this.parseInline(token.tokens), token.depth, unescape$1(this.parseInline(token.tokens, this.textRenderer)), this.slugger); + continue; + } - /** - * Parse Text Tokens - */ - _proto.parseText = function parseText() { - var body = this.token.text; + case 'code': + { + out += this.renderer.code(token.text, token.lang, token.escaped); + continue; + } - while (this.peek().type === 'text') { - body += '\n' + this.next().text; - } + case 'table': + { + header = ''; // header - return this.inline.output(body); - }; + cell = ''; + l2 = token.header.length; - /** - * Parse Current Token - */ - _proto.tok = function tok() { - var body = ''; + for (j = 0; j < l2; j++) { + cell += this.renderer.tablecell(this.parseInline(token.tokens.header[j]), { + header: true, + align: token.align[j] + }); + } - switch (this.token.type) { - case 'space': - { - return ''; - } + header += this.renderer.tablerow(cell); + body = ''; + l2 = token.cells.length; - case 'hr': - { - return this.renderer.hr(); - } + for (j = 0; j < l2; j++) { + row = token.tokens.cells[j]; + cell = ''; + l3 = row.length; - case 'heading': - { - return this.renderer.heading(this.inline.output(this.token.text), this.token.depth, unescape$1(this.inlineText.output(this.token.text)), this.slugger); - } + for (k = 0; k < l3; k++) { + cell += this.renderer.tablecell(this.parseInline(row[k]), { + header: false, + align: token.align[k] + }); + } - case 'code': - { - return this.renderer.code(this.token.text, this.token.lang, this.token.escaped); - } + body += this.renderer.tablerow(cell); + } - case 'table': - { - var header = '', - i, - row, - cell, - j; // header - - cell = ''; - - for (i = 0; i < this.token.header.length; i++) { - cell += this.renderer.tablecell(this.inline.output(this.token.header[i]), { - header: true, - align: this.token.align[i] - }); + out += this.renderer.table(header, body); + continue; } - header += this.renderer.tablerow(cell); + case 'blockquote': + { + body = this.parse(token.tokens); + out += this.renderer.blockquote(body); + continue; + } - for (i = 0; i < this.token.cells.length; i++) { - row = this.token.cells[i]; - cell = ''; + case 'list': + { + ordered = token.ordered; + start = token.start; + loose = token.loose; + l2 = token.items.length; + body = ''; + + for (j = 0; j < l2; j++) { + item = token.items[j]; + checked = item.checked; + task = item.task; + itemBody = ''; + + if (item.task) { + checkbox = this.renderer.checkbox(checked); + + if (loose) { + if (item.tokens[0].type === 'text') { + item.tokens[0].text = checkbox + ' ' + item.tokens[0].text; + + if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') { + item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text; + } + } else { + item.tokens.unshift({ + type: 'text', + text: checkbox + }); + } + } else { + itemBody += checkbox; + } + } - for (j = 0; j < row.length; j++) { - cell += this.renderer.tablecell(this.inline.output(row[j]), { - header: false, - align: this.token.align[j] - }); + itemBody += this.parse(item.tokens, loose); + body += this.renderer.listitem(itemBody, task, checked); } - body += this.renderer.tablerow(cell); + out += this.renderer.list(body, ordered, start); + continue; } - return this.renderer.table(header, body); - } - - case 'blockquote_start': - { - body = ''; + case 'html': + { + // TODO parse inline content if parameter markdown=1 + out += this.renderer.html(token.text); + continue; + } - while (this.next().type !== 'blockquote_end') { - body += this.tok(); + case 'paragraph': + { + out += this.renderer.paragraph(this.parseInline(token.tokens)); + continue; } - return this.renderer.blockquote(body); - } + case 'text': + { + body = token.tokens ? this.parseInline(token.tokens) : token.text; - case 'list_start': - { - body = ''; - var ordered = this.token.ordered, - start = this.token.start; + while (i + 1 < l && tokens[i + 1].type === 'text') { + token = tokens[++i]; + body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text); + } - while (this.next().type !== 'list_end') { - body += this.tok(); + out += top ? this.renderer.paragraph(body) : body; + continue; } - return this.renderer.list(body, ordered, start); - } + default: + { + var errMsg = 'Token with "' + token.type + '" type was not found.'; - case 'list_item_start': - { - body = ''; - var loose = this.token.loose; - var checked = this.token.checked; - var task = this.token.task; - - if (this.token.task) { - if (loose) { - if (this.peek().type === 'text') { - var nextToken = this.peek(); - nextToken.text = this.renderer.checkbox(checked) + ' ' + nextToken.text; - } else { - this.tokens.push({ - type: 'text', - text: this.renderer.checkbox(checked) - }); - } + if (this.options.silent) { + console.error(errMsg); } else { - body += this.renderer.checkbox(checked); + throw new Error(errMsg); } } + } + } - while (this.next().type !== 'list_item_end') { - body += !loose && this.token.type === 'text' ? this.parseText() : this.tok(); + return out; + } + /** + * Parse Inline Tokens + */ + ; + + _proto.parseInline = function parseInline(tokens, renderer) { + renderer = renderer || this.renderer; + var out = '', + i, + token; + var l = tokens.length; + + for (i = 0; i < l; i++) { + token = tokens[i]; + + switch (token.type) { + case 'escape': + { + out += token.text; + break; } - return this.renderer.listitem(body, task, checked); - } + case 'html': + { + out += renderer.html(token.text); + break; + } - case 'html': - { - // TODO parse inline content if parameter markdown=1 - return this.renderer.html(this.token.text); - } + case 'link': + { + out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer)); + break; + } - case 'paragraph': - { - return this.renderer.paragraph(this.inline.output(this.token.text)); - } + case 'image': + { + out += renderer.image(token.href, token.title, token.text); + break; + } - case 'text': - { - return this.renderer.paragraph(this.parseText()); - } + case 'strong': + { + out += renderer.strong(this.parseInline(token.tokens, renderer)); + break; + } - default: - { - var errMsg = 'Token with "' + this.token.type + '" type was not found.'; + case 'em': + { + out += renderer.em(this.parseInline(token.tokens, renderer)); + break; + } - if (this.options.silent) { - console.log(errMsg); - } else { - throw new Error(errMsg); + case 'codespan': + { + out += renderer.codespan(token.text); + break; } - } + + case 'br': + { + out += renderer.br(); + break; + } + + case 'del': + { + out += renderer.del(this.parseInline(token.tokens, renderer)); + break; + } + + case 'text': + { + out += renderer.text(token.text); + break; + } + + default: + { + var errMsg = 'Token with "' + this.token.type + '" type was not found.'; + + if (this.options.silent) { + console.error(errMsg); + } else { + throw new Error(errMsg); + } + } + } } + + return out; }; return Parser; }(); - var merge$3 = helpers.merge, + var merge$2 = helpers.merge, checkSanitizeDeprecation$1 = helpers.checkSanitizeDeprecation, - escape$4 = helpers.escape; + escape$3 = helpers.escape; var getDefaults = defaults.getDefaults, changeDefaults = defaults.changeDefaults, - defaults$5 = defaults.defaults; + defaults$4 = defaults.defaults; /** * Marked */ @@ -1668,7 +1927,7 @@ opt = null; } - opt = merge$3({}, marked.defaults, opt || {}); + opt = merge$2({}, marked.defaults, opt || {}); checkSanitizeDeprecation$1(opt); var highlight = opt.highlight; var tokens, @@ -1743,14 +2002,14 @@ } try { - opt = merge$3({}, marked.defaults, opt || {}); + opt = merge$2({}, marked.defaults, opt || {}); checkSanitizeDeprecation$1(opt); return Parser_1.parse(Lexer_1.lex(src, opt), opt); } catch (e) { e.message += '\nPlease report this to https://github.com/markedjs/marked.'; if ((opt || marked.defaults).silent) { - return '

    An error occurred:

    ' + escape$4(e.message + '', true) + '
    '; + return '

    An error occurred:

    ' + escape$3(e.message + '', true) + '
    '; } throw e; @@ -1762,13 +2021,13 @@ marked.options = marked.setOptions = function (opt) { - merge$3(marked.defaults, opt); + merge$2(marked.defaults, opt); changeDefaults(marked.defaults); return marked; }; marked.getDefaults = getDefaults; - marked.defaults = defaults$5; + marked.defaults = defaults$4; /** * Expose */ @@ -1779,8 +2038,6 @@ marked.TextRenderer = TextRenderer_1; marked.Lexer = Lexer_1; marked.lexer = Lexer_1.lex; - marked.InlineLexer = InlineLexer_1; - marked.inlineLexer = InlineLexer_1.output; marked.Slugger = Slugger_1; marked.parse = marked; var marked_1 = marked; diff --git a/marked.min.js b/marked.min.js index 7c4f6a0866..d63d3952b0 100644 --- a/marked.min.js +++ b/marked.min.js @@ -3,4 +3,4 @@ * Copyright (c) 2011-2020, Christopher Jeffrey. (MIT Licensed) * https://github.com/markedjs/marked */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).marked=t()}(this,function(){"use strict";function r(e,t){for(var n=0;n"']/),l=/[&<>"']/g,a=/[<>"']|&(?!#?\w+;)/,o=/[<>"']|&(?!#?\w+;)/g,h={"&":"&","<":"<",">":">",'"':""","'":"'"};var u=/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/gi;function c(e){return e.replace(u,function(e,t){return"colon"===(t=t.toLowerCase())?":":"#"===t.charAt(0)?"x"===t.charAt(1)?String.fromCharCode(parseInt(t.substring(2),16)):String.fromCharCode(+t.substring(1)):""})}var p=/(^|[^\[])\^/g;var g=/[^\w:]/g,f=/^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;var d={},b=/^[^:]+:\/*[^/]*$/,m=/^([^:]+:)[\s\S]*$/,k=/^([^:]+:\/*[^/]*)[\s\S]*$/;function x(e,t){d[" "+e]||(b.test(e)?d[" "+e]=e+"/":d[" "+e]=_(e,"/",!0));var n=-1===(e=d[" "+e]).indexOf(":");return"//"===t.substring(0,2)?n?t:e.replace(m,"$1")+t:"/"===t.charAt(0)?n?t:e.replace(k,"$1")+t:e+t}function _(e,t,n){var r=e.length;if(0===r)return"";for(var s=0;st)n.splice(t);else for(;n.length ?(paragraph|[^\n]*)(?:\n|$))+/,list:/^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,html:"^ {0,3}(?:<(script|pre|style)[\\s>][\\s\\S]*?(?:[^\\n]*\\n+|$)|comment[^\\n]*(\\n+|$)|<\\?[\\s\\S]*?\\?>\\n*|\\n*|\\n*|)[\\s\\S]*?(?:\\n{2,}|$)|<(?!script|pre|style)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$)|(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$))",def:/^ {0,3}\[(label)\]: *\n? *]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,nptable:Z,table:Z,lheading:/^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,_paragraph:/^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html)[^\n]+)*)/,text:/^[^\n]+/,_label:/(?!\s*\])(?:\\[\[\]]|[^\[\]])+/,_title:/(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/};C.def=q(C.def).replace("label",C._label).replace("title",C._title).getRegex(),C.bullet=/(?:[*+-]|\d{1,9}\.)/,C.item=/^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/,C.item=q(C.item,"gm").replace(/bull/g,C.bullet).getRegex(),C.list=q(C.list).replace(/bull/g,C.bullet).replace("hr","\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))").replace("def","\\n+(?="+C.def.source+")").getRegex(),C._tag="address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul",C._comment=//,C.html=q(C.html,"i").replace("comment",C._comment).replace("tag",C._tag).replace("attribute",/ +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex(),C.paragraph=q(C._paragraph).replace("hr",C.hr).replace("heading"," {0,3}#{1,6} ").replace("|lheading","").replace("blockquote"," {0,3}>").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)]) ").replace("html",")|<(?:script|pre|style|!--)").replace("tag",C._tag).getRegex(),C.blockquote=q(C.blockquote).replace("paragraph",C.paragraph).getRegex(),C.normal=L({},C),C.gfm=L({},C.normal,{nptable:"^ *([^|\\n ].*\\|.*)\\n *([-:]+ *\\|[-| :]*)(?:\\n((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)",table:"^ *\\|(.+)\\n *\\|?( *[-:]+[-| :]*)(?:\\n *((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)"}),C.gfm.nptable=q(C.gfm.nptable).replace("hr",C.hr).replace("heading"," {0,3}#{1,6} ").replace("blockquote"," {0,3}>").replace("code"," {4}[^\\n]").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)]) ").replace("html",")|<(?:script|pre|style|!--)").replace("tag",C._tag).getRegex(),C.gfm.table=q(C.gfm.table).replace("hr",C.hr).replace("heading"," {0,3}#{1,6} ").replace("blockquote"," {0,3}>").replace("code"," {4}[^\\n]").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)]) ").replace("html",")|<(?:script|pre|style|!--)").replace("tag",C._tag).getRegex(),C.pedantic=L({},C.normal,{html:q("^ *(?:comment *(?:\\n|\\s*$)|<(tag)[\\s\\S]+? *(?:\\n{2,}|\\s*$)|\\s]*)*?/?> *(?:\\n{2,}|\\s*$))").replace("comment",C._comment).replace(/tag/g,"(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:|[^\\w\\s@]*@)\\b").getRegex(),def:/^ *\[([^\]]+)\]: *]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,heading:/^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/,fences:Z,paragraph:q(C.normal._paragraph).replace("hr",C.hr).replace("heading"," *#{1,6} *[^\n]").replace("lheading",C.lheading).replace("blockquote"," {0,3}>").replace("|fences","").replace("|list","").replace("|html","").getRegex()});var O={escape:/^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,autolink:/^<(scheme:[^\s\x00-\x1f<>]*|email)>/,url:Z,tag:"^comment|^|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>|^<\\?[\\s\\S]*?\\?>|^|^",link:/^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,reflink:/^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,nolink:/^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,strong:/^__([^\s_])__(?!_)|^\*\*([^\s*])\*\*(?!\*)|^__([^\s][\s\S]*?[^\s])__(?!_)|^\*\*([^\s][\s\S]*?[^\s])\*\*(?!\*)/,em:/^_([^\s_])_(?!_)|^\*([^\s*<\[])\*(?!\*)|^_([^\s<][\s\S]*?[^\s_])_(?!_|[^\spunctuation])|^_([^\s_<][\s\S]*?[^\s])_(?!_|[^\spunctuation])|^\*([^\s<"][\s\S]*?[^\s\*])\*(?!\*|[^\spunctuation])|^\*([^\s*"<\[][\s\S]*?[^\s])\*(?!\*)/,code:/^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,br:/^( {2,}|\\)\n(?!\s*$)/,del:Z,text:/^(`+|[^`])(?:[\s\S]*?(?:(?=[\\?@\\[^_{|}~"};O.em=q(O.em).replace(/punctuation/g,O._punctuation).getRegex(),O._escapes=/\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g,O._scheme=/[a-zA-Z][a-zA-Z0-9+.-]{1,31}/,O._email=/[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/,O.autolink=q(O.autolink).replace("scheme",O._scheme).replace("email",O._email).getRegex(),O._attribute=/\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/,O.tag=q(O.tag).replace("comment",C._comment).replace("attribute",O._attribute).getRegex(),O._label=/(?:\[[^\[\]]*\]|\\.|`[^`]*`|[^\[\]\\`])*?/,O._href=/<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*/,O._title=/"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/,O.link=q(O.link).replace("label",O._label).replace("href",O._href).replace("title",O._title).getRegex(),O.reflink=q(O.reflink).replace("label",O._label).getRegex(),O.normal=L({},O),O.pedantic=L({},O.normal,{strong:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,em:/^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/,link:q(/^!?\[(label)\]\((.*?)\)/).replace("label",O._label).getRegex(),reflink:q(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace("label",O._label).getRegex()}),O.gfm=L({},O.normal,{escape:q(O.escape).replace("])","~|])").getRegex(),_extended_email:/[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,url:/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,_backpedal:/(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,del:/^~+(?=\S)([\s\S]*?\S)~+/,text:/^(`+|[^`])(?:[\s\S]*?(?:(?=[\\ ?/gm,""),this.token(s,t),this.tokens.push({type:"blockquote_end"});else if(s=this.rules.list.exec(e)){for(e=e.substring(s[0].length),o={type:"list_start",ordered:d=1<(i=s[2]).length,start:d?+i:"",loose:!1},this.tokens.push(o),n=!(h=[]),f=(s=s[0].match(this.rules.item)).length,p=0;p'+(n?e:N(e,!0))+"\n":"
    "+(n?e:N(e,!0))+"
    "},t.blockquote=function(e){return"
    \n"+e+"
    \n"},t.html=function(e){return e},t.heading=function(e,t,n,r){return this.options.headerIds?"'+e+"\n":""+e+"\n"},t.hr=function(){return this.options.xhtml?"
    \n":"
    \n"},t.list=function(e,t,n){var r=t?"ol":"ul";return"<"+r+(t&&1!==n?' start="'+n+'"':"")+">\n"+e+"\n"},t.listitem=function(e){return"
  • "+e+"
  • \n"},t.checkbox=function(e){return" "},t.paragraph=function(e){return"

    "+e+"

    \n"},t.table=function(e,t){return"\n\n"+e+"\n"+(t=t&&""+t+"")+"
    \n"},t.tablerow=function(e){return"\n"+e+"\n"},t.tablecell=function(e,t){var n=t.header?"th":"td";return(t.align?"<"+n+' align="'+t.align+'">':"<"+n+">")+e+"\n"},t.strong=function(e){return""+e+""},t.em=function(e){return""+e+""},t.codespan=function(e){return""+e+""},t.br=function(){return this.options.xhtml?"
    ":"
    "},t.del=function(e){return""+e+""},t.link=function(e,t,n){if(null===(e=F(this.options.sanitize,this.options.baseUrl,e)))return n;var r='
    "},t.image=function(e,t,n){if(null===(e=F(this.options.sanitize,this.options.baseUrl,e)))return n;var r=''+n+'":">"},t.text=function(e){return e},e}(),G=function(){function e(){this.seen={}}return e.prototype.slug=function(e){var t=e.toLowerCase().trim().replace(/<[!\/a-z].*?>/gi,"").replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g,"").replace(/\s/g,"-");if(this.seen.hasOwnProperty(t))for(var n=t;this.seen[n]++,t=n+"-"+this.seen[n],this.seen.hasOwnProperty(t););return this.seen[t]=0,t},e}(),M=s.defaults,V=D.inline,H=A,J=y,K=function(){function u(e,t){if(this.options=t||M,this.links=e,this.rules=V.normal,this.options.renderer=this.options.renderer||new X,this.renderer=this.options.renderer,this.renderer.options=this.options,!this.links)throw new Error("Tokens array requires a `links` property.");this.options.pedantic?this.rules=V.pedantic:this.options.gfm&&(this.options.breaks?this.rules=V.breaks:this.rules=V.gfm)}u.output=function(e,t,n){return new u(t,n).output(e)};var e=u.prototype;return e.output=function(e){for(var t,n,r,s,i,l,a="";e;)if(i=this.rules.escape.exec(e))e=e.substring(i[0].length),a+=J(i[1]);else if(i=this.rules.tag.exec(e))!this.inLink&&/^/i.test(i[0])&&(this.inLink=!1),!this.inRawBlock&&/^<(pre|code|kbd|script)(\s|>)/i.test(i[0])?this.inRawBlock=!0:this.inRawBlock&&/^<\/(pre|code|kbd|script)(\s|>)/i.test(i[0])&&(this.inRawBlock=!1),e=e.substring(i[0].length),a+=this.renderer.html(this.options.sanitize?this.options.sanitizer?this.options.sanitizer(i[0]):J(i[0]):i[0]);else if(i=this.rules.link.exec(e)){var o=H(i[2],"()");if(-1$/,"$1"),a+=this.outputLink(i,{href:u.escapes(r),title:u.escapes(s)}),this.inLink=!1}else if((i=this.rules.reflink.exec(e))||(i=this.rules.nolink.exec(e))){if(e=e.substring(i[0].length),t=(i[2]||i[1]).replace(/\s+/g," "),!(t=this.links[t.toLowerCase()])||!t.href){a+=i[0].charAt(0),e=i[0].substring(1)+e;continue}this.inLink=!0,a+=this.outputLink(i,t),this.inLink=!1}else if(i=this.rules.strong.exec(e))e=e.substring(i[0].length),a+=this.renderer.strong(this.output(i[4]||i[3]||i[2]||i[1]));else if(i=this.rules.em.exec(e))e=e.substring(i[0].length),a+=this.renderer.em(this.output(i[6]||i[5]||i[4]||i[3]||i[2]||i[1]));else if(i=this.rules.code.exec(e))e=e.substring(i[0].length),a+=this.renderer.codespan(J(i[2].trim(),!0));else if(i=this.rules.br.exec(e))e=e.substring(i[0].length),a+=this.renderer.br();else if(i=this.rules.del.exec(e))e=e.substring(i[0].length),a+=this.renderer.del(this.output(i[1]));else if(i=this.rules.autolink.exec(e))e=e.substring(i[0].length),r="@"===i[2]?"mailto:"+(n=J(this.mangle(i[1]))):n=J(i[1]),a+=this.renderer.link(r,null,n);else if(this.inLink||!(i=this.rules.url.exec(e))){if(i=this.rules.text.exec(e))e=e.substring(i[0].length),this.inRawBlock?a+=this.renderer.text(this.options.sanitize?this.options.sanitizer?this.options.sanitizer(i[0]):J(i[0]):i[0]):a+=this.renderer.text(J(this.smartypants(i[0])));else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0))}else{if("@"===i[2])r="mailto:"+(n=J(i[0]));else{for(;l=i[0],i[0]=this.rules._backpedal.exec(i[0])[0],l!==i[0];);n=J(i[0]),r="www."===i[1]?"http://"+n:n}e=e.substring(i[0].length),a+=this.renderer.link(r,null,n)}return a},u.escapes=function(e){return e?e.replace(u.rules._escapes,"$1"):e},e.outputLink=function(e,t){var n=t.href,r=t.title?J(t.title):null;return"!"!==e[0].charAt(0)?this.renderer.link(n,r,this.output(e[1])):this.renderer.image(n,r,J(e[1]))},e.smartypants=function(e){return this.options.smartypants?e.replace(/---/g,"—").replace(/--/g,"–").replace(/(^|[-\u2014/(\[{"\s])'/g,"$1‘").replace(/'/g,"’").replace(/(^|[-\u2014/(\[{\u2018\s])"/g,"$1“").replace(/"/g,"”").replace(/\.{3}/g,"…"):e},e.mangle=function(e){if(!this.options.mangle)return e;for(var t,n=e.length,r="",s=0;sAn error occurred:

    "+se(e.message+"",!0)+"
    ";throw e}}return oe.options=oe.setOptions=function(e){return ne(oe.defaults,e),le(oe.defaults),oe},oe.getDefaults=ie,oe.defaults=ae,oe.Parser=te,oe.parser=te.parse,oe.Renderer=X,oe.TextRenderer=Q,oe.Lexer=B,oe.lexer=B.lex,oe.InlineLexer=K,oe.inlineLexer=K.output,oe.Slugger=G,oe.parse=oe}); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).marked=t()}(this,function(){"use strict";function i(e,t){for(var n=0;n"']/),s=/[&<>"']/g,l=/[<>"']|&(?!#?\w+;)/,a=/[<>"']|&(?!#?\w+;)/g,o={"&":"&","<":"<",">":">",'"':""","'":"'"};var c=/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/gi;function h(e){return e.replace(c,function(e,t){return"colon"===(t=t.toLowerCase())?":":"#"===t.charAt(0)?"x"===t.charAt(1)?String.fromCharCode(parseInt(t.substring(2),16)):String.fromCharCode(+t.substring(1)):""})}var u=/(^|[^\[])\^/g;var p=/[^\w:]/g,g=/^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;var f={},d=/^[^:]+:\/*[^/]*$/,b=/^([^:]+:)[\s\S]*$/,k=/^([^:]+:\/*[^/]*)[\s\S]*$/;function m(e,t){f[" "+e]||(d.test(e)?f[" "+e]=e+"/":f[" "+e]=x(e,"/",!0));var n=-1===(e=f[" "+e]).indexOf(":");return"//"===t.substring(0,2)?n?t:e.replace(b,"$1")+t:"/"===t.charAt(0)?n?t:e.replace(k,"$1")+t:e+t}function x(e,t,n){var r=e.length;if(0===r)return"";for(var s=0;st)n.splice(t);else for(;n.length ?(paragraph|[^\n]*)(?:\n|$))+/,list:/^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,html:"^ {0,3}(?:<(script|pre|style)[\\s>][\\s\\S]*?(?:[^\\n]*\\n+|$)|comment[^\\n]*(\\n+|$)|<\\?[\\s\\S]*?\\?>\\n*|\\n*|\\n*|)[\\s\\S]*?(?:\\n{2,}|$)|<(?!script|pre|style)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$)|(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:\\n{2,}|$))",def:/^ {0,3}\[(label)\]: *\n? *]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,nptable:R,table:R,lheading:/^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,_paragraph:/^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html)[^\n]+)*)/,text:/^[^\n]+/,_label:/(?!\s*\])(?:\\[\[\]]|[^\[\]])+/,_title:/(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/};q.def=Z(q.def).replace("label",q._label).replace("title",q._title).getRegex(),q.bullet=/(?:[*+-]|\d{1,9}\.)/,q.item=/^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/,q.item=Z(q.item,"gm").replace(/bull/g,q.bullet).getRegex(),q.list=Z(q.list).replace(/bull/g,q.bullet).replace("hr","\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))").replace("def","\\n+(?="+q.def.source+")").getRegex(),q._tag="address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul",q._comment=//,q.html=Z(q.html,"i").replace("comment",q._comment).replace("tag",q._tag).replace("attribute",/ +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex(),q.paragraph=Z(q._paragraph).replace("hr",q.hr).replace("heading"," {0,3}#{1,6} ").replace("|lheading","").replace("blockquote"," {0,3}>").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)]) ").replace("html",")|<(?:script|pre|style|!--)").replace("tag",q._tag).getRegex(),q.blockquote=Z(q.blockquote).replace("paragraph",q.paragraph).getRegex(),q.normal=O({},q),q.gfm=O({},q.normal,{nptable:"^ *([^|\\n ].*\\|.*)\\n *([-:]+ *\\|[-| :]*)(?:\\n((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)",table:"^ *\\|(.+)\\n *\\|?( *[-:]+[-| :]*)(?:\\n *((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)"}),q.gfm.nptable=Z(q.gfm.nptable).replace("hr",q.hr).replace("heading"," {0,3}#{1,6} ").replace("blockquote"," {0,3}>").replace("code"," {4}[^\\n]").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)]) ").replace("html",")|<(?:script|pre|style|!--)").replace("tag",q._tag).getRegex(),q.gfm.table=Z(q.gfm.table).replace("hr",q.hr).replace("heading"," {0,3}#{1,6} ").replace("blockquote"," {0,3}>").replace("code"," {4}[^\\n]").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)]) ").replace("html",")|<(?:script|pre|style|!--)").replace("tag",q._tag).getRegex(),q.pedantic=O({},q.normal,{html:Z("^ *(?:comment *(?:\\n|\\s*$)|<(tag)[\\s\\S]+? *(?:\\n{2,}|\\s*$)|\\s]*)*?/?> *(?:\\n{2,}|\\s*$))").replace("comment",q._comment).replace(/tag/g,"(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:|[^\\w\\s@]*@)\\b").getRegex(),def:/^ *\[([^\]]+)\]: *]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,heading:/^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/,fences:R,paragraph:Z(q.normal._paragraph).replace("hr",q.hr).replace("heading"," *#{1,6} *[^\n]").replace("lheading",q.lheading).replace("blockquote"," {0,3}>").replace("|fences","").replace("|list","").replace("|html","").getRegex()});var I={escape:/^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,autolink:/^<(scheme:[^\s\x00-\x1f<>]*|email)>/,url:R,tag:"^comment|^|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>|^<\\?[\\s\\S]*?\\?>|^|^",link:/^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,reflink:/^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,nolink:/^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,strong:/^__([^\s_])__(?!_)|^\*\*([^\s*])\*\*(?!\*)|^__([^\s][\s\S]*?[^\s])__(?!_)|^\*\*([^\s][\s\S]*?[^\s])\*\*(?!\*)/,em:/^_([^\s_])_(?!_)|^\*([^\s*<\[])\*(?!\*)|^_([^\s<][\s\S]*?[^\s_])_(?!_|[^\spunctuation])|^_([^\s_<][\s\S]*?[^\s])_(?!_|[^\spunctuation])|^\*([^\s<"][\s\S]*?[^\s\*])\*(?!\*|[^\spunctuation])|^\*([^\s*"<\[][\s\S]*?[^\s])\*(?!\*)/,code:/^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,br:/^( {2,}|\\)\n(?!\s*$)/,del:R,text:/^(`+|[^`])(?:[\s\S]*?(?:(?=[\\?@\\[^_{|}~"};I.em=Z(I.em).replace(/punctuation/g,I._punctuation).getRegex(),I._escapes=/\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g,I._scheme=/[a-zA-Z][a-zA-Z0-9+.-]{1,31}/,I._email=/[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/,I.autolink=Z(I.autolink).replace("scheme",I._scheme).replace("email",I._email).getRegex(),I._attribute=/\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/,I.tag=Z(I.tag).replace("comment",q._comment).replace("attribute",I._attribute).getRegex(),I._label=/(?:\[[^\[\]]*\]|\\.|`[^`]*`|[^\[\]\\`])*?/,I._href=/<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*/,I._title=/"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/,I.link=Z(I.link).replace("label",I._label).replace("href",I._href).replace("title",I._title).getRegex(),I.reflink=Z(I.reflink).replace("label",I._label).getRegex(),I.normal=O({},I),I.pedantic=O({},I.normal,{strong:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,em:/^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/,link:Z(/^!?\[(label)\]\((.*?)\)/).replace("label",I._label).getRegex(),reflink:Z(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace("label",I._label).getRegex()}),I.gfm=O({},I.normal,{escape:Z(I.escape).replace("])","~|])").getRegex(),_extended_email:/[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,url:/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,_backpedal:/(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,del:/^~+(?=\S)([\s\S]*?\S)~+/,text:/^(`+|[^`])(?:[\s\S]*?(?:(?=[\\ ?/gm,""),e.push({type:"blockquote",raw:x,tokens:this.blockTokens([],i,n)});else if(i=this.rules.block.list.exec(t))for(t=t.substring(i[0].length),c={type:"list",raw:x=i[0],ordered:f=1<(l=i[2]).length,start:f?+l:"",loose:!1,items:[]},e.push(c),r=!1,g=(i=i[0].match(this.rules.block.item)).length,u=0;u/i.test(a[0])&&(this.inLink=!1),!this.inRawBlock&&/^<(pre|code|kbd|script)(\s|>)/i.test(a[0])?this.inRawBlock=!0:this.inRawBlock&&/^<\/(pre|code|kbd|script)(\s|>)/i.test(a[0])&&(this.inRawBlock=!1),e=e.substring(a[0].length),u=a[0],r=this.options.sanitize?this.options.sanitizer?this.options.sanitizer(a[0]):P(a[0]):a[0],t.push({type:this.options.sanitize?"text":"html",raw:u,text:r}),p+=r;else if(a=this.rules.inline.link.exec(e))-1<(c=B(a[2],"()"))&&(h=(0===a[0].indexOf("!")?5:4)+a[1].length+c,a[2]=a[2].substring(0,c),a[0]=a[0].substring(0,h).trim(),a[3]=""),e=e.substring(a[0].length),u=a[0],this.inLink=!0,i=a[2],l=this.options.pedantic?(n=/^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(i))?(i=n[1],n[3]):"":a[3]?a[3].slice(1,-1):"",i=i.trim().replace(/^<([\s\S]*)>$/,"$1"),p+=this.outputLink(a,{href:this.escapes(i),title:this.escapes(l)},t,u),this.inLink=!1;else if((a=this.rules.inline.reflink.exec(e))||(a=this.rules.inline.nolink.exec(e))){if(e=e.substring(a[0].length),u=a[0],n=(a[2]||a[1]).replace(/\s+/g," "),!(n=this.tokens.links[n.toLowerCase()])||!n.href){p+=r=a[0].charAt(0),t.push({type:"text",raw:r,text:r}),e=a[0].substring(1)+e;continue}this.inLink=!0,p+=this.outputLink(a,n,t,u),this.inLink=!1}else if(a=this.rules.inline.strong.exec(e))e=e.substring(a[0].length),u=a[0],s=t?[]:null,r=this.inlineOutput(a[4]||a[3]||a[2]||a[1],s),t.push({type:"strong",raw:u,text:r,tokens:s}),p+=r;else if(a=this.rules.inline.em.exec(e))e=e.substring(a[0].length),u=a[0],s=t?[]:null,r=this.inlineOutput(a[6]||a[5]||a[4]||a[3]||a[2]||a[1],s),t.push({type:"em",raw:u,text:r,tokens:s}),p+=r;else if(a=this.rules.inline.code.exec(e))e=e.substring(a[0].length),u=a[0],r=P(a[2].trim(),!0),t.push({type:"codespan",raw:u,text:r}),p+=r;else if(a=this.rules.inline.br.exec(e))e=e.substring(a[0].length),u=a[0],t.push({type:"br",raw:u}),p+="\n";else if(a=this.rules.inline.del.exec(e))e=e.substring(a[0].length),u=a[0],s=t?[]:null,r=this.inlineOutput(a[1],s),t.push({type:"del",raw:u,text:r,tokens:s}),p+=r;else if(a=this.rules.inline.autolink.exec(e))e=e.substring(a[0].length),u=a[0],i="@"===a[2]?"mailto:"+(r=P(this.options.mangle?this.mangle(a[1]):a[1])):r=P(a[1]),t.push({type:"link",raw:u,text:r,href:i,tokens:[{type:"text",raw:r,text:r}]}),p+=r;else if(this.inLink||!(a=this.rules.inline.url.exec(e))){if(a=this.rules.inline.text.exec(e))e=e.substring(a[0].length),u=a[0],r=this.inRawBlock?this.options.sanitize?this.options.sanitizer?this.options.sanitizer(a[0]):P(a[0]):a[0]:P(this.options.smartypants?this.smartypants(a[0]):a[0]),t.push({type:"text",raw:u,text:r}),p+=r;else if(e){var g="Infinite loop on byte: "+e.charCodeAt(0);if(!this.options.silent)throw new Error(g);console.error(g)}}else{if("@"===a[2])i="mailto:"+(r=P(this.options.mangle?this.mangle(a[0]):a[0]));else{for(;o=a[0],a[0]=this.rules.inline._backpedal.exec(a[0])[0],o!==a[0];);r=P(a[0]),i="www."===a[1]?"http://"+r:r}e=e.substring(a[0].length),u=a[0],t.push({type:"link",raw:u,text:r,href:i,tokens:[{type:"text",raw:r,text:r}]}),p+=r}return p},s.escapes=function(e){return e?e.replace(D._escapes,"$1"):e},s.outputLink=function(e,t,n,r){var s=t.href,i=t.title?P(t.title):null,l=n?[]:null;if("!"!==e[0].charAt(0)){var a=this.inlineOutput(e[1],l);return n.push({type:"link",raw:r,text:a,href:s,title:i,tokens:l}),a}var o=P(e[1]);return n.push({type:"image",raw:r,text:o,href:s,title:i}),o},s.smartypants=function(e){return e.replace(/---/g,"—").replace(/--/g,"–").replace(/(^|[-\u2014/(\[{"\s])'/g,"$1‘").replace(/'/g,"’").replace(/(^|[-\u2014/(\[{\u2018\s])"/g,"$1“").replace(/"/g,"”").replace(/\.{3}/g,"…")},s.mangle=function(e){var t,n,r="",s=e.length;for(t=0;t'+(n?e:X(e,!0))+"\n":"
    "+(n?e:X(e,!0))+"
    "},t.blockquote=function(e){return"
    \n"+e+"
    \n"},t.html=function(e){return e},t.heading=function(e,t,n,r){return this.options.headerIds?"'+e+"\n":""+e+"\n"},t.hr=function(){return this.options.xhtml?"
    \n":"
    \n"},t.list=function(e,t,n){var r=t?"ol":"ul";return"<"+r+(t&&1!==n?' start="'+n+'"':"")+">\n"+e+"\n"},t.listitem=function(e){return"
  • "+e+"
  • \n"},t.checkbox=function(e){return" "},t.paragraph=function(e){return"

    "+e+"

    \n"},t.table=function(e,t){return"\n\n"+e+"\n"+(t=t&&""+t+"")+"
    \n"},t.tablerow=function(e){return"\n"+e+"\n"},t.tablecell=function(e,t){var n=t.header?"th":"td";return(t.align?"<"+n+' align="'+t.align+'">':"<"+n+">")+e+"\n"},t.strong=function(e){return""+e+""},t.em=function(e){return""+e+""},t.codespan=function(e){return""+e+""},t.br=function(){return this.options.xhtml?"
    ":"
    "},t.del=function(e){return""+e+""},t.link=function(e,t,n){if(null===(e=N(this.options.sanitize,this.options.baseUrl,e)))return n;var r='
    "},t.image=function(e,t,n){if(null===(e=N(this.options.sanitize,this.options.baseUrl,e)))return n;var r=''+n+'":">"},t.text=function(e){return e},e}(),M=function(){function e(){}var t=e.prototype;return t.strong=function(e){return e},t.em=function(e){return e},t.codespan=function(e){return e},t.del=function(e){return e},t.html=function(e){return e},t.text=function(e){return e},t.link=function(e,t,n){return""+n},t.image=function(e,t,n){return""+n},t.br=function(){return""},e}(),V=function(){function e(){this.seen={}}return e.prototype.slug=function(e){var t=e.toLowerCase().trim().replace(/<[!\/a-z].*?>/gi,"").replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g,"").replace(/\s/g,"-");if(this.seen.hasOwnProperty(t))for(var n=t;this.seen[n]++,t=n+"-"+this.seen[n],this.seen.hasOwnProperty(t););return this.seen[t]=0,t},e}(),H=t.defaults,J=_,K=function(){function n(e){this.options=e||H,this.options.renderer=this.options.renderer||new G,this.renderer=this.options.renderer,this.textRenderer=new M,this.renderer.options=this.options,this.slugger=new V}n.parse=function(e,t){return new n(t).parse(e)};var e=n.prototype;return e.parse=function(e,t){void 0===t&&(t=!0);var n,r,s,i,l,a,o,c,h,u,p,g,f,d="",b=e.length;for(n=0;nAn error occurred:

    "+Y(e.message+"",!0)+"
    ";throw e}}return re.options=re.setOptions=function(e){return Q(re.defaults,e),te(re.defaults),re},re.getDefaults=ee,re.defaults=ne,re.Parser=K,re.parser=K.parse,re.Renderer=G,re.TextRenderer=M,re.Lexer=U,re.lexer=U.lex,re.Slugger=V,re.parse=re}); \ No newline at end of file diff --git a/src/InlineLexer.js b/src/InlineLexer.js deleted file mode 100644 index 86f62b01fa..0000000000 --- a/src/InlineLexer.js +++ /dev/null @@ -1,293 +0,0 @@ -const Renderer = require('./Renderer.js'); -const { defaults } = require('./defaults.js'); -const { inline } = require('./rules.js'); -const { - findClosingBracket, - escape -} = require('./helpers.js'); - -/** - * Inline Lexer & Compiler - */ -module.exports = class InlineLexer { - constructor(links, options) { - this.options = options || defaults; - this.links = links; - this.rules = inline.normal; - this.options.renderer = this.options.renderer || new Renderer(); - this.renderer = this.options.renderer; - this.renderer.options = this.options; - - if (!this.links) { - throw new Error('Tokens array requires a `links` property.'); - } - - if (this.options.pedantic) { - this.rules = inline.pedantic; - } else if (this.options.gfm) { - if (this.options.breaks) { - this.rules = inline.breaks; - } else { - this.rules = inline.gfm; - } - } - } - - /** - * Expose Inline Rules - */ - static get rules() { - return inline; - } - - /** - * Static Lexing/Compiling Method - */ - static output(src, links, options) { - const inline = new InlineLexer(links, options); - return inline.output(src); - } - - /** - * Lexing/Compiling - */ - output(src) { - let out = '', - link, - text, - href, - title, - cap, - prevCapZero; - - while (src) { - // escape - if (cap = this.rules.escape.exec(src)) { - src = src.substring(cap[0].length); - out += escape(cap[1]); - continue; - } - - // tag - if (cap = this.rules.tag.exec(src)) { - if (!this.inLink && /^
    /i.test(cap[0])) { - this.inLink = false; - } - if (!this.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) { - this.inRawBlock = true; - } else if (this.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) { - this.inRawBlock = false; - } - - src = src.substring(cap[0].length); - out += this.renderer.html(this.options.sanitize - ? (this.options.sanitizer - ? this.options.sanitizer(cap[0]) - : escape(cap[0])) - : cap[0]); - continue; - } - - // link - if (cap = this.rules.link.exec(src)) { - const lastParenIndex = findClosingBracket(cap[2], '()'); - if (lastParenIndex > -1) { - const start = cap[0].indexOf('!') === 0 ? 5 : 4; - const linkLen = start + cap[1].length + lastParenIndex; - cap[2] = cap[2].substring(0, lastParenIndex); - cap[0] = cap[0].substring(0, linkLen).trim(); - cap[3] = ''; - } - src = src.substring(cap[0].length); - this.inLink = true; - href = cap[2]; - if (this.options.pedantic) { - link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href); - - if (link) { - href = link[1]; - title = link[3]; - } else { - title = ''; - } - } else { - title = cap[3] ? cap[3].slice(1, -1) : ''; - } - href = href.trim().replace(/^<([\s\S]*)>$/, '$1'); - out += this.outputLink(cap, { - href: InlineLexer.escapes(href), - title: InlineLexer.escapes(title) - }); - this.inLink = false; - continue; - } - - // reflink, nolink - if ((cap = this.rules.reflink.exec(src)) - || (cap = this.rules.nolink.exec(src))) { - src = src.substring(cap[0].length); - link = (cap[2] || cap[1]).replace(/\s+/g, ' '); - link = this.links[link.toLowerCase()]; - if (!link || !link.href) { - out += cap[0].charAt(0); - src = cap[0].substring(1) + src; - continue; - } - this.inLink = true; - out += this.outputLink(cap, link); - this.inLink = false; - continue; - } - - // strong - if (cap = this.rules.strong.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.strong(this.output(cap[4] || cap[3] || cap[2] || cap[1])); - continue; - } - - // em - if (cap = this.rules.em.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.em(this.output(cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1])); - continue; - } - - // code - if (cap = this.rules.code.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.codespan(escape(cap[2].trim(), true)); - continue; - } - - // br - if (cap = this.rules.br.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.br(); - continue; - } - - // del (gfm) - if (cap = this.rules.del.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.del(this.output(cap[1])); - continue; - } - - // autolink - if (cap = this.rules.autolink.exec(src)) { - src = src.substring(cap[0].length); - if (cap[2] === '@') { - text = escape(this.mangle(cap[1])); - href = 'mailto:' + text; - } else { - text = escape(cap[1]); - href = text; - } - out += this.renderer.link(href, null, text); - continue; - } - - // url (gfm) - if (!this.inLink && (cap = this.rules.url.exec(src))) { - if (cap[2] === '@') { - text = escape(cap[0]); - href = 'mailto:' + text; - } else { - // do extended autolink path validation - do { - prevCapZero = cap[0]; - cap[0] = this.rules._backpedal.exec(cap[0])[0]; - } while (prevCapZero !== cap[0]); - text = escape(cap[0]); - if (cap[1] === 'www.') { - href = 'http://' + text; - } else { - href = text; - } - } - src = src.substring(cap[0].length); - out += this.renderer.link(href, null, text); - continue; - } - - // text - if (cap = this.rules.text.exec(src)) { - src = src.substring(cap[0].length); - if (this.inRawBlock) { - out += this.renderer.text(this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0])) : cap[0]); - } else { - out += this.renderer.text(escape(this.smartypants(cap[0]))); - } - continue; - } - - if (src) { - throw new Error('Infinite loop on byte: ' + src.charCodeAt(0)); - } - } - - return out; - } - - static escapes(text) { - return text ? text.replace(InlineLexer.rules._escapes, '$1') : text; - } - - /** - * Compile Link - */ - outputLink(cap, link) { - const href = link.href, - title = link.title ? escape(link.title) : null; - - return cap[0].charAt(0) !== '!' - ? this.renderer.link(href, title, this.output(cap[1])) - : this.renderer.image(href, title, escape(cap[1])); - } - - /** - * Smartypants Transformations - */ - smartypants(text) { - if (!this.options.smartypants) return text; - return text - // em-dashes - .replace(/---/g, '\u2014') - // en-dashes - .replace(/--/g, '\u2013') - // opening singles - .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018') - // closing singles & apostrophes - .replace(/'/g, '\u2019') - // opening doubles - .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c') - // closing doubles - .replace(/"/g, '\u201d') - // ellipses - .replace(/\.{3}/g, '\u2026'); - } - - /** - * Mangle Links - */ - mangle(text) { - if (!this.options.mangle) return text; - const l = text.length; - let out = '', - i = 0, - ch; - - for (; i < l; i++) { - ch = text.charCodeAt(i); - if (Math.random() > 0.5) { - ch = 'x' + ch.toString(16); - } - out += '&#' + ch + ';'; - } - - return out; - } -}; diff --git a/src/Lexer.js b/src/Lexer.js index d4ab566294..b119241591 100644 --- a/src/Lexer.js +++ b/src/Lexer.js @@ -1,9 +1,10 @@ const { defaults } = require('./defaults.js'); -const { block } = require('./rules.js'); +const { block, inline } = require('./rules.js'); const { rtrim, splitCells, - escape + escape, + findClosingBracket } = require('./helpers.js'); /** @@ -14,12 +15,21 @@ module.exports = class Lexer { this.tokens = []; this.tokens.links = Object.create(null); this.options = options || defaults; - this.rules = block.normal; + this.rules = { + block: block.normal, + inline: inline.normal + }; if (this.options.pedantic) { - this.rules = block.pedantic; + this.rules.block = block.pedantic; + this.rules.inline = inline.pedantic; } else if (this.options.gfm) { - this.rules = block.gfm; + this.rules.block = block.gfm; + if (this.options.breaks) { + this.rules.inline = inline.breaks; + } else { + this.rules.inline = inline.gfm; + } } } @@ -27,7 +37,10 @@ module.exports = class Lexer { * Expose Block Rules */ static get rules() { - return block; + return { + block, + inline + }; } /** @@ -36,7 +49,7 @@ module.exports = class Lexer { static lex(src, options) { const lexer = new Lexer(options); return lexer.lex(src); - }; + } /** * Preprocessing @@ -46,13 +59,17 @@ module.exports = class Lexer { .replace(/\r\n|\r/g, '\n') .replace(/\t/g, ' '); - return this.token(src, true); - }; + this.blockTokens(src, this.tokens); + + this.inline(this.tokens); + + return this.tokens; + } /** * Lexing */ - token(src, top) { + blockTokens(src, tokens, top = true) { src = src.replace(/^ +$/gm, ''); let next, loose, @@ -60,39 +77,45 @@ module.exports = class Lexer { bull, b, item, - listStart, - listItems, - t, + list, space, i, tag, l, isordered, istask, - ischecked; + ischecked, + lastToken, + addBack, + raw; while (src) { // newline - if (cap = this.rules.newline.exec(src)) { + if (cap = this.rules.block.newline.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; if (cap[0].length > 1) { - this.tokens.push({ - type: 'space' + tokens.push({ + type: 'space', + raw }); } } // code - if (cap = this.rules.code.exec(src)) { - const lastToken = this.tokens[this.tokens.length - 1]; + if (cap = this.rules.block.code.exec(src)) { + lastToken = tokens[tokens.length - 1]; src = src.substring(cap[0].length); + raw = cap[0]; // An indented code block cannot interrupt a paragraph. if (lastToken && lastToken.type === 'paragraph') { lastToken.text += '\n' + cap[0].trimRight(); + lastToken.raw += '\n' + raw; } else { cap = cap[0].replace(/^ {4}/gm, ''); - this.tokens.push({ + tokens.push({ type: 'code', + raw, codeBlockStyle: 'indented', text: !this.options.pedantic ? rtrim(cap, '\n') @@ -103,10 +126,12 @@ module.exports = class Lexer { } // fences - if (cap = this.rules.fences.exec(src)) { + if (cap = this.rules.block.fences.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'code', + raw, lang: cap[2] ? cap[2].trim() : cap[2], text: cap[3] || '' }); @@ -114,10 +139,12 @@ module.exports = class Lexer { } // heading - if (cap = this.rules.heading.exec(src)) { + if (cap = this.rules.block.heading.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'heading', + raw, depth: cap[1].length, text: cap[2] }); @@ -125,7 +152,7 @@ module.exports = class Lexer { } // table no leading pipe (gfm) - if (cap = this.rules.nptable.exec(src)) { + if (cap = this.rules.block.nptable.exec(src)) { item = { type: 'table', header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')), @@ -135,8 +162,11 @@ module.exports = class Lexer { if (item.header.length === item.align.length) { src = src.substring(cap[0].length); + raw = cap[0]; + item.raw = raw; - for (i = 0; i < item.align.length; i++) { + l = item.align.length; + for (i = 0; i < l; i++) { if (/^ *-+: *$/.test(item.align[i])) { item.align[i] = 'right'; } else if (/^ *:-+: *$/.test(item.align[i])) { @@ -148,72 +178,71 @@ module.exports = class Lexer { } } - for (i = 0; i < item.cells.length; i++) { + l = item.cells.length; + for (i = 0; i < l; i++) { item.cells[i] = splitCells(item.cells[i], item.header.length); } - this.tokens.push(item); + tokens.push(item); continue; } } // hr - if (cap = this.rules.hr.exec(src)) { + if (cap = this.rules.block.hr.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ - type: 'hr' + raw = cap[0]; + tokens.push({ + type: 'hr', + raw }); continue; } // blockquote - if (cap = this.rules.blockquote.exec(src)) { + if (cap = this.rules.block.blockquote.exec(src)) { src = src.substring(cap[0].length); - - this.tokens.push({ - type: 'blockquote_start' - }); + raw = cap[0]; cap = cap[0].replace(/^ *> ?/gm, ''); - // Pass `top` to keep the current - // "toplevel" state. This is exactly - // how markdown.pl works. - this.token(cap, top); - - this.tokens.push({ - type: 'blockquote_end' + tokens.push({ + type: 'blockquote', + raw, + tokens: this.blockTokens(cap, [], top) }); continue; } // list - if (cap = this.rules.list.exec(src)) { + if (cap = this.rules.block.list.exec(src)) { src = src.substring(cap[0].length); + raw = cap[0]; bull = cap[2]; isordered = bull.length > 1; - listStart = { - type: 'list_start', + list = { + type: 'list', + raw, ordered: isordered, start: isordered ? +bull : '', - loose: false + loose: false, + items: [] }; - this.tokens.push(listStart); + tokens.push(list); // Get each top-level item. - cap = cap[0].match(this.rules.item); + cap = cap[0].match(this.rules.block.item); - listItems = []; next = false; - l = cap.length; - i = 0; - for (; i < l; i++) { + l = cap.length; + for (i = 0; i < l; i++) { item = cap[i]; + raw = item.trim(); // Remove the list item's bullet // so it is seen as the next token. @@ -235,7 +264,9 @@ module.exports = class Lexer { b = block.bullet.exec(cap[i + 1])[0]; if (bull.length > 1 ? b.length === 1 : (b.length > 1 || (this.options.smartLists && b !== bull))) { - src = cap.slice(i + 1).join('\n') + src; + addBack = cap.slice(i + 1).join('\n'); + src = addBack + src; + list.raw = list.raw.substring(list.raw.length - addBack.length); i = l - 1; } } @@ -250,7 +281,7 @@ module.exports = class Lexer { } if (loose) { - listStart.loose = true; + list.loose = true; } // Check for task list items @@ -261,46 +292,27 @@ module.exports = class Lexer { item = item.replace(/^\[[ xX]\] +/, ''); } - t = { - type: 'list_item_start', + list.items.push({ + raw, task: istask, checked: ischecked, - loose: loose - }; - - listItems.push(t); - this.tokens.push(t); - - // Recurse. - this.token(item, false); - - this.tokens.push({ - type: 'list_item_end' + loose: loose, + tokens: this.blockTokens(item, [], false) }); } - if (listStart.loose) { - l = listItems.length; - i = 0; - for (; i < l; i++) { - listItems[i].loose = true; - } - } - - this.tokens.push({ - type: 'list_end' - }); - continue; } // html - if (cap = this.rules.html.exec(src)) { + if (cap = this.rules.block.html.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: this.options.sanitize ? 'paragraph' : 'html', + raw, pre: !this.options.sanitizer && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'), text: this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0])) : cap[0] @@ -309,7 +321,7 @@ module.exports = class Lexer { } // def - if (top && (cap = this.rules.def.exec(src))) { + if (top && (cap = this.rules.block.def.exec(src))) { src = src.substring(cap[0].length); if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1); tag = cap[1].toLowerCase().replace(/\s+/g, ' '); @@ -323,7 +335,7 @@ module.exports = class Lexer { } // table (gfm) - if (cap = this.rules.table.exec(src)) { + if (cap = this.rules.block.table.exec(src)) { item = { type: 'table', header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')), @@ -333,8 +345,10 @@ module.exports = class Lexer { if (item.header.length === item.align.length) { src = src.substring(cap[0].length); + item.raw = cap[0]; - for (i = 0; i < item.align.length; i++) { + l = item.align.length; + for (i = 0; i < l; i++) { if (/^ *-+: *$/.test(item.align[i])) { item.align[i] = 'right'; } else if (/^ *:-+: *$/.test(item.align[i])) { @@ -346,23 +360,26 @@ module.exports = class Lexer { } } - for (i = 0; i < item.cells.length; i++) { + l = item.cells.length; + for (i = 0; i < l; i++) { item.cells[i] = splitCells( item.cells[i].replace(/^ *\| *| *\| *$/g, ''), item.header.length); } - this.tokens.push(item); + tokens.push(item); continue; } } // lheading - if (cap = this.rules.lheading.exec(src)) { + if (cap = this.rules.block.lheading.exec(src)) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'heading', + raw, depth: cap[2].charAt(0) === '=' ? 1 : 2, text: cap[1] }); @@ -370,10 +387,12 @@ module.exports = class Lexer { } // top-level paragraph - if (top && (cap = this.rules.paragraph.exec(src))) { + if (top && (cap = this.rules.block.paragraph.exec(src))) { src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'paragraph', + raw, text: cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1] @@ -382,21 +401,463 @@ module.exports = class Lexer { } // text - if (cap = this.rules.text.exec(src)) { + if (cap = this.rules.block.text.exec(src)) { // Top-level should never reach here. src = src.substring(cap[0].length); - this.tokens.push({ + raw = cap[0]; + tokens.push({ type: 'text', + raw, text: cap[0] }); continue; } if (src) { - throw new Error('Infinite loop on byte: ' + src.charCodeAt(0)); + const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0); + if (this.options.silent) { + console.error(errMsg); + } else { + throw new Error(errMsg); + } } } - return this.tokens; - }; + return tokens; + } + + inline(tokens) { + let i, + j, + k, + l2, + row, + token; + + const l = tokens.length; + for (i = 0; i < l; i++) { + token = tokens[i]; + switch (token.type) { + case 'paragraph': + case 'text': + case 'heading': { + token.tokens = []; + this.inlineTokens(token.text, token.tokens); + break; + } + case 'table': { + token.tokens = { + header: [], + cells: [] + }; + + // header + l2 = token.header.length; + for (j = 0; j < l2; j++) { + token.tokens.header[j] = []; + this.inlineTokens(token.header[j], token.tokens.header[j]); + } + + // cells + l2 = token.cells.length; + for (j = 0; j < l2; j++) { + row = token.cells[j]; + token.tokens.cells[j] = []; + for (k = 0; k < row.length; k++) { + token.tokens.cells[j][k] = []; + this.inlineTokens(row[k], token.tokens.cells[j][k]); + } + } + + break; + } + case 'blockquote': { + this.inline(token.tokens); + break; + } + case 'list': { + l2 = token.items.length; + for (j = 0; j < l2; j++) { + this.inline(token.items[j].tokens); + } + break; + } + default: { + // do nothing + } + } + } + + return tokens; + } + + /** + * Lexing/Compiling + */ + inlineTokens(src, tokens) { + let out = '', + link, + text, + newTokens, + href, + title, + cap, + prevCapZero, + lastParenIndex, + start, + linkLen, + raw; + + while (src) { + // escape + if (cap = this.rules.inline.escape.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + text = escape(cap[1]); + out += text; + tokens.push({ + type: 'escape', + raw, + text + }); + continue; + } + + // tag + if (cap = this.rules.inline.tag.exec(src)) { + if (!this.inLink && /^/i.test(cap[0])) { + this.inLink = false; + } + if (!this.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) { + this.inRawBlock = true; + } else if (this.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) { + this.inRawBlock = false; + } + + src = src.substring(cap[0].length); + raw = cap[0]; + text = this.options.sanitize + ? (this.options.sanitizer + ? this.options.sanitizer(cap[0]) + : escape(cap[0])) + : cap[0]; + tokens.push({ + type: this.options.sanitize + ? 'text' + : 'html', + raw, + text + }); + out += text; + continue; + } + + // link + if (cap = this.rules.inline.link.exec(src)) { + lastParenIndex = findClosingBracket(cap[2], '()'); + if (lastParenIndex > -1) { + start = cap[0].indexOf('!') === 0 ? 5 : 4; + linkLen = start + cap[1].length + lastParenIndex; + cap[2] = cap[2].substring(0, lastParenIndex); + cap[0] = cap[0].substring(0, linkLen).trim(); + cap[3] = ''; + } + src = src.substring(cap[0].length); + raw = cap[0]; + this.inLink = true; + href = cap[2]; + if (this.options.pedantic) { + link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href); + + if (link) { + href = link[1]; + title = link[3]; + } else { + title = ''; + } + } else { + title = cap[3] ? cap[3].slice(1, -1) : ''; + } + href = href.trim().replace(/^<([\s\S]*)>$/, '$1'); + out += this.outputLink(cap, { + href: this.escapes(href), + title: this.escapes(title) + }, tokens, raw); + this.inLink = false; + continue; + } + + // reflink, nolink + if ((cap = this.rules.inline.reflink.exec(src)) + || (cap = this.rules.inline.nolink.exec(src))) { + src = src.substring(cap[0].length); + raw = cap[0]; + link = (cap[2] || cap[1]).replace(/\s+/g, ' '); + link = this.tokens.links[link.toLowerCase()]; + if (!link || !link.href) { + text = cap[0].charAt(0); + out += text; + tokens.push({ + type: 'text', + raw: text, + text + }); + src = cap[0].substring(1) + src; + continue; + } + this.inLink = true; + out += this.outputLink(cap, link, tokens, raw); + this.inLink = false; + continue; + } + + // strong + if (cap = this.rules.inline.strong.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + newTokens = tokens ? [] : null; + text = this.inlineTokens(cap[4] || cap[3] || cap[2] || cap[1], newTokens); + + tokens.push({ + type: 'strong', + raw, + text, + tokens: newTokens + }); + out += text; + continue; + } + + // em + if (cap = this.rules.inline.em.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + newTokens = tokens ? [] : null; + text = this.inlineTokens(cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1], newTokens); + tokens.push({ + type: 'em', + raw, + text, + tokens: newTokens + }); + out += text; + continue; + } + + // code + if (cap = this.rules.inline.code.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + text = escape(cap[2].trim(), true); + tokens.push({ + type: 'codespan', + raw, + text + }); + out += text; + continue; + } + + // br + if (cap = this.rules.inline.br.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + tokens.push({ + type: 'br', + raw + }); + out += '\n'; + continue; + } + + // del (gfm) + if (cap = this.rules.inline.del.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + newTokens = tokens ? [] : null; + text = this.inlineTokens(cap[1], newTokens); + tokens.push({ + type: 'del', + raw, + text, + tokens: newTokens + }); + out += text; + continue; + } + + // autolink + if (cap = this.rules.inline.autolink.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + if (cap[2] === '@') { + text = escape(this.options.mangle ? this.mangle(cap[1]) : cap[1]); + href = 'mailto:' + text; + } else { + text = escape(cap[1]); + href = text; + } + tokens.push({ + type: 'link', + raw, + text, + href, + tokens: [ + { + type: 'text', + raw: text, + text + } + ] + }); + out += text; + continue; + } + + // url (gfm) + if (!this.inLink && (cap = this.rules.inline.url.exec(src))) { + if (cap[2] === '@') { + text = escape(this.options.mangle ? this.mangle(cap[0]) : cap[0]); + href = 'mailto:' + text; + } else { + // do extended autolink path validation + do { + prevCapZero = cap[0]; + cap[0] = this.rules.inline._backpedal.exec(cap[0])[0]; + } while (prevCapZero !== cap[0]); + text = escape(cap[0]); + if (cap[1] === 'www.') { + href = 'http://' + text; + } else { + href = text; + } + } + src = src.substring(cap[0].length); + raw = cap[0]; + tokens.push({ + type: 'link', + raw, + text, + href, + tokens: [ + { + type: 'text', + raw: text, + text + } + ] + }); + out += text; + continue; + } + + // text + if (cap = this.rules.inline.text.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + if (this.inRawBlock) { + text = this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0])) : cap[0]; + } else { + text = escape(this.options.smartypants ? this.smartypants(cap[0]) : cap[0]); + } + tokens.push({ + type: 'text', + raw, + text + }); + out += text; + continue; + } + + if (src) { + const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0); + if (this.options.silent) { + console.error(errMsg); + } else { + throw new Error(errMsg); + } + } + } + + return out; + } + + escapes(text) { + return text ? text.replace(inline._escapes, '$1') : text; + } + + /** + * tokenize Link + */ + outputLink(cap, link, tokens, raw) { + const href = link.href; + const title = link.title ? escape(link.title) : null; + const newTokens = tokens ? [] : null; + + if (cap[0].charAt(0) !== '!') { + const text = this.inlineTokens(cap[1], newTokens); + tokens.push({ + type: 'link', + raw, + text, + href, + title, + tokens: newTokens + }); + return text; + } else { + const text = escape(cap[1]); + tokens.push({ + type: 'image', + raw, + text, + href, + title + }); + return text; + } + } + + /** + * Smartypants Transformations + */ + smartypants(text) { + return text + // em-dashes + .replace(/---/g, '\u2014') + // en-dashes + .replace(/--/g, '\u2013') + // opening singles + .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018') + // closing singles & apostrophes + .replace(/'/g, '\u2019') + // opening doubles + .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c') + // closing doubles + .replace(/"/g, '\u201d') + // ellipses + .replace(/\.{3}/g, '\u2026'); + } + + /** + * Mangle Links + */ + mangle(text) { + let out = '', + i, + ch; + + const l = text.length; + for (i = 0; i < l; i++) { + ch = text.charCodeAt(i); + if (Math.random() > 0.5) { + ch = 'x' + ch.toString(16); + } + out += '&#' + ch + ';'; + } + + return out; + } }; diff --git a/src/Parser.js b/src/Parser.js index 30011dfe4b..d53aebef5c 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -1,10 +1,8 @@ const Renderer = require('./Renderer.js'); -const Slugger = require('./Slugger.js'); -const InlineLexer = require('./InlineLexer.js'); const TextRenderer = require('./TextRenderer.js'); +const Slugger = require('./Slugger.js'); const { defaults } = require('./defaults.js'); const { - merge, unescape } = require('./helpers.js'); @@ -13,12 +11,11 @@ const { */ module.exports = class Parser { constructor(options) { - this.tokens = []; - this.token = null; this.options = options || defaults; this.options.renderer = this.options.renderer || new Renderer(); this.renderer = this.options.renderer; this.renderer.options = this.options; + this.textRenderer = new TextRenderer(); this.slugger = new Slugger(); } @@ -28,179 +25,231 @@ module.exports = class Parser { static parse(tokens, options) { const parser = new Parser(options); return parser.parse(tokens); - }; + } /** * Parse Loop */ - parse(tokens) { - this.inline = new InlineLexer(tokens.links, this.options); - // use an InlineLexer with a TextRenderer to extract pure text - this.inlineText = new InlineLexer( - tokens.links, - merge({}, this.options, { renderer: new TextRenderer() }) - ); - this.tokens = tokens.reverse(); - - let out = ''; - while (this.next()) { - out += this.tok(); - } - - return out; - }; - - /** - * Next Token - */ - next() { - this.token = this.tokens.pop(); - return this.token; - }; - - /** - * Preview Next Token - */ - peek() { - return this.tokens[this.tokens.length - 1] || 0; - }; - - /** - * Parse Text Tokens - */ - parseText() { - let body = this.token.text; - - while (this.peek().type === 'text') { - body += '\n' + this.next().text; - } - - return this.inline.output(body); - }; - - /** - * Parse Current Token - */ - tok() { - let body = ''; - switch (this.token.type) { - case 'space': { - return ''; - } - case 'hr': { - return this.renderer.hr(); - } - case 'heading': { - return this.renderer.heading( - this.inline.output(this.token.text), - this.token.depth, - unescape(this.inlineText.output(this.token.text)), - this.slugger); - } - case 'code': { - return this.renderer.code(this.token.text, - this.token.lang, - this.token.escaped); - } - case 'table': { - let header = '', - i, - row, - cell, - j; - - // header - cell = ''; - for (i = 0; i < this.token.header.length; i++) { - cell += this.renderer.tablecell( - this.inline.output(this.token.header[i]), - { header: true, align: this.token.align[i] } - ); - } - header += this.renderer.tablerow(cell); - - for (i = 0; i < this.token.cells.length; i++) { - row = this.token.cells[i]; + parse(tokens, top = true) { + let out = '', + i, + j, + k, + l2, + l3, + row, + cell, + header, + body, + token, + ordered, + start, + loose, + itemBody, + item, + checked, + task, + checkbox; + + const l = tokens.length; + for (i = 0; i < l; i++) { + token = tokens[i]; + switch (token.type) { + case 'space': { + continue; + } + case 'hr': { + out += this.renderer.hr(); + continue; + } + case 'heading': { + out += this.renderer.heading( + this.parseInline(token.tokens), + token.depth, + unescape(this.parseInline(token.tokens, this.textRenderer)), + this.slugger); + continue; + } + case 'code': { + out += this.renderer.code(token.text, + token.lang, + token.escaped); + continue; + } + case 'table': { + header = ''; + // header cell = ''; - for (j = 0; j < row.length; j++) { + l2 = token.header.length; + for (j = 0; j < l2; j++) { cell += this.renderer.tablecell( - this.inline.output(row[j]), - { header: false, align: this.token.align[j] } + this.parseInline(token.tokens.header[j]), + { header: true, align: token.align[j] } ); } + header += this.renderer.tablerow(cell); + + body = ''; + l2 = token.cells.length; + for (j = 0; j < l2; j++) { + row = token.tokens.cells[j]; + + cell = ''; + l3 = row.length; + for (k = 0; k < l3; k++) { + cell += this.renderer.tablecell( + this.parseInline(row[k]), + { header: false, align: token.align[k] } + ); + } - body += this.renderer.tablerow(cell); + body += this.renderer.tablerow(cell); + } + out += this.renderer.table(header, body); + continue; } - return this.renderer.table(header, body); - } - case 'blockquote_start': { - body = ''; - - while (this.next().type !== 'blockquote_end') { - body += this.tok(); + case 'blockquote': { + body = this.parse(token.tokens); + out += this.renderer.blockquote(body); + continue; } + case 'list': { + ordered = token.ordered; + start = token.start; + loose = token.loose; + l2 = token.items.length; + + body = ''; + for (j = 0; j < l2; j++) { + item = token.items[j]; + checked = item.checked; + task = item.task; + + itemBody = ''; + if (item.task) { + checkbox = this.renderer.checkbox(checked); + if (loose) { + if (item.tokens[0].type === 'text') { + item.tokens[0].text = checkbox + ' ' + item.tokens[0].text; + if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') { + item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text; + } + } else { + item.tokens.unshift({ + type: 'text', + text: checkbox + }); + } + } else { + itemBody += checkbox; + } + } - return this.renderer.blockquote(body); - } - case 'list_start': { - body = ''; - const ordered = this.token.ordered, - start = this.token.start; + itemBody += this.parse(item.tokens, loose); + body += this.renderer.listitem(itemBody, task, checked); + } - while (this.next().type !== 'list_end') { - body += this.tok(); + out += this.renderer.list(body, ordered, start); + continue; } - - return this.renderer.list(body, ordered, start); - } - case 'list_item_start': { - body = ''; - const loose = this.token.loose; - const checked = this.token.checked; - const task = this.token.task; - - if (this.token.task) { - if (loose) { - if (this.peek().type === 'text') { - const nextToken = this.peek(); - nextToken.text = this.renderer.checkbox(checked) + ' ' + nextToken.text; - } else { - this.tokens.push({ - type: 'text', - text: this.renderer.checkbox(checked) - }); - } + case 'html': { + // TODO parse inline content if parameter markdown=1 + out += this.renderer.html(token.text); + continue; + } + case 'paragraph': { + out += this.renderer.paragraph(this.parseInline(token.tokens)); + continue; + } + case 'text': { + body = token.tokens ? this.parseInline(token.tokens) : token.text; + while (i + 1 < l && tokens[i + 1].type === 'text') { + token = tokens[++i]; + body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text); + } + out += top ? this.renderer.paragraph(body) : body; + continue; + } + default: { + const errMsg = 'Token with "' + token.type + '" type was not found.'; + if (this.options.silent) { + console.error(errMsg); + return; } else { - body += this.renderer.checkbox(checked); + throw new Error(errMsg); } } + } + } - while (this.next().type !== 'list_item_end') { - body += !loose && this.token.type === 'text' - ? this.parseText() - : this.tok(); + return out; + } + + /** + * Parse Inline Tokens + */ + parseInline(tokens, renderer) { + renderer = renderer || this.renderer; + let out = '', + i, + token; + + const l = tokens.length; + for (i = 0; i < l; i++) { + token = tokens[i]; + switch (token.type) { + case 'escape': { + out += token.text; + break; } - return this.renderer.listitem(body, task, checked); - } - case 'html': { - // TODO parse inline content if parameter markdown=1 - return this.renderer.html(this.token.text); - } - case 'paragraph': { - return this.renderer.paragraph(this.inline.output(this.token.text)); - } - case 'text': { - return this.renderer.paragraph(this.parseText()); - } - default: { - const errMsg = 'Token with "' + this.token.type + '" type was not found.'; - if (this.options.silent) { - console.log(errMsg); - } else { - throw new Error(errMsg); + case 'html': { + out += renderer.html(token.text); + break; + } + case 'link': { + out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer)); + break; + } + case 'image': { + out += renderer.image(token.href, token.title, token.text); + break; + } + case 'strong': { + out += renderer.strong(this.parseInline(token.tokens, renderer)); + break; + } + case 'em': { + out += renderer.em(this.parseInline(token.tokens, renderer)); + break; + } + case 'codespan': { + out += renderer.codespan(token.text); + break; + } + case 'br': { + out += renderer.br(); + break; + } + case 'del': { + out += renderer.del(this.parseInline(token.tokens, renderer)); + break; + } + case 'text': { + out += renderer.text(token.text); + break; + } + default: { + const errMsg = 'Token with "' + token.type + '" type was not found.'; + if (this.options.silent) { + console.error(errMsg); + return; + } else { + throw new Error(errMsg); + } } } } - }; + return out; + } }; diff --git a/src/Renderer.js b/src/Renderer.js index d86341a810..a86732cf12 100644 --- a/src/Renderer.js +++ b/src/Renderer.js @@ -34,15 +34,15 @@ module.exports = class Renderer { + '">' + (escaped ? code : escape(code, true)) + '\n'; - }; + } blockquote(quote) { return '
    \n' + quote + '
    \n'; - }; + } html(html) { return html; - }; + } heading(text, level, raw, slugger) { if (this.options.headerIds) { @@ -59,21 +59,21 @@ module.exports = class Renderer { } // ignore IDs return '' + text + '\n'; - }; + } hr() { return this.options.xhtml ? '
    \n' : '
    \n'; - }; + } list(body, ordered, start) { const type = ordered ? 'ol' : 'ul', startatt = (ordered && start !== 1) ? (' start="' + start + '"') : ''; return '<' + type + startatt + '>\n' + body + '\n'; - }; + } listitem(text) { return '
  • ' + text + '
  • \n'; - }; + } checkbox(checked) { return ' '; - }; + } paragraph(text) { return '

    ' + text + '

    \n'; - }; + } table(header, body) { if (body) body = '' + body + ''; @@ -96,11 +96,11 @@ module.exports = class Renderer { + '\n' + body + '\n'; - }; + } tablerow(content) { return '\n' + content + '\n'; - }; + } tablecell(content, flags) { const type = flags.header ? 'th' : 'td'; @@ -108,28 +108,28 @@ module.exports = class Renderer { ? '<' + type + ' align="' + flags.align + '">' : '<' + type + '>'; return tag + content + '\n'; - }; + } // span level renderer strong(text) { return '' + text + ''; - }; + } em(text) { return '' + text + ''; - }; + } codespan(text) { return '' + text + ''; - }; + } br() { return this.options.xhtml ? '
    ' : '
    '; - }; + } del(text) { return '' + text + ''; - }; + } link(href, title, text) { href = cleanUrl(this.options.sanitize, this.options.baseUrl, href); @@ -142,7 +142,7 @@ module.exports = class Renderer { } out += '>' + text + '
    '; return out; - }; + } image(href, title, text) { href = cleanUrl(this.options.sanitize, this.options.baseUrl, href); @@ -156,9 +156,9 @@ module.exports = class Renderer { } out += this.options.xhtml ? '/>' : '>'; return out; - }; + } text(text) { return text; - }; + } }; diff --git a/src/Slugger.js b/src/Slugger.js index 00f63e48f1..7095454ac3 100644 --- a/src/Slugger.js +++ b/src/Slugger.js @@ -29,5 +29,5 @@ module.exports = class Slugger { this.seen[slug] = 0; return slug; - }; + } }; diff --git a/src/marked.js b/src/marked.js index 10704b0764..b575eec23d 100644 --- a/src/marked.js +++ b/src/marked.js @@ -2,7 +2,6 @@ const Lexer = require('./Lexer.js'); const Parser = require('./Parser.js'); const Renderer = require('./Renderer.js'); const TextRenderer = require('./TextRenderer.js'); -const InlineLexer = require('./InlineLexer.js'); const Slugger = require('./Slugger.js'); const { merge, @@ -140,9 +139,6 @@ marked.TextRenderer = TextRenderer; marked.Lexer = Lexer; marked.lexer = Lexer.lex; -marked.InlineLexer = InlineLexer; -marked.inlineLexer = InlineLexer.output; - marked.Slugger = Slugger; marked.parse = marked; diff --git a/test/helpers/helpers.js b/test/helpers/helpers.js index a71db73cd9..477f936300 100644 --- a/test/helpers/helpers.js +++ b/test/helpers/helpers.js @@ -21,6 +21,22 @@ beforeEach(() => { return result; } }; + }, + toEqualHtml: () => { + return { + compare: async(actual, expected) => { + const result = {}; + result.pass = await htmlDiffer.isEqual(expected, actual); + + if (result.pass) { + result.message = `Expected '${actual}' not to equal '${expected}'`; + } else { + const diff = await htmlDiffer.firstDiff(actual, expected); + result.message = `Expected: ${diff.expected}\n Actual: ${diff.actual}`; + } + return result; + } + }; } }); }); diff --git a/test/unit/Lexer-spec.js b/test/unit/Lexer-spec.js new file mode 100644 index 0000000000..0d334358a0 --- /dev/null +++ b/test/unit/Lexer-spec.js @@ -0,0 +1,1019 @@ +const Lexer = require('../../src/Lexer.js'); + +function expectTokens({ md, options, tokens = [], links = {} }) { + const lexer = new Lexer(options); + const actual = lexer.lex(md); + const expected = tokens; + expected.links = links; + // console.log(JSON.stringify(actual, null, 2)); + expect(actual).toEqual(expected); +} + +function expectInlineTokens({ md, options, output = jasmine.any(String), tokens = jasmine.any(Array), links = {} }) { + const lexer = new Lexer(options); + lexer.tokens.links = links; + const outTokens = []; + const outOutput = lexer.inlineTokens(md, outTokens); + expect({ + output: outOutput, + tokens: outTokens + }).toEqual({ output, tokens }); +} + +function expectInline({ token, options, tokens }) { + const lexer = new Lexer(options); + lexer.inline([token]); + expect(token.tokens).toEqual(tokens); +} + +describe('Lexer', () => { + describe('paragraph', () => { + it('space between paragraphs', () => { + expectTokens({ + md: 'paragraph 1\n\nparagraph 2', + tokens: [ + { + type: 'paragraph', + raw: 'paragraph 1', + text: 'paragraph 1', + tokens: [{ type: 'text', raw: 'paragraph 1', text: 'paragraph 1' }] + }, + { type: 'space', raw: '\n\n' }, + { + type: 'paragraph', + raw: 'paragraph 2', + text: 'paragraph 2', + tokens: [{ type: 'text', raw: 'paragraph 2', text: 'paragraph 2' }] + } + ] + }); + }); + }); + + describe('code', () => { + it('indented code', () => { + expectTokens({ + md: ' code', + tokens: [ + { type: 'code', raw: ' code', text: 'code', codeBlockStyle: 'indented' } + ] + }); + }); + + it('fenced code', () => { + expectTokens({ + md: '```\ncode\n```', + tokens: [ + { type: 'code', raw: '```\ncode\n```', text: 'code', lang: '' } + ] + }); + }); + + it('fenced code lang', () => { + expectTokens({ + md: '```text\ncode\n```', + tokens: [ + { type: 'code', raw: '```text\ncode\n```', text: 'code', lang: 'text' } + ] + }); + }); + }); + + describe('headings', () => { + it('depth', () => { + expectTokens({ + md: ` +# heading 1 + +## heading 2 + +### heading 3 + +#### heading 4 + +##### heading 5 + +###### heading 6 + +lheading 1 +========== + +lheading 2 +---------- +`, + tokens: [ + { + type: 'heading', + raw: '# heading 1\n\n', + depth: 1, + text: 'heading 1', + tokens: [{ type: 'text', raw: 'heading 1', text: 'heading 1' }] + }, + { + type: 'heading', + raw: '## heading 2\n\n', + depth: 2, + text: 'heading 2', + tokens: [{ type: 'text', raw: 'heading 2', text: 'heading 2' }] + }, + { + type: 'heading', + raw: '### heading 3\n\n', + depth: 3, + text: 'heading 3', + tokens: [{ type: 'text', raw: 'heading 3', text: 'heading 3' }] + }, + { + type: 'heading', + raw: '#### heading 4\n\n', + depth: 4, + text: 'heading 4', + tokens: [{ type: 'text', raw: 'heading 4', text: 'heading 4' }] + }, + { + type: 'heading', + raw: '##### heading 5\n\n', + depth: 5, + text: 'heading 5', + tokens: [{ type: 'text', raw: 'heading 5', text: 'heading 5' }] + }, + { + type: 'heading', + raw: '###### heading 6\n\n', + depth: 6, + text: 'heading 6', + tokens: [{ type: 'text', raw: 'heading 6', text: 'heading 6' }] + }, + { + type: 'heading', + raw: 'lheading 1\n==========\n\n', + depth: 1, + text: 'lheading 1', + tokens: [{ type: 'text', raw: 'lheading 1', text: 'lheading 1' }] + }, + { + type: 'heading', + raw: 'lheading 2\n----------\n', + depth: 2, + text: 'lheading 2', + tokens: [{ type: 'text', raw: 'lheading 2', text: 'lheading 2' }] + } + ] + }); + }); + + it('should not be heading if depth > 6', () => { + expectTokens({ + md: '####### heading 7', + tokens: [{ + type: 'paragraph', + raw: '####### heading 7', + text: '####### heading 7', + tokens: [{ type: 'text', raw: '####### heading 7', text: '####### heading 7' }] + }] + }); + }); + }); + + describe('table', () => { + it('pipe table', () => { + expectTokens({ + md: ` +| a | b | +|---|---| +| 1 | 2 | +`, + tokens: [{ + type: 'table', + header: ['a', 'b'], + align: [null, null], + cells: [['1', '2']], + raw: '| a | b |\n|---|---|\n| 1 | 2 |\n', + tokens: { + header: [ + [{ type: 'text', raw: 'a', text: 'a' }], + [{ type: 'text', raw: 'b', text: 'b' }] + ], + cells: [[ + [{ type: 'text', raw: '1', text: '1' }], + [{ type: 'text', raw: '2', text: '2' }] + ]] + } + }] + }); + }); + + it('align table', () => { + expectTokens({ + md: ` +| a | b | c | +|:--|:-:|--:| +| 1 | 2 | 3 | +`, + tokens: [{ + type: 'table', + header: ['a', 'b', 'c'], + align: ['left', 'center', 'right'], + cells: [['1', '2', '3']], + raw: '| a | b | c |\n|:--|:-:|--:|\n| 1 | 2 | 3 |\n', + tokens: { + header: [ + [{ type: 'text', raw: 'a', text: 'a' }], + [{ type: 'text', raw: 'b', text: 'b' }], + [{ type: 'text', raw: 'c', text: 'c' }] + ], + cells: [[ + [{ type: 'text', raw: '1', text: '1' }], + [{ type: 'text', raw: '2', text: '2' }], + [{ type: 'text', raw: '3', text: '3' }] + ]] + } + }] + }); + }); + + it('no pipe table', () => { + expectTokens({ + md: ` +a | b +--|-- +1 | 2 +`, + tokens: [{ + type: 'table', + header: ['a', 'b'], + align: [null, null], + cells: [['1', '2']], + raw: 'a | b\n--|--\n1 | 2\n', + tokens: { + header: [ + [{ type: 'text', raw: 'a', text: 'a' }], + [{ type: 'text', raw: 'b', text: 'b' }] + ], + cells: [[ + [{ type: 'text', raw: '1', text: '1' }], + [{ type: 'text', raw: '2', text: '2' }] + ]] + } + }] + }); + }); + }); + + describe('hr', () => { + it('hr', () => { + expectTokens({ + md: '---', + tokens: [ + { type: 'hr', raw: '---' } + ] + }); + }); + }); + + describe('blockquote', () => { + it('start, inner-tokens, end', () => { + expectTokens({ + md: '> blockquote', + tokens: [ + { + type: 'blockquote', + raw: '> blockquote', + tokens: [{ + type: 'paragraph', + raw: 'blockquote', + text: 'blockquote', + tokens: [ + { type: 'text', raw: 'blockquote', text: 'blockquote' } + ] + }] + } + ] + }); + }); + }); + + describe('list', () => { + it('unordered', () => { + expectTokens({ + md: ` +- item 1 +- item 2 +`, + tokens: [ + { + type: 'list', + raw: '- item 1\n- item 2\n', + ordered: false, + start: '', + loose: false, + items: [ + { + raw: '- item 1', + task: false, + checked: undefined, + loose: false, + tokens: [{ + type: 'text', + raw: 'item 1', + text: 'item 1', + tokens: [{ type: 'text', raw: 'item 1', text: 'item 1' }] + }] + }, + { + raw: '- item 2', + task: false, + checked: undefined, + loose: false, + tokens: [{ + type: 'text', + raw: 'item 2', + text: 'item 2', + tokens: [{ type: 'text', raw: 'item 2', text: 'item 2' }] + }] + } + ] + } + ] + }); + }); + + it('ordered', () => { + expectTokens({ + md: ` +1. item 1 +2. item 2 +`, + tokens: jasmine.arrayContaining([ + jasmine.objectContaining({ + type: 'list', + raw: '1. item 1\n2. item 2\n', + ordered: true, + start: 1, + items: [ + jasmine.objectContaining({ + raw: '1. item 1' + }), + jasmine.objectContaining({ + raw: '2. item 2' + }) + ] + }) + ]) + }); + }); + + it('start', () => { + expectTokens({ + md: ` +2. item 1 +3. item 2 +`, + tokens: jasmine.arrayContaining([ + jasmine.objectContaining({ + type: 'list', + raw: '2. item 1\n3. item 2\n', + ordered: true, + start: 2, + items: [ + jasmine.objectContaining({ + raw: '2. item 1' + }), + jasmine.objectContaining({ + raw: '3. item 2' + }) + ] + }) + ]) + }); + }); + + it('loose', () => { + expectTokens({ + md: ` +- item 1 + +- item 2 +`, + tokens: jasmine.arrayContaining([ + jasmine.objectContaining({ + type: 'list', + raw: '- item 1\n\n- item 2\n', + loose: true + }) + ]) + }); + }); + + it('task', () => { + expectTokens({ + md: ` +- [ ] item 1 +- [x] item 2 +`, + tokens: jasmine.arrayContaining([ + jasmine.objectContaining({ + type: 'list', + raw: '- [ ] item 1\n- [x] item 2\n', + items: [ + jasmine.objectContaining({ + raw: '- [ ] item 1', + task: true, + checked: false + }), + jasmine.objectContaining({ + raw: '- [x] item 2', + task: true, + checked: true + }) + ] + }) + ]) + }); + }); + }); + + describe('html', () => { + it('div', () => { + expectTokens({ + md: '
    html
    ', + tokens: [ + { + type: 'html', + raw: '
    html
    ', + pre: false, + text: '
    html
    ' + } + ] + }); + }); + + it('pre', () => { + expectTokens({ + md: '
    html
    ', + tokens: [ + { + type: 'html', + raw: '
    html
    ', + pre: true, + text: '
    html
    ' + } + ] + }); + }); + + it('sanitize', () => { + expectTokens({ + md: '
    html
    ', + options: { sanitize: true }, + tokens: [ + { + type: 'paragraph', + raw: '
    html
    ', + pre: false, + text: '<div>html</div>', + tokens: [{ + type: 'text', + raw: '<div>html</div>', + text: '<div>html</div>' + }] + } + ] + }); + }); + }); + + describe('def', () => { + it('link', () => { + expectTokens({ + md: '[link]: https://example.com', + links: { + link: { href: 'https://example.com', title: undefined } + } + }); + }); + + it('title', () => { + expectTokens({ + md: '[link]: https://example.com "title"', + links: { + link: { href: 'https://example.com', title: 'title' } + } + }); + }); + }); + + describe('inline', () => { + describe('tokens', () => { + it('paragraph', () => { + expectInline({ + token: { type: 'paragraph', text: 'text' }, + tokens: [ + { type: 'text', raw: 'text', text: 'text' } + ] + }); + }); + + it('text', () => { + expectInline({ + token: { type: 'text', text: 'text' }, + tokens: [ + { type: 'text', raw: 'text', text: 'text' } + ] + }); + }); + + it('heading', () => { + expectInline({ + token: { type: 'heading', text: 'text' }, + tokens: [ + { type: 'text', raw: 'text', text: 'text' } + ] + }); + }); + + it('table', () => { + expectInline({ + token: { + type: 'table', + header: ['a', 'b'], + align: [null, null], + cells: [['1', '2']] + }, + tokens: { + header: [ + [{ type: 'text', raw: 'a', text: 'a' }], + [{ type: 'text', raw: 'b', text: 'b' }] + ], + cells: [ + [ + [{ type: 'text', raw: '1', text: '1' }], + [{ type: 'text', raw: '2', text: '2' }] + ] + ] + } + }); + }); + + it('code no inline tokens', () => { + expectInline({ + token: { type: 'code', text: 'code' }, + tokens: undefined + }); + }); + }); + + describe('output', () => { + it('escape', () => { + expectInlineTokens({ + md: '\\>', + output: '>', + tokens: [ + { type: 'escape', raw: '\\>', text: '>' } + ] + }); + }); + + it('html', () => { + expectInlineTokens({ + md: '
    html
    ', + output: '
    html
    ', + tokens: [ + { type: 'html', raw: '
    ', text: '
    ' }, + { type: 'text', raw: 'html', text: 'html' }, + { type: 'html', raw: '
    ', text: '
    ' } + ] + }); + }); + + it('html sanitize', () => { + expectInlineTokens({ + md: '
    html
    ', + options: { sanitize: true }, + output: '<div>html</div>', + tokens: [ + { type: 'text', raw: '
    ', text: '<div>' }, + { type: 'text', raw: 'html', text: 'html' }, + { type: 'text', raw: '
    ', text: '</div>' } + ] + }); + }); + + it('link', () => { + expectInlineTokens({ + md: '[link](https://example.com)', + output: 'link', + tokens: [ + { + type: 'link', + raw: '[link](https://example.com)', + text: 'link', + href: 'https://example.com', + title: null, + tokens: [ + { type: 'text', raw: 'link', text: 'link' } + ] + } + ] + }); + }); + + it('link title', () => { + expectInlineTokens({ + md: '[link](https://example.com "title")', + output: 'link', + tokens: [ + { + type: 'link', + raw: '[link](https://example.com "title")', + text: 'link', + href: 'https://example.com', + title: 'title', + tokens: [ + { type: 'text', raw: 'link', text: 'link' } + ] + } + ] + }); + }); + + it('image', () => { + expectInlineTokens({ + md: '![image](https://example.com/image.png)', + output: 'image', + tokens: [ + { + type: 'image', + raw: '![image](https://example.com/image.png)', + text: 'image', + href: 'https://example.com/image.png', + title: null + } + ] + }); + }); + + it('image title', () => { + expectInlineTokens({ + md: '![image](https://example.com/image.png "title")', + output: 'image', + tokens: [ + { + type: 'image', + raw: '![image](https://example.com/image.png "title")', + text: 'image', + href: 'https://example.com/image.png', + title: 'title' + } + ] + }); + }); + + describe('reflink', () => { + it('reflink', () => { + expectInlineTokens({ + md: '[link][]', + links: { + link: { href: 'https://example.com', title: 'title' } + }, + output: 'link', + tokens: [ + { + type: 'link', + raw: '[link][]', + text: 'link', + href: 'https://example.com', + title: 'title', + tokens: [{ + type: 'text', + raw: 'link', + text: 'link' + }] + } + ] + }); + }); + + it('nolink', () => { + expectInlineTokens({ + md: '[link]', + links: { + link: { href: 'https://example.com', title: 'title' } + }, + output: 'link', + tokens: [ + { + type: 'link', + raw: '[link]', + text: 'link', + href: 'https://example.com', + title: 'title', + tokens: [{ + type: 'text', + raw: 'link', + text: 'link' + }] + } + ] + }); + }); + + it('no def', () => { + expectInlineTokens({ + md: '[link]', + output: '[link]', + tokens: [ + { type: 'text', raw: '[', text: '[' }, + { type: 'text', raw: 'link]', text: 'link]' } + ] + }); + }); + }); + + it('strong', () => { + expectInlineTokens({ + md: '**strong**', + output: 'strong', + tokens: [ + { + type: 'strong', + raw: '**strong**', + text: 'strong', + tokens: [ + { type: 'text', raw: 'strong', text: 'strong' } + ] + } + ] + }); + }); + + it('em', () => { + expectInlineTokens({ + md: '*em*', + output: 'em', + tokens: [ + { + type: 'em', + raw: '*em*', + text: 'em', + tokens: [ + { type: 'text', raw: 'em', text: 'em' } + ] + } + ] + }); + }); + + it('code', () => { + expectInlineTokens({ + md: '`code`', + output: 'code', + tokens: [ + { type: 'codespan', raw: '`code`', text: 'code' } + ] + }); + }); + + it('br', () => { + expectInlineTokens({ + md: 'a\nb', + options: { gfm: true, breaks: true }, + output: 'a\nb', + tokens: jasmine.arrayContaining([ + { type: 'br', raw: '\n' } + ]) + }); + }); + + it('del', () => { + expectInlineTokens({ + md: '~~del~~', + output: 'del', + tokens: [ + { + type: 'del', + raw: '~~del~~', + text: 'del', + tokens: [ + { type: 'text', raw: 'del', text: 'del' } + ] + } + ] + }); + }); + + describe('url', () => { + it('autolink', () => { + expectInlineTokens({ + md: '', + output: 'https://example.com', + tokens: [ + { + type: 'link', + raw: '', + text: 'https://example.com', + href: 'https://example.com', + tokens: [ + { type: 'text', raw: 'https://example.com', text: 'https://example.com' } + ] + } + ] + }); + }); + + it('autolink email', () => { + expectInlineTokens({ + md: '', + options: { mangle: false }, + output: 'test@example.com', + tokens: [ + { + type: 'link', + raw: '', + text: 'test@example.com', + href: 'mailto:test@example.com', + tokens: [ + { type: 'text', raw: 'test@example.com', text: 'test@example.com' } + ] + } + ] + }); + }); + + it('autolink mangle email', () => { + expectInlineTokens({ + md: '', + options: { mangle: true }, + output: jasmine.stringMatching('&#'), + tokens: [ + { + type: 'link', + raw: '', + text: jasmine.stringMatching(/^(&#x?[0-9a-f]+;)+$/), + href: jasmine.stringMatching(/^mailto:(&#x?[0-9a-f]+;)+$/), + tokens: [ + { + type: 'text', + raw: jasmine.stringMatching(/^(&#x?[0-9a-f]+;)+$/), + text: jasmine.stringMatching(/^(&#x?[0-9a-f]+;)+$/) + } + ] + } + ] + }); + }); + + it('url', () => { + expectInlineTokens({ + md: 'https://example.com', + output: 'https://example.com', + tokens: [ + { + type: 'link', + raw: 'https://example.com', + text: 'https://example.com', + href: 'https://example.com', + tokens: [ + { type: 'text', raw: 'https://example.com', text: 'https://example.com' } + ] + } + ] + }); + }); + + it('url email', () => { + expectInlineTokens({ + md: 'test@example.com', + options: { gfm: true, mangle: false }, + output: 'test@example.com', + tokens: [ + { + type: 'link', + raw: 'test@example.com', + text: 'test@example.com', + href: 'mailto:test@example.com', + tokens: [ + { type: 'text', raw: 'test@example.com', text: 'test@example.com' } + ] + } + ] + }); + }); + + it('url mangle email', () => { + expectInlineTokens({ + md: 'test@example.com', + options: { gfm: true, mangle: true }, + output: jasmine.stringMatching('&#'), + tokens: [ + { + type: 'link', + raw: 'test@example.com', + text: jasmine.stringMatching(/^(&#x?[0-9a-f]+;)+$/), + href: jasmine.stringMatching(/^mailto:(&#x?[0-9a-f]+;)+$/), + tokens: [ + { + type: 'text', + raw: jasmine.stringMatching(/^(&#x?[0-9a-f]+;)+$/), + text: jasmine.stringMatching(/^(&#x?[0-9a-f]+;)+$/) + } + ] + } + ] + }); + }); + }); + + it('text', () => { + expectInlineTokens({ + md: 'text', + output: 'text', + tokens: [ + { + type: 'text', + raw: 'text', + text: 'text' + } + ] + }); + }); + + describe('smartypants', () => { + it('single quotes', () => { + expectInlineTokens({ + md: "'single quotes'", + options: { smartypants: true }, + output: '‘single quotes’', + tokens: [ + { + type: 'text', + raw: "'single quotes'", + text: '‘single quotes’' + } + ] + }); + }); + + it('double quotes', () => { + expectInlineTokens({ + md: '"double quotes"', + options: { smartypants: true }, + output: '“double quotes”', + tokens: [ + { + type: 'text', + raw: '"double quotes"', + text: '“double quotes”' + } + ] + }); + }); + + it('ellipses', () => { + expectInlineTokens({ + md: 'ellipses...', + options: { smartypants: true }, + output: 'ellipses…', + tokens: [ + { + type: 'text', + raw: 'ellipses...', + text: 'ellipses…' + } + ] + }); + }); + + it('en-dash', () => { + expectInlineTokens({ + md: 'en--dash', + options: { smartypants: true }, + output: 'en–dash', + tokens: [ + { + type: 'text', + raw: 'en--dash', + text: 'en–dash' + } + ] + }); + }); + + it('em-dash', () => { + expectInlineTokens({ + md: 'em---dash', + options: { smartypants: true }, + output: 'em—dash', + tokens: [ + { + type: 'text', + raw: 'em---dash', + text: 'em—dash' + } + ] + }); + }); + }); + }); + }); +}); diff --git a/test/unit/Parser-spec.js b/test/unit/Parser-spec.js new file mode 100644 index 0000000000..10efd2d0db --- /dev/null +++ b/test/unit/Parser-spec.js @@ -0,0 +1,449 @@ +const Parser = require('../../src/Parser.js'); + +async function expectHtml({ tokens, options, html, inline }) { + const parser = new Parser(options); + const actual = parser[inline ? 'parseInline' : 'parse'](tokens); + await expectAsync(actual).toEqualHtml(html); +} + +describe('Parser', () => { + describe('block', () => { + it('space between paragraphs', async() => { + await expectHtml({ + tokens: [ + { + type: 'paragraph', + text: 'paragraph 1', + tokens: [ + { type: 'text', text: 'paragraph 1' } + ] + }, + { type: 'space' }, + { + type: 'paragraph', + text: 'paragraph 2', + tokens: [ + { type: 'text', text: 'paragraph 2' } + ] + } + ], + html: '

    paragraph 1

    paragraph 2

    ' + }); + }); + + it('hr', async() => { + await expectHtml({ + tokens: [{ + type: 'hr' + }], + html: '
    ' + }); + }); + + it('heading', async() => { + await expectHtml({ + tokens: [{ + type: 'heading', + depth: 1, + text: 'heading', + tokens: [ + { type: 'text', text: 'heading' } + ] + }], + html: '

    heading

    ' + }); + }); + + it('code', async() => { + await expectHtml({ + tokens: [{ + type: 'code', + text: 'code' + }], + html: '
    code
    ' + }); + }); + + it('table', async() => { + await expectHtml({ + tokens: [{ + type: 'table', + header: ['a', 'b'], + align: ['left', 'right'], + cells: [['1', '2']], + tokens: { + header: [ + [{ type: 'text', text: 'a' }], + [{ type: 'text', text: 'b' }] + ], + cells: [ + [ + [{ type: 'text', text: '1' }], + [{ type: 'text', text: '2' }] + ] + ] + } + }], + html: ` + + + + + + + + + + + + + +
    ab
    12
    ` + }); + }); + + it('blockquote', async() => { + await expectHtml({ + tokens: [ + { + type: 'blockquote', + tokens: [{ + type: 'paragraph', + text: 'blockquote', + tokens: [ + { type: 'text', text: 'blockquote' } + ] + }] + } + ], + html: '

    blockquote

    ' + }); + }); + + describe('list', () => { + it('unordered', async() => { + await expectHtml({ + tokens: [ + { + type: 'list', + ordered: false, + start: '', + loose: false, + items: [ + { + task: false, + checked: undefined, + tokens: [{ + type: 'text', + text: 'item 1', + tokens: [{ type: 'text', text: 'item 1' }] + }] + }, + { + task: false, + checked: undefined, + tokens: [{ + type: 'text', + text: 'item 2', + tokens: [{ type: 'text', text: 'item 2' }] + }] + } + ] + } + ], + html: ` +
      +
    • item 1
    • +
    • item 2
    • +
    ` + }); + }); + + it('ordered', async() => { + await expectHtml({ + tokens: [ + { + type: 'list', + ordered: true, + start: 2, + loose: false, + items: [ + { + task: false, + checked: undefined, + tokens: [{ + type: 'text', + text: 'item 1', + tokens: [{ type: 'text', text: 'item 1' }] + }] + }, + { + task: false, + checked: undefined, + tokens: [{ + type: 'text', + text: 'item 2', + tokens: [{ type: 'text', text: 'item 2' }] + }] + } + ] + } + ], + html: ` +
      +
    1. item 1
    2. +
    3. item 2
    4. +
    ` + }); + }); + + it('tasks', async() => { + await expectHtml({ + tokens: [ + { + type: 'list', + ordered: false, + start: '', + loose: false, + items: [ + { + task: true, + checked: false, + tokens: [{ + type: 'text', + text: 'item 1', + tokens: [{ type: 'text', text: 'item 1' }] + }] + }, + { + task: true, + checked: true, + tokens: [{ + type: 'text', + text: 'item 2', + tokens: [{ type: 'text', text: 'item 2' }] + }] + } + ] + } + ], + html: ` +
      +
    • item 1
    • +
    • item 2
    • +
    ` + }); + }); + + it('loose', async() => { + await expectHtml({ + tokens: [ + { + type: 'list', + ordered: false, + start: '', + loose: true, + items: [ + { + task: false, + checked: undefined, + tokens: [{ + type: 'text', + text: 'item 1', + tokens: [{ type: 'text', text: 'item 1' }] + }] + }, + { + task: false, + checked: undefined, + tokens: [{ + type: 'text', + text: 'item 2', + tokens: [{ type: 'text', text: 'item 2' }] + }] + } + ] + } + ], + html: ` +
      +
    • item 1

    • +
    • item 2

    • +
    ` + }); + }); + }); + + it('html', async() => { + await expectHtml({ + tokens: [{ + type: 'html', + text: '
    html
    ' + }], + html: '
    html
    ' + }); + }); + + it('paragraph', async() => { + await expectHtml({ + tokens: [{ + type: 'paragraph', + text: 'paragraph 1', + tokens: [ + { type: 'text', text: 'paragraph 1' } + ] + }], + html: '

    paragraph 1

    ' + }); + }); + + it('text', async() => { + await expectHtml({ + tokens: [ + { type: 'text', text: 'text 1' }, + { type: 'text', text: 'text 2' } + ], + html: '

    text 1\ntext 2

    ' + }); + }); + }); + + describe('inline', () => { + it('escape', async() => { + await expectHtml({ + inline: true, + tokens: [ + { type: 'escape', text: '>' } + ], + html: '>' + }); + }); + + it('html', async() => { + await expectHtml({ + inline: true, + tokens: [ + { type: 'html', text: '
    ' }, + { type: 'text', text: 'html' }, + { type: 'html', text: '
    ' } + ], + html: '
    html
    ' + }); + }); + + it('link', async() => { + await expectHtml({ + inline: true, + tokens: [ + { + type: 'link', + text: 'link', + href: 'https://example.com', + title: 'title', + tokens: [ + { type: 'text', text: 'link' } + ] + } + ], + html: 'link' + }); + }); + + it('image', async() => { + await expectHtml({ + inline: true, + tokens: [ + { + type: 'image', + text: 'image', + href: 'image.png', + title: 'title' + } + ], + html: 'image' + }); + }); + + it('strong', async() => { + await expectHtml({ + inline: true, + tokens: [ + { + type: 'strong', + text: 'strong', + tokens: [ + { type: 'text', text: 'strong' } + ] + } + ], + html: 'strong' + }); + }); + + it('em', async() => { + await expectHtml({ + inline: true, + tokens: [ + { + type: 'em', + text: 'em', + tokens: [ + { type: 'text', text: 'em' } + ] + } + ], + html: 'em' + }); + }); + + it('codespan', async() => { + await expectHtml({ + inline: true, + tokens: [ + { + type: 'codespan', + text: 'code' + } + ], + html: 'code' + }); + }); + + it('br', async() => { + await expectHtml({ + inline: true, + tokens: [{ + type: 'br' + }], + html: '
    ' + }); + }); + + it('del', async() => { + await expectHtml({ + inline: true, + tokens: [ + { + type: 'del', + text: 'del', + tokens: [ + { type: 'text', text: 'del' } + ] + } + ], + html: 'del' + }); + }); + + it('text', async() => { + await expectHtml({ + inline: true, + tokens: [ + { type: 'text', text: 'text 1' }, + { type: 'text', text: 'text 2' } + ], + html: 'text 1text 2' + }); + }); + }); +}); diff --git a/test/unit/marked-spec.js b/test/unit/marked-spec.js index b0c730c68c..cc84d16191 100644 --- a/test/unit/marked-spec.js +++ b/test/unit/marked-spec.js @@ -58,6 +58,11 @@ describe('Test slugger functionality', () => { const slugger = new marked.Slugger(); expect(slugger.slug('file.txt')).toBe('filetxt'); }); + + it('should remove html tags', () => { + const slugger = new marked.Slugger(); + expect(slugger.slug('html')).toBe('html'); + }); }); describe('Test paragraph token type', () => { @@ -67,8 +72,8 @@ describe('Test paragraph token type', () => { const tokens = marked.lexer(md); expect(tokens[0].type).toBe('paragraph'); - expect(tokens[3].type).toBe('paragraph'); - expect(tokens[7].type).toBe('text'); + expect(tokens[2].tokens[0].type).toBe('paragraph'); + expect(tokens[3].items[0].tokens[0].type).toBe('text'); }); });