Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to the CommonMark specification, a tab character aligns to the next 4-space tab stop. This means that 1 to 3 spaces followed by a tab (e.g., \t) also constitute an indentation of 4 spaces and should start an indented code block.

Updating the regex to /^( {4,}| {0,3}\t)/ ensures that these valid indented code blocks are correctly recognized and preserved, while still preventing 2 or 3 spaces of regular text (without a tab) from being incorrectly treated as code blocks.

Suggested change
const tabRe = /^( {4,}|\t)/
const tabRe = /^( {4,}| {0,3}\\t)/

const fenceRe =
/^(?<indent> {0,3})(?<fence>(`{3,20}|~{3,20}))(?:(?<js>js|javascript|ts|typescript)|(?<bash>sh|shell|bash)|.*)$/

Expand Down
20 changes: 19 additions & 1 deletion test/md.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")'
)
})
Comment on lines +32 to +37

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add a test case to verify that blocks indented with 1-3 spaces followed by a tab (which are equivalent to 4 spaces of indentation under CommonMark) are also correctly preserved as indented code blocks.

    test('preserves 4-space indented blocks after a blank line', () => {
      assert.equal(
        transformMarkdown('\\n    console.log("hello")'),
        '// \\n    console.log("hello")'
      )
    })

    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.'
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)', () => {
Expand Down
Loading