mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
perf: avoid scanning table bodies for header-only decisions (#4213)
* perf: reject mismatched table headers before scanning body rows Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> * perf: share header-only GFM table lookahead Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> * test: cover table header rejection before body matching Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --------- Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
This commit is contained in:
co-authored by
generaltony007
parent
73d5116de4
commit
a0925cca02
+15
-2
@@ -733,6 +733,19 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
|
||||
}
|
||||
|
||||
table(src: string): Tokens.Table | undefined {
|
||||
let headers: string[] | undefined;
|
||||
let aligns: string[] | undefined;
|
||||
if (this.options.gfm && !this.options.pedantic) {
|
||||
const header = this.rules.other.tableHeader.exec(src);
|
||||
if (!header || !this.rules.other.tableDelimiter.test(header[2])) {
|
||||
return;
|
||||
}
|
||||
headers = splitCells(header[1]);
|
||||
aligns = header[2].replace(this.rules.other.tableAlignChars, '').split('|');
|
||||
if (headers.length !== aligns.length) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
const cap = this.rules.block.table.exec(src);
|
||||
if (!cap) {
|
||||
return;
|
||||
@@ -743,8 +756,8 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
|
||||
return;
|
||||
}
|
||||
|
||||
const headers = splitCells(cap[1]);
|
||||
const aligns = cap[2].replace(this.rules.other.tableAlignChars, '').split('|');
|
||||
headers ??= splitCells(cap[1]);
|
||||
aligns ??= cap[2].replace(this.rules.other.tableAlignChars, '').split('|');
|
||||
const rows = cap[3]?.trim() ? cap[3].replace(this.rules.other.tableRowBlankLine, '').split('\n') : [];
|
||||
|
||||
const item: Tokens.Table = {
|
||||
|
||||
+6
-4
@@ -40,6 +40,9 @@ try {
|
||||
}
|
||||
})();
|
||||
|
||||
const gfmTableHeader = '^ *([^\\n ].*)\\n' // Header
|
||||
+ ' {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)'; // Align
|
||||
|
||||
export const other = {
|
||||
codeRemoveIndent: /^(?: {0,3}\t| {1,4})/gm,
|
||||
listInterruptsParagraph: /^ {0,3}(?:[*+-]|1[.)])[ \t]+[^ \t\n]/,
|
||||
@@ -71,6 +74,7 @@ export const other = {
|
||||
listTaskCheckbox: /\[[ xX]\]/,
|
||||
anyLine: /\n.*\n/,
|
||||
hrefBrackets: /^<(.*)>$/,
|
||||
tableHeader: new RegExp(gfmTableHeader + '(?:\\n|$)'),
|
||||
tableDelimiter: /[:|]/,
|
||||
tableAlignChars: /^\||\| *$/g,
|
||||
tableRowBlankLine: /\n[ \t]*$/,
|
||||
@@ -242,9 +246,7 @@ type BlockKeys = keyof typeof blockNormal;
|
||||
* GFM Block Grammar
|
||||
*/
|
||||
|
||||
const gfmTable = edit(
|
||||
'^ *([^\\n ].*)\\n' // Header
|
||||
+ ' {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)' // Align
|
||||
const gfmTable = edit(gfmTableHeader
|
||||
+ '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)') // Cells
|
||||
.replace('hr', hr)
|
||||
.replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
|
||||
@@ -263,7 +265,7 @@ const blockGfm: Record<BlockKeys, RegExp> = {
|
||||
.replace('hr', hr)
|
||||
.replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
|
||||
.replace('|lheading', '') // setext headings don't interrupt commonmark paragraphs
|
||||
.replace('table', gfmTable) // interrupt paragraphs with table
|
||||
.replace('table', other.tableHeader) // only the header and delimiter are needed to interrupt
|
||||
.replace('blockquote', ' {0,3}>')
|
||||
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*(?:\\n|$))|~~~)[^\\n]*(?:\\n|$)')
|
||||
.replace('list', ' {0,3}(?:[*+-]|1[.)])[ \\t]+[^ \\t\\n]') // only non-empty lists starting from 1 can interrupt
|
||||
|
||||
@@ -329,6 +329,31 @@ lheading 2
|
||||
});
|
||||
});
|
||||
|
||||
describe('rejected table headers', () => {
|
||||
it('does not scan table body rows after a header-column mismatch', () => {
|
||||
const lexer = new Lexer({ gfm: true });
|
||||
let bodyScans = 0;
|
||||
const originalTable = lexer.tokenizer.rules.block.table;
|
||||
lexer.tokenizer.rules = {
|
||||
...lexer.tokenizer.rules,
|
||||
block: {
|
||||
...lexer.tokenizer.rules.block,
|
||||
table: { exec(src) { bodyScans++; return originalTable.exec(src); } },
|
||||
},
|
||||
};
|
||||
assert.strictEqual(lexer.tokenizer.table('a|b\n-|-|-\n1|2|3\n4|5|6'), undefined);
|
||||
assert.strictEqual(bodyScans, 0);
|
||||
});
|
||||
|
||||
it('still scans and tokenizes the body of a valid table', () => {
|
||||
const lexer = new Lexer({ gfm: true });
|
||||
const table = lexer.tokenizer.table('a|b\n-|-\n1|2\n3|4');
|
||||
assert.strictEqual(table.type, 'table');
|
||||
assert.strictEqual(table.rows.length, 2);
|
||||
assert.deepEqual(table.rows.map(row => row.map(cell => cell.text)), [['1', '2'], ['3', '4']]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('table', () => {
|
||||
it('pipe table', () => {
|
||||
expectTokens({
|
||||
|
||||
Reference in New Issue
Block a user