Skip to content

Commit f8c7348

Browse files
committed
Add preferShortcut option
Closes: GH-12.
1 parent 0c253fe commit f8c7348

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export interface ToMarkdownOptions {
1717
* (default: `true`).
1818
*/
1919
collapseEmptyAttributes?: boolean | null | undefined
20+
/**
21+
* Prefer `#` and `.` shortcuts for `id` and `class`
22+
* (default: `true`).
23+
*/
24+
preferShortcut?: boolean | null | undefined
2025
/**
2126
* Leave attributes unquoted if that results in less bytes
2227
* (default: `false`).

lib/index.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,15 @@ export function directiveToMarkdown(options) {
214214
) {
215215
const value = String(attributes[key])
216216

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"`
217221
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)
221226
} else if (key === 'class') {
222227
const list = value.split(/[\t\n\r ]+/g)
223228
/** @type {Array<string>} */
@@ -227,9 +232,10 @@ export function directiveToMarkdown(options) {
227232
let index = -1
228233

229234
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])
233239
}
234240

235241
classesFull =

readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ Configuration.
267267
* `collapseEmptyAttributes`
268268
(`boolean`, default: `true`)
269269
— collapse empty attributes: get `title` instead of `title=""`
270+
* `preferShortcut`
271+
(`boolean`, default: `true`)
272+
— prefer `#` and `.` shortcuts for `id` and `class`
270273
* `preferUnquoted`
271274
(`boolean`, default: `false`)
272275
— leave attributes unquoted if that results in less bytes

test.js

+56
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,62 @@ test('directiveToMarkdown()', async function (t) {
12061206
)
12071207
})
12081208

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+
12091265
await t.test('preferUnquoted', async function (t) {
12101266
await t.test('should omit quotes in `preferUnquoted`', async function () {
12111267
assert.deepEqual(

0 commit comments

Comments
 (0)