Skip to content

Commit 52d9d7f

Browse files
committed
Add support for <ol>, <ul>, and <li>
1 parent 1de75a8 commit 52d9d7f

File tree

9 files changed

+305
-0
lines changed

9 files changed

+305
-0
lines changed

handlers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ exports.comment = require('./comment');
77
exports.head = exports.math = exports.script = exports.style = exports.svg =
88
exports.template = exports.title = ignore;
99

10+
exports.ol = exports.ul = require('./list');
11+
exports.li = require('./list-item');
1012
exports.strong = exports.b = require('./strong');
1113
exports.em = exports.i = require('./emphasis');
1214
exports.del = exports.s = exports.strike = require('./delete');

handlers/list-item.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
module.exports = listItem;
4+
5+
var is = require('hast-util-is-element');
6+
var all = require('../all');
7+
8+
function listItem(h, node, parent) {
9+
var children = node.children;
10+
var tail = !parent || parent.children[parent.children.length - 1] === node;
11+
var head = children[0];
12+
var checked = null;
13+
var loose = false;
14+
var checkbox;
15+
var grandchildren;
16+
var content;
17+
18+
/* Check if this node starts with a checkbox. */
19+
if (head && is(head, 'p')) {
20+
grandchildren = head.children;
21+
checkbox = grandchildren[0];
22+
23+
if (checkbox && is(checkbox, 'input') && checkbox.properties.type === 'checkbox') {
24+
checked = Boolean(checkbox.properties.checked);
25+
} else {
26+
checkbox = null;
27+
}
28+
}
29+
30+
content = all(h, node);
31+
32+
/* Remove initial spacing if we previously found a checkbox. */
33+
if (checked !== null) {
34+
grandchildren = content[0] && content[0].children;
35+
head = grandchildren && grandchildren[0];
36+
37+
if (head && head.type === 'text' && head.value.charAt(0) === ' ') {
38+
/* Remove text with one space, or remove that one initial space */
39+
if (head.value.length === 1) {
40+
content[0].children = grandchildren.slice(1);
41+
} else {
42+
head.value = head.value.slice(1);
43+
}
44+
}
45+
}
46+
47+
if (content.length > 1) {
48+
/* More than one node is always loose. */
49+
loose = true;
50+
} else if (content.length === 1) {
51+
head = content[0];
52+
53+
if (head.type === 'text') {
54+
/* Wrap `text` in `paragraph` (always tight). */
55+
content = [{type: 'paragraph', children: content}];
56+
loose = false;
57+
} else if (head.type === 'paragraph') {
58+
/* One `paragraph`, always tight. Ensure it isn’t empty. */
59+
loose = false;
60+
61+
if (head.children.length === 0) {
62+
content = [];
63+
}
64+
}
65+
}
66+
67+
/* Last list-item is never loose. */
68+
if (tail) {
69+
loose = false;
70+
}
71+
72+
return h(node, 'listItem', {loose: loose, checked: checked}, content);
73+
}

handlers/list.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
module.exports = list;
4+
5+
var has = require('hast-util-has-property');
6+
var all = require('../all');
7+
8+
function list(h, node) {
9+
var ordered = node.tagName === 'ol';
10+
var start = null;
11+
var loose = false;
12+
var content;
13+
var length;
14+
var index;
15+
16+
if (ordered) {
17+
start = has(node, 'start') ? node.properties.start : 1;
18+
}
19+
20+
content = all(h, node);
21+
length = content.length;
22+
index = -1;
23+
24+
while (++index < length) {
25+
if (content[index].loose === true) {
26+
loose = true;
27+
break;
28+
}
29+
}
30+
31+
return h(node, 'list', {
32+
ordered: ordered,
33+
start: start,
34+
loose: loose
35+
}, content);
36+
}

tests/fixtures/ol/index.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<ol start="2">
2+
<li>Alpha</li>
3+
<li><p>Bravo</p></li>
4+
<li><pre><code class="language-js">charlie();</code></pre></li>
5+
</ol>
6+
7+
<p>Foo.</p>
8+
9+
<ol>
10+
<li>Alpha</li>
11+
<li>Bravo</li>
12+
<li>Charlie</li>
13+
</ol>
14+
15+
<p>Bar.</p>
16+
17+
<ol start="3">
18+
<li><p>Alpha</p></li>
19+
<li><p>Bravo</p></li>
20+
<li><p>Charlie</p></li>
21+
</ol>
22+
23+
<p>Baz.</p>
24+
25+
<ol>
26+
<li></li>
27+
<li><div><p>Something else</p></div></li>
28+
</ol>
29+
30+
<p>Qux.</p>
31+
32+
<ol>
33+
<li><div><p>Something else</p></div></li>
34+
<li><script>foo();</script></li>
35+
</ol>
36+
37+
<p>Quux.</p>
38+
39+
<ol>
40+
<li><div><p>Something else</p></div></li>
41+
<li><p><input type="text"></p></li>
42+
</ol>
43+
44+
<p>Quuux.</p>
45+
46+
<ol>
47+
<li><p><input type="checkbox" disabled checked> Bravo</p></li>
48+
<li><p><input type="checkbox" disabled> Charlie</p></li>
49+
<li class="task-list-item"><p><input type="checkbox" checked> Delta</p></li>
50+
<li><p><input type="checkbox">Echo</p></li>
51+
<li><p><input type="checkbox"><strong>Foxtrot</strong></p></li>
52+
<li><p><input type="checkbox"> <strong>Golf</strong></p></li>
53+
</ol>

tests/fixtures/ol/index.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"fragment": true
3+
}

tests/fixtures/ol/index.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2. Alpha
2+
3. Bravo
3+
4. ```js
4+
charlie();
5+
```
6+
7+
Foo.
8+
9+
1. Alpha
10+
2. Bravo
11+
3. Charlie
12+
13+
Bar.
14+
15+
3. Alpha
16+
4. Bravo
17+
5. Charlie
18+
19+
Baz.
20+
21+
1.··
22+
2. Something else
23+
24+
Qux.
25+
26+
1. Something else
27+
2.··
28+
29+
Quux.
30+
31+
1. Something else
32+
2.··
33+
34+
Quuux.
35+
36+
1. [x] Bravo
37+
2. [ ] Charlie
38+
3. [x] Delta
39+
4. [ ] Echo
40+
5. [ ] **Foxtrot**
41+
6. [ ] **Golf**

tests/fixtures/ul/index.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<ul>
2+
<li>Alpha</li>
3+
<li><p>Bravo</p></li>
4+
<li><pre><code class="language-js">charlie();</code></pre></li>
5+
</ul>
6+
7+
<p>Foo.</p>
8+
9+
<ul>
10+
<li>Alpha</li>
11+
<li>Bravo</li>
12+
<li>Charlie</li>
13+
</ul>
14+
15+
<p>Bar.</p>
16+
17+
<ul>
18+
<li><p>Alpha</p></li>
19+
<li><p>Bravo</p></li>
20+
<li><p>Charlie</p></li>
21+
</ul>
22+
23+
<p>Baz.</p>
24+
25+
<ul>
26+
<li></li>
27+
<li><div><p>Something else</p></div></li>
28+
</ul>
29+
30+
<p>Qux.</p>
31+
32+
<ul>
33+
<li><div><p>Something else</p></div></li>
34+
<li><script>foo();</script></li>
35+
</ul>
36+
37+
<p>Quux.</p>
38+
39+
<ul>
40+
<li><div><p>Something else</p></div></li>
41+
<li><p><input type="text"></p></li>
42+
</ul>
43+
44+
<p>Quuux.</p>
45+
46+
<ul>
47+
<li><p><input type="checkbox" disabled checked> Bravo</p></li>
48+
<li><p><input type="checkbox" disabled> Charlie</p></li>
49+
<li class="task-list-item"><p><input type="checkbox" checked> Delta</p></li>
50+
<li><p><input type="checkbox">Echo</p></li>
51+
<li><p><input type="checkbox"><strong>Foxtrot</strong></p></li>
52+
<li><p><input type="checkbox"> <strong>Golf</strong></p></li>
53+
</ul>

tests/fixtures/ul/index.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"fragment": true
3+
}

tests/fixtures/ul/index.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
- Alpha
2+
- Bravo
3+
- ```js
4+
charlie();
5+
```
6+
7+
Foo.
8+
9+
- Alpha
10+
- Bravo
11+
- Charlie
12+
13+
Bar.
14+
15+
- Alpha
16+
- Bravo
17+
- Charlie
18+
19+
Baz.
20+
21+
-···
22+
- Something else
23+
24+
Qux.
25+
26+
- Something else
27+
-···
28+
29+
Quux.
30+
31+
- Something else
32+
-···
33+
34+
Quuux.
35+
36+
- [x] Bravo
37+
- [ ] Charlie
38+
- [x] Delta
39+
- [ ] Echo
40+
- [ ] **Foxtrot**
41+
- [ ] **Golf**

0 commit comments

Comments
 (0)