Skip to content

Commit 59747bf

Browse files
committed
Fix formatting of (expression) attributes
Closes GH-4.
1 parent 57fea6d commit 59747bf

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

index.js

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @typedef {import('mdast-util-from-markdown').Token} Token
77
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
88
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
9+
* @typedef {import('mdast-util-to-markdown').Map} ToMarkdownMap
910
* @typedef {import('estree-jsx').Program} Program
1011
* @typedef {import('./complex-types').MDXJsxAttributeValueExpression} MDXJsxAttributeValueExpression
1112
* @typedef {import('./complex-types').MDXJsxAttribute} MDXJsxAttribute
@@ -22,8 +23,7 @@ import {stringifyEntitiesLight} from 'stringify-entities'
2223
import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js'
2324
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
2425
import {checkQuote} from 'mdast-util-to-markdown/lib/util/check-quote.js'
25-
26-
const eol = /\r?\n|\r/g
26+
import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js'
2727

2828
mdxElement.peek = peekElement
2929

@@ -332,6 +332,7 @@ function mdxElement(node, _, context) {
332332
node.name && (!node.children || node.children.length === 0)
333333
const quote = checkQuote(context)
334334
const exit = context.enter(node.type)
335+
let attributeValue = ''
335336
let index = -1
336337
/** @type {Array.<string>} */
337338
const attributes = []
@@ -344,6 +345,9 @@ function mdxElement(node, _, context) {
344345
throw new Error('Cannot serialize fragment w/ attributes')
345346
}
346347

348+
const isMultiFlow =
349+
node.type === 'mdxJsxFlowElement' && node.attributes.length > 1
350+
347351
while (++index < node.attributes.length) {
348352
const attribute = node.attributes[index]
349353

@@ -366,19 +370,16 @@ function mdxElement(node, _, context) {
366370
quote))
367371
}
368372

369-
attributes.push(result)
373+
attributes.push((isMultiFlow ? '\n ' : ' ') + result)
370374
}
375+
376+
attributeValue = attributes.join('') + (isMultiFlow ? '\n' : '')
371377
}
372378

373379
const value =
374380
'<' +
375381
(node.name || '') +
376-
(node.type === 'mdxJsxFlowElement' && attributes.length > 1
377-
? // Flow w/ multiple attributes.
378-
'\n' + indent(attributes.join('\n')) + '\n'
379-
: attributes.length > 0 // Text or flow w/ a single attribute.
380-
? ' ' + dedentStart(indent(attributes.join(' ')))
381-
: '') +
382+
attributeValue +
382383
(selfClosing ? '/' : '') +
383384
'>' +
384385
(node.children && node.children.length > 0
@@ -391,48 +392,23 @@ function mdxElement(node, _, context) {
391392
exit()
392393
return value
393394
}
395+
394396
/**
395397
* @type {ToMarkdownHandle}
396398
*/
397-
398399
function peekElement() {
399400
return '<'
400401
}
401402

402-
/**
403-
* @param {string} value
404-
* @returns {string}
405-
*/
406-
function dedentStart(value) {
407-
return value.replace(/^ +/, '')
408-
}
409-
410403
/**
411404
* @param {string} value
412405
* @returns {string}
413406
*/
414407
function indent(value) {
415-
/** @type {Array.<string>} */
416-
const result = []
417-
let start = 0
418-
/** @type {RegExpExecArray|null} */
419-
let match
420-
421-
while ((match = eol.exec(value))) {
422-
one(value.slice(start, match.index))
423-
result.push(match[0])
424-
start = match.index + match[0].length
425-
}
426-
427-
one(value.slice(start))
428-
429-
return result.join('')
408+
return indentLines(value, map)
430409

431-
/**
432-
* @param {string} slice
433-
* @returns {void}
434-
*/
435-
function one(slice) {
436-
result.push((slice ? ' ' : '') + slice)
410+
/** @type {ToMarkdownMap} */
411+
function map(line, _, blank) {
412+
return (blank ? '' : ' ') + line
437413
}
438414
}

0 commit comments

Comments
 (0)