File tree 4 files changed +76
-6
lines changed
4 files changed +76
-6
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,11 @@ export interface ToMarkdownOptions {
17
17
* (default: `true`).
18
18
*/
19
19
collapseEmptyAttributes ?: boolean | null | undefined
20
+ /**
21
+ * Prefer `#` and `.` shortcuts for `id` and `class`
22
+ * (default: `true`).
23
+ */
24
+ preferShortcut ?: boolean | null | undefined
20
25
/**
21
26
* Leave attributes unquoted if that results in less bytes
22
27
* (default: `false`).
Original file line number Diff line number Diff line change @@ -214,10 +214,15 @@ export function directiveToMarkdown(options) {
214
214
) {
215
215
const value = String ( attributes [ key ] )
216
216
217
+ // To do: next major:
218
+ // Do not reorder `id` and `class` attributes when they do not turn into
219
+ // shortcuts.
220
+ // Additionally, join shortcuts: `#a .b.c d="e"` -> `#a.b.c d="e"`
217
221
if ( key === 'id' ) {
218
- id = shortcut . test ( value )
219
- ? '#' + value
220
- : quoted ( 'id' , value , node , state )
222
+ id =
223
+ settings . preferShortcut !== false && shortcut . test ( value )
224
+ ? '#' + value
225
+ : quoted ( 'id' , value , node , state )
221
226
} else if ( key === 'class' ) {
222
227
const list = value . split ( / [ \t \n \r ] + / g)
223
228
/** @type {Array<string> } */
@@ -227,9 +232,10 @@ export function directiveToMarkdown(options) {
227
232
let index = - 1
228
233
229
234
while ( ++ index < list . length ) {
230
- ; ( shortcut . test ( list [ index ] ) ? classesList : classesFullList ) . push (
231
- list [ index ]
232
- )
235
+ ; ( settings . preferShortcut !== false && shortcut . test ( list [ index ] )
236
+ ? classesList
237
+ : classesFullList
238
+ ) . push ( list [ index ] )
233
239
}
234
240
235
241
classesFull =
Original file line number Diff line number Diff line change @@ -267,6 +267,9 @@ Configuration.
267
267
* ` collapseEmptyAttributes `
268
268
(` boolean ` , default: ` true ` )
269
269
— collapse empty attributes: get ` title ` instead of ` title="" `
270
+ * ` preferShortcut `
271
+ (` boolean ` , default: ` true ` )
272
+ — prefer ` # ` and ` . ` shortcuts for ` id ` and ` class `
270
273
* ` preferUnquoted `
271
274
(` boolean ` , default: ` false ` )
272
275
— leave attributes unquoted if that results in less bytes
Original file line number Diff line number Diff line change @@ -1206,6 +1206,62 @@ test('directiveToMarkdown()', async function (t) {
1206
1206
)
1207
1207
} )
1208
1208
1209
+ await t . test ( 'preferShortcut' , async function ( t ) {
1210
+ await t . test (
1211
+ 'should use `#` for `id`, `.` for `class` by default' ,
1212
+ async function ( ) {
1213
+ assert . deepEqual (
1214
+ toMarkdown (
1215
+ {
1216
+ type : 'textDirective' ,
1217
+ name : 'i' ,
1218
+ attributes : { class : 'a b' , id : 'c' } ,
1219
+ children : [ ]
1220
+ } ,
1221
+ { extensions : [ directiveToMarkdown ( ) ] }
1222
+ ) ,
1223
+ ':i{#c .a.b}\n'
1224
+ )
1225
+ }
1226
+ )
1227
+
1228
+ await t . test (
1229
+ 'should use `#` for `id`, `.` for `class` w/ `preferShortcut: true`' ,
1230
+ async function ( ) {
1231
+ assert . deepEqual (
1232
+ toMarkdown (
1233
+ {
1234
+ type : 'textDirective' ,
1235
+ name : 'i' ,
1236
+ attributes : { class : 'a b' , id : 'c' } ,
1237
+ children : [ ]
1238
+ } ,
1239
+ { extensions : [ directiveToMarkdown ( { preferShortcut : true } ) ] }
1240
+ ) ,
1241
+ ':i{#c .a.b}\n'
1242
+ )
1243
+ }
1244
+ )
1245
+
1246
+ await t . test (
1247
+ 'should not use use `#` for `id`, `.` for `class` w/ `preferShortcut: false`' ,
1248
+ async function ( ) {
1249
+ assert . deepEqual (
1250
+ toMarkdown (
1251
+ {
1252
+ type : 'textDirective' ,
1253
+ name : 'i' ,
1254
+ attributes : { class : 'a b' , id : 'c' } ,
1255
+ children : [ ]
1256
+ } ,
1257
+ { extensions : [ directiveToMarkdown ( { preferShortcut : false } ) ] }
1258
+ ) ,
1259
+ ':i{id="c" class="a b"}\n'
1260
+ )
1261
+ }
1262
+ )
1263
+ } )
1264
+
1209
1265
await t . test ( 'preferUnquoted' , async function ( t ) {
1210
1266
await t . test ( 'should omit quotes in `preferUnquoted`' , async function ( ) {
1211
1267
assert . deepEqual (
You can’t perform that action at this time.
0 commit comments