-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
usr_toc.js
109 lines (82 loc) 路 1.96 KB
/
usr_toc.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* blocks/usr_toc.js
*/
const { getLinkToTag } = require('../parser/inline.js');
const { wrapHTML } = require('../helpers.js');
const { toKebabCase } = require('../helpers.js');
const { isEmpty } = require('./helpers.js');
const { isSeparator } = require('./helpers.js');
const RE_SUB_HEADER = /^\|usr_(\d{2}).txt\|/;
const RE_TOC_ITEM_NB = /^\t{2}\|(\d{2}\.\d{1,2})\|\t/;
module.exports = {
/**
* Document
*/
document: {
start: () => true,
end: () => false,
containedBlocks: [
'toc',
'sectionHeader',
'subSectionHeader',
'paragraph',
],
wrapper: lines => lines.join('')
},
/**
* Section header
*/
sectionHeader: {
start: ct => isSeparator(ct.previousLine),
end: () => true,
containedBlocks: [],
disableInlineParsing: true,
transformLines: line => line.replace(' ~', ''),
wrapper: 'h2'
},
/**
* Sub-section header
*/
subSectionHeader: {
start: ct => RE_SUB_HEADER.test(ct.line),
end: () => true,
containedBlocks: [],
disableInlineParsing: true,
wrapper(lines) {
const header = lines[0].split(' ')[1];
const number = lines[0].match(RE_SUB_HEADER)[1];
return wrapHTML(`${number}. ` + wrapHTML(header, 'a', { href: `/${number}-${toKebabCase(header)}` }), 'h3');
}
},
/**
* Table of contents
*/
toc: {
start: ct => ct.line.startsWith('\t\t'),
end: ct => isEmpty(ct.nextLine),
containedBlocks: [
'tocItem',
],
wrapper: ['ol', { class: 'table-of-contents' }]
},
/**
* Table of contents item
*/
tocItem: {
start: () => true,
end: () => true,
containedBlocks: [],
disableInlineParsing: true,
wrapper: lines => wrapHTML(wrapHTML(lines[0].replace(RE_TOC_ITEM_NB, ''), 'a', { href: getLinkToTag('usr_toc', lines[0].match(RE_TOC_ITEM_NB)[1]) }), 'li')
},
/**
* Paragraph
*/
paragraph: {
start: ct => !isEmpty(ct.line) && !isSeparator(ct.line),
end: () => true,
containedBlocks: [],
disableInlineParsing: true,
wrapper: 'p'
},
};