Skip to content

Commit 58e7ca3

Browse files
committed
Fix wrong indent on text elements when serializing
Closes GH-9.
1 parent e001bbd commit 58e7ca3

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
coverage/
22
*.md
3+
*.mdx

lib/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ export function mdxJsxToMarkdown(options) {
523523
*/
524524
// eslint-disable-next-line complexity
525525
function mdxElement(node, _, context, safeOptions) {
526+
const flow = node.type === 'mdxJsxFlowElement'
526527
const selfClosing = node.name
527528
? !node.children || node.children.length === 0
528529
: false
@@ -532,7 +533,7 @@ export function mdxJsxToMarkdown(options) {
532533
const trackerMultiLine = track(safeOptions)
533534
/** @type {Array<string>} */
534535
const serializedAttributes = []
535-
const prefix = currentIndent + '<' + (node.name || '')
536+
const prefix = (flow ? currentIndent : '') + '<' + (node.name || '')
536537
const exit = context.enter(node.type)
537538

538539
trackerOneLine.move(prefix)
@@ -590,7 +591,7 @@ export function mdxJsxToMarkdown(options) {
590591

591592
if (
592593
// Block:
593-
node.type === 'mdxJsxFlowElement' &&
594+
flow &&
594595
// Including a line ending (expressions).
595596
(/\r?\n|\r/.test(attributesOnOneLine) ||
596597
// Current position (including `<tag`).
@@ -653,7 +654,9 @@ export function mdxJsxToMarkdown(options) {
653654
}
654655

655656
if (!selfClosing) {
656-
value += tracker.move(currentIndent + '</' + (node.name || '') + '>')
657+
value += tracker.move(
658+
(flow ? currentIndent : '') + '</' + (node.name || '') + '>'
659+
)
657660
}
658661

659662
exit()

test.js

+14
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,20 @@ test('roundtrip', () => {
20772077
'children in nested elements'
20782078
)
20792079

2080+
equal(
2081+
`<video src="#">
2082+
Download the <a href="#">WEBM</a> or
2083+
<a href="#">MP4</a> video.
2084+
</video>
2085+
`,
2086+
`<video src="#">
2087+
Download the <a href="#">WEBM</a> or
2088+
<a href="#">MP4</a> video.
2089+
</video>
2090+
`,
2091+
'text children in flow elements'
2092+
)
2093+
20802094
/**
20812095
* @param {string} input
20822096
* @param {string} output

0 commit comments

Comments
 (0)