From bd324a7e74d7ab83dbaf0bafcabaa96953986431 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Tue, 19 Mar 2024 13:14:58 -0400 Subject: [PATCH] Fix crash for DL, disallow block tokens as DT, add test --- shared/naturalcrit/markdown.js | 6 ++++-- tests/markdown/definition-lists.test.js | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index e38c31c3b..939c2cc81 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -337,12 +337,14 @@ const definitionListsMultiline = { const definitions = []; while (match = regex.exec(src)) { if(match[1]) { + if(this.lexer.blockTokens(match[1].trim())[0].type !== 'paragraph') // DT must not be another block-level token besides

+ break; definitions.push({ dt : this.lexer.inlineTokens(match[1].trim()), dds : [] }); } - if(match[2]) { + if(match[2] && definitions.length) { definitions[definitions.length - 1].dds.push( this.lexer.inlineTokens(match[2].trim().replace(/\s/g, ' ')) ); @@ -615,7 +617,7 @@ function MarkedVariables() { //^=====--------------------< Variable Handling >-------------------=====^// Marked.use(MarkedVariables()); -Marked.use({ extensions: [mustacheSpans, mustacheDivs, mustacheInjectInline, definitionListsInline, definitionListsMultiline, superSubScripts] }); +Marked.use({ extensions: [mustacheSpans, mustacheDivs, mustacheInjectInline, definitionListsMultiline, definitionListsInline, superSubScripts] }); Marked.use(mustacheInjectBlock); Marked.use({ renderer: renderer, tokenizer: tokenizer, mangle: false }); Marked.use(MarkedExtendedTables(), MarkedGFMHeadingId(), MarkedSmartypantsLite()); diff --git a/tests/markdown/definition-lists.test.js b/tests/markdown/definition-lists.test.js index ee7911729..87ff6f617 100644 --- a/tests/markdown/definition-lists.test.js +++ b/tests/markdown/definition-lists.test.js @@ -76,4 +76,10 @@ describe('Multiline Definition Lists', ()=>{ const rendered = Markdown.render(source).trim(); expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('

Term 1
\n
Definition 1 and more and more
\n
Term 2
\n
Definition 1
\n
Definition 2

Paragraph

'); }); + + test('Block Token cannot be the Term of a multi-line definition', function() { + const source = '## Header\n::Definition 1 of a single-line DL\n::Definition 1 of another single-line DL'; + const rendered = Markdown.render(source).trim(); + expect(rendered, `Input:\n${source}`, { showPrefix: false }).toBe('\n
Definition 1 of a single-line DL
\n
Definition 1 of another single-line DL
\n
'); + }); });