Skip to content

fix: handle indented code fences in markdown transformation#1418

Closed
fresh3nough wants to merge 1 commit intogoogle:mainfrom
fresh3nough:fix/md-indented-code-fence-parsing
Closed

fix: handle indented code fences in markdown transformation#1418
fresh3nough wants to merge 1 commit intogoogle:mainfrom
fresh3nough:fix/md-indented-code-fence-parsing

Conversation

@fresh3nough
Copy link
Contributor

Fixes #1389

Problem

Running zx --verbose on a markdown file containing code fences indented inside list items (e.g. with 2 spaces) causes a TypeError: $(...) is not a function.

The transformMarkdown() parser checks for tab-indentation (tabRe) before checking for code fences (codeBlockRe). When a fenced code block is indented with 2+ spaces (common inside list items), the tab check matches first and the line is emitted as raw code rather than being recognized as a bash/js fence. The raw backticks in the output produce invalid JavaScript.

How to reproduce

Create a markdown file with a code fence inside a list item:

- item 1

  ```bash
  echo "hello"

Run: `zx --verbose file.md`

Result: `TypeError: $(...) is not a function`

## Fix

Per the CommonMark spec, code fences may be indented by 0-3 spaces. This change:

1. Reorders the `root` state to check for code fences (after stripping 0-3 leading spaces) **before** checking for tab indentation
2. Updates closing fence detection in `js`, `bash`, and `other` states to also strip 0-3 leading spaces before comparison

Tab-indented lines (4+ spaces or a tab character) continue to be handled as before.

## Test

Added a test case reproducing the exact markdown from issue #1389, verifying all three bash code blocks (including the indented one) are correctly converted to `await $\`` invocations.

node --experimental-transform-types --test test/md.test.ts

md

transformMarkdown()

transformMarkdown() handles indented code fences in list items (#1389)

tests 2, pass 2, fail 0


- [x] **Build**: I've run `npm build` before committing and verified the bundle updates correctly.
- [x] **Tests**: I've run `test` and confirmed all tests succeed. Added tests to cover my changes if needed.
- [x] **Sign** Commits follow conventional commits spec.
- [x] **CoC**: My changes follow the project's coding guidelines and Code of Conduct.

@antongolub
Copy link
Collaborator

antongolub commented Feb 23, 2026

There are a lot of pitfalls here. I'll take care of it myself.

CC @Illuminati9

@antongolub
Copy link
Collaborator

Superseded by #1419

@antongolub antongolub closed this Feb 23, 2026
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]: TypeError: $(...) is not a function

2 participants