From f8c734830440b41cd2004d34e2075af7a1a9c18b Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 22 Jan 2025 15:45:26 +0100 Subject: [PATCH] Add `preferShortcut` option Closes: GH-12. --- index.d.ts | 5 +++++ lib/index.js | 18 +++++++++++------ readme.md | 3 +++ test.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 77cc9ea..fece913 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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`). diff --git a/lib/index.js b/lib/index.js index 0bdc958..0adb8f9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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} */ @@ -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 = diff --git a/readme.md b/readme.md index be24de1..229b061 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test.js b/test.js index f0d7e8d..ca69ca4 100644 --- a/test.js +++ b/test.js @@ -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(