Skip to content

Commit 1a95c30

Browse files
committed
Add options.tightSelfClosing for space before slash
Related to: GH-3.
1 parent 1779d91 commit 1a95c30

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

lib/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* Preferred quote to use around attribute values.
2323
* @property {boolean} [quoteSmart=false]
2424
* Use the other quote if that results in less bytes.
25+
* @property {boolean} [tightSelfClosing=true]
26+
* Do not use an extra space when closing self-closing elements: `<img/>`
27+
* instead of `<img />`.
2528
*/
2629

2730
import {ccount} from 'ccount'
@@ -363,7 +366,7 @@ export function mdxJsxFromMarkdown() {
363366
* @returns {ToMarkdownExtension}
364367
*/
365368
export function mdxJsxToMarkdown(options = {}) {
366-
const {quote = '"', quoteSmart} = options
369+
const {quote = '"', quoteSmart, tightSelfClosing = true} = options
367370
const alternative = quote === '"' ? "'" : '"'
368371

369372
if (quote !== '"' && quote !== "'") {
@@ -458,7 +461,9 @@ export function mdxJsxToMarkdown(options = {}) {
458461
'<' +
459462
(node.name || '') +
460463
attributeValue +
461-
(selfClosing ? '/' : '') +
464+
(selfClosing
465+
? (tightSelfClosing || /[ \n]$/.test(attributeValue) ? '' : ' ') + '/'
466+
: '') +
462467
'>' +
463468
(node.children && node.children.length > 0
464469
? node.type === 'mdxJsxFlowElement'

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ Preferred quote to use around attribute values (`'"'` or `"'"`, default: `'"'`).
242242

243243
Use the other quote if that results in less bytes (`boolean`, default: `false`).
244244

245+
###### `options.tightSelfClosing`
246+
247+
Do not use an extra space when closing self-closing elements: `<img/>` instead
248+
of `<img />` (`boolean`, default: `true`).
249+
245250
## Syntax tree
246251

247252
The following interfaces are added to **[mdast][]** by this utility.

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,5 +1923,23 @@ test('mdast -> markdown', (t) => {
19231923
'should support `options.quoteSmart`: use quote w/ more alternative quotes'
19241924
)
19251925

1926+
t.deepEqual(
1927+
toMarkdown(
1928+
{type: 'mdxJsxFlowElement', name: 'x', attributes: [], children: []},
1929+
{extensions: [mdxJsxToMarkdown({tightSelfClosing: false})]}
1930+
),
1931+
'<x />\n',
1932+
'should support `options.tightSelfClosing`: no space when `false`'
1933+
)
1934+
1935+
t.deepEqual(
1936+
toMarkdown(
1937+
{type: 'mdxJsxFlowElement', name: 'x', attributes: [], children: []},
1938+
{extensions: [mdxJsxToMarkdown({tightSelfClosing: true})]}
1939+
),
1940+
'<x/>\n',
1941+
'should support `options.tightSelfClosing`: space when `true`'
1942+
)
1943+
19261944
t.end()
19271945
})

0 commit comments

Comments
 (0)