Skip to content

Commit 9b2af88

Browse files
committed
Add support for checked, unchecked options
1 parent fdafac8 commit 9b2af88

File tree

7 files changed

+50
-6
lines changed

7 files changed

+50
-6
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ var handlers = require('./lib/handlers')
1010

1111
function toMdast(tree, options) {
1212
var settings = options || {}
13-
var opts = {newlines: settings.newlines === true}
1413
var byId = {}
1514

1615
h.nodeById = byId
@@ -22,9 +21,12 @@ function toMdast(tree, options) {
2221
h.augment = augment
2322
h.document = settings.document
2423

24+
h.checked = settings.checked || '[x]'
25+
h.unchecked = settings.unchecked || '[ ]'
26+
2527
visit(tree, onvisit)
2628

27-
return one(h, minify(opts)(tree), null)
29+
return one(h, minify({newlines: settings.newlines === true})(tree), null)
2830

2931
function h(node, type, props, children) {
3032
var result

lib/handlers/input.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ function input(h, node) {
2727
}
2828

2929
if (type === 'checkbox' || type === 'radio') {
30-
return {type: 'text', value: '[' + (props.checked ? 'x' : ' ') + ']'}
30+
return {
31+
type: 'text',
32+
value: wrapText(h, h[props.checked ? 'checked' : 'unchecked'])
33+
}
3134
}
3235

3336
if (type === 'image' && props.alt) {

lib/handlers/list-item.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ function listItem(h, node) {
3939
if (
4040
head &&
4141
head.type === 'text' &&
42-
head.value.length === 3 &&
43-
head.value.charAt(0) === '[' &&
44-
head.value.charAt(2) === ']'
42+
(head.value === h.checked || head.value === h.unchecked)
4543
) {
4644
grandchildren.shift()
4745
}

readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ If `document: true`, all mdast phrasing children are wrapped in paragraphs.
8787
Whether to collapse to a line feed (`\n`) instead of a single space (default) if
8888
a streak of white-space in a text node contains a newline.
8989

90+
###### `options.checked`
91+
92+
Value to use when serializing a checked checkbox or radio input (`string`,
93+
default: `[x]`).
94+
95+
###### `options.unchecked`
96+
97+
Value to use when serializing an unchecked checkbox or radio input (`string`,
98+
default: `[ ]`).
99+
90100
##### Returns
91101

92102
[`MdastNode`][mdast-node].
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- Checkbox -->
2+
3+
<p><label><input type=checkbox> Alpha</label></p>
4+
<p><label><input type=checkbox checked> Bravo</label></p>
5+
<p><label><input type=checkbox checked value=Charlie> Delta</label></p>
6+
7+
<!-- Radio -->
8+
9+
<p><label><input type=radio> Alpha</label></p>
10+
<p><label><input type=radio checked> Bravo</label></p>
11+
<p><label><input type=radio checked value=Charlie> Delta</label></p>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"fragment": true,
3+
"checked": "",
4+
"unchecked": ""
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!-- Checkbox -->
2+
3+
✗ Alpha
4+
5+
✓ Bravo
6+
7+
✓ Delta
8+
9+
<!-- Radio -->
10+
11+
✗ Alpha
12+
13+
✓ Bravo
14+
15+
✓ Delta

0 commit comments

Comments
 (0)