Skip to content

fix(md): require 4 spaces or tab for indented code blocks (#1388) - #1528

Open
joshi-07 wants to merge 2 commits into
google:mainfrom
joshi-07:fix/md-indented-code-blocks
Open

joshi-07 wants to merge 2 commits into
google:mainfrom
joshi-07:fix/md-indented-code-blocks

Conversation

@joshi-07

Copy link
Copy Markdown

Fixes #1388

Problem

When executing Markdown files (zx file.md), any text indented by 2 or 3 spaces after a blank line (for example, continuation paragraphs in list items or nested documentation notes) was treated as raw JavaScript instead of being commented out:

- item 1
- item 2

  This template assumes you have pnpm installed.

In src/md.ts, tabRe = /^( +|\t)/ matched 2 spaces ( +), incorrectly transitioning state to 'tab' and emitting the text verbatim into the generated script, resulting in syntax errors such as:
SyntaxError: Unexpected identifier 'assumes'

Solution

Per the CommonMark specification, indented code blocks require an indentation of at least four spaces or a tab.

  • Updated tabRe to /^( {4,}|\t)/ so lines with 2 or 3 spaces remain regular Markdown prose and are properly commented out with // .

  • Retained support for legacy 4-space and tab-indented code blocks.

  • Added test coverage in test/md.test.ts for both 4-space indented blocks and list continuation text indented with <4 spaces.

  • Setup Set the latest Node.js LTS version.

  • Tests: Confirmed transformMarkdown() unit tests succeed. Added tests to cover changes.

  • Sign: Follows conventional commits spec.

  • CoC: My changes follow the project’s coding guidelines and Code of Conduct.

  • Review: This PR represents original work and is not solely generated by AI tools.

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 google#1388

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the markdown transformation logic in src/md.ts to ensure that only lines with at least 4 spaces or a tab are treated as indented code blocks, preventing text with fewer spaces from being incorrectly parsed, and adds corresponding test cases in test/md.test.ts. The feedback recommends refining the regular expression to correctly handle cases where 1 to 3 spaces are followed by a tab (which is equivalent to 4 spaces under the CommonMark specification) and adding a test case to verify this behavior.

Comment thread src/md.ts Outdated
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)/

Comment thread test/md.test.ts
Comment on lines +32 to +37
test('preserves 4-space indented blocks after a blank line', () => {
assert.equal(
transformMarkdown('\n console.log("hello")'),
'// \n console.log("hello")'
)
})

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")'
      )
    })

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: cannot parse indented content

1 participant