Skip to content

Commit 8e5d290

Browse files
committed
Fix to add some indent in expressions like before
Related-to: micromark/micromark-extension-mdx-expression@103af9a.
1 parent 48cbf69 commit 8e5d290

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/index.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* @import {CompileContext, Extension as FromMarkdownExtension, Handle as FromMarkdownHandle} from 'mdast-util-from-markdown'
3-
* @import {Handle as ToMarkdownHandle, Options as ToMarkdownExtension} from 'mdast-util-to-markdown'
4-
* @import {MdxFlowExpression, MdxTextExpression} from '../index.js'
3+
* @import {MdxFlowExpression, MdxTextExpression} from 'mdast-util-mdx-expression'
4+
* @import {Handle as ToMarkdownHandle, Options as ToMarkdownExtension, State} from 'mdast-util-to-markdown'
5+
* @import {Parents} from 'mdast'
56
*/
67

78
import {ok as assert} from 'devlop'
@@ -98,8 +99,22 @@ function exitMdxExpressionData(token) {
9899
/**
99100
* @type {ToMarkdownHandle}
100101
* @param {MdxFlowExpression | MdxTextExpression} node
102+
* Node.
103+
* @param {Parents | undefined} parent
104+
* Parent, if any.
105+
* @param {State} state
106+
* Info passed around about the current state.
107+
* @returns {string}
108+
* Serialized markdown.
101109
*/
102-
function handleMdxExpression(node) {
110+
function handleMdxExpression(node, parent, state) {
103111
const value = node.value || ''
104-
return '{' + value + '}'
112+
const result = state.indentLines(value, function (line, index, blank) {
113+
// Tab-size to eat has to be the same as what we serialize as.
114+
// While in some places in markdown that’s 4, in JS it’s more common as 2.
115+
// Which is what’s also in `mdast-util-mdx-jsx`:
116+
// <https://github.com/syntax-tree/mdast-util-mdx-jsx/blob/40b951b/lib/index.js#L52>
117+
return (index === 0 || blank ? '' : ' ') + line
118+
})
119+
return '{' + result + '}'
105120
}

test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ test('mdxExpressionFromMarkdown()', async function (t) {
5050
)
5151

5252
await t.test(
53-
'should support a flow expression (agnostic)',
53+
'should support an indented flow expression (agnostic)',
5454
async function () {
5555
assert.deepEqual(
5656
fromMarkdown('{\n 1 + 1\n}', {
@@ -62,7 +62,7 @@ test('mdxExpressionFromMarkdown()', async function (t) {
6262
children: [
6363
{
6464
type: 'mdxFlowExpression',
65-
value: '\n 1 + 1\n',
65+
value: '\n1 + 1\n',
6666
position: {
6767
start: {line: 1, column: 1, offset: 0},
6868
end: {line: 3, column: 2, offset: 11}
@@ -467,7 +467,7 @@ test('mdxExpressionToMarkdown()', async function (t) {
467467
},
468468
{extensions: [mdxExpressionToMarkdown()]}
469469
),
470-
'{a + b}\n\n{\nc +\n1\n}\n\n{}\n\nd\n'
470+
'{a + b}\n\n{\n c +\n 1\n}\n\n{}\n\nd\n'
471471
)
472472
})
473473

@@ -516,7 +516,7 @@ test('mdxExpressionToMarkdown()', async function (t) {
516516

517517
test('roundtrip', async function (t) {
518518
await t.test(
519-
'should *not* strip superfluous whitespace depending on the opening prefix, when roundtripping expressions (flow)',
519+
'should strip superfluous whitespace depending on the opening prefix, when roundtripping expressions (flow)',
520520
async function () {
521521
assert.deepEqual(
522522
toMarkdown(
@@ -526,7 +526,7 @@ test('roundtrip', async function (t) {
526526
}),
527527
{extensions: [mdxExpressionToMarkdown()]}
528528
),
529-
'{`\n a\n `}\n'
529+
'{`\n a\n `}\n'
530530
)
531531
}
532532
)

0 commit comments

Comments
 (0)