Skip to content

Commit 0c253fe

Browse files
committed
Add collapseEmptyAttributes option
Related-to: GH-12.
1 parent 6331d95 commit 0c253fe

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export {directiveFromMarkdown, directiveToMarkdown} from './lib/index.js'
1212
* Configuration.
1313
*/
1414
export interface ToMarkdownOptions {
15+
/**
16+
* Collapse empty attributes: get `title` instead of `title=""`
17+
* (default: `true`).
18+
*/
19+
collapseEmptyAttributes?: boolean | null | undefined
1520
/**
1621
* Leave attributes unquoted if that results in less bytes
1722
* (default: `false`).

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export function directiveToMarkdown(options) {
266266
* @returns {string}
267267
*/
268268
function quoted(key, value, node, state) {
269-
if (!value) return key
269+
if (settings.collapseEmptyAttributes !== false && !value) return key
270270

271271
if (settings.preferUnquoted && unquoted.test(value)) {
272272
return key + '=' + value

readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ Configuration.
264264

265265
###### Parameters
266266

267+
* `collapseEmptyAttributes`
268+
(`boolean`, default: `true`)
269+
— collapse empty attributes: get `title` instead of `title=""`
267270
* `preferUnquoted`
268271
(`boolean`, default: `false`)
269272
— leave attributes unquoted if that results in less bytes

test.js

+85
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,91 @@ test('directiveToMarkdown()', async function (t) {
11211121
}
11221122
)
11231123

1124+
await t.test('collapseEmptyAttributes', async function (t) {
1125+
await t.test(
1126+
'should hide empty string attributes by default',
1127+
async function () {
1128+
assert.deepEqual(
1129+
toMarkdown(
1130+
{
1131+
type: 'textDirective',
1132+
name: 'i',
1133+
attributes: {title: ''},
1134+
children: []
1135+
},
1136+
{extensions: [directiveToMarkdown()]}
1137+
),
1138+
':i{title}\n'
1139+
)
1140+
}
1141+
)
1142+
1143+
await t.test(
1144+
'should hide empty string attributes w/ `collapseEmptyAttributes: true`',
1145+
async function () {
1146+
assert.deepEqual(
1147+
toMarkdown(
1148+
{
1149+
type: 'textDirective',
1150+
name: 'i',
1151+
attributes: {title: ''},
1152+
children: []
1153+
},
1154+
{extensions: [directiveToMarkdown({collapseEmptyAttributes: true})]}
1155+
),
1156+
':i{title}\n'
1157+
)
1158+
}
1159+
)
1160+
1161+
await t.test(
1162+
'should show empty string attributes w/ `collapseEmptyAttributes: false`',
1163+
async function () {
1164+
assert.deepEqual(
1165+
toMarkdown(
1166+
{
1167+
type: 'textDirective',
1168+
name: 'i',
1169+
attributes: {title: ''},
1170+
children: []
1171+
},
1172+
{
1173+
extensions: [
1174+
directiveToMarkdown({collapseEmptyAttributes: false})
1175+
]
1176+
}
1177+
),
1178+
':i{title=""}\n'
1179+
)
1180+
}
1181+
)
1182+
1183+
await t.test(
1184+
'should use quotes for empty string attributes w/ `collapseEmptyAttributes: false` and `preferUnquoted: true`',
1185+
async function () {
1186+
assert.deepEqual(
1187+
toMarkdown(
1188+
{
1189+
type: 'textDirective',
1190+
name: 'i',
1191+
attributes: {title: ''},
1192+
children: []
1193+
},
1194+
{
1195+
extensions: [
1196+
directiveToMarkdown({
1197+
collapseEmptyAttributes: false,
1198+
preferUnquoted: true
1199+
})
1200+
]
1201+
}
1202+
),
1203+
':i{title=""}\n'
1204+
)
1205+
}
1206+
)
1207+
})
1208+
11241209
await t.test('preferUnquoted', async function (t) {
11251210
await t.test('should omit quotes in `preferUnquoted`', async function () {
11261211
assert.deepEqual(

0 commit comments

Comments
 (0)