Skip to content

Commit deee95e

Browse files
committed
Add support for custom quotes
1 parent 9b2af88 commit deee95e

File tree

9 files changed

+46
-9
lines changed

9 files changed

+46
-9
lines changed

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ function toMdast(tree, options) {
1616
h.baseFound = false
1717
h.frozenBaseUrl = null
1818
h.wrapText = true
19+
h.qNesting = 0
1920

2021
h.handlers = xtend(handlers, settings.handlers || {})
2122
h.augment = augment
2223
h.document = settings.document
2324

2425
h.checked = settings.checked || '[x]'
2526
h.unchecked = settings.unchecked || '[ ]'
27+
h.quotes = settings.quotes || ['"']
2628

2729
visit(tree, onvisit)
2830

lib/handlers/q.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,31 @@ module.exports = q
44

55
var all = require('../all')
66

7-
var quote = '"'
8-
97
function q(h, node) {
10-
var contents = all(h, node)
11-
var head = contents[0]
12-
var tail = contents[contents.length - 1]
8+
var expected = h.quotes[h.qNesting % h.quotes.length]
9+
var open = expected.length === 1 ? expected : expected.charAt(0)
10+
var close = expected.length === 1 ? expected : expected.charAt(1)
11+
var contents
12+
var head
13+
var tail
14+
15+
h.qNesting++
16+
contents = all(h, node)
17+
h.qNesting--
18+
19+
head = contents[0]
20+
tail = contents[contents.length - 1]
1321

1422
if (head && head.type === 'text') {
15-
head.value = quote + head.value
23+
head.value = open + head.value
1624
} else {
17-
contents.unshift({type: 'text', value: quote})
25+
contents.unshift({type: 'text', value: open})
1826
}
1927

2028
if (tail && tail.type === 'text') {
21-
tail.value += quote
29+
tail.value += close
2230
} else {
23-
contents.push({type: 'text', value: quote})
31+
contents.push({type: 'text', value: close})
2432
}
2533

2634
return contents

readme.md

+15
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ default: `[x]`).
9797
Value to use when serializing an unchecked checkbox or radio input (`string`,
9898
default: `[ ]`).
9999

100+
###### `options.quotes`
101+
102+
List of quotes to use (`string[]`, default: `['"']`).
103+
Each value can be one or two characters.
104+
When two, the first character determines the opening quote and the second the
105+
closing quote at that level.
106+
When one, both the opening and closing quote are that character.
107+
The order in which the preferred quotes appear determines which quotes to use at
108+
which level of nesting.
109+
So, to prefer `‘’` at the first level of nesting, and `“”` at the second, pass:
110+
`['‘’', '“”']`.
111+
If `<q>`s are nested deeper than the given amount of quotes, the markers wrap
112+
around: a third level of nesting when using `['«»', '‹›']` should have double
113+
guillemets, a fourth single, a fifth double again, etc.
114+
100115
##### Returns
101116

102117
[`MdastNode`][mdast-node].

test/fixtures/quotes-alt/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<q>alpha <q>bravo <q>charlie <q>delta <q>echo</q>.</q></q></q></q>

test/fixtures/quotes-alt/index.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fragment": true,
3+
"quotes": ["«»", "‹›", "“”"]
4+
}

test/fixtures/quotes-alt/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
«alpha ‹bravo “charlie «delta ‹echo›.»”›»

test/fixtures/quotes/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<q>alpha <q>bravo <q>charlie <q>delta <q>echo</q>.</q></q></q></q>

test/fixtures/quotes/index.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fragment": true,
3+
"quotes": ["“”", "‘’"]
4+
}

test/fixtures/quotes/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
“alpha ‘bravo “charlie ‘delta “echo”.’”’”

0 commit comments

Comments
 (0)