From b70895f47b818bfe49daf32c6f00fb5dcfaed299 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Mon, 6 Apr 2026 23:32:53 -0600 Subject: [PATCH] fix: trim blank lines from block tokens (#3939) BREAKING CHANGE: trim trailing blank lines from block tokens --- src/Tokenizer.ts | 17 ++++++++++------- src/helpers.ts | 14 ++++++++++++++ test/unit/Lexer.test.js | 41 ++++++++++++++++++++++++++++++++++++++++ test/unit/marked.test.js | 1 + 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index ab1f0029..3f3a9855 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -4,6 +4,7 @@ import { splitCells, findClosingBracket, expandTabs, + trimTrailingBlankLines, } from './helpers.ts'; import type { Rules } from './rules.ts'; import type { _Lexer } from './Lexer.ts'; @@ -81,14 +82,15 @@ export class _Tokenizer { code(src: string): Tokens.Code | undefined { const cap = this.rules.block.code.exec(src); if (cap) { - const text = cap[0].replace(this.rules.other.codeRemoveIndent, ''); + const raw = this.options.pedantic + ? cap[0] + : trimTrailingBlankLines(cap[0]); + const text = raw.replace(this.rules.other.codeRemoveIndent, ''); return { type: 'code', - raw: cap[0], + raw, codeBlockStyle: 'indented', - text: !this.options.pedantic - ? rtrim(text, '\n') - : text, + text, }; } } @@ -481,12 +483,13 @@ export class _Tokenizer { html(src: string): Tokens.HTML | undefined { const cap = this.rules.block.html.exec(src); if (cap) { + const raw = trimTrailingBlankLines(cap[0]); const token: Tokens.HTML = { type: 'html', block: true, - raw: cap[0], + raw, pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style', - text: cap[0], + text: raw, }; return token; } diff --git a/src/helpers.ts b/src/helpers.ts index 3df842cb..f835b488 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -109,6 +109,20 @@ export function rtrim(str: string, c: string, invert?: boolean) { return str.slice(0, l - suffLen); } +export function trimTrailingBlankLines(str: string) { + const lines = str.split('\n'); + let end = lines.length - 1; + while (end >= 0 && !lines[end].trim()) { + end--; + } + if (lines.length - end <= 2) { + // we want to keep single trailing blank lines + return str; + } + + return lines.slice(0, end + 1).join('\n'); +} + export function findClosingBracket(str: string, b: string) { if (str.indexOf(b[1]) === -1) { return -1; diff --git a/test/unit/Lexer.test.js b/test/unit/Lexer.test.js index ea025839..fe5c0cd8 100644 --- a/test/unit/Lexer.test.js +++ b/test/unit/Lexer.test.js @@ -59,6 +59,22 @@ describe('Lexer', () => { }); }); + it('indented code with trailing blank lines', () => { + expectTokens({ + md: ' code\n \n\t\t\n \t\n\nnext', + tokens: [ + { type: 'code', raw: ' code', text: 'code', codeBlockStyle: 'indented' }, + { type: 'space', raw: '\n \n\t\t\n \t\n\n' }, + { + type: 'paragraph', + raw: 'next', + text: 'next', + tokens: [{ type: 'text', raw: 'next', text: 'next', escaped: false }], + }, + ], + }); + }); + it('fenced code', () => { expectTokens({ md: '```\ncode\n```', @@ -1426,6 +1442,31 @@ paragraph ], }); }); + + it('html with trailing blank lines', () => { + expectTokens({ + md: '
\n html\n
\n \n\t\n\nnext', + tokens: [ + { + type: 'html', + raw: '
\n html\n
', + pre: false, + block: true, + text: '
\n html\n
', + }, + { + type: 'space', + raw: '\n \n\t\n\n', + }, + { + type: 'paragraph', + raw: 'next', + text: 'next', + tokens: [{ type: 'text', raw: 'next', text: 'next', escaped: false }], + }, + ], + }); + }); }); describe('def', () => { diff --git a/test/unit/marked.test.js b/test/unit/marked.test.js index ad928b7e..d7348a86 100644 --- a/test/unit/marked.test.js +++ b/test/unit/marked.test.js @@ -1027,6 +1027,7 @@ br ['text', 'list'], ['space', ''], ['html', '
html
'], + ['space', ''], ['paragraph', '[link](https://example.com)'], ['link', '[link](https://example.com)'], ['text', 'link'],