Skip to content

Commit

Permalink
Add support for custom quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Mar 8, 2020
1 parent 9b2af88 commit deee95e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ function toMdast(tree, options) {
h.baseFound = false
h.frozenBaseUrl = null
h.wrapText = true
h.qNesting = 0

h.handlers = xtend(handlers, settings.handlers || {})
h.augment = augment
h.document = settings.document

h.checked = settings.checked || '[x]'
h.unchecked = settings.unchecked || '[ ]'
h.quotes = settings.quotes || ['"']

visit(tree, onvisit)

Expand Down
26 changes: 17 additions & 9 deletions lib/handlers/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ module.exports = q

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

var quote = '"'

function q(h, node) {
var contents = all(h, node)
var head = contents[0]
var tail = contents[contents.length - 1]
var expected = h.quotes[h.qNesting % h.quotes.length]
var open = expected.length === 1 ? expected : expected.charAt(0)
var close = expected.length === 1 ? expected : expected.charAt(1)
var contents
var head
var tail

h.qNesting++
contents = all(h, node)
h.qNesting--

head = contents[0]
tail = contents[contents.length - 1]

if (head && head.type === 'text') {
head.value = quote + head.value
head.value = open + head.value
} else {
contents.unshift({type: 'text', value: quote})
contents.unshift({type: 'text', value: open})
}

if (tail && tail.type === 'text') {
tail.value += quote
tail.value += close
} else {
contents.push({type: 'text', value: quote})
contents.push({type: 'text', value: close})
}

return contents
Expand Down
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ default: `[x]`).
Value to use when serializing an unchecked checkbox or radio input (`string`,
default: `[ ]`).

###### `options.quotes`

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

##### Returns

[`MdastNode`][mdast-node].
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/quotes-alt/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<q>alpha <q>bravo <q>charlie <q>delta <q>echo</q>.</q></q></q></q>
4 changes: 4 additions & 0 deletions test/fixtures/quotes-alt/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"fragment": true,
"quotes": ["«»", "‹›", "“”"]
}
1 change: 1 addition & 0 deletions test/fixtures/quotes-alt/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
«alpha ‹bravo “charlie «delta ‹echo›.»”›»
1 change: 1 addition & 0 deletions test/fixtures/quotes/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<q>alpha <q>bravo <q>charlie <q>delta <q>echo</q>.</q></q></q></q>
4 changes: 4 additions & 0 deletions test/fixtures/quotes/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"fragment": true,
"quotes": ["“”", "‘’"]
}
1 change: 1 addition & 0 deletions test/fixtures/quotes/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
“alpha ‘bravo “charlie ‘delta “echo”.’”’”

0 comments on commit deee95e

Please sign in to comment.