Repository navigation
Conversation
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
There was a problem hiding this comment.
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.
| export function transformMarkdown(buf: Buffer | string): string { | ||
| const out: string[] = [] | ||
| const tabRe = /^( +|\t)/ | ||
| const tabRe = /^( {4,}|\t)/ |
There was a problem hiding this comment.
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.
| const tabRe = /^( {4,}|\t)/ | |
| const tabRe = /^( {4,}| {0,3}\\t)/ |
| test('preserves 4-space indented blocks after a blank line', () => { | ||
| assert.equal( | ||
| transformMarkdown('\n console.log("hello")'), | ||
| '// \n console.log("hello")' | ||
| ) | ||
| }) |
There was a problem hiding this comment.
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.
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:In
src/md.ts,tabRe = /^( +|\t)/matched 2 spaces (+), incorrectly transitioningstateto'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
tabReto/^( {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.tsfor 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.