forked from cmaas/markdown-it-table-of-contents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
276 lines (243 loc) · 8.46 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//@ts-check
'use strict';
/*
* markdown-it-table-of-contents
*
* The algorithm works as follows:
* Step 1: Gather all headline tokens from a Markdown document and put them in an array.
* Step 2: Turn the flat array into a nested tree, respecting the correct headline level.
* Step 3: Turn the nested tree into HTML code.
*/
const slugify = function (s) {
return encodeURIComponent(String(s).trim().toLowerCase().replace(/\s+/g, '-'));
};
const defaults = {
includeLevel: [1, 2],
containerClass: 'table-of-contents',
slugify: slugify,
markerPattern: /^\[\[toc\]\]/im,
listType: 'ul',
format: function (content, md) {
return md.renderInline(content);
},
forceFullToc: false,
containerHeaderHtml: undefined,
containerFooterHtml: undefined,
transformLink: undefined,
};
/**
* @typedef {Object} HeadlineItem
* @property {number} level Headline level
* @property {string} anchor Anchor target
* @property {string} text Text of headline
*/
/**
* @typedef {Object} TocItem
* @property {number} level Item level
* @property {string} text Text of link
* @property {string} anchor Target of link
* @property {Array<TocItem>} children Sub-items for this list item
* @property {TocItem} parent Parent this item belongs to
*/
/**
* Finds all headline items for the defined levels in a Markdown document.
* @param {Array<number>} levels includeLevels like `[1, 2, 3]`
* @param {*} tokens Tokens gathered by the plugin
* @param {*} options Plugin options
* @returns {Array<HeadlineItem>}
*/
function findHeadlineElements(levels, tokens, options) {
const headings = [];
let currentHeading = null;
tokens.forEach(token => {
if (token.type === 'heading_open') {
const id = findExistingIdAttr(token);
const level = parseInt(token.tag.toLowerCase().replace('h', ''), 10);
if (levels.indexOf(level) >= 0) {
currentHeading = {
level: level,
text: null,
anchor: id || null
};
}
}
else if (currentHeading && token.type === 'inline') {
const textContent = token.children
.filter((childToken) => childToken.type === 'text' || childToken.type === 'code_inline')
.reduce((acc, t) => acc + t.content, '');
currentHeading.text = textContent;
if (! currentHeading.anchor) {
currentHeading.anchor = options.slugify(textContent, token.content);
}
}
else if (token.type === 'heading_close') {
if (currentHeading) {
headings.push(currentHeading);
}
currentHeading = null;
}
});
return headings;
}
/**
* Helper to find an existing id attr on a token. Should be a heading_open token, but could be anything really
* Provided by markdown-it-anchor or markdown-it-attrs
* @param {any} token Token
* @returns {string} Id attribute to use as anchor
*/
function findExistingIdAttr(token) {
if (token && token.attrs && token.attrs.length > 0) {
const idAttr = token.attrs.find( (attr) => {
if (Array.isArray(attr) && attr.length >= 2) {
return attr[0] === 'id';
}
return false;
});
if (idAttr && Array.isArray(idAttr) && idAttr.length >= 2) {
const [key, val] = idAttr;
return val;
}
}
return null;
}
/**
* Helper to get minimum headline level so that the TOC is nested correctly
* @param {Array<HeadlineItem>} headlineItems Search these
* @returns {number} Minimum level
*/
function getMinLevel(headlineItems) {
return Math.min(...headlineItems.map(item => item.level));
}
/**
* Helper that creates a TOCItem
* @param {number} level
* @param {string} text
* @param {string} anchor
* @param {TocItem} rootNode
* @returns {TocItem}
*/
function addListItem(level, text, anchor, rootNode) {
const listItem = { level, text, anchor, children: [], parent: rootNode };
rootNode.children.push(listItem);
return listItem;
}
/**
* Turns a list of flat headline items into a nested tree object representing the TOC
* @param {Array<HeadlineItem>} headlineItems
* @returns {TocItem} Tree of TOC items
*/
function flatHeadlineItemsToNestedTree(headlineItems) {
// create a root node with no text that holds the entire TOC. this won't be rendered, but only its children
const toc = { level: getMinLevel(headlineItems) - 1, anchor: null, text: null, children: [], parent: null };
// pointer that tracks the last root item of the current list
let currentRootNode = toc;
// pointer that tracks the last item (to turn it into a new root node if necessary)
let prevListItem = currentRootNode;
headlineItems.forEach(headlineItem => {
// if level is bigger, take the previous node, add a child list, set current list to this new child list
if (headlineItem.level > prevListItem.level) {
// eslint-disable-next-line no-unused-vars
Array.from({ length: headlineItem.level - prevListItem.level }).forEach(_ => {
currentRootNode = prevListItem;
prevListItem = addListItem(headlineItem.level, null, null, currentRootNode);
});
prevListItem.text = headlineItem.text;
prevListItem.anchor = headlineItem.anchor;
}
// if level is same, add to the current list
else if (headlineItem.level === prevListItem.level) {
prevListItem = addListItem(headlineItem.level, headlineItem.text, headlineItem.anchor, currentRootNode);
}
// if level is smaller, set current list to currentlist.parent
else if (headlineItem.level < prevListItem.level) {
for (let i = 0; i < prevListItem.level - headlineItem.level; i++) {
currentRootNode = currentRootNode.parent;
}
prevListItem = addListItem(headlineItem.level, headlineItem.text, headlineItem.anchor, currentRootNode);
}
});
return toc;
}
/**
* Recursively turns a nested tree of tocItems to HTML.
* @param {TocItem} tocItem
* @returns {string}
*/
function tocItemToHtml(tocItem, options, md) {
return '<' + options.listType + '>' + tocItem.children.map(childItem => {
let li = '<li>';
let anchor = childItem.anchor;
if (options && options.transformLink) {
anchor = options.transformLink(anchor);
}
let text = childItem.text ? options.format(childItem.text, md, anchor) : null;
li += anchor ? `<a href="#${anchor}">${text}</a>` : (text || '');
return li + (childItem.children.length > 0 ? tocItemToHtml(childItem, options, md) : '') + '</li>';
}).join('') + '</' + options.listType + '>';
}
module.exports = function (md, o) {
const options = Object.assign({}, defaults, o);
const tocRegexp = options.markerPattern;
let gstate;
function toc(state, silent) {
let token;
let match;
// Reject if the token does not start with [
if (state.src.charCodeAt(state.pos) !== 0x5B /* [ */) {
return false;
}
// Don't run any pairs in validation mode
if (silent) {
return false;
}
// Detect TOC markdown
match = tocRegexp.exec(state.src.substr(state.pos));
match = !match ? [] : match.filter(function (m) { return m; });
if (match.length < 1) {
return false;
}
// Build content
token = state.push('toc_open', 'toc', 1);
token.markup = '[[toc]]';
token = state.push('toc_body', '', 0);
token = state.push('toc_close', 'toc', -1);
// Update pos so the parser can continue
var newline = state.src.indexOf('\n', state.pos);
if (newline !== -1) {
state.pos = newline;
} else {
state.pos = state.pos + state.posMax + 1;
}
return true;
}
md.renderer.rules.toc_open = function (tokens, index) {
var tocOpenHtml = '<div class="' + options.containerClass + '">';
if (options.containerHeaderHtml) {
tocOpenHtml += options.containerHeaderHtml;
}
return tocOpenHtml;
};
md.renderer.rules.toc_close = function (tokens, index) {
var tocFooterHtml = '';
if (options.containerFooterHtml) {
tocFooterHtml = options.containerFooterHtml;
}
return tocFooterHtml + '</div>';
};
md.renderer.rules.toc_body = function (tokens, index) {
if (options.forceFullToc) {
throw ("forceFullToc was removed in version 0.5.0. For more information, see https://github.com/Oktavilla/markdown-it-table-of-contents/pull/41");
} else {
const headlineItems = findHeadlineElements(options.includeLevel, gstate.tokens, options);
const toc = flatHeadlineItemsToNestedTree(headlineItems);
const html = tocItemToHtml(toc, options, md);
return html;
}
};
// Catch all the tokens for iteration later
md.core.ruler.push('grab_state', function (state) {
gstate = state;
});
// Insert TOC
md.inline.ruler.after('emphasis', 'toc', toc);
};