Skip to content

Commit d62407c

Browse files
committed
Add tests for improved indent handling
1 parent 8094d91 commit d62407c

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"c8": "^7.0.0",
5454
"mdast-util-from-markdown": "^1.1.0",
5555
"micromark-extension-mdx-jsx": "^1.0.0",
56+
"micromark-extension-mdx-md": "^1.0.0",
5657
"prettier": "^2.0.0",
5758
"remark-cli": "^10.0.0",
5859
"remark-preset-wooorm": "^9.0.0",

test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {fromMarkdown} from 'mdast-util-from-markdown'
44
import {toMarkdown} from 'mdast-util-to-markdown'
55
import {removePosition} from 'unist-util-remove-position'
66
import {mdxJsx} from 'micromark-extension-mdx-jsx'
7+
import {mdxMd} from 'micromark-extension-mdx-md'
78
import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from './index.js'
89

910
test('markdown -> mdast', (t) => {
@@ -1989,3 +1990,82 @@ test('mdast -> markdown', (t) => {
19891990

19901991
t.end()
19911992
})
1993+
1994+
test('roundtrip', (t) => {
1995+
equal('<a x="a\nb\nc" />', '<a\n x="a\n b\n c"\n/>\n', 'attribute')
1996+
1997+
equal(
1998+
'<a>\n<b x="a\nb\nc" />\n</a>',
1999+
'<a>\n <b\n x="a\n b\n c"\n />\n</a>\n',
2000+
'attribute in nested element'
2001+
)
2002+
2003+
equal(
2004+
'<a>\n <b>\n <c x="a\nb\nc" />\n </b>\n</a>',
2005+
'<a>\n <b>\n <c\n x="a\n b\n c"\n />\n </b>\n</a>\n',
2006+
'attribute in nested elements'
2007+
)
2008+
2009+
equal(
2010+
'<a x={`a\nb\nc`} />',
2011+
'<a\n x={`a\n b\n c`}\n/>\n',
2012+
'attribute expression'
2013+
)
2014+
2015+
equal(
2016+
'<a>\n<b x={`a\nb\nc`} />\n</a>',
2017+
'<a>\n <b\n x={`a\n b\n c`}\n />\n</a>\n',
2018+
'attribute expression in nested element'
2019+
)
2020+
2021+
equal(
2022+
'<a>\n <b>\n <c x={`a\nb\nc`} />\n </b>\n</a>',
2023+
'<a>\n <b>\n <c\n x={`a\n b\n c`}\n />\n </b>\n</a>\n',
2024+
'attribute expression in nested elements'
2025+
)
2026+
2027+
equal('<a {\n...a\n} />', '<a\n {\n ...a\n }\n/>\n', 'expression')
2028+
2029+
equal(
2030+
'<a>\n<b {\n...a\n} />\n</a>',
2031+
'<a>\n <b\n {\n ...a\n }\n />\n</a>\n',
2032+
'expression in nested element'
2033+
)
2034+
2035+
equal(
2036+
'<a>\n <b>\n <c {\n...a\n} />\n </b>\n</a>',
2037+
'<a>\n <b>\n <c\n {\n ...a\n }\n />\n </b>\n</a>\n',
2038+
'expression in nested elements'
2039+
)
2040+
2041+
/**
2042+
* @param {string} input
2043+
* @param {string} output
2044+
* @param {string} message
2045+
*/
2046+
function equal(input, output, message) {
2047+
const intermediate1 = process(input)
2048+
t.equal(intermediate1, output, message + ' (#1)')
2049+
const intermediate2 = process(intermediate1)
2050+
t.equal(intermediate2, output, message + ' (#2)')
2051+
const intermediate3 = process(intermediate2)
2052+
t.equal(intermediate3, output, message + ' (#3)')
2053+
const intermediate4 = process(intermediate3)
2054+
t.equal(intermediate4, output, message + ' (#4)')
2055+
}
2056+
2057+
/**
2058+
* @param {string} input
2059+
*/
2060+
function process(input) {
2061+
return toMarkdown(
2062+
fromMarkdown(input, {
2063+
extensions: [mdxMd, mdxJsx()],
2064+
mdastExtensions: [mdxJsxFromMarkdown()]
2065+
}),
2066+
{extensions: [mdxJsxToMarkdown()]}
2067+
)
2068+
}
2069+
2070+
t.end()
2071+
})

0 commit comments

Comments
 (0)