From 8e5d29000db2fc18e73bb8baa76dd4da8a72b2ce Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 13 Sep 2024 12:27:55 +0200 Subject: [PATCH] Fix to add some indent in expressions like before Related-to: micromark/micromark-extension-mdx-expression@103af9a. --- lib/index.js | 23 +++++++++++++++++++---- test.js | 10 +++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index 0f0c138..53f09cb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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' @@ -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`: + // + return (index === 0 || blank ? '' : ' ') + line + }) + return '{' + result + '}' } diff --git a/test.js b/test.js index 8ac0f0f..9de830d 100644 --- a/test.js +++ b/test.js @@ -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}', { @@ -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} @@ -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' ) }) @@ -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( @@ -526,7 +526,7 @@ test('roundtrip', async function (t) { }), {extensions: [mdxExpressionToMarkdown()]} ), - '{`\n a\n `}\n' + '{`\n a\n `}\n' ) } )