Skip to content

Commit 4bae3ca

Browse files
committed
Fix formatting by not de- and indenting
Related-to: GH-1.
1 parent 55e3ea6 commit 4bae3ca

File tree

3 files changed

+5
-59
lines changed

3 files changed

+5
-59
lines changed

index.js

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
* @typedef {import('./complex-types').MDXTextExpression} MDXTextExpression
99
*/
1010

11-
import stripIndent from 'strip-indent'
12-
13-
const eol = /\r?\n|\r/g
14-
1511
/** @type {FromMarkdownExtension} */
1612
export const mdxExpressionFromMarkdown = {
1713
enter: {
@@ -59,7 +55,7 @@ function exitMdxExpression(token) {
5955
const node = /** @type {MDXFlowExpression|MDXTextExpression} */ (
6056
this.exit(token)
6157
)
62-
node.value = token.type === 'mdxFlowExpression' ? dedent(value) : value
58+
node.value = value
6359

6460
if (estree) {
6561
node.data = {estree}
@@ -78,54 +74,5 @@ function exitMdxExpressionData(token) {
7874
*/
7975
function handleMdxExpression(node) {
8076
const value = node.value || ''
81-
return '{' + (node.type === 'mdxFlowExpression' ? indent(value) : value) + '}'
82-
}
83-
84-
/**
85-
* @param {string} value
86-
* @returns {string}
87-
*/
88-
function dedent(value) {
89-
const firstLineEnding = /\r?\n|\r/.exec(value)
90-
const position = firstLineEnding
91-
? firstLineEnding.index + firstLineEnding[0].length
92-
: -1
93-
94-
if (position > -1) {
95-
return value.slice(0, position) + stripIndent(value.slice(position))
96-
}
97-
98-
return value
99-
}
100-
101-
/**
102-
* @param {string} value
103-
* @returns {string}
104-
*/
105-
function indent(value) {
106-
/** @type {Array.<string>} */
107-
const result = []
108-
let start = 0
109-
let line = 0
110-
/** @type {RegExpExecArray|null} */
111-
let match
112-
113-
while ((match = eol.exec(value))) {
114-
one(value.slice(start, match.index))
115-
result.push(match[0])
116-
start = match.index + match[0].length
117-
line++
118-
}
119-
120-
one(value.slice(start))
121-
122-
return result.join('')
123-
124-
/**
125-
* @param {string} slice
126-
* @returns {void}
127-
*/
128-
function one(slice) {
129-
result.push((line && slice ? ' ' : '') + slice)
130-
}
77+
return '{' + value + '}'
13178
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
"index.js"
3737
],
3838
"dependencies": {
39-
"@types/estree-jsx": "^0.0.1",
40-
"strip-indent": "^4.0.0"
39+
"@types/estree-jsx": "^0.0.1"
4140
},
4241
"devDependencies": {
4342
"@types/acorn": "^4.0.0",

test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ test('markdown -> mdast', (t) => {
4242
children: [
4343
{
4444
type: 'mdxFlowExpression',
45-
value: '\n1 + 1\n',
45+
value: '\n 1 + 1\n',
4646
position: {
4747
start: {line: 1, column: 1, offset: 0},
4848
end: {line: 3, column: 2, offset: 11}
@@ -426,7 +426,7 @@ test('mdast -> markdown', (t) => {
426426
},
427427
{extensions: [mdxExpressionToMarkdown]}
428428
),
429-
'{a + b}\n\n{\n c +\n 1\n}\n\n{}\n\nd\n',
429+
'{a + b}\n\n{\nc +\n1\n}\n\n{}\n\nd\n',
430430
'should serialize flow expressions'
431431
)
432432

0 commit comments

Comments
 (0)