From 39465f9663b8a15b71f45426fba139e1e804c44c Mon Sep 17 00:00:00 2001 From: Sughosha K Joshi Date: Fri, 16 Oct 2026 21:00:05 +0530 Subject: [PATCH 1/2] fix(md): require 4 spaces or tab for indented code blocks (#1388) Update tabRe to require at least four spaces or a tab (/^( {4,}|\t)/) per CommonMark specification for indented code blocks. Previously, /^( +|\t)/ matched lines indented with only 2-3 spaces (such as list continuation paragraphs), emitting them as raw JavaScript and triggering syntax errors during execution. Fixes #1388 --- src/md.ts | 2 +- test/md.test.ts | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/md.ts b/src/md.ts index 2b6932e8ad..60f703768c 100644 --- a/src/md.ts +++ b/src/md.ts @@ -17,7 +17,7 @@ import { bufToString } from './util.ts' export function transformMarkdown(buf: Buffer | string): string { const out: string[] = [] - const tabRe = /^( +|\t)/ + const tabRe = /^( {4,}|\t)/ const fenceRe = /^(? {0,3})(?(`{3,20}|~{3,20}))(?:(?js|javascript|ts|typescript)|(?sh|shell|bash)|.*)$/ diff --git a/test/md.test.ts b/test/md.test.ts index d37b9c9f6e..76913059c7 100644 --- a/test/md.test.ts +++ b/test/md.test.ts @@ -23,7 +23,25 @@ describe('transformMarkdown()', () => { }) test('preserves tab-indented blocks after a blank line (legacy behavior)', () => { - assert.equal(transformMarkdown(' \n '), ' \n ') + assert.equal( + transformMarkdown('\n\tconsole.log("hello")'), + '// \n\tconsole.log("hello")' + ) + }) + + test('preserves 4-space indented blocks after a blank line', () => { + assert.equal( + transformMarkdown('\n console.log("hello")'), + '// \n console.log("hello")' + ) + }) + + test('does not treat indented list/paragraph text with <4 spaces as code blocks (#1388)', () => { + const input = + '- item 1\n- item 2\n\n This template assumes you have pnpm installed.' + const expected = + '// - item 1\n// - item 2\n// \n// This template assumes you have pnpm installed.' + assert.equal(transformMarkdown(input), expected) }) test('does not treat a mid-paragraph fence as a fenced block (legacy behavior)', () => { From 2b08517f2429842d3ca3355a1f5075c8649f55ff Mon Sep 17 00:00:00 2001 From: Sughosha K Joshi Date: Fri, 16 Oct 2026 21:09:53 +0530 Subject: [PATCH 2/2] test(md): account for tabs preceded by 1-3 spaces per CommonMark Update tabRe to /^( {4,}| {0,3}\t)/ to align with CommonMark tab stop rules where 1-3 spaces before a tab expand to 4-space indentation. Add corresponding unit test case. --- src/md.ts | 2 +- test/md.test.ts | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/md.ts b/src/md.ts index 60f703768c..47cc032d7c 100644 --- a/src/md.ts +++ b/src/md.ts @@ -17,7 +17,7 @@ import { bufToString } from './util.ts' export function transformMarkdown(buf: Buffer | string): string { const out: string[] = [] - const tabRe = /^( {4,}|\t)/ + const tabRe = /^( {4,}| {0,3}\t)/ const fenceRe = /^(? {0,3})(?(`{3,20}|~{3,20}))(?:(?js|javascript|ts|typescript)|(?sh|shell|bash)|.*)$/ diff --git a/test/md.test.ts b/test/md.test.ts index 76913059c7..a081662f60 100644 --- a/test/md.test.ts +++ b/test/md.test.ts @@ -36,6 +36,13 @@ describe('transformMarkdown()', () => { ) }) + test('preserves blocks indented with spaces and a tab (equivalent to 4 spaces)', () => { + assert.equal( + transformMarkdown('\n \tconsole.log("hello")'), + '// \n \tconsole.log("hello")' + ) + }) + test('does not treat indented list/paragraph text with <4 spaces as code blocks (#1388)', () => { const input = '- item 1\n- item 2\n\n This template assumes you have pnpm installed.'