-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
66 lines (59 loc) · 1.4 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
const heading = require('mdast-util-heading-range')
const toString = require('mdast-util-to-string')
function defaultSummarizer (str) {
return 'Open ' + str
}
function isString (str) {
return typeof str === 'string'
}
function isFunction (fn) {
return typeof fn === 'function'
}
module.exports = function (opts) {
if (opts == null || opts.test == null) throw new Error('options.test must be given')
const summarizer = opts.summary == null
? defaultSummarizer
: isString(opts.summary)
? () => opts.summary
: opts.summary
if (!isFunction(summarizer)) throw new Error('options.summary must be function')
return function (node) {
heading(node, opts.test, function (start, nodes, end) {
return [
start,
{
type: 'paragraph',
children: [
{
type: 'html',
value: '<details>'
},
{
type: 'html',
value: '<summary>'
},
{
type: 'text',
value: summarizer(toString(start))
},
{
type: 'html',
value: '</summary>'
}
]
},
...nodes,
{
type: 'paragraph',
children: [
{
type: 'html',
value: '</details>'
}
]
},
end
]
})
}
}