-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
68 lines (52 loc) · 1.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict'
module.exports = toMdast
var minify = require('rehype-minify-whitespace')
var visit = require('unist-util-visit')
var xtend = require('xtend')
var one = require('./lib/one')
var handlers = require('./lib/handlers')
function toMdast(tree, options) {
var settings = options || {}
var opts = {newlines: settings.newlines === true}
var byId = {}
h.nodeById = byId
h.baseFound = false
h.frozenBaseURL = null
h.handlers = xtend(handlers, settings.handlers || {})
h.augment = augment
h.document = settings.document
visit(tree, onvisit)
return one(h, minify(opts)(tree), null)
function h(node, type, props, children) {
var result
if (
!children &&
(typeof props === 'string' ||
(typeof props === 'object' && 'length' in props))
) {
children = props
props = {}
}
result = xtend({type: type}, props)
if (typeof children === 'string') {
result.value = children
} else if (children) {
result.children = children
}
return augment(node, result)
}
// `right` is the finalized mdast node, created from `left`, a hast node.
function augment(left, right) {
if (left.position) {
right.position = left.position
}
return right
}
function onvisit(node) {
var props = node.properties || {}
var id = props.id
if (id && !(id in node)) {
byId[id] = node
}
}
}