File tree 4 files changed +94
-1
lines changed
4 files changed +94
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,11 @@ export {directiveFromMarkdown, directiveToMarkdown} from './lib/index.js'
12
12
* Configuration.
13
13
*/
14
14
export interface ToMarkdownOptions {
15
+ /**
16
+ * Collapse empty attributes: get `title` instead of `title=""`
17
+ * (default: `true`).
18
+ */
19
+ collapseEmptyAttributes ?: boolean | null | undefined
15
20
/**
16
21
* Leave attributes unquoted if that results in less bytes
17
22
* (default: `false`).
Original file line number Diff line number Diff line change @@ -266,7 +266,7 @@ export function directiveToMarkdown(options) {
266
266
* @returns {string }
267
267
*/
268
268
function quoted ( key , value , node , state ) {
269
- if ( ! value ) return key
269
+ if ( settings . collapseEmptyAttributes !== false && ! value ) return key
270
270
271
271
if ( settings . preferUnquoted && unquoted . test ( value ) ) {
272
272
return key + '=' + value
Original file line number Diff line number Diff line change @@ -264,6 +264,9 @@ Configuration.
264
264
265
265
###### Parameters
266
266
267
+ * ` collapseEmptyAttributes `
268
+ (` boolean ` , default: ` true ` )
269
+ — collapse empty attributes: get ` title ` instead of ` title="" `
267
270
* ` preferUnquoted `
268
271
(` boolean ` , default: ` false ` )
269
272
— leave attributes unquoted if that results in less bytes
Original file line number Diff line number Diff line change @@ -1121,6 +1121,91 @@ test('directiveToMarkdown()', async function (t) {
1121
1121
}
1122
1122
)
1123
1123
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
+
1124
1209
await t . test ( 'preferUnquoted' , async function ( t ) {
1125
1210
await t . test ( 'should omit quotes in `preferUnquoted`' , async function ( ) {
1126
1211
assert . deepEqual (
You can’t perform that action at this time.
0 commit comments