fix: only create tokens inside tokenizers (#3755)

BREAKING CHANGE:
- Change how consecutive text tokens work in lists
- Simplify listItem renderer
- Checkbox token is added in list tokenizer
- Checkbox token add type and raw property
- Change loose list text tokens to paragraph type in the list tokenizer
This commit is contained in:
Tony Brix
2025-11-06 23:33:11 -07:00
committed by GitHub
parent 244941a192
commit 7b192315b2
11 changed files with 98 additions and 96 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ import markedEslintConfig from '@markedjs/eslint-config';
export default [
{
ignores: ['**/lib', '**/public'],
ignores: ['**/lib', '**/public', 'test.js', 'vuln.js'],
},
...markedEslintConfig,
];
+21 -28
View File
@@ -39,7 +39,7 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
/**
* 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<ParserOutput = string, RendererOutput = string> {
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<ParserOutput = string, RendererOutput = string> {
out += renderer.image(token);
break;
}
case 'checkbox': {
out += renderer.checkbox(token);
break;
}
case 'strong': {
out += renderer.strong(token);
break;
+2 -27
View File
@@ -77,38 +77,13 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
}
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 `<li>${itemBody}</li>\n` as RendererOutput;
return `<li>${this.parser.parse(item.tokens)}</li>\n` as RendererOutput;
}
checkbox({ checked }: Tokens.Checkbox): RendererOutput {
return '<input '
+ (checked ? 'checked="" ' : '')
+ 'disabled="" type="checkbox">' as RendererOutput;
+ 'disabled="" type="checkbox"> ' as RendererOutput;
}
paragraph({ tokens }: Tokens.Paragraph): RendererOutput {
+4
View File
@@ -41,4 +41,8 @@ export class _TextRenderer<RendererOutput = string> {
br(): RendererOutput {
return '' as RendererOutput;
}
checkbox({ raw }: Tokens.Checkbox): RendererOutput {
return raw as RendererOutput;
}
}
+37 -8
View File
@@ -381,12 +381,10 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
}
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<ParserOutput = string, RendererOutput = string> {
type: 'list_item',
raw,
task: !!istask,
checked: ischecked,
loose: false,
text: itemContents,
tokens: [],
@@ -416,13 +413,40 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
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<ParserOutput = string, RendererOutput = string> {
// 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';
}
}
}
}
+3
View File
@@ -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;
}
+1
View File
@@ -48,6 +48,7 @@ export const other = {
listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g,
listIsTask: /^\[[ xX]\] /,
listReplaceTask: /^\[[ xX]\] +/,
listTaskCheckbox: /\[[ xX]\]/,
anyLine: /\n.*\n/,
hrefBrackets: /^<(.*)>$/,
tableDelimiter: /[:|]/,
+1 -1
View File
@@ -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 });
+16 -27
View File
@@ -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: [
+11 -3
View File
@@ -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: '<p>text 1\ntext 2</p>',
html: 'text 1text 2',
});
});
});
+1 -1
View File
@@ -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, '<ul>\n<li><p><input disabled="" type="checkbox"> \nitem 1</p>\n</li>\n<li><p><input disabled="" type="checkbox"> \nitem 2</p>\n</li>\n</ul>\n');
assert.strictEqual(html, '<ul>\n<li><p><input disabled="" type="checkbox"> item 1</p>\n</li>\n<li><p><input disabled="" type="checkbox"> item 2</p>\n</li>\n</ul>\n');
});
});