Skip to content

Commit 1610c2c

Browse files
authored
Merge pull request #13 from syntax-tree/even-more
Even more
2 parents 2cf9aa6 + 52d9d7f commit 1610c2c

34 files changed

+421
-1
lines changed

handlers/comment.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
module.exports = comment;
4+
5+
function comment(h, node) {
6+
return h(node, 'html', '<!--' + node.value + '-->');
7+
}

handlers/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
'use strict';
22

33
exports.root = require('./root');
4-
54
exports.text = require('./text');
5+
exports.comment = require('./comment');
6+
7+
exports.head = exports.math = exports.script = exports.style = exports.svg =
8+
exports.template = exports.title = ignore;
69

10+
exports.ol = exports.ul = require('./list');
11+
exports.li = require('./list-item');
712
exports.strong = exports.b = require('./strong');
813
exports.em = exports.i = require('./emphasis');
914
exports.del = exports.s = exports.strike = require('./delete');
@@ -19,3 +24,5 @@ exports.h1 = exports.h2 = exports.h3 =
1924
exports.h4 = exports.h5 = exports.h6 = require('./heading');
2025

2126
exports.a = require('./link');
27+
28+
function ignore() {}

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/comment/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!-- foo -->
2+
3+
<p>Alpha.</p>
4+
5+
<!-- -->
6+
7+
<p>Bravo.</p>
8+
9+
<!--
10+
charlie
11+
delta echo
12+
-->
13+
14+
<p>Foxtrot.</p>

tests/fixtures/comment/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/comment/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!-- foo -->
2+
3+
Alpha.
4+
5+
<!-- -->
6+
7+
Bravo.
8+
9+
<!--
10+
charlie
11+
delta echo
12+
-->
13+
14+
Foxtrot.

tests/fixtures/doctype/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!doctype html>
2+
<p>Alpha</p>

tests/fixtures/doctype/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Alpha

tests/fixtures/head/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<!doctype html>
2+
<head><noscript>Alpha</noscript></head>
3+
<p>Bravo</p>

0 commit comments

Comments
 (0)