Skip to content

Commit 103af9a

Browse files
committed
Fix to eat some indent in expressions like before
Closes mdx-js/mdx#2533.
1 parent d0532dc commit 103af9a

File tree

3 files changed

+288
-44
lines changed

3 files changed

+288
-44
lines changed

packages/micromark-factory-mdx-expression/dev/index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@
2323
*/
2424

2525
import {ok as assert} from 'devlop'
26-
import {markdownLineEnding} from 'micromark-util-character'
26+
import {factorySpace} from 'micromark-factory-space'
27+
import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
2728
import {eventsToAcorn} from 'micromark-util-events-to-acorn'
2829
import {codes, types} from 'micromark-util-symbol'
2930
import {positionFromEstree} from 'unist-util-position-from-estree'
3031
import {VFileMessage} from 'vfile-message'
3132

33+
// Tab-size to eat has to be the same as what we serialize as.
34+
// While in some places in markdown that’s 4, in JS it’s more common as 2.
35+
// Which is what’s also in `mdast-util-mdx-jsx`:
36+
// <https://github.com/syntax-tree/mdast-util-mdx-jsx/blob/40b951b/lib/index.js#L52>
37+
const indentSize = 2
38+
3239
const trouble =
3340
'https://github.com/micromark/micromark-extension-mdx-expression/tree/main/packages/micromark-extension-mdx-expression'
3441

@@ -247,6 +254,8 @@ export function factoryMdxExpression(
247254
throw error
248255
}
249256

257+
// Note: `markdown-rs` uses `4`, but we use `2`.
258+
//
250259
// Idea: investigate if we’d need to use more complex stripping.
251260
// Take this example:
252261
//
@@ -256,12 +265,18 @@ export function factoryMdxExpression(
256265
// > `} /> eee
257266
// ```
258267
//
259-
// The block quote takes one space from each line, the paragraph doesn’t.
260-
// The intent above is *perhaps* for the split to be as `>␠␠|␠␠␠␠|d`,
261-
// Currently, we *don’t* do anything at all, it’s `>␠|␠␠␠␠␠|d` instead.
262-
//
263-
// Note: we used to have some handling here, and `markdown-rs` still does,
264-
// which should be removed.
268+
// Currently, the “paragraph” starts at `> | aaa`, so for the next line
269+
// here we split it into `>␠|␠␠|␠␠␠d` (prefix, this indent here,
270+
// expression data).
271+
if (markdownSpace(code)) {
272+
return factorySpace(
273+
effects,
274+
before,
275+
types.linePrefix,
276+
indentSize + 1
277+
)(code)
278+
}
279+
265280
return before(code)
266281
}
267282
}

packages/micromark-factory-mdx-expression/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"dependencies": {
4040
"@types/estree": "^1.0.0",
4141
"devlop": "^1.0.0",
42+
"micromark-factory-space": "^2.0.0",
4243
"micromark-util-character": "^2.0.0",
4344
"micromark-util-events-to-acorn": "^2.0.0",
4445
"micromark-util-symbol": "^2.0.0",

0 commit comments

Comments
 (0)