From 7e532a8d6d1474b386bcf4ebb415c64fde645562 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 1 Apr 2020 21:08:44 -0500 Subject: [PATCH 01/11] update docs --- docs/USING_PRO.md | 28 ++++++++++++++++++---------- docs/demo/demo.js | 14 +++++++------- docs/demo/worker.js | 41 +++++++++++++++++++++++++++++------------ 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/docs/USING_PRO.md b/docs/USING_PRO.md index 5e9451bee1..7c902db340 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. @@ -91,29 +91,36 @@ slugger.slug('foo-1') // foo-1-2

The lexer

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

The inline lexer

+ +The inline lexer adds inline tokens to the block level tokens.

The parser

-The parser is... +The parser takes tokens as input and calls the renderer functions that are accosiated with those tokens. ***

Access to lexer and parser

-You also have direct access to the lexer and parser if you so desire. +You also have direct access to the lexer, inline lexer, and parser if you so desire. ``` js -const tokens = marked.lexer(text, options); +const blocks = marked.lexer(markdown, options); +const tokens = marked.inlineLexer(blocks, options); console.log(marked.parser(tokens, options)); ``` ``` js const lexer = new marked.Lexer(options); -const tokens = lexer.lex(text); +const inlineLexer = new marked.InlineLexer(options); +const blocks = lexer.lex(markdown); +const tokens = inlineLexer.lex(blocks); console.log(tokens); -console.log(lexer.rules); +console.log(lexer.rules); // block level rules +console.log(inlineLexer.rules); // inline level rules ``` ``` bash @@ -126,8 +133,8 @@ $ node 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, +The Lexer and InlineLexer build an array of tokens, which will be passed to the Parser. +The Parser processes each token in the token array, which are removed from the array of tokens: ``` js @@ -141,7 +148,8 @@ const md = ` [1]: #heading "heading" `; -const tokens = marked.lexer(md); +const blocks = marked.lexer(md); +const tokens = marked.inlineLexer(blocks); console.log(tokens); const html = marked.parser(tokens); 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..1d62e09ebe 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') { + 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; From eeccdaabc5eea377b725712a9beb1d3d14402a02 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 2 Apr 2020 00:22:40 -0500 Subject: [PATCH 02/11] add tests --- test/helpers/helpers.js | 16 + test/unit/Lexer-spec.js | 1019 ++++++++++++++++++++++++++++++++++++++ test/unit/Parser-spec.js | 449 +++++++++++++++++ test/unit/marked-spec.js | 9 +- 4 files changed, 1491 insertions(+), 2 deletions(-) create mode 100644 test/unit/Lexer-spec.js create mode 100644 test/unit/Parser-spec.js 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..13e92d8add --- /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 expectInlineOutput({ 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.inlineOutput(md, outTokens); + expect({ + output: outOutput, + tokens: outTokens + }).toEqual({ output, tokens }); +} + +function expectInlineTokens({ token, options, tokens }) { + const lexer = new Lexer(options); + lexer.inlineTokens([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', () => { + expectInlineTokens({ + token: { type: 'paragraph', text: 'text' }, + tokens: [ + { type: 'text', raw: 'text', text: 'text' } + ] + }); + }); + + it('text', () => { + expectInlineTokens({ + token: { type: 'text', text: 'text' }, + tokens: [ + { type: 'text', raw: 'text', text: 'text' } + ] + }); + }); + + it('heading', () => { + expectInlineTokens({ + token: { type: 'heading', text: 'text' }, + tokens: [ + { type: 'text', raw: 'text', text: 'text' } + ] + }); + }); + + it('table', () => { + expectInlineTokens({ + 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', () => { + expectInlineTokens({ + token: { type: 'code', text: 'code' }, + tokens: undefined + }); + }); + }); + + describe('output', () => { + it('escape', () => { + expectInlineOutput({ + md: '\\>', + output: '>', + tokens: [ + { type: 'escape', raw: '\\>', text: '>' } + ] + }); + }); + + it('html', () => { + expectInlineOutput({ + md: '
html
', + output: '
html
', + tokens: [ + { type: 'html', raw: '
', text: '
' }, + { type: 'text', raw: 'html', text: 'html' }, + { type: 'html', raw: '
', text: '
' } + ] + }); + }); + + it('html sanitize', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + md: '[link]', + output: '[link]', + tokens: [ + { type: 'text', raw: '[', text: '[' }, + { type: 'text', raw: 'link]', text: 'link]' } + ] + }); + }); + }); + + it('strong', () => { + expectInlineOutput({ + md: '**strong**', + output: 'strong', + tokens: [ + { + type: 'strong', + raw: '**strong**', + text: 'strong', + tokens: [ + { type: 'text', raw: 'strong', text: 'strong' } + ] + } + ] + }); + }); + + it('em', () => { + expectInlineOutput({ + md: '*em*', + output: 'em', + tokens: [ + { + type: 'em', + raw: '*em*', + text: 'em', + tokens: [ + { type: 'text', raw: 'em', text: 'em' } + ] + } + ] + }); + }); + + it('code', () => { + expectInlineOutput({ + md: '`code`', + output: 'code', + tokens: [ + { type: 'codespan', raw: '`code`', text: 'code' } + ] + }); + }); + + it('br', () => { + expectInlineOutput({ + md: 'a\nb', + options: { gfm: true, breaks: true }, + output: 'a\nb', + tokens: jasmine.arrayContaining([ + { type: 'br', raw: '\n' } + ]) + }); + }); + + it('del', () => { + expectInlineOutput({ + md: '~~del~~', + output: 'del', + tokens: [ + { + type: 'del', + raw: '~~del~~', + text: 'del', + tokens: [ + { type: 'text', raw: 'del', text: 'del' } + ] + } + ] + }); + }); + + describe('url', () => { + it('autolink', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + 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', () => { + expectInlineOutput({ + md: 'text', + output: 'text', + tokens: [ + { + type: 'text', + raw: 'text', + text: 'text' + } + ] + }); + }); + + describe('smartypants', () => { + it('single quotes', () => { + expectInlineOutput({ + md: "'single quotes'", + options: { smartypants: true }, + output: '‘single quotes’', + tokens: [ + { + type: 'text', + raw: "'single quotes'", + text: '‘single quotes’' + } + ] + }); + }); + + it('double quotes', () => { + expectInlineOutput({ + md: '"double quotes"', + options: { smartypants: true }, + output: '“double quotes”', + tokens: [ + { + type: 'text', + raw: '"double quotes"', + text: '“double quotes”' + } + ] + }); + }); + + it('ellipses', () => { + expectInlineOutput({ + md: 'ellipses...', + options: { smartypants: true }, + output: 'ellipses…', + tokens: [ + { + type: 'text', + raw: 'ellipses...', + text: 'ellipses…' + } + ] + }); + }); + + it('en-dash', () => { + expectInlineOutput({ + md: 'en--dash', + options: { smartypants: true }, + output: 'en–dash', + tokens: [ + { + type: 'text', + raw: 'en--dash', + text: 'en–dash' + } + ] + }); + }); + + it('em-dash', () => { + expectInlineOutput({ + 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'); }); }); From fb7011ef885703aececfcb538188f287cf8efd29 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 2 Apr 2020 00:23:29 -0500 Subject: [PATCH 03/11] create tokens before sending to parser --- .eslintrc.json | 1 + src/InlineLexer.js | 293 -------------------- src/Lexer.js | 648 ++++++++++++++++++++++++++++++++++++++------- src/Parser.js | 354 ++++++++++++++----------- src/Renderer.js | 40 +-- src/Slugger.js | 2 +- src/marked.js | 4 - 7 files changed, 770 insertions(+), 572 deletions(-) delete mode 100644 src/InlineLexer.js 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/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..4a4d543f0e 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(this.tokens, src, true); + + this.inlineTokens(this.tokens); + + return this.tokens; + } /** * Lexing */ - token(src, top) { + blockTokens(tokens, src, top) { src = src.replace(/^ +$/gm, ''); let next, loose, @@ -60,39 +77,43 @@ module.exports = class Lexer { bull, b, item, - listStart, - listItems, - t, + list, space, i, tag, l, isordered, istask, - ischecked; + ischecked, + 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)) { + const 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 +124,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 +137,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 +150,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 +160,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 +176,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 +262,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; + const 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 +279,7 @@ module.exports = class Lexer { } if (loose) { - listStart.loose = true; + list.loose = true; } // Check for task list items @@ -261,46 +290,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 +319,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 +333,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 +343,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 +358,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 +385,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 +399,460 @@ 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; + } + + 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: [] + }; + + // header + l2 = token.header.length; + for (j = 0; j < l2; j++) { + token.tokens.header[j] = []; + this.inlineOutput(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.inlineOutput(row[k], token.tokens.cells[j][k]); + } + } + + 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; + } + default: { + // do nothing + } + } + } + + return tokens; + } + + /** + * Lexing/Compiling + */ + inlineOutput(src, tokens) { + let out = '', + link, + text, + newTokens, + href, + title, + cap, + prevCapZero, + 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)) { + 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); + 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.inlineOutput(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.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; + } + + // 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.inlineOutput(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.inlineOutput(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..f844739039 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,11 +11,10 @@ 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.textRenderer = new TextRenderer(); this.renderer.options = this.options; this.slugger = new Slugger(); } @@ -28,179 +25,220 @@ 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 = '', + body, + token; + + const l = tokens.length; + for (let 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': { + let header = '', + i, + row, + cell, + j, + l2, + l3; + + // header cell = ''; - for (j = 0; j < row.length; j++) { + l2 = token.header.length; + for (i = 0; i < l2; i++) { cell += this.renderer.tablecell( - this.inline.output(row[j]), - { header: false, align: this.token.align[j] } + this.parseInline(token.tokens.header[i]), + { header: true, align: token.align[i] } ); } + header += this.renderer.tablerow(cell); + + body = ''; + l2 = token.cells.length; + for (i = 0; i < l2; i++) { + row = token.tokens.cells[i]; + + cell = ''; + l3 = row.length; + for (j = 0; j < l3; j++) { + cell += this.renderer.tablecell( + this.parseInline(row[j]), + { header: false, align: token.align[j] } + ); + } - 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': { + const ordered = token.ordered, + start = token.start, + loose = token.loose, + l2 = token.items.length; + let itemBody; + + body = ''; + for (let j = 0; j < l2; j++) { + const item = token.items[j]; + const checked = item.checked; + const task = item.task; + + itemBody = ''; + if (item.task) { + const 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 = 'Inline token with "' + this.token.type + '" type was not found.'; + if (this.options.silent) { + console.error(errMsg); + } 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; From fa7193d0f48dee468b92d91e9643c81b1a612607 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 2 Apr 2020 00:23:40 -0500 Subject: [PATCH 04/11] build --- lib/marked.esm.js | 1562 +++++++++++++++++++++++++-------------------- lib/marked.js | 1302 ++++++++++++++++++++++--------------- marked.min.js | 2 +- 3 files changed, 1652 insertions(+), 1214 deletions(-) diff --git a/lib/marked.esm.js b/lib/marked.esm.js index 6543d55699..99f86a76b0 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, true); + + this.inlineTokens(this.tokens); + + return this.tokens; + } /** * Lexing */ - token(src, top) { + blockTokens(tokens, src, top) { src = src.replace(/^ +$/gm, ''); let next, loose, @@ -622,39 +639,43 @@ var Lexer_1 = class Lexer { bull, b, item, - listStart, - listItems, - t, + list, space, i, tag, l, isordered, istask, - ischecked; + ischecked, + 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)) { + const 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 +686,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 +699,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 +712,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 +722,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 +738,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 +824,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; + const 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 +841,7 @@ var Lexer_1 = class Lexer { } if (loose) { - listStart.loose = true; + list.loose = true; } // Check for task list items @@ -823,46 +852,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 +881,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 +895,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 +905,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 +920,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 +947,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,515 +961,624 @@ 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'; - }; + return tokens; + } - list(body, ordered, start) { - const type = ordered ? 'ol' : 'ul', - startatt = (ordered && start !== 1) ? (' start="' + start + '"') : ''; - return '<' + type + startatt + '>\n' + body + '\n'; - }; + /** + * Lexing/Compiling + */ + inlineOutput(src, tokens) { + let out = '', + link, + text, + newTokens, + href, + title, + cap, + prevCapZero, + raw; - listitem(text) { - return '
  • ' + text + '
  • \n'; - }; + 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; + } - checkbox(checked) { - return ' '; - }; + // 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; + } - paragraph(text) { - return '

    ' + text + '

    \n'; - }; + 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; + } - table(header, body) { - if (body) body = '' + body + ''; + // link + if (cap = this.rules.inline.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); + raw = cap[0]; + this.inLink = true; + href = cap[2]; + if (this.options.pedantic) { + link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href); - return '\n' - + '\n' - + header - + '\n' - + body - + '
    \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; + } - tablerow(content) { - return '\n' + 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; + } - tablecell(content, flags) { - const type = flags.header ? 'th' : 'td'; - const tag = flags.align - ? '<' + type + ' align="' + flags.align + '">' - : '<' + type + '>'; - return tag + content + '\n'; - }; + // 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; + } - // span level renderer - strong(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; + } - em(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; + } - codespan(text) { - return '' + text + ''; - }; + // 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; + } - br() { - return this.options.xhtml ? '
    ' : '
    '; - }; + // 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; + } - del(text) { - return '' + text + ''; - }; + // autolink + if (cap = this.rules.inline.autolink.exec(src)) { + src = src.substring(cap[0].length); + raw = cap[0]; + if (cap[2] === '@') { + text = escape$1(this.options.mangle ? this.mangle(cap[1]) : cap[1]); + href = 'mailto:' + text; + } else { + text = escape$1(cap[1]); + href = text; + } + tokens.push({ + type: 'link', + raw, + text, + href, + tokens: [ + { + type: 'text', + raw: text, + text + } + ] + }); + out += text; + continue; + } - link(href, title, text) { - href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); - if (href === null) { - return text; - } - let out = '
    '; - return out; - }; + // url (gfm) + if (!this.inLink && (cap = this.rules.inline.url.exec(src))) { + if (cap[2] === '@') { + 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.inline._backpedal.exec(cap[0])[0]; + } while (prevCapZero !== cap[0]); + text = escape$1(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; + } - image(href, title, text) { - href = cleanUrl$1(this.options.sanitize, this.options.baseUrl, href); - if (href === null) { - return text; - } + // 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$1(cap[0])) : cap[0]; + } else { + text = escape$1(this.options.smartypants ? this.smartypants(cap[0]) : cap[0]); + } + tokens.push({ + type: 'text', + raw, + text + }); + out += text; + continue; + } - let out = '' + text + '' : '>'; + return out; - }; + } - text(text) { - return text; - }; -}; + escapes(text) { + return text ? text.replace(inline$1._escapes, '$1') : text; + } -/** - * Slugger generates header id - */ -var Slugger_1 = class Slugger { - constructor() { - this.seen = {}; + /** + * tokenize Link + */ + 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; + } } /** - * Convert string to unique id + * Smartypants Transformations */ - 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, '-'); + 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'); + } - if (this.seen.hasOwnProperty(slug)) { - const originalSlug = slug; - do { - this.seen[originalSlug]++; - slug = originalSlug + '-' + this.seen[originalSlug]; - } while (this.seen.hasOwnProperty(slug)); + /** + * 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 + ';'; } - this.seen[slug] = 0; - return slug; - }; + return out; + } }; -const { defaults: defaults$3 } = defaults; -const { inline: inline$1 } = rules; +const { defaults: defaults$2 } = defaults; const { - findClosingBracket: findClosingBracket$1, - escape: escape$3 + cleanUrl: cleanUrl$1, + escape: escape$2 } = helpers; /** - * Inline Lexer & Compiler + * Renderer */ -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; +var Renderer_1 = class Renderer { + constructor(options) { + this.options = options || defaults$2; + } - if (!this.links) { - throw new Error('Tokens array requires a `links` property.'); + 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 (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; - } + if (!lang) { + return '
    '
    +        + (escaped ? code : escape$2(code, true))
    +        + '
    '; } - } - /** - * Expose Inline Rules - */ - static get rules() { - return inline$1; + return '
    '
    +      + (escaped ? code : escape$2(code, true))
    +      + '
    \n'; } - /** - * Static Lexing/Compiling Method - */ - static output(src, links, options) { - const inline = new InlineLexer(links, options); - return inline.output(src); + blockquote(quote) { + return '
    \n' + quote + '
    \n'; } - /** - * 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; - } + html(html) { + return html; + } - // 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; - } + heading(text, level, raw, slugger) { + if (this.options.headerIds) { + return '' + + text + + '\n'; + } + // ignore IDs + return '' + text + '\n'; + } - // 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; - } + hr() { + return this.options.xhtml ? '
    \n' : '
    \n'; + } - // br - if (cap = this.rules.br.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.br(); - continue; - } + list(body, ordered, start) { + const type = ordered ? 'ol' : 'ul', + startatt = (ordered && start !== 1) ? (' start="' + start + '"') : ''; + return '<' + type + startatt + '>\n' + body + '\n'; + } - // del (gfm) - if (cap = this.rules.del.exec(src)) { - src = src.substring(cap[0].length); - out += this.renderer.del(this.output(cap[1])); - continue; - } + listitem(text) { + return '
  • ' + text + '
  • \n'; + } - // autolink - if (cap = this.rules.autolink.exec(src)) { - src = src.substring(cap[0].length); - if (cap[2] === '@') { - text = escape$3(this.mangle(cap[1])); - href = 'mailto:' + text; - } else { - text = escape$3(cap[1]); - href = text; - } - out += this.renderer.link(href, null, text); - continue; - } + checkbox(checked) { + return ' '; + } - // url (gfm) - if (!this.inLink && (cap = this.rules.url.exec(src))) { - if (cap[2] === '@') { - text = escape$3(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$3(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; - } + paragraph(text) { + return '

    ' + text + '

    \n'; + } - // 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$3(cap[0])) : cap[0]); - } else { - out += this.renderer.text(escape$3(this.smartypants(cap[0]))); - } - continue; - } + table(header, body) { + if (body) body = '' + body + ''; - if (src) { - throw new Error('Infinite loop on byte: ' + src.charCodeAt(0)); - } - } + return '\n' + + '\n' + + header + + '\n' + + body + + '
    \n'; + } - return out; + tablerow(content) { + return '\n' + content + '\n'; } - static escapes(text) { - return text ? text.replace(InlineLexer.rules._escapes, '$1') : text; + tablecell(content, flags) { + const type = flags.header ? 'th' : 'td'; + const tag = flags.align + ? '<' + type + ' align="' + flags.align + '">' + : '<' + type + '>'; + return tag + content + '\n'; } - /** - * Compile Link - */ - outputLink(cap, link) { - const href = link.href, - title = link.title ? escape$3(link.title) : null; + // span level renderer + strong(text) { + return '' + text + ''; + } - return cap[0].charAt(0) !== '!' - ? this.renderer.link(href, title, this.output(cap[1])) - : this.renderer.image(href, title, escape$3(cap[1])); + em(text) { + return '' + text + ''; } - /** - * 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'); + codespan(text) { + return '' + text + ''; } - /** - * Mangle Links - */ - mangle(text) { - if (!this.options.mangle) return text; - const l = text.length; - let out = '', - i = 0, - ch; + br() { + return this.options.xhtml ? '
    ' : '
    '; + } - for (; i < l; i++) { - ch = text.charCodeAt(i); - if (Math.random() > 0.5) { - ch = 'x' + ch.toString(16); - } - out += '&#' + ch + ';'; + 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; + } }; /** @@ -1498,9 +1624,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 +1668,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 +1682,233 @@ 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 = '', + body, + token; + + const l = tokens.length; + for (let 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': { + let header = '', + i, + row, + cell, + j, + l2, + l3; + + // header cell = ''; - for (j = 0; j < row.length; j++) { + l2 = token.header.length; + for (i = 0; i < l2; i++) { cell += this.renderer.tablecell( - this.inline.output(row[j]), - { header: false, align: this.token.align[j] } + this.parseInline(token.tokens.header[i]), + { header: true, align: token.align[i] } ); } + header += this.renderer.tablerow(cell); + + body = ''; + l2 = token.cells.length; + for (i = 0; i < l2; i++) { + row = token.tokens.cells[i]; + + cell = ''; + l3 = row.length; + for (j = 0; j < l3; j++) { + cell += this.renderer.tablecell( + this.parseInline(row[j]), + { header: false, align: token.align[j] } + ); + } - 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': { + const ordered = token.ordered, + start = token.start, + loose = token.loose, + l2 = token.items.length; + let itemBody; + + body = ''; + for (let j = 0; j < l2; j++) { + const item = token.items[j]; + const checked = item.checked; + const task = item.task; + + itemBody = ''; + if (item.task) { + const 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); } } + } + } + + return out; + } - while (this.next().type !== 'list_item_end') { - body += !loose && this.token.type === 'text' - ? this.parseText() - : this.tok(); + /** + * 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 = 'Inline 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 +1930,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 +1994,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 +2014,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 +2036,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..2809bbdb4b 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,57 @@ 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, true); + this.inlineTokens(this.tokens); + return this.tokens; + } /** * Lexing */ - _proto.token = function token(src, top) { + ; + + _proto.blockTokens = function blockTokens(tokens, src, top) { 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, 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)) { + var 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 +614,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 +627,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 +640,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 +650,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 +666,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 +741,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; + var 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 +759,7 @@ } if (loose) { - listStart.loose = true; + list.loose = true; } // Check for task list items @@ -739,41 +771,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 +797,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 +813,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 +823,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 +838,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 +863,145 @@ } // 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]; + var errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0); - if (this.options.highlight) { - var out = this.options.highlight(_code, lang); - - 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, + 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,12 +1015,19 @@ } 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)) { + if (cap = this.rules.inline.link.exec(src)) { var lastParenIndex = findClosingBracket$1(cap[2], '()'); if (lastParenIndex > -1) { @@ -1162,6 +1039,7 @@ } src = src.substring(cap[0].length); + raw = cap[0]; this.inLink = true; href = cap[2]; @@ -1180,95 +1058,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 +1214,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 +1307,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 +1322,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 +1340,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 +1551,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 +1609,288 @@ 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 = '', + body, + token; + var l = tokens.length; - /** - * Next Token - */ - _proto.next = function next() { - this.token = this.tokens.pop(); - return this.token; - }; + for (var i = 0; i < l; i++) { + token = tokens[i]; - /** - * Preview Next Token - */ - _proto.peek = function peek() { - return this.tokens[this.tokens.length - 1] || 0; - }; + switch (token.type) { + case 'space': + { + continue; + } - /** - * Parse Text Tokens - */ - _proto.parseText = function parseText() { - var body = this.token.text; + case 'hr': + { + out += this.renderer.hr(); + continue; + } - while (this.peek().type === 'text') { - body += '\n' + this.next().text; - } + case 'heading': + { + out += this.renderer.heading(this.parseInline(token.tokens), token.depth, unescape$1(this.parseInline(token.tokens, this.textRenderer)), this.slugger); + continue; + } - return this.inline.output(body); - }; + case 'code': + { + out += this.renderer.code(token.text, token.lang, token.escaped); + continue; + } - /** - * Parse Current Token - */ - _proto.tok = function tok() { - var body = ''; + case 'table': + { + var header = '', + _i = void 0, + row = void 0, + cell = void 0, + j = void 0, + l2 = void 0, + l3 = void 0; // header - switch (this.token.type) { - case 'space': - { - return ''; - } - case 'hr': - { - return this.renderer.hr(); - } + cell = ''; + l2 = token.header.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 (_i = 0; _i < l2; _i++) { + cell += this.renderer.tablecell(this.parseInline(token.tokens.header[_i]), { + header: true, + align: token.align[_i] + }); + } - case 'code': - { - return this.renderer.code(this.token.text, this.token.lang, this.token.escaped); - } + header += this.renderer.tablerow(cell); + body = ''; + l2 = token.cells.length; - 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] - }); + for (_i = 0; _i < l2; _i++) { + row = token.tokens.cells[_i]; + cell = ''; + l3 = row.length; + + for (j = 0; j < l3; j++) { + cell += this.renderer.tablecell(this.parseInline(row[j]), { + header: false, + align: token.align[j] + }); + } + + body += this.renderer.tablerow(cell); + } + + 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': + { + var ordered = token.ordered, + start = token.start, + loose = token.loose, + _l = token.items.length; + var itemBody = void 0; + body = ''; + + for (var _j = 0; _j < _l; _j++) { + var item = token.items[_j]; + var checked = item.checked; + var task = item.task; + itemBody = ''; + + if (item.task) { + var 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); } } + } + } + + 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; + } - while (this.next().type !== 'list_item_end') { - body += !loose && this.token.type === 'text' ? this.parseText() : this.tok(); + case 'html': + { + out += renderer.html(token.text); + break; } - return this.renderer.listitem(body, task, checked); - } + case 'link': + { + out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer)); + break; + } - case 'html': - { - // TODO parse inline content if parameter markdown=1 - return this.renderer.html(this.token.text); - } + case 'image': + { + out += renderer.image(token.href, token.title, token.text); + break; + } - case 'paragraph': - { - return this.renderer.paragraph(this.inline.output(this.token.text)); - } + case 'strong': + { + out += renderer.strong(this.parseInline(token.tokens, renderer)); + break; + } - case 'text': - { - return this.renderer.paragraph(this.parseText()); - } + case 'em': + { + out += renderer.em(this.parseInline(token.tokens, renderer)); + break; + } - default: - { - var errMsg = 'Token with "' + this.token.type + '" type was not found.'; + case 'codespan': + { + out += renderer.codespan(token.text); + break; + } - if (this.options.silent) { - console.log(errMsg); - } else { - throw new Error(errMsg); + 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 = 'Inline 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 +1912,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 +1987,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 +2006,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 +2023,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..9586cd0c6c 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 r(e,t){for(var n=0;n"']/),l=/[&<>"']/g,a=/[<>"']|&(?!#?\w+;)/,o=/[<>"']|&(?!#?\w+;)/g,h={"&":"&","<":"<",">":">",'"':""","'":"'"};var c=/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/gi;function u(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 p=/(^|[^\[])\^/g;var g=/[^\w:]/g,f=/^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;var d={},k=/^[^:]+:\/*[^/]*$/,b=/^([^:]+:)[\s\S]*$/,m=/^([^:]+:\/*[^/]*)[\s\S]*$/;function x(e,t){d[" "+e]||(k.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(b,"$1")+t:"/"===t.charAt(0)?n?t:e.replace(m,"$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?'|\([^()]*\))/};L.def=q(L.def).replace("label",L._label).replace("title",L._title).getRegex(),L.bullet=/(?:[*+-]|\d{1,9}\.)/,L.item=/^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/,L.item=q(L.item,"gm").replace(/bull/g,L.bullet).getRegex(),L.list=q(L.list).replace(/bull/g,L.bullet).replace("hr","\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))").replace("def","\\n+(?="+L.def.source+")").getRegex(),L._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",L._comment=//,L.html=q(L.html,"i").replace("comment",L._comment).replace("tag",L._tag).replace("attribute",/ +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex(),L.paragraph=q(L._paragraph).replace("hr",L.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",L._tag).getRegex(),L.blockquote=q(L.blockquote).replace("paragraph",L.paragraph).getRegex(),L.normal=I({},L),L.gfm=I({},L.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*|$)"}),L.gfm.nptable=q(L.gfm.nptable).replace("hr",L.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",L._tag).getRegex(),L.gfm.table=q(L.gfm.table).replace("hr",L.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",L._tag).getRegex(),L.pedantic=I({},L.normal,{html:q("^ *(?:comment *(?:\\n|\\s*$)|<(tag)[\\s\\S]+? *(?:\\n{2,}|\\s*$)|\\s]*)*?/?> *(?:\\n{2,}|\\s*$))").replace("comment",L._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(L.normal._paragraph).replace("hr",L.hr).replace("heading"," *#{1,6} *[^\n]").replace("lheading",L.lheading).replace("blockquote"," {0,3}>").replace("|fences","").replace("|list","").replace("|html","").getRegex()});var C={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]*?(?:(?=[\\?@\\[^_{|}~"};C.em=q(C.em).replace(/punctuation/g,C._punctuation).getRegex(),C._escapes=/\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g,C._scheme=/[a-zA-Z][a-zA-Z0-9+.-]{1,31}/,C._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])?)+(?![-_])/,C.autolink=q(C.autolink).replace("scheme",C._scheme).replace("email",C._email).getRegex(),C._attribute=/\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/,C.tag=q(C.tag).replace("comment",L._comment).replace("attribute",C._attribute).getRegex(),C._label=/(?:\[[^\[\]]*\]|\\.|`[^`]*`|[^\[\]\\`])*?/,C._href=/<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*/,C._title=/"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/,C.link=q(C.link).replace("label",C._label).replace("href",C._href).replace("title",C._title).getRegex(),C.reflink=q(C.reflink).replace("label",C._label).getRegex(),C.normal=I({},C),C.pedantic=I({},C.normal,{strong:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,em:/^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/,link:q(/^!?\[(label)\]\((.*?)\)/).replace("label",C._label).getRegex(),reflink:q(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace("label",C._label).getRegex()}),C.gfm=I({},C.normal,{escape:q(C.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",raw:m=s[0],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(){}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}(),M=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}(),V=s.defaults,H=y,J=function(){function n(e){this.tokens=[],this.token=null,this.options=e||V,this.options.renderer=this.options.renderer||new X,this.renderer=this.options.renderer,this.textRenderer=new G,this.renderer.options=this.options,this.slugger=new M}n.parse=function(e,t){return new n(t).parse(e)};var e=n.prototype;return e.parse=function(e){this.tokens=e.reverse();for(var t="";this.next();)t+=this.tok();return t},e.next=function(){return this.token=this.tokens.pop(),this.token},e.peek=function(){return this.tokens[this.tokens.length-1]||0},e.parseText=function(){for(var e=this.token.tokens?this.parseInline(this.token.tokens):this.token.text;"text"===this.peek().type;)this.next(),e+="\n"+(this.token.tokens?this.parseInline(this.token.tokens):this.token.text);return e},e.tok=function(){var e="";switch(this.token.type){case"space":return"";case"hr":return this.renderer.hr();case"heading":return this.renderer.heading(this.parseInline(this.token.tokens),this.token.depth,H(this.parseInline(this.token.tokens,this.textRenderer)),this.slugger);case"code":return this.renderer.code(this.token.text,this.token.lang,this.token.escaped);case"table":var t,n,r,s,i,l,a="";for(r="",i=this.token.header.length,t=0;t/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),h=a[0],r=this.options.sanitize?this.options.sanitizer?this.options.sanitizer(a[0]):Y(a[0]):a[0],t.push({type:this.options.sanitize?"text":"html",raw:h,text:r}),c+=r;else if(a=this.rules.link.exec(e)){var u=W(a[2],"()");if(-1$/,"$1"),c+=this.outputLink(a,{href:f.escapes(i),title:f.escapes(l)},t,h),this.inLink=!1}else if((a=this.rules.reflink.exec(e))||(a=this.rules.nolink.exec(e))){if(e=e.substring(a[0].length),h=a[0],n=(a[2]||a[1]).replace(/\s+/g," "),!(n=this.links[n.toLowerCase()])||!n.href){c+=r=a[0].charAt(0),t.push({type:"text",raw:r,text:r}),e=a[0].substring(1)+e;continue}this.inLink=!0,c+=this.outputLink(a,n,t,h),this.inLink=!1}else if(a=this.rules.strong.exec(e))e=e.substring(a[0].length),h=a[0],s=t?[]:null,r=this.output(a[4]||a[3]||a[2]||a[1],s),t.push({type:"strong",raw:h,text:r,tokens:s}),c+=r;else if(a=this.rules.em.exec(e))e=e.substring(a[0].length),h=a[0],s=t?[]:null,r=this.output(a[6]||a[5]||a[4]||a[3]||a[2]||a[1],s),t.push({type:"em",raw:h,text:r,tokens:s}),c+=r;else if(a=this.rules.code.exec(e))e=e.substring(a[0].length),h=a[0],r=Y(a[2].trim(),!0),t.push({type:"codespan",raw:h,text:r}),c+=r;else if(a=this.rules.br.exec(e))e=e.substring(a[0].length),h=a[0],t.push({type:"br",raw:h}),c+="\n";else if(a=this.rules.del.exec(e))e=e.substring(a[0].length),h=a[0],s=t?[]:null,r=this.output(a[1],s),t.push({type:"del",raw:h,text:r,tokens:s}),c+=r;else if(a=this.rules.autolink.exec(e))e=e.substring(a[0].length),h=a[0],i="@"===a[2]?"mailto:"+(r=Y(this.options.mangle?this.mangle(a[1]):a[1])):r=Y(a[1]),t.push({type:"link",raw:h,text:r,href:i,tokens:[{type:"text",raw:r,text:r}]}),c+=r;else if(this.inLink||!(a=this.rules.url.exec(e))){if(a=this.rules.text.exec(e))e=e.substring(a[0].length),h=a[0],r=this.inRawBlock?this.options.sanitize?this.options.sanitizer?this.options.sanitizer(a[0]):Y(a[0]):a[0]:Y(this.options.smartypants?this.smartypants(a[0]):a[0]),t.push({type:"text",raw:h,text:r}),c+=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=Y(this.options.mangle?this.mangle(a[0]):a[0]));else{for(;o=a[0],a[0]=this.rules._backpedal.exec(a[0])[0],o!==a[0];);r=Y(a[0]),i="www."===a[1]?"http://"+r:r}e=e.substring(a[0].length),h=a[0],t.push({type:"link",raw:h,text:r,href:i,tokens:[{type:"text",raw:r,text:r}]}),c+=r}return c},f.escapes=function(e){return e?e.replace(f.rules._escapes,"$1"):e},e.outputLink=function(e,t,n,r){var s=t.href,i=t.title?Y(t.title):null,l=n?[]:null;if("!"!==e[0].charAt(0)){var a=this.output(e[1],l);return n.push({type:"link",raw:r,text:a,href:s,title:i,tokens:l}),a}var o=Y(e[1]);return n.push({type:"image",raw:r,text:o,href:s,title:i}),o},e.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,"…")},e.mangle=function(e){var t,n,r="",s=e.length;for(t=0;tAn error occurred:

    "+re(e.message+"",!0)+"
    ";throw e}}return ae.options=ae.setOptions=function(e){return te(ae.defaults,e),ie(ae.defaults),ae},ae.getDefaults=se,ae.defaults=le,ae.Parser=J,ae.parser=J.parse,ae.Renderer=X,ae.TextRenderer=G,ae.Lexer=B,ae.lexer=B.lex,ae.InlineLexer=ee,ae.inlineLexer=ee.lex,ae.Slugger=M,ae.parse=ae}); \ No newline at end of file From ce687b8952c06e5d88b02c62734f2cd1e20e7fce Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 2 Apr 2020 00:34:08 -0500 Subject: [PATCH 05/11] show items in lexer data --- docs/demo/worker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/demo/worker.js b/docs/demo/worker.js index 1d62e09ebe..df69e9204c 100644 --- a/docs/demo/worker.js +++ b/docs/demo/worker.js @@ -68,7 +68,7 @@ function getLexedList(lexed, level) { for (var i = 0; i < lexed.length; i++) { var lexedLine = []; for (var j in lexed[i]) { - if (j === 'tokens') { + if (j === 'tokens' || j === 'items') { lexedLine.push(j + ': [\n' + getLexedList(lexed[i][j], level + 1) + '\n]'); } else { lexedLine.push(j + ':' + jsonString(lexed[i][j])); From 441470104aba41d666b2fa8ee35f202b020a27ef Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 2 Apr 2020 01:04:56 -0500 Subject: [PATCH 06/11] default top to true --- src/Lexer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Lexer.js b/src/Lexer.js index 4a4d543f0e..d9e8ec3262 100644 --- a/src/Lexer.js +++ b/src/Lexer.js @@ -59,7 +59,7 @@ module.exports = class Lexer { .replace(/\r\n|\r/g, '\n') .replace(/\t/g, ' '); - this.blockTokens(this.tokens, src, true); + this.blockTokens(this.tokens, src); this.inlineTokens(this.tokens); @@ -69,7 +69,7 @@ module.exports = class Lexer { /** * Lexing */ - blockTokens(tokens, src, top) { + blockTokens(tokens, src, top = true) { src = src.replace(/^ +$/gm, ''); let next, loose, From 8bf237d04974852a1a423d3d0579a1869aded5fd Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 2 Apr 2020 01:05:04 -0500 Subject: [PATCH 07/11] update docs --- docs/USING_PRO.md | 119 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 35 deletions(-) diff --git a/docs/USING_PRO.md b/docs/USING_PRO.md index 7c902db340..23fb7b3158 100644 --- a/docs/USING_PRO.md +++ b/docs/USING_PRO.md @@ -29,7 +29,7 @@ renderer.heading = function (text, level) { }; // Run marked -console.log(marked('# heading+', { renderer: renderer })); +console.log(marked('# heading+', { renderer })); ``` **Output:** @@ -91,51 +91,59 @@ slugger.slug('foo-1') // foo-1-2

    The lexer

    -The lexer turns a markdown string into block level tokens. - -

    The inline lexer

    - -The inline lexer adds inline tokens to the block level tokens. +The lexer turns a markdown string into tokens.

    The parser

    -The parser takes tokens as input and calls the renderer functions that are accosiated with those tokens. +The parser takes tokens as input and calls the renderer functions. ***

    Access to lexer and parser

    -You also have direct access to the lexer, inline lexer, and parser if you so desire. +You also have direct access to the lexer and parser if you so desire. ``` js -const blocks = marked.lexer(markdown, options); -const tokens = marked.inlineLexer(blocks, options); +const tokens = marked.lexer(markdown, options); console.log(marked.parser(tokens, options)); ``` ``` js const lexer = new marked.Lexer(options); -const inlineLexer = new marked.InlineLexer(options); -const blocks = lexer.lex(markdown); -const tokens = inlineLexer.lex(blocks); +const tokens = lexer.lex(markdown); console.log(tokens); -console.log(lexer.rules); // block level rules -console.log(inlineLexer.rules); // inline level 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 Lexer and InlineLexer build an array of tokens, which will be passed to the Parser. -The Parser processes each token in the token array, -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'); @@ -148,24 +156,65 @@ const md = ` [1]: #heading "heading" `; -const blocks = marked.lexer(md); -const tokens = marked.inlineLexer(blocks); +const tokens = marked.lexer(md); 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' } } ] ``` From 02e605d05b950f78598b72c6601474ef1fbf5065 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 2 Apr 2020 01:22:01 -0500 Subject: [PATCH 08/11] declare variables outside loops --- lib/marked.esm.js | 84 ++++++++++++++++++++++++++------------------- lib/marked.js | 87 +++++++++++++++++++++++++++-------------------- marked.min.js | 2 +- src/Lexer.js | 15 +++++--- src/Parser.js | 65 ++++++++++++++++++++--------------- 5 files changed, 148 insertions(+), 105 deletions(-) diff --git a/lib/marked.esm.js b/lib/marked.esm.js index 99f86a76b0..b48c13368d 100644 --- a/lib/marked.esm.js +++ b/lib/marked.esm.js @@ -621,7 +621,7 @@ var Lexer_1 = class Lexer { .replace(/\r\n|\r/g, '\n') .replace(/\t/g, ' '); - this.blockTokens(this.tokens, src, true); + this.blockTokens(this.tokens, src); this.inlineTokens(this.tokens); @@ -631,7 +631,7 @@ var Lexer_1 = class Lexer { /** * Lexing */ - blockTokens(tokens, src, top) { + blockTokens(tokens, src, top = true) { src = src.replace(/^ +$/gm, ''); let next, loose, @@ -647,6 +647,8 @@ var Lexer_1 = class Lexer { isordered, istask, ischecked, + lastToken, + addBack, raw; while (src) { @@ -664,7 +666,7 @@ var Lexer_1 = class Lexer { // code if (cap = this.rules.block.code.exec(src)) { - const lastToken = tokens[tokens.length - 1]; + lastToken = tokens[tokens.length - 1]; src = src.substring(cap[0].length); raw = cap[0]; // An indented code block cannot interrupt a paragraph. @@ -824,7 +826,7 @@ 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))) { - const addBack = cap.slice(i + 1).join('\n'); + addBack = cap.slice(i + 1).join('\n'); src = addBack + src; list.raw = list.raw.substring(list.raw.length - addBack.length); i = l - 1; @@ -1060,6 +1062,9 @@ var Lexer_1 = class Lexer { title, cap, prevCapZero, + lastParenIndex, + start, + linkLen, raw; while (src) { @@ -1110,10 +1115,10 @@ var Lexer_1 = class Lexer { // link if (cap = this.rules.inline.link.exec(src)) { - const lastParenIndex = findClosingBracket$1(cap[2], '()'); + lastParenIndex = findClosingBracket$1(cap[2], '()'); if (lastParenIndex > -1) { - const start = cap[0].indexOf('!') === 0 ? 5 : 4; - const 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] = ''; @@ -1689,11 +1694,27 @@ var Parser_1 = class Parser { */ parse(tokens, top = true) { let out = '', + i, + j, + k, + l2, + l3, + row, + cell, + header, body, - token; + token, + ordered, + start, + loose, + itemBody, + item, + checked, + task, + checkbox; const l = tokens.length; - for (let i = 0; i < l; i++) { + for (i = 0; i < l; i++) { token = tokens[i]; switch (token.type) { case 'space': { @@ -1718,36 +1739,30 @@ var Parser_1 = class Parser { continue; } case 'table': { - let header = '', - i, - row, - cell, - j, - l2, - l3; + header = ''; // header cell = ''; l2 = token.header.length; - for (i = 0; i < l2; i++) { + for (j = 0; j < l2; j++) { cell += this.renderer.tablecell( - this.parseInline(token.tokens.header[i]), - { header: true, align: token.align[i] } + this.parseInline(token.tokens.header[j]), + { header: true, align: token.align[j] } ); } header += this.renderer.tablerow(cell); body = ''; l2 = token.cells.length; - for (i = 0; i < l2; i++) { - row = token.tokens.cells[i]; + for (j = 0; j < l2; j++) { + row = token.tokens.cells[j]; cell = ''; l3 = row.length; - for (j = 0; j < l3; j++) { + for (k = 0; k < l3; k++) { cell += this.renderer.tablecell( - this.parseInline(row[j]), - { header: false, align: token.align[j] } + this.parseInline(row[k]), + { header: false, align: token.align[k] } ); } @@ -1762,21 +1777,20 @@ var Parser_1 = class Parser { continue; } case 'list': { - const ordered = token.ordered, - start = token.start, - loose = token.loose, - l2 = token.items.length; - let itemBody; + ordered = token.ordered; + start = token.start; + loose = token.loose; + l2 = token.items.length; body = ''; - for (let j = 0; j < l2; j++) { - const item = token.items[j]; - const checked = item.checked; - const task = item.task; + for (j = 0; j < l2; j++) { + item = token.items[j]; + checked = item.checked; + task = item.task; itemBody = ''; if (item.task) { - const checkbox = this.renderer.checkbox(checked); + checkbox = this.renderer.checkbox(checked); if (loose) { if (item.tokens[0].type === 'text') { item.tokens[0].text = checkbox + ' ' + item.tokens[0].text; @@ -1887,7 +1901,7 @@ var Parser_1 = class Parser { break; } default: { - const errMsg = 'Inline token with "' + this.token.type + '" type was not found.'; + const errMsg = 'Token with "' + this.token.type + '" type was not found.'; if (this.options.silent) { console.error(errMsg); } else { diff --git a/lib/marked.js b/lib/marked.js index 2809bbdb4b..d25ef2836c 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -564,7 +564,7 @@ _proto.lex = function lex(src) { src = src.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' '); - this.blockTokens(this.tokens, src, true); + this.blockTokens(this.tokens, src); this.inlineTokens(this.tokens); return this.tokens; } @@ -574,8 +574,12 @@ ; _proto.blockTokens = function blockTokens(tokens, src, top) { + if (top === void 0) { + top = true; + } + src = src.replace(/^ +$/gm, ''); - var next, loose, cap, bull, b, item, list, space, i, tag, l, isordered, istask, ischecked, raw; + var next, loose, cap, bull, b, item, list, space, i, tag, l, isordered, istask, ischecked, lastToken, addBack, raw; while (src) { // newline @@ -593,7 +597,7 @@ if (cap = this.rules.block.code.exec(src)) { - var lastToken = tokens[tokens.length - 1]; + lastToken = tokens[tokens.length - 1]; src = src.substring(cap[0].length); raw = cap[0]; // An indented code block cannot interrupt a paragraph. @@ -741,7 +745,7 @@ b = block$1.bullet.exec(cap[i + 1])[0]; if (bull.length > 1 ? b.length === 1 : b.length > 1 || this.options.smartLists && b !== bull) { - var addBack = cap.slice(i + 1).join('\n'); + addBack = cap.slice(i + 1).join('\n'); src = addBack + src; list.raw = list.raw.substring(list.raw.length - addBack.length); i = l - 1; @@ -983,6 +987,9 @@ title, cap, prevCapZero, + lastParenIndex, + start, + linkLen, raw; while (src) { @@ -1028,11 +1035,11 @@ if (cap = this.rules.inline.link.exec(src)) { - var lastParenIndex = findClosingBracket$1(cap[2], '()'); + 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] = ''; @@ -1623,11 +1630,27 @@ } var out = '', + i, + j, + k, + l2, + l3, + row, + cell, + header, body, - token; + token, + ordered, + start, + loose, + itemBody, + item, + checked, + task, + checkbox; var l = tokens.length; - for (var i = 0; i < l; i++) { + for (i = 0; i < l; i++) { token = tokens[i]; switch (token.type) { @@ -1656,22 +1679,15 @@ case 'table': { - var header = '', - _i = void 0, - row = void 0, - cell = void 0, - j = void 0, - l2 = void 0, - l3 = void 0; // header - + header = ''; // header cell = ''; l2 = token.header.length; - for (_i = 0; _i < l2; _i++) { - cell += this.renderer.tablecell(this.parseInline(token.tokens.header[_i]), { + for (j = 0; j < l2; j++) { + cell += this.renderer.tablecell(this.parseInline(token.tokens.header[j]), { header: true, - align: token.align[_i] + align: token.align[j] }); } @@ -1679,15 +1695,15 @@ body = ''; l2 = token.cells.length; - for (_i = 0; _i < l2; _i++) { - row = token.tokens.cells[_i]; + for (j = 0; j < l2; j++) { + row = token.tokens.cells[j]; cell = ''; l3 = row.length; - for (j = 0; j < l3; j++) { - cell += this.renderer.tablecell(this.parseInline(row[j]), { + for (k = 0; k < l3; k++) { + cell += this.renderer.tablecell(this.parseInline(row[k]), { header: false, - align: token.align[j] + align: token.align[k] }); } @@ -1707,21 +1723,20 @@ case 'list': { - var ordered = token.ordered, - start = token.start, - loose = token.loose, - _l = token.items.length; - var itemBody = void 0; + ordered = token.ordered; + start = token.start; + loose = token.loose; + l2 = token.items.length; body = ''; - for (var _j = 0; _j < _l; _j++) { - var item = token.items[_j]; - var checked = item.checked; - var task = item.task; + for (j = 0; j < l2; j++) { + item = token.items[j]; + checked = item.checked; + task = item.task; itemBody = ''; if (item.task) { - var checkbox = this.renderer.checkbox(checked); + checkbox = this.renderer.checkbox(checked); if (loose) { if (item.tokens[0].type === 'text') { @@ -1868,7 +1883,7 @@ default: { - var errMsg = 'Inline token with "' + this.token.type + '" type was not found.'; + var errMsg = 'Token with "' + this.token.type + '" type was not found.'; if (this.options.silent) { console.error(errMsg); diff --git a/marked.min.js b/marked.min.js index 9586cd0c6c..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 c=/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/gi;function u(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 p=/(^|[^\[])\^/g;var g=/[^\w:]/g,f=/^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;var d={},k=/^[^:]+:\/*[^/]*$/,b=/^([^:]+:)[\s\S]*$/,m=/^([^:]+:\/*[^/]*)[\s\S]*$/;function x(e,t){d[" "+e]||(k.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(b,"$1")+t:"/"===t.charAt(0)?n?t:e.replace(m,"$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?'|\([^()]*\))/};L.def=q(L.def).replace("label",L._label).replace("title",L._title).getRegex(),L.bullet=/(?:[*+-]|\d{1,9}\.)/,L.item=/^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/,L.item=q(L.item,"gm").replace(/bull/g,L.bullet).getRegex(),L.list=q(L.list).replace(/bull/g,L.bullet).replace("hr","\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))").replace("def","\\n+(?="+L.def.source+")").getRegex(),L._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",L._comment=//,L.html=q(L.html,"i").replace("comment",L._comment).replace("tag",L._tag).replace("attribute",/ +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex(),L.paragraph=q(L._paragraph).replace("hr",L.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",L._tag).getRegex(),L.blockquote=q(L.blockquote).replace("paragraph",L.paragraph).getRegex(),L.normal=I({},L),L.gfm=I({},L.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*|$)"}),L.gfm.nptable=q(L.gfm.nptable).replace("hr",L.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",L._tag).getRegex(),L.gfm.table=q(L.gfm.table).replace("hr",L.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",L._tag).getRegex(),L.pedantic=I({},L.normal,{html:q("^ *(?:comment *(?:\\n|\\s*$)|<(tag)[\\s\\S]+? *(?:\\n{2,}|\\s*$)|\\s]*)*?/?> *(?:\\n{2,}|\\s*$))").replace("comment",L._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(L.normal._paragraph).replace("hr",L.hr).replace("heading"," *#{1,6} *[^\n]").replace("lheading",L.lheading).replace("blockquote"," {0,3}>").replace("|fences","").replace("|list","").replace("|html","").getRegex()});var C={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]*?(?:(?=[\\?@\\[^_{|}~"};C.em=q(C.em).replace(/punctuation/g,C._punctuation).getRegex(),C._escapes=/\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g,C._scheme=/[a-zA-Z][a-zA-Z0-9+.-]{1,31}/,C._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])?)+(?![-_])/,C.autolink=q(C.autolink).replace("scheme",C._scheme).replace("email",C._email).getRegex(),C._attribute=/\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/,C.tag=q(C.tag).replace("comment",L._comment).replace("attribute",C._attribute).getRegex(),C._label=/(?:\[[^\[\]]*\]|\\.|`[^`]*`|[^\[\]\\`])*?/,C._href=/<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*/,C._title=/"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/,C.link=q(C.link).replace("label",C._label).replace("href",C._href).replace("title",C._title).getRegex(),C.reflink=q(C.reflink).replace("label",C._label).getRegex(),C.normal=I({},C),C.pedantic=I({},C.normal,{strong:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,em:/^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/,link:q(/^!?\[(label)\]\((.*?)\)/).replace("label",C._label).getRegex(),reflink:q(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace("label",C._label).getRegex()}),C.gfm=I({},C.normal,{escape:q(C.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",raw:m=s[0],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(){}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}(),M=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}(),V=s.defaults,H=y,J=function(){function n(e){this.tokens=[],this.token=null,this.options=e||V,this.options.renderer=this.options.renderer||new X,this.renderer=this.options.renderer,this.textRenderer=new G,this.renderer.options=this.options,this.slugger=new M}n.parse=function(e,t){return new n(t).parse(e)};var e=n.prototype;return e.parse=function(e){this.tokens=e.reverse();for(var t="";this.next();)t+=this.tok();return t},e.next=function(){return this.token=this.tokens.pop(),this.token},e.peek=function(){return this.tokens[this.tokens.length-1]||0},e.parseText=function(){for(var e=this.token.tokens?this.parseInline(this.token.tokens):this.token.text;"text"===this.peek().type;)this.next(),e+="\n"+(this.token.tokens?this.parseInline(this.token.tokens):this.token.text);return e},e.tok=function(){var e="";switch(this.token.type){case"space":return"";case"hr":return this.renderer.hr();case"heading":return this.renderer.heading(this.parseInline(this.token.tokens),this.token.depth,H(this.parseInline(this.token.tokens,this.textRenderer)),this.slugger);case"code":return this.renderer.code(this.token.text,this.token.lang,this.token.escaped);case"table":var t,n,r,s,i,l,a="";for(r="",i=this.token.header.length,t=0;t/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),h=a[0],r=this.options.sanitize?this.options.sanitizer?this.options.sanitizer(a[0]):Y(a[0]):a[0],t.push({type:this.options.sanitize?"text":"html",raw:h,text:r}),c+=r;else if(a=this.rules.link.exec(e)){var u=W(a[2],"()");if(-1$/,"$1"),c+=this.outputLink(a,{href:f.escapes(i),title:f.escapes(l)},t,h),this.inLink=!1}else if((a=this.rules.reflink.exec(e))||(a=this.rules.nolink.exec(e))){if(e=e.substring(a[0].length),h=a[0],n=(a[2]||a[1]).replace(/\s+/g," "),!(n=this.links[n.toLowerCase()])||!n.href){c+=r=a[0].charAt(0),t.push({type:"text",raw:r,text:r}),e=a[0].substring(1)+e;continue}this.inLink=!0,c+=this.outputLink(a,n,t,h),this.inLink=!1}else if(a=this.rules.strong.exec(e))e=e.substring(a[0].length),h=a[0],s=t?[]:null,r=this.output(a[4]||a[3]||a[2]||a[1],s),t.push({type:"strong",raw:h,text:r,tokens:s}),c+=r;else if(a=this.rules.em.exec(e))e=e.substring(a[0].length),h=a[0],s=t?[]:null,r=this.output(a[6]||a[5]||a[4]||a[3]||a[2]||a[1],s),t.push({type:"em",raw:h,text:r,tokens:s}),c+=r;else if(a=this.rules.code.exec(e))e=e.substring(a[0].length),h=a[0],r=Y(a[2].trim(),!0),t.push({type:"codespan",raw:h,text:r}),c+=r;else if(a=this.rules.br.exec(e))e=e.substring(a[0].length),h=a[0],t.push({type:"br",raw:h}),c+="\n";else if(a=this.rules.del.exec(e))e=e.substring(a[0].length),h=a[0],s=t?[]:null,r=this.output(a[1],s),t.push({type:"del",raw:h,text:r,tokens:s}),c+=r;else if(a=this.rules.autolink.exec(e))e=e.substring(a[0].length),h=a[0],i="@"===a[2]?"mailto:"+(r=Y(this.options.mangle?this.mangle(a[1]):a[1])):r=Y(a[1]),t.push({type:"link",raw:h,text:r,href:i,tokens:[{type:"text",raw:r,text:r}]}),c+=r;else if(this.inLink||!(a=this.rules.url.exec(e))){if(a=this.rules.text.exec(e))e=e.substring(a[0].length),h=a[0],r=this.inRawBlock?this.options.sanitize?this.options.sanitizer?this.options.sanitizer(a[0]):Y(a[0]):a[0]:Y(this.options.smartypants?this.smartypants(a[0]):a[0]),t.push({type:"text",raw:h,text:r}),c+=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=Y(this.options.mangle?this.mangle(a[0]):a[0]));else{for(;o=a[0],a[0]=this.rules._backpedal.exec(a[0])[0],o!==a[0];);r=Y(a[0]),i="www."===a[1]?"http://"+r:r}e=e.substring(a[0].length),h=a[0],t.push({type:"link",raw:h,text:r,href:i,tokens:[{type:"text",raw:r,text:r}]}),c+=r}return c},f.escapes=function(e){return e?e.replace(f.rules._escapes,"$1"):e},e.outputLink=function(e,t,n,r){var s=t.href,i=t.title?Y(t.title):null,l=n?[]:null;if("!"!==e[0].charAt(0)){var a=this.output(e[1],l);return n.push({type:"link",raw:r,text:a,href:s,title:i,tokens:l}),a}var o=Y(e[1]);return n.push({type:"image",raw:r,text:o,href:s,title:i}),o},e.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,"…")},e.mangle=function(e){var t,n,r="",s=e.length;for(t=0;tAn error occurred:

    "+re(e.message+"",!0)+"
    ";throw e}}return ae.options=ae.setOptions=function(e){return te(ae.defaults,e),ie(ae.defaults),ae},ae.getDefaults=se,ae.defaults=le,ae.Parser=J,ae.parser=J.parse,ae.Renderer=X,ae.TextRenderer=G,ae.Lexer=B,ae.lexer=B.lex,ae.InlineLexer=ee,ae.inlineLexer=ee.lex,ae.Slugger=M,ae.parse=ae}); \ 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/Lexer.js b/src/Lexer.js index d9e8ec3262..4d2bc6574b 100644 --- a/src/Lexer.js +++ b/src/Lexer.js @@ -85,6 +85,8 @@ module.exports = class Lexer { isordered, istask, ischecked, + lastToken, + addBack, raw; while (src) { @@ -102,7 +104,7 @@ module.exports = class Lexer { // code if (cap = this.rules.block.code.exec(src)) { - const lastToken = tokens[tokens.length - 1]; + lastToken = tokens[tokens.length - 1]; src = src.substring(cap[0].length); raw = cap[0]; // An indented code block cannot interrupt a paragraph. @@ -262,7 +264,7 @@ 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))) { - const addBack = cap.slice(i + 1).join('\n'); + addBack = cap.slice(i + 1).join('\n'); src = addBack + src; list.raw = list.raw.substring(list.raw.length - addBack.length); i = l - 1; @@ -501,6 +503,9 @@ module.exports = class Lexer { title, cap, prevCapZero, + lastParenIndex, + start, + linkLen, raw; while (src) { @@ -551,10 +556,10 @@ module.exports = class Lexer { // link if (cap = this.rules.inline.link.exec(src)) { - const lastParenIndex = findClosingBracket(cap[2], '()'); + lastParenIndex = findClosingBracket(cap[2], '()'); if (lastParenIndex > -1) { - const start = cap[0].indexOf('!') === 0 ? 5 : 4; - const 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] = ''; diff --git a/src/Parser.js b/src/Parser.js index f844739039..72fa4789ac 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -32,11 +32,27 @@ module.exports = class Parser { */ parse(tokens, top = true) { let out = '', + i, + j, + k, + l2, + l3, + row, + cell, + header, body, - token; + token, + ordered, + start, + loose, + itemBody, + item, + checked, + task, + checkbox; const l = tokens.length; - for (let i = 0; i < l; i++) { + for (i = 0; i < l; i++) { token = tokens[i]; switch (token.type) { case 'space': { @@ -61,36 +77,30 @@ module.exports = class Parser { continue; } case 'table': { - let header = '', - i, - row, - cell, - j, - l2, - l3; + header = ''; // header cell = ''; l2 = token.header.length; - for (i = 0; i < l2; i++) { + for (j = 0; j < l2; j++) { cell += this.renderer.tablecell( - this.parseInline(token.tokens.header[i]), - { header: true, align: token.align[i] } + this.parseInline(token.tokens.header[j]), + { header: true, align: token.align[j] } ); } header += this.renderer.tablerow(cell); body = ''; l2 = token.cells.length; - for (i = 0; i < l2; i++) { - row = token.tokens.cells[i]; + for (j = 0; j < l2; j++) { + row = token.tokens.cells[j]; cell = ''; l3 = row.length; - for (j = 0; j < l3; j++) { + for (k = 0; k < l3; k++) { cell += this.renderer.tablecell( - this.parseInline(row[j]), - { header: false, align: token.align[j] } + this.parseInline(row[k]), + { header: false, align: token.align[k] } ); } @@ -105,21 +115,20 @@ module.exports = class Parser { continue; } case 'list': { - const ordered = token.ordered, - start = token.start, - loose = token.loose, - l2 = token.items.length; - let itemBody; + ordered = token.ordered; + start = token.start; + loose = token.loose; + l2 = token.items.length; body = ''; - for (let j = 0; j < l2; j++) { - const item = token.items[j]; - const checked = item.checked; - const task = item.task; + for (j = 0; j < l2; j++) { + item = token.items[j]; + checked = item.checked; + task = item.task; itemBody = ''; if (item.task) { - const checkbox = this.renderer.checkbox(checked); + checkbox = this.renderer.checkbox(checked); if (loose) { if (item.tokens[0].type === 'text') { item.tokens[0].text = checkbox + ' ' + item.tokens[0].text; @@ -230,7 +239,7 @@ module.exports = class Parser { break; } default: { - const errMsg = 'Inline token with "' + this.token.type + '" type was not found.'; + const errMsg = 'Token with "' + this.token.type + '" type was not found.'; if (this.options.silent) { console.error(errMsg); } else { From ea520840344a1ae8e2e49a76fd8a2ed3e746b9ce Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Fri, 3 Apr 2020 09:29:08 -0500 Subject: [PATCH 09/11] return on error --- src/Parser.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Parser.js b/src/Parser.js index 72fa4789ac..4d333269e9 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -14,8 +14,8 @@ module.exports = class Parser { this.options = options || defaults; this.options.renderer = this.options.renderer || new Renderer(); this.renderer = this.options.renderer; - this.textRenderer = new TextRenderer(); this.renderer.options = this.options; + this.textRenderer = new TextRenderer(); this.slugger = new Slugger(); } @@ -175,6 +175,7 @@ module.exports = class Parser { const errMsg = 'Token with "' + token.type + '" type was not found.'; if (this.options.silent) { console.error(errMsg); + return; } else { throw new Error(errMsg); } @@ -242,6 +243,7 @@ module.exports = class Parser { const errMsg = 'Token with "' + this.token.type + '" type was not found.'; if (this.options.silent) { console.error(errMsg); + return; } else { throw new Error(errMsg); } From 11b026c254c7e93316d3843556bdbc38df162ce0 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Mon, 6 Apr 2020 22:28:47 -0500 Subject: [PATCH 10/11] rename functions --- src/Lexer.js | 32 +++++++++--------- test/unit/Lexer-spec.js | 72 ++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/Lexer.js b/src/Lexer.js index 4d2bc6574b..b119241591 100644 --- a/src/Lexer.js +++ b/src/Lexer.js @@ -59,9 +59,9 @@ module.exports = class Lexer { .replace(/\r\n|\r/g, '\n') .replace(/\t/g, ' '); - this.blockTokens(this.tokens, src); + this.blockTokens(src, this.tokens); - this.inlineTokens(this.tokens); + this.inline(this.tokens); return this.tokens; } @@ -69,7 +69,7 @@ module.exports = class Lexer { /** * Lexing */ - blockTokens(tokens, src, top = true) { + blockTokens(src, tokens, top = true) { src = src.replace(/^ +$/gm, ''); let next, loose, @@ -210,7 +210,7 @@ module.exports = class Lexer { tokens.push({ type: 'blockquote', raw, - tokens: this.blockTokens([], cap, top) + tokens: this.blockTokens(cap, [], top) }); continue; @@ -297,7 +297,7 @@ module.exports = class Lexer { task: istask, checked: ischecked, loose: loose, - tokens: this.blockTokens([], item, false) + tokens: this.blockTokens(item, [], false) }); } @@ -426,7 +426,7 @@ module.exports = class Lexer { return tokens; } - inlineTokens(tokens) { + inline(tokens) { let i, j, k, @@ -442,7 +442,7 @@ module.exports = class Lexer { case 'text': case 'heading': { token.tokens = []; - this.inlineOutput(token.text, token.tokens); + this.inlineTokens(token.text, token.tokens); break; } case 'table': { @@ -455,7 +455,7 @@ module.exports = class Lexer { l2 = token.header.length; for (j = 0; j < l2; j++) { token.tokens.header[j] = []; - this.inlineOutput(token.header[j], token.tokens.header[j]); + this.inlineTokens(token.header[j], token.tokens.header[j]); } // cells @@ -465,20 +465,20 @@ module.exports = class Lexer { 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]); + this.inlineTokens(row[k], token.tokens.cells[j][k]); } } break; } case 'blockquote': { - this.inlineTokens(token.tokens); + this.inline(token.tokens); break; } case 'list': { l2 = token.items.length; for (j = 0; j < l2; j++) { - this.inlineTokens(token.items[j].tokens); + this.inline(token.items[j].tokens); } break; } @@ -494,7 +494,7 @@ module.exports = class Lexer { /** * Lexing/Compiling */ - inlineOutput(src, tokens) { + inlineTokens(src, tokens) { let out = '', link, text, @@ -618,7 +618,7 @@ module.exports = class Lexer { src = src.substring(cap[0].length); raw = cap[0]; newTokens = tokens ? [] : null; - text = this.inlineOutput(cap[4] || cap[3] || cap[2] || cap[1], newTokens); + text = this.inlineTokens(cap[4] || cap[3] || cap[2] || cap[1], newTokens); tokens.push({ type: 'strong', @@ -635,7 +635,7 @@ module.exports = class Lexer { 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); + text = this.inlineTokens(cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1], newTokens); tokens.push({ type: 'em', raw, @@ -677,7 +677,7 @@ module.exports = class Lexer { src = src.substring(cap[0].length); raw = cap[0]; newTokens = tokens ? [] : null; - text = this.inlineOutput(cap[1], newTokens); + text = this.inlineTokens(cap[1], newTokens); tokens.push({ type: 'del', raw, @@ -797,7 +797,7 @@ module.exports = class Lexer { const newTokens = tokens ? [] : null; if (cap[0].charAt(0) !== '!') { - const text = this.inlineOutput(cap[1], newTokens); + const text = this.inlineTokens(cap[1], newTokens); tokens.push({ type: 'link', raw, diff --git a/test/unit/Lexer-spec.js b/test/unit/Lexer-spec.js index 13e92d8add..0d334358a0 100644 --- a/test/unit/Lexer-spec.js +++ b/test/unit/Lexer-spec.js @@ -9,20 +9,20 @@ function expectTokens({ md, options, tokens = [], links = {} }) { expect(actual).toEqual(expected); } -function expectInlineOutput({ md, options, output = jasmine.any(String), tokens = jasmine.any(Array), links = {} }) { +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.inlineOutput(md, outTokens); + const outOutput = lexer.inlineTokens(md, outTokens); expect({ output: outOutput, tokens: outTokens }).toEqual({ output, tokens }); } -function expectInlineTokens({ token, options, tokens }) { +function expectInline({ token, options, tokens }) { const lexer = new Lexer(options); - lexer.inlineTokens([token]); + lexer.inline([token]); expect(token.tokens).toEqual(tokens); } @@ -506,7 +506,7 @@ a | b describe('inline', () => { describe('tokens', () => { it('paragraph', () => { - expectInlineTokens({ + expectInline({ token: { type: 'paragraph', text: 'text' }, tokens: [ { type: 'text', raw: 'text', text: 'text' } @@ -515,7 +515,7 @@ a | b }); it('text', () => { - expectInlineTokens({ + expectInline({ token: { type: 'text', text: 'text' }, tokens: [ { type: 'text', raw: 'text', text: 'text' } @@ -524,7 +524,7 @@ a | b }); it('heading', () => { - expectInlineTokens({ + expectInline({ token: { type: 'heading', text: 'text' }, tokens: [ { type: 'text', raw: 'text', text: 'text' } @@ -533,7 +533,7 @@ a | b }); it('table', () => { - expectInlineTokens({ + expectInline({ token: { type: 'table', header: ['a', 'b'], @@ -556,7 +556,7 @@ a | b }); it('code no inline tokens', () => { - expectInlineTokens({ + expectInline({ token: { type: 'code', text: 'code' }, tokens: undefined }); @@ -565,7 +565,7 @@ a | b describe('output', () => { it('escape', () => { - expectInlineOutput({ + expectInlineTokens({ md: '\\>', output: '>', tokens: [ @@ -575,7 +575,7 @@ a | b }); it('html', () => { - expectInlineOutput({ + expectInlineTokens({ md: '
    html
    ', output: '
    html
    ', tokens: [ @@ -587,7 +587,7 @@ a | b }); it('html sanitize', () => { - expectInlineOutput({ + expectInlineTokens({ md: '
    html
    ', options: { sanitize: true }, output: '<div>html</div>', @@ -600,7 +600,7 @@ a | b }); it('link', () => { - expectInlineOutput({ + expectInlineTokens({ md: '[link](https://example.com)', output: 'link', tokens: [ @@ -619,7 +619,7 @@ a | b }); it('link title', () => { - expectInlineOutput({ + expectInlineTokens({ md: '[link](https://example.com "title")', output: 'link', tokens: [ @@ -638,7 +638,7 @@ a | b }); it('image', () => { - expectInlineOutput({ + expectInlineTokens({ md: '![image](https://example.com/image.png)', output: 'image', tokens: [ @@ -654,7 +654,7 @@ a | b }); it('image title', () => { - expectInlineOutput({ + expectInlineTokens({ md: '![image](https://example.com/image.png "title")', output: 'image', tokens: [ @@ -671,7 +671,7 @@ a | b describe('reflink', () => { it('reflink', () => { - expectInlineOutput({ + expectInlineTokens({ md: '[link][]', links: { link: { href: 'https://example.com', title: 'title' } @@ -695,7 +695,7 @@ a | b }); it('nolink', () => { - expectInlineOutput({ + expectInlineTokens({ md: '[link]', links: { link: { href: 'https://example.com', title: 'title' } @@ -719,7 +719,7 @@ a | b }); it('no def', () => { - expectInlineOutput({ + expectInlineTokens({ md: '[link]', output: '[link]', tokens: [ @@ -731,7 +731,7 @@ a | b }); it('strong', () => { - expectInlineOutput({ + expectInlineTokens({ md: '**strong**', output: 'strong', tokens: [ @@ -748,7 +748,7 @@ a | b }); it('em', () => { - expectInlineOutput({ + expectInlineTokens({ md: '*em*', output: 'em', tokens: [ @@ -765,7 +765,7 @@ a | b }); it('code', () => { - expectInlineOutput({ + expectInlineTokens({ md: '`code`', output: 'code', tokens: [ @@ -775,7 +775,7 @@ a | b }); it('br', () => { - expectInlineOutput({ + expectInlineTokens({ md: 'a\nb', options: { gfm: true, breaks: true }, output: 'a\nb', @@ -786,7 +786,7 @@ a | b }); it('del', () => { - expectInlineOutput({ + expectInlineTokens({ md: '~~del~~', output: 'del', tokens: [ @@ -804,7 +804,7 @@ a | b describe('url', () => { it('autolink', () => { - expectInlineOutput({ + expectInlineTokens({ md: '', output: 'https://example.com', tokens: [ @@ -822,7 +822,7 @@ a | b }); it('autolink email', () => { - expectInlineOutput({ + expectInlineTokens({ md: '', options: { mangle: false }, output: 'test@example.com', @@ -841,7 +841,7 @@ a | b }); it('autolink mangle email', () => { - expectInlineOutput({ + expectInlineTokens({ md: '', options: { mangle: true }, output: jasmine.stringMatching('&#'), @@ -864,7 +864,7 @@ a | b }); it('url', () => { - expectInlineOutput({ + expectInlineTokens({ md: 'https://example.com', output: 'https://example.com', tokens: [ @@ -882,7 +882,7 @@ a | b }); it('url email', () => { - expectInlineOutput({ + expectInlineTokens({ md: 'test@example.com', options: { gfm: true, mangle: false }, output: 'test@example.com', @@ -901,7 +901,7 @@ a | b }); it('url mangle email', () => { - expectInlineOutput({ + expectInlineTokens({ md: 'test@example.com', options: { gfm: true, mangle: true }, output: jasmine.stringMatching('&#'), @@ -925,7 +925,7 @@ a | b }); it('text', () => { - expectInlineOutput({ + expectInlineTokens({ md: 'text', output: 'text', tokens: [ @@ -940,7 +940,7 @@ a | b describe('smartypants', () => { it('single quotes', () => { - expectInlineOutput({ + expectInlineTokens({ md: "'single quotes'", options: { smartypants: true }, output: '‘single quotes’', @@ -955,7 +955,7 @@ a | b }); it('double quotes', () => { - expectInlineOutput({ + expectInlineTokens({ md: '"double quotes"', options: { smartypants: true }, output: '“double quotes”', @@ -970,7 +970,7 @@ a | b }); it('ellipses', () => { - expectInlineOutput({ + expectInlineTokens({ md: 'ellipses...', options: { smartypants: true }, output: 'ellipses…', @@ -985,7 +985,7 @@ a | b }); it('en-dash', () => { - expectInlineOutput({ + expectInlineTokens({ md: 'en--dash', options: { smartypants: true }, output: 'en–dash', @@ -1000,7 +1000,7 @@ a | b }); it('em-dash', () => { - expectInlineOutput({ + expectInlineTokens({ md: 'em---dash', options: { smartypants: true }, output: 'em—dash', From 838e963fbd88f0e1ace1501c8e954f9604cc1cf4 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 7 Apr 2020 00:22:39 -0500 Subject: [PATCH 11/11] fix inline token error --- src/Parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parser.js b/src/Parser.js index 4d333269e9..d53aebef5c 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -240,7 +240,7 @@ module.exports = class Parser { break; } default: { - const errMsg = 'Token with "' + this.token.type + '" type was not found.'; + const errMsg = 'Token with "' + token.type + '" type was not found.'; if (this.options.silent) { console.error(errMsg); return;