mdast-util-mdx icon indicating copy to clipboard operation
mdast-util-mdx copied to clipboard

Serializing MDX expressions inside MDX elements indents the expression

Open remcohaszing opened this issue 4 months ago • 0 comments

Initial checklist

Affected package

[email protected]

Steps to reproduce

Run the following script:

import { fromMarkdown } from 'mdast-util-from-markdown'
import { mdxFromMarkdown, mdxToMarkdown } from 'mdast-util-mdx'
import { toMarkdown } from 'mdast-util-to-markdown'
import { mdxjs } from 'micromark-extension-mdxjs'

let content = `
{
  ' This is fine'
}

<div>
  {\`a
  b
    c
      d\`
        + 'e'}
</div>
`

for (let count = 0; count < 10; count++) {
  const tree = fromMarkdown(content, {
    extensions: [mdxjs()],
    mdastExtensions: [mdxFromMarkdown()]
  })

  content = toMarkdown(tree, { extensions: [mdxToMarkdown()] })

  console.log(content)
}

Actual behavior

Every iteration, the lines with b, c, d, and e get indented by 2 spaces. This is mostly inconvenient. There’s no way to format the MDX consistently. For multiline strings however, this changes the meaning of the content. The indentation spaces become part of the string after re-parsing.

Expected behavior

The expression isn’t indented.

Runtime

Node.js v24.10.0

Package manager

npm 11.6.1

Operating system

Pop!_OS 22.04

Build and bundle tools

No response

remcohaszing avatar Oct 24 '25 15:10 remcohaszing