Skip to content

Commit 6331d95

Browse files
committed
Add preferUnquoted option
Related-to: GH-12.
1 parent 7c8d606 commit 6331d95

File tree

4 files changed

+225
-154
lines changed

4 files changed

+225
-154
lines changed

Diff for: index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export {directiveFromMarkdown, directiveToMarkdown} from './lib/index.js'
1212
* Configuration.
1313
*/
1414
export interface ToMarkdownOptions {
15+
/**
16+
* Leave attributes unquoted if that results in less bytes
17+
* (default: `false`).
18+
*/
19+
preferUnquoted?: boolean | null | undefined
1520
/**
1621
* Use the other quote if that results in less bytes
1722
* (default: `false`).

Diff for: lib/index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const own = {}.hasOwnProperty
2727
const emptyOptions = {}
2828

2929
const shortcut = /^[^\t\n\r "#'.<=>`}]+$/
30+
const unquoted = /^[^\t\n\r "'<=>`}]+$/
3031

3132
/**
3233
* Create an extension for `mdast-util-from-markdown` to enable directives in
@@ -89,6 +90,7 @@ export function directiveFromMarkdown() {
8990
*/
9091
export function directiveToMarkdown(options) {
9192
const settings = options || emptyOptions
93+
9294
if (
9395
settings.quote !== '"' &&
9496
settings.quote !== "'" &&
@@ -266,6 +268,10 @@ export function directiveToMarkdown(options) {
266268
function quoted(key, value, node, state) {
267269
if (!value) return key
268270

271+
if (settings.preferUnquoted && unquoted.test(value)) {
272+
return key + '=' + value
273+
}
274+
269275
// If the alternative is less common than `quote`, switch.
270276
const preferred = settings.quote || state.options.quote || '"'
271277
const alternative = preferred === '"' ? "'" : '"'
@@ -282,10 +288,10 @@ export function directiveToMarkdown(options) {
282288

283289
return (
284290
key +
285-
('=' +
286-
appliedQuote +
287-
stringifyEntitiesLight(value, {subset}) +
288-
appliedQuote)
291+
'=' +
292+
appliedQuote +
293+
stringifyEntitiesLight(value, {subset}) +
294+
appliedQuote
289295
)
290296
}
291297
}

Diff for: readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ Configuration.
264264

265265
###### Parameters
266266

267+
* `preferUnquoted`
268+
(`boolean`, default: `false`)
269+
— leave attributes unquoted if that results in less bytes
267270
* `quoteSmart`
268271
(`boolean`, default: `false`)
269272
— use the other quote if that results in less bytes

0 commit comments

Comments
 (0)