Skip to content

Commit c6d9454

Browse files
authored
Merge pull request #2 from syntax-tree/moar-tags
Moar tags
2 parents 780e53f + b28f105 commit c6d9454

35 files changed

+137
-78
lines changed

handlers/emphasis.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = emphasis
2+
3+
var u = require('unist-builder')
4+
var all = require('../all')
5+
6+
function emphasis (h, node) {
7+
return h(node, 'emphasis', all(h, node))
8+
}

handlers/heading.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
module.exports = function (depth) {
2-
return heading(depth)
3-
}
1+
module.exports = heading
42

53
var u = require('unist-builder')
64
var all = require('../all')
75

8-
function heading (depth) {
9-
return function (h, node) {
10-
return h(node, 'heading', { depth: depth }, all(h, node))
11-
}
6+
function heading (h, node) {
7+
var depth = Number(node.tagName.charAt(1)) || 1;
8+
return h(node, 'heading', {depth: depth}, all(h, node))
129
}

handlers/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
module.exports = {
2-
heading: require('./heading'),
3-
text: require('./text'),
4-
root: require('./root'),
5-
strong: require('./strong'),
6-
p: require('./p')
7-
}
1+
exports.root = require('./root')
2+
3+
exports.text = require('./text')
4+
5+
exports.strong = exports.b = require('./strong')
6+
exports.em = exports.i = require('./emphasis')
7+
exports.code = exports.kbd = exports.samp = exports.var = require('./inline-code')
8+
9+
exports.p = require('./paragraph')
10+
exports.h1 = exports.h2 = exports.h3 =
11+
exports.h4 = exports.h5 = exports.h6 = require('./heading')

handlers/inline-code.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = inlineCode
2+
3+
var toString = require('hast-util-to-string');
4+
var u = require('unist-builder')
5+
var all = require('../all')
6+
7+
function inlineCode (h, node) {
8+
return h(node, 'inlineCode', toString(node))
9+
}
File renamed without changes.

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@ function factory (tree, options) {
1717
return h
1818

1919
function h (node, type, props, children) {
20-
if (children === null &&typeof props === 'object' && props.length) {
20+
if (!children && ((typeof props === 'object' && 'length' in props) || typeof props === 'string')) {
2121
children = props
2222
props = {}
2323
}
2424

25-
var result = augment(node, {
26-
type: type,
27-
children: children
28-
})
25+
var result = augment(node, {type: type})
26+
27+
if (typeof children === 'string') {
28+
result.value = children;
29+
} else if (children) {
30+
result.children = children;
31+
}
2932

3033
return xtend(result, props)
3134
}
3235

33-
/* `right` is the finalized MDAST node,
34-
created from `left`, an HAST node */
36+
/* `right` is the finalized MDAST node,
37+
created from `left`, a HAST node */
3538
function augment (left, right) {
3639
var data
3740
var ctx

one.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,9 @@ var handlers = require('./handlers')
88

99
function one (h, node, parent) {
1010
var fn = null
11-
12-
var tagName = node && node.tagName && node.tagName !== undefined
13-
? node.tagName
14-
: null
15-
16-
if (tagName && has(handlers, tagName)) {
17-
fn = has(handlers, tagName) ? handlers[tagName] : null
18-
} else if (tagName) {
19-
var matchHeader = tagName.match(/h([1-6?])/g)
20-
21-
if (matchHeader) {
22-
var depth = tagName.split('h')[1]
23-
fn = handlers.heading(depth)
24-
}
25-
} else if (node.type && has(handlers, node.type)) {
11+
if (node.type === 'element' && has(handlers, node.tagName)) {
12+
fn = handlers[node.tagName]
13+
} else if (has(handlers, node.type)) {
2614
fn = handlers[node.type]
2715
}
2816

@@ -35,5 +23,5 @@ function unknown (h, node) {
3523
return h.augment(node, u('text', node.value))
3624
}
3725

38-
return h(node, all(h, node))
26+
return all(h, node)
3927
}

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,21 @@
2626
"homepage": "https://github.com/syntax-tree/hast-util-to-mdast#readme",
2727
"dependencies": {
2828
"has": "^1.0.1",
29+
"hast-util-to-string": "^1.0.0",
2930
"trim": "0.0.1",
3031
"trim-lines": "^1.1.0",
3132
"unist-builder": "^1.0.2",
3233
"xtend": "^4.0.1"
3334
},
3435
"devDependencies": {
36+
"is-hidden": "^1.1.0",
37+
"mdast-util-assert": "^1.0.0",
38+
"negate": "^1.0.0",
3539
"rehype-parse": "^2.0.1",
40+
"remark-parse": "^2.2.0",
3641
"remark-stringify": "^2.3.0",
3742
"tape": "^4.6.3",
38-
"unified": "^5.1.0"
43+
"unified": "^5.1.0",
44+
"unist-util-remove-position": "^1.1.0"
3945
}
4046
}

tests/fixtures/b/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p><b>Hello World.</b></p>

tests/fixtures/b/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+
}

0 commit comments

Comments
 (0)