Skip to content

Commit

Permalink
Add preferShortcut option
Browse files Browse the repository at this point in the history
Closes: GH-12.
  • Loading branch information
wooorm committed Jan 22, 2025
1 parent 0c253fe commit f8c7348
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface ToMarkdownOptions {
* (default: `true`).
*/
collapseEmptyAttributes?: boolean | null | undefined
/**
* Prefer `#` and `.` shortcuts for `id` and `class`
* (default: `true`).
*/
preferShortcut?: boolean | null | undefined
/**
* Leave attributes unquoted if that results in less bytes
* (default: `false`).
Expand Down
18 changes: 12 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,15 @@ export function directiveToMarkdown(options) {
) {
const value = String(attributes[key])

// To do: next major:
// Do not reorder `id` and `class` attributes when they do not turn into
// shortcuts.
// Additionally, join shortcuts: `#a .b.c d="e"` -> `#a.b.c d="e"`
if (key === 'id') {
id = shortcut.test(value)
? '#' + value
: quoted('id', value, node, state)
id =
settings.preferShortcut !== false && shortcut.test(value)
? '#' + value
: quoted('id', value, node, state)
} else if (key === 'class') {
const list = value.split(/[\t\n\r ]+/g)
/** @type {Array<string>} */
Expand All @@ -227,9 +232,10 @@ export function directiveToMarkdown(options) {
let index = -1

while (++index < list.length) {
;(shortcut.test(list[index]) ? classesList : classesFullList).push(
list[index]
)
;(settings.preferShortcut !== false && shortcut.test(list[index])
? classesList
: classesFullList
).push(list[index])
}

classesFull =
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ Configuration.
* `collapseEmptyAttributes`
(`boolean`, default: `true`)
— collapse empty attributes: get `title` instead of `title=""`
* `preferShortcut`
(`boolean`, default: `true`)
— prefer `#` and `.` shortcuts for `id` and `class`
* `preferUnquoted`
(`boolean`, default: `false`)
— leave attributes unquoted if that results in less bytes
Expand Down
56 changes: 56 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,62 @@ test('directiveToMarkdown()', async function (t) {
)
})

await t.test('preferShortcut', async function (t) {
await t.test(
'should use `#` for `id`, `.` for `class` by default',
async function () {
assert.deepEqual(
toMarkdown(
{
type: 'textDirective',
name: 'i',
attributes: {class: 'a b', id: 'c'},
children: []
},
{extensions: [directiveToMarkdown()]}
),
':i{#c .a.b}\n'
)
}
)

await t.test(
'should use `#` for `id`, `.` for `class` w/ `preferShortcut: true`',
async function () {
assert.deepEqual(
toMarkdown(
{
type: 'textDirective',
name: 'i',
attributes: {class: 'a b', id: 'c'},
children: []
},
{extensions: [directiveToMarkdown({preferShortcut: true})]}
),
':i{#c .a.b}\n'
)
}
)

await t.test(
'should not use use `#` for `id`, `.` for `class` w/ `preferShortcut: false`',
async function () {
assert.deepEqual(
toMarkdown(
{
type: 'textDirective',
name: 'i',
attributes: {class: 'a b', id: 'c'},
children: []
},
{extensions: [directiveToMarkdown({preferShortcut: false})]}
),
':i{id="c" class="a b"}\n'
)
}
)
})

await t.test('preferUnquoted', async function (t) {
await t.test('should omit quotes in `preferUnquoted`', async function () {
assert.deepEqual(
Expand Down

0 comments on commit f8c7348

Please sign in to comment.