-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
82 lines (73 loc) · 1.71 KB
/
node.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @file: node
* @author: Cuttle Cong
* @date: 2018/1/25
* @description:
*/
var util = require('./lib/util-doc/lib/index')
var slug = require('remark-slug')
function opts(options) {
var settings = options || {};
return Object.assign({
test: '<TABLE_OF_CONTENTS>',
maxDepth: 5,
tight: true
}, settings)
}
exports.markdownTransformer = function (options, gift) {
options = opts(options)
if (!options.test) {
return gift.data
}
return gift.data.replace(new RegExp('^(' + options.test + ')', 'mi'),
new Array(parseInt(options.maxDepth) + 1).join('#') + ' $1'
)
}
exports.remarkTransformer = function (options) {
var settings = opts(options);
var heading = settings.test;
var depth = settings.maxDepth;
var tight = settings.tight;
this.use(slug);
return transformer
/* Adds an example section based on a valid example
* JavaScript document to a `Usage` section. */
function transformer(node) {
var result = util(node, {
heading: heading,
maxDepth: depth,
tight: tight
});
if (!result.map || result.index === -1) {
return;
}
if (!heading) {
result.index = result.endIndex = 0
}
else {
// REMOVE PLACEHOLDER
result.endIndex = result.index
if (result.index > 0) {
result.index--
}
}
/* Replace markdown. */
node.children = [].concat(
node.children.slice(0, result.index),
{
type: 'html',
value: '<div class="picidae-toc">'
},
result.map,
{
type: 'html',
value: '</div>'
},
node.children.slice(result.endIndex)
);
}
// return function (node) {
// var result = search(node, options)
//
// }
}