6
6
* @typedef {import('mdast-util-from-markdown').Token } Token
7
7
* @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
8
8
* @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
9
+ * @typedef {import('mdast-util-to-markdown').Map } ToMarkdownMap
9
10
* @typedef {import('estree-jsx').Program } Program
10
11
* @typedef {import('./complex-types').MDXJsxAttributeValueExpression } MDXJsxAttributeValueExpression
11
12
* @typedef {import('./complex-types').MDXJsxAttribute } MDXJsxAttribute
@@ -22,8 +23,7 @@ import {stringifyEntitiesLight} from 'stringify-entities'
22
23
import { containerFlow } from 'mdast-util-to-markdown/lib/util/container-flow.js'
23
24
import { containerPhrasing } from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
24
25
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'
27
27
28
28
mdxElement . peek = peekElement
29
29
@@ -332,6 +332,7 @@ function mdxElement(node, _, context) {
332
332
node . name && ( ! node . children || node . children . length === 0 )
333
333
const quote = checkQuote ( context )
334
334
const exit = context . enter ( node . type )
335
+ let attributeValue = ''
335
336
let index = - 1
336
337
/** @type {Array.<string> } */
337
338
const attributes = [ ]
@@ -344,6 +345,9 @@ function mdxElement(node, _, context) {
344
345
throw new Error ( 'Cannot serialize fragment w/ attributes' )
345
346
}
346
347
348
+ const isMultiFlow =
349
+ node . type === 'mdxJsxFlowElement' && node . attributes . length > 1
350
+
347
351
while ( ++ index < node . attributes . length ) {
348
352
const attribute = node . attributes [ index ]
349
353
@@ -366,19 +370,16 @@ function mdxElement(node, _, context) {
366
370
quote ) )
367
371
}
368
372
369
- attributes . push ( result )
373
+ attributes . push ( ( isMultiFlow ? '\n ' : ' ' ) + result )
370
374
}
375
+
376
+ attributeValue = attributes . join ( '' ) + ( isMultiFlow ? '\n' : '' )
371
377
}
372
378
373
379
const value =
374
380
'<' +
375
381
( 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 +
382
383
( selfClosing ? '/' : '' ) +
383
384
'>' +
384
385
( node . children && node . children . length > 0
@@ -391,48 +392,23 @@ function mdxElement(node, _, context) {
391
392
exit ( )
392
393
return value
393
394
}
395
+
394
396
/**
395
397
* @type {ToMarkdownHandle }
396
398
*/
397
-
398
399
function peekElement ( ) {
399
400
return '<'
400
401
}
401
402
402
- /**
403
- * @param {string } value
404
- * @returns {string }
405
- */
406
- function dedentStart ( value ) {
407
- return value . replace ( / ^ + / , '' )
408
- }
409
-
410
403
/**
411
404
* @param {string } value
412
405
* @returns {string }
413
406
*/
414
407
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 )
430
409
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
437
413
}
438
414
}
0 commit comments