Skip to content

Commit

Permalink
Fix to add some indent in expressions like before
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 13, 2024
1 parent 48cbf69 commit 8e5d290
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
23 changes: 19 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* @import {CompileContext, Extension as FromMarkdownExtension, Handle as FromMarkdownHandle} from 'mdast-util-from-markdown'
* @import {Handle as ToMarkdownHandle, Options as ToMarkdownExtension} from 'mdast-util-to-markdown'
* @import {MdxFlowExpression, MdxTextExpression} from '../index.js'
* @import {MdxFlowExpression, MdxTextExpression} from 'mdast-util-mdx-expression'
* @import {Handle as ToMarkdownHandle, Options as ToMarkdownExtension, State} from 'mdast-util-to-markdown'
* @import {Parents} from 'mdast'
*/

import {ok as assert} from 'devlop'
Expand Down Expand Up @@ -98,8 +99,22 @@ function exitMdxExpressionData(token) {
/**
* @type {ToMarkdownHandle}
* @param {MdxFlowExpression | MdxTextExpression} node
* Node.
* @param {Parents | undefined} parent
* Parent, if any.
* @param {State} state
* Info passed around about the current state.
* @returns {string}
* Serialized markdown.
*/
function handleMdxExpression(node) {
function handleMdxExpression(node, parent, state) {
const value = node.value || ''
return '{' + value + '}'
const result = state.indentLines(value, function (line, index, blank) {
// Tab-size to eat has to be the same as what we serialize as.
// While in some places in markdown that’s 4, in JS it’s more common as 2.
// Which is what’s also in `mdast-util-mdx-jsx`:
// <https://github.com/syntax-tree/mdast-util-mdx-jsx/blob/40b951b/lib/index.js#L52>
return (index === 0 || blank ? '' : ' ') + line
})
return '{' + result + '}'
}
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test('mdxExpressionFromMarkdown()', async function (t) {
)

await t.test(
'should support a flow expression (agnostic)',
'should support an indented flow expression (agnostic)',
async function () {
assert.deepEqual(
fromMarkdown('{\n 1 + 1\n}', {
Expand All @@ -62,7 +62,7 @@ test('mdxExpressionFromMarkdown()', async function (t) {
children: [
{
type: 'mdxFlowExpression',
value: '\n 1 + 1\n',
value: '\n1 + 1\n',
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 3, column: 2, offset: 11}
Expand Down Expand Up @@ -467,7 +467,7 @@ test('mdxExpressionToMarkdown()', async function (t) {
},
{extensions: [mdxExpressionToMarkdown()]}
),
'{a + b}\n\n{\nc +\n1\n}\n\n{}\n\nd\n'
'{a + b}\n\n{\n c +\n 1\n}\n\n{}\n\nd\n'
)
})

Expand Down Expand Up @@ -516,7 +516,7 @@ test('mdxExpressionToMarkdown()', async function (t) {

test('roundtrip', async function (t) {
await t.test(
'should *not* strip superfluous whitespace depending on the opening prefix, when roundtripping expressions (flow)',
'should strip superfluous whitespace depending on the opening prefix, when roundtripping expressions (flow)',
async function () {
assert.deepEqual(
toMarkdown(
Expand All @@ -526,7 +526,7 @@ test('roundtrip', async function (t) {
}),
{extensions: [mdxExpressionToMarkdown()]}
),
'{`\n a\n `}\n'
'{`\n a\n `}\n'
)
}
)
Expand Down

0 comments on commit 8e5d290

Please sign in to comment.