diff --git a/eslint.config.js b/eslint.config.js index 6e248863..0353927c 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -2,7 +2,7 @@ import markedEslintConfig from '@markedjs/eslint-config'; export default [ { - ignores: ['**/lib', '**/public'], + ignores: ['**/lib', '**/public', 'test.js', 'vuln.js'], }, ...markedEslintConfig, ]; diff --git a/src/Parser.ts b/src/Parser.ts index 741fdee6..18d33b3a 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -39,7 +39,7 @@ export class _Parser { /** * Parse Loop */ - parse(tokens: Token[], top = true): ParserOutput { + parse(tokens: Token[]): ParserOutput { let out = ''; for (let i = 0; i < tokens.length; i++) { @@ -60,62 +60,51 @@ export class _Parser { switch (token.type) { case 'space': { out += this.renderer.space(token); - continue; + break; } case 'hr': { out += this.renderer.hr(token); - continue; + break; } case 'heading': { out += this.renderer.heading(token); - continue; + break; } case 'code': { out += this.renderer.code(token); - continue; + break; } case 'table': { out += this.renderer.table(token); - continue; + break; } case 'blockquote': { out += this.renderer.blockquote(token); - continue; + break; } case 'list': { out += this.renderer.list(token); - continue; + break; + } + case 'checkbox': { + out += this.renderer.checkbox(token); + break; } case 'html': { out += this.renderer.html(token); - continue; + break; } case 'def': { out += this.renderer.def(token); - continue; + break; } case 'paragraph': { out += this.renderer.paragraph(token); - continue; + break; } case 'text': { - let textToken = token; - let body = this.renderer.text(textToken) as string; - while (i + 1 < tokens.length && tokens[i + 1].type === 'text') { - textToken = tokens[++i] as Tokens.Text; - body += ('\n' + this.renderer.text(textToken)); - } - if (top) { - out += this.renderer.paragraph({ - type: 'paragraph', - raw: body, - text: body, - tokens: [{ type: 'text', raw: body, text: body, escaped: true }], - }); - } else { - out += body; - } - continue; + out += this.renderer.text(token); + break; } default: { @@ -170,6 +159,10 @@ export class _Parser { out += renderer.image(token); break; } + case 'checkbox': { + out += renderer.checkbox(token); + break; + } case 'strong': { out += renderer.strong(token); break; diff --git a/src/Renderer.ts b/src/Renderer.ts index 9082ffcf..69e7870a 100644 --- a/src/Renderer.ts +++ b/src/Renderer.ts @@ -77,38 +77,13 @@ export class _Renderer { } listitem(item: Tokens.ListItem): RendererOutput { - let itemBody = ''; - if (item.task) { - const checkbox = this.checkbox({ checked: !!item.checked }); - if (item.loose) { - if (item.tokens[0]?.type === 'paragraph') { - 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 + ' ' + escape(item.tokens[0].tokens[0].text); - item.tokens[0].tokens[0].escaped = true; - } - } else { - item.tokens.unshift({ - type: 'text', - raw: checkbox + ' ', - text: checkbox + ' ', - escaped: true, - }); - } - } else { - itemBody += checkbox + ' '; - } - } - - itemBody += this.parser.parse(item.tokens, !!item.loose); - - return `
  • ${itemBody}
  • \n` as RendererOutput; + return `
  • ${this.parser.parse(item.tokens)}
  • \n` as RendererOutput; } checkbox({ checked }: Tokens.Checkbox): RendererOutput { return '' as RendererOutput; + + 'disabled="" type="checkbox"> ' as RendererOutput; } paragraph({ tokens }: Tokens.Paragraph): RendererOutput { diff --git a/src/TextRenderer.ts b/src/TextRenderer.ts index 56be8a2e..15f724f9 100644 --- a/src/TextRenderer.ts +++ b/src/TextRenderer.ts @@ -41,4 +41,8 @@ export class _TextRenderer { br(): RendererOutput { return '' as RendererOutput; } + + checkbox({ raw }: Tokens.Checkbox): RendererOutput { + return raw as RendererOutput; + } } diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index 925074e7..34674e02 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -381,12 +381,10 @@ export class _Tokenizer { } let istask: RegExpExecArray | null = null; - let ischecked: boolean | undefined; // Check for task list items if (this.options.gfm) { istask = this.rules.other.listIsTask.exec(itemContents); if (istask) { - ischecked = istask[0] !== '[ ] '; itemContents = itemContents.replace(this.rules.other.listReplaceTask, ''); } } @@ -395,7 +393,6 @@ export class _Tokenizer { type: 'list_item', raw, task: !!istask, - checked: ischecked, loose: false, text: itemContents, tokens: [], @@ -416,13 +413,40 @@ export class _Tokenizer { list.raw = list.raw.trimEnd(); // Item child tokens handled here at end because we needed to have the final item to trim it first - for (let i = 0; i < list.items.length; i++) { + for (const item of list.items) { this.lexer.state.top = false; - list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []); + item.tokens = this.lexer.blockTokens(item.text, []); + if (item.task) { + const taskRaw = this.rules.other.listTaskCheckbox.exec(item.raw); + if (taskRaw) { + const checkboxToken: Tokens.Checkbox = { + type: 'checkbox', + raw: taskRaw[0] + ' ', + checked: taskRaw[0] !== '[ ]', + }; + item.checked = checkboxToken.checked; + if (list.loose) { + if (item.tokens[0] && ['paragraph', 'text'].includes(item.tokens[0].type) && 'tokens' in item.tokens[0] && item.tokens[0].tokens) { + item.tokens[0].raw = checkboxToken.raw + item.tokens[0].raw; + item.tokens[0].text = checkboxToken.raw + item.tokens[0].text; + item.tokens[0].tokens.unshift(checkboxToken); + } else { + item.tokens.unshift({ + type: 'paragraph', + raw: checkboxToken.raw, + text: checkboxToken.raw, + tokens: [checkboxToken], + }); + } + } else { + item.tokens.unshift(checkboxToken); + } + } + } if (!list.loose) { // Check if list should be loose - const spacers = list.items[i].tokens.filter(t => t.type === 'space'); + const spacers = item.tokens.filter(t => t.type === 'space'); const hasMultipleLineBreaks = spacers.length > 0 && spacers.some(t => this.rules.other.anyLine.test(t.raw)); list.loose = hasMultipleLineBreaks; @@ -431,8 +455,13 @@ export class _Tokenizer { // Set all items to loose if list is loose if (list.loose) { - for (let i = 0; i < list.items.length; i++) { - list.items[i].loose = true; + for (const item of list.items) { + item.loose = true; + for (const token of item.tokens) { + if (token.type === 'text') { + token.type = 'paragraph'; + } + } } } diff --git a/src/Tokens.ts b/src/Tokens.ts index 77649c81..35c05869 100644 --- a/src/Tokens.ts +++ b/src/Tokens.ts @@ -3,6 +3,7 @@ export type MarkedToken = ( Tokens.Blockquote | Tokens.Br + | Tokens.Checkbox | Tokens.Code | Tokens.Codespan | Tokens.Def @@ -42,6 +43,8 @@ export namespace Tokens { } export interface Checkbox { + type: 'checkbox'; + raw: string; checked: boolean; } diff --git a/src/rules.ts b/src/rules.ts index 84be5eff..acd62073 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -48,6 +48,7 @@ export const other = { listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g, listIsTask: /^\[[ xX]\] /, listReplaceTask: /^\[[ xX]\] +/, + listTaskCheckbox: /\[[ xX]\]/, anyLine: /\n.*\n/, hrefBrackets: /^<(.*)>$/, tableDelimiter: /[:|]/, diff --git a/test/types/marked.ts b/test/types/marked.ts index 43712b60..01502b3d 100644 --- a/test/types/marked.ts +++ b/test/types/marked.ts @@ -93,7 +93,7 @@ class ExtendedRenderer extends marked.Renderer { hr = ({ type, raw }: Tokens.Hr): string => super.hr({ type, raw }); list = ({ type, raw, ordered, start, loose, items }: Tokens.List): string => super.list({ type, raw, ordered, start, loose, items}); listitem = ({ type, raw, task, checked, loose, text, tokens }: Tokens.ListItem): string => super.listitem({ type, raw, task, checked, loose, text, tokens }); - checkbox = ({ checked }: Tokens.Checkbox): string => super.checkbox({ checked }); + checkbox = ({ type, raw, checked }: Tokens.Checkbox): string => super.checkbox({ type, raw, checked }); paragraph = ({ type, raw, pre, text, tokens }: Tokens.Paragraph): string => super.paragraph({ type, raw, pre, text, tokens }); table = ({ type, raw, align, header, rows }: Tokens.Table): string => super.table({ type, raw, align, header, rows }); tablerow = ({ text }: Tokens.TableRow): string => super.tablerow({ text }); diff --git a/test/unit/Lexer.test.js b/test/unit/Lexer.test.js index ba9b417d..9af98f47 100644 --- a/test/unit/Lexer.test.js +++ b/test/unit/Lexer.test.js @@ -462,7 +462,6 @@ a | b type: 'list_item', raw: '- > blockquote', task: false, - checked: undefined, loose: false, text: '> blockquote', tokens: [ @@ -512,7 +511,6 @@ a | b type: 'list_item', raw: '- item 1\n', task: false, - checked: undefined, loose: false, text: 'item 1', tokens: [{ @@ -526,7 +524,6 @@ a | b type: 'list_item', raw: '- item 2', task: false, - checked: undefined, loose: false, text: 'item 2', tokens: [{ @@ -564,7 +561,6 @@ a | b type: 'list_item', raw: '1. item 1\n', task: false, - checked: undefined, loose: false, text: 'item 1', tokens: [ @@ -587,7 +583,6 @@ a | b type: 'list_item', raw: '2. item 2', task: false, - checked: undefined, loose: false, text: 'item 2', tokens: [ @@ -634,7 +629,6 @@ a | b type: 'list_item', raw: '1) item 1\n', task: false, - checked: undefined, loose: false, text: 'item 1', tokens: [ @@ -657,7 +651,6 @@ a | b type: 'list_item', raw: '2) item 2', task: false, - checked: undefined, loose: false, text: 'item 2', tokens: [ @@ -706,7 +699,6 @@ paragraph type: 'list_item', raw: '- item 1\n', task: false, - checked: undefined, loose: false, text: 'item 1', tokens: [ @@ -729,7 +721,6 @@ paragraph type: 'list_item', raw: '- item 2', task: false, - checked: undefined, loose: false, text: 'item 2', tokens: [ @@ -793,7 +784,6 @@ paragraph type: 'list_item', raw: '2. item 1\n', task: false, - checked: undefined, loose: false, text: 'item 1', tokens: [ @@ -816,7 +806,6 @@ paragraph type: 'list_item', raw: '3. item 2', task: false, - checked: undefined, loose: false, text: 'item 2', tokens: [ @@ -864,12 +853,11 @@ paragraph type: 'list_item', raw: '- item 1\n\n', task: false, - checked: undefined, loose: true, text: 'item 1\n', tokens: [ { - type: 'text', + type: 'paragraph', raw: 'item 1\n', text: 'item 1', tokens: [ @@ -887,12 +875,11 @@ paragraph type: 'list_item', raw: '- item 2', task: false, - checked: undefined, loose: true, text: 'item 2', tokens: [ { - type: 'text', + type: 'paragraph', raw: 'item 2', text: 'item 2', tokens: [ @@ -937,12 +924,11 @@ paragraph type: 'list_item', raw: '- item 1\n', task: false, - checked: undefined, loose: true, text: 'item 1', tokens: [ { - type: 'text', + type: 'paragraph', raw: 'item 1', text: 'item 1', tokens: [ @@ -960,12 +946,11 @@ paragraph type: 'list_item', raw: '- item 2\n\n item 2a\n', task: false, - checked: undefined, loose: true, text: 'item 2\n\nitem 2a', tokens: [ { - type: 'text', + type: 'paragraph', raw: 'item 2', text: 'item 2', tokens: [ @@ -982,7 +967,7 @@ paragraph raw: '\n\n', }, { - type: 'text', + type: 'paragraph', raw: 'item 2a', text: 'item 2a', tokens: [ @@ -1000,12 +985,11 @@ paragraph type: 'list_item', raw: '- item 3', task: false, - checked: undefined, loose: true, text: 'item 3', tokens: [ { - type: 'text', + type: 'paragraph', raw: 'item 3', text: 'item 3', tokens: [ @@ -1047,7 +1031,6 @@ paragraph type: 'list_item', raw: '- item 1\n - item 2', task: false, - checked: undefined, loose: false, text: 'item 1\n- item 2', tokens: [ @@ -1075,7 +1058,6 @@ paragraph type: 'list_item', raw: '- item 2', task: false, - checked: undefined, loose: false, text: 'item 2', tokens: [ @@ -1130,6 +1112,11 @@ paragraph loose: false, text: 'item 1', tokens: [ + { + type: 'checkbox', + raw: '[ ] ', + checked: false, + }, { type: 'text', raw: 'item 1', @@ -1153,6 +1140,11 @@ paragraph loose: false, text: 'item 2', tokens: [ + { + type: 'checkbox', + raw: '[x] ', + checked: true, + }, { type: 'text', raw: 'item 2', @@ -1195,7 +1187,6 @@ paragraph type: 'list_item', raw: '- line 1\n line 2', task: false, - checked: undefined, loose: false, text: 'line 1\nline 2', tokens: [{ @@ -1232,7 +1223,6 @@ paragraph type: 'list_item', raw: '- a\n - b', task: false, - checked: undefined, loose: false, text: 'a\n - b', tokens: [ @@ -1271,7 +1261,6 @@ paragraph type: 'list_item', raw: '- hello\n[1]: hello', task: false, - checked: undefined, loose: false, text: 'hello\n[1]: hello', tokens: [ diff --git a/test/unit/Parser.test.js b/test/unit/Parser.test.js index 118beed5..2792744c 100644 --- a/test/unit/Parser.test.js +++ b/test/unit/Parser.test.js @@ -240,6 +240,10 @@ describe('Parser', () => { task: true, checked: false, tokens: [ + { + type: 'checkbox', + checked: false, + }, { type: 'text', text: 'item 1', @@ -251,6 +255,10 @@ describe('Parser', () => { task: true, checked: true, tokens: [ + { + type: 'checkbox', + checked: true, + }, { type: 'text', text: 'item 2', @@ -284,7 +292,7 @@ describe('Parser', () => { loose: true, tokens: [ { - type: 'text', + type: 'paragraph', text: 'item 1', tokens: [{ type: 'text', text: 'item 1' }], }, @@ -296,7 +304,7 @@ describe('Parser', () => { loose: true, tokens: [ { - type: 'text', + type: 'paragraph', text: 'item 2', tokens: [{ type: 'text', text: 'item 2' }], }, @@ -345,7 +353,7 @@ describe('Parser', () => { { type: 'text', text: 'text 1' }, { type: 'text', text: 'text 2' }, ], - html: '

    text 1\ntext 2

    ', + html: 'text 1text 2', }); }); }); diff --git a/test/unit/marked.test.js b/test/unit/marked.test.js index 7f0f130c..c4a64822 100644 --- a/test/unit/marked.test.js +++ b/test/unit/marked.test.js @@ -52,7 +52,7 @@ describe('marked unit', () => { it('space after loose checkbox', () => { const html = marked.parse('- [ ] item 1\n\n- [ ] item 2'); - assert.strictEqual(html, '
      \n
    • \nitem 1

      \n
    • \n
    • \nitem 2

      \n
    • \n
    \n'); + assert.strictEqual(html, '
      \n
    • item 1

      \n
    • \n
    • item 2

      \n
    • \n
    \n'); }); });