-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
352 lines (306 loc) · 11.4 KB
/
app.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
'use strict';
const app = (mainCallback) => {
const MARKDOWN_LINK_MATCH_REGEXP = /\[([^\]]+)\]\([^\)]+\)/g,
MARKDOWN_INLINE_CODE_START_END_REGEXP = /^`[^`]+`$/,
CODE_BLOCK_INDENT_REGEXP = /^ {4,}|\t/,
CODE_BLOCK_FENCED_REGEXP = /^``` *([a-z]+)?$/,
HEADER_HASH_REGEXP = /^(#{1,6})( +.+)$/,
HEADER_UNDERLINE_REGEXP = /^(=+|-+)$/;
function $(id) {
return document.getElementById(id);
}
function markdownStripMeta(text) {
// attempt to strip out [links](#url) segments, leaving just [links]
// note: very basic, won't handle repeated [] or () segments in link values well
text = text.replace(
MARKDOWN_LINK_MATCH_REGEXP,
(match,linkLabel) => linkLabel
);
// if text starts and ends with a *single set* of inline code backticks, remove them
if (MARKDOWN_INLINE_CODE_START_END_REGEXP.test(text)) {
return text.slice(1,-1);
}
return text;
}
function headerListFromMarkdown(markdown) {
const markdownLineList = markdown.trim().split(/\r?\n/),
headerList = [];
let codeBlockFenceActive = false,
lineItemPrevious;
function addItem(level,text) {
headerList.push({
level: level,
text: text
});
lineItemPrevious = undefined;
}
// work over each markdown line
for (const lineItem of markdownLineList) {
// indented code block line? if so, skip.
if (CODE_BLOCK_INDENT_REGEXP.test(lineItem)) {
continue;
}
const lineItemTrim = lineItem.trim();
// fenced code block start/end?
if (CODE_BLOCK_FENCED_REGEXP.test(lineItemTrim)) {
codeBlockFenceActive = !codeBlockFenceActive;
continue;
}
if (codeBlockFenceActive) {
// skip all lines within a fenced code block
continue;
}
// line match hash header style?
const headerHashMatch = HEADER_HASH_REGEXP.exec(lineItemTrim);
if (headerHashMatch) {
addItem(
headerHashMatch[1].length, // heading level
markdownStripMeta(headerHashMatch[2].trim())
);
continue;
}
// line match underline header style?
if (
lineItemPrevious &&
HEADER_UNDERLINE_REGEXP.test(lineItemTrim)
) {
addItem(
// '=' = level 1 header, '-' = level 2
(lineItemTrim[0] == '=') ? 1 : 2,
markdownStripMeta(lineItemPrevious)
);
continue;
}
lineItemPrevious = lineItemTrim;
}
return headerList;
}
function indentWith(style) {
// tab mode
if (style == 'tab') {
return '\t';
}
// spaces mode
const match = /^space-([0-9])$/.exec(style);
return ' '.repeat((match) ? match[1] : 1);
}
function buildPageAnchor(text) {
return stripPunctuation(text)
.replace(/ /g,'-')
.toLowerCase();
}
function buildTOCMarkdown(headerList,indentWith,skipFirst) {
const pageAnchorSeenCollection = {};
let currentHeaderLevel = -1,
currentIndent = -1,
markdownTOC = '';
for (const headerItem of headerList) {
// skip the first heading found?
if (skipFirst) {
skipFirst = false;
continue;
}
// raise/lower indent level for next TOC item
const headerLevel = headerItem.level;
if (headerLevel > currentHeaderLevel) {
currentIndent++;
} else if (headerLevel < currentHeaderLevel) {
currentIndent -= (currentHeaderLevel - headerLevel);
currentIndent = Math.max(currentIndent,0);
}
currentHeaderLevel = headerLevel;
let pageAnchor = buildPageAnchor(headerItem.text);
if (pageAnchorSeenCollection[pageAnchor] === undefined) {
// new page anchor
pageAnchorSeenCollection[pageAnchor] = 1;
} else {
// add increment to an already seen pageAnchor name
pageAnchor = `${pageAnchor}-${pageAnchorSeenCollection[pageAnchor]++}`;
}
// build TOC line
markdownTOC += (
indentWith.repeat(currentIndent) +
`- [${headerItem.text}](#${pageAnchor})\n`
);
}
return markdownTOC;
}
function copyFormElementToClipboard(el) {
// select element, copy content to clipboard then un-focus/select
el.select();
document.execCommand('copy');
window.getSelection().removeAllRanges();
el.blur();
}
function stripPunctuation(text) {
let result = '';
for (const char of text) {
if ((char == '-') || (char == '_') || !isPunctuation(char.charCodeAt())) {
result += char;
}
}
return result;
}
function isPunctuation(charCode) {
return (
// simple characters
((charCode >= 33) && (charCode <= 47)) ||
((charCode >= 58) && (charCode <= 64)) ||
((charCode >= 91) && (charCode <= 96)) ||
((charCode >= 123) && (charCode <= 126)) ||
// extended characters
(charCode == 161) || (charCode == 167) || (charCode == 171) || (charCode == 182) ||
(charCode == 183) || (charCode == 187) || (charCode == 191) || (charCode == 894) ||
(charCode == 903) ||
((charCode >= 1370) && (charCode <= 1375)) ||
(charCode == 1417) || (charCode == 1418) || (charCode == 1470) || (charCode == 1472) ||
(charCode == 1475) || (charCode == 1478) || (charCode == 1523) || (charCode == 1524) ||
(charCode == 1545) || (charCode == 1546) || (charCode == 1548) || (charCode == 1549) ||
(charCode == 1563) || (charCode == 1566) || (charCode == 1567) ||
((charCode >= 1642) && (charCode <= 1645)) ||
(charCode == 1748) ||
((charCode >= 1792) && (charCode <= 1805)) ||
((charCode >= 2039) && (charCode <= 2041)) ||
((charCode >= 2096) && (charCode <= 2110)) ||
(charCode == 2142) || (charCode == 2404) || (charCode == 2405) || (charCode == 2416) ||
(charCode == 2800) || (charCode == 3572) || (charCode == 3663) || (charCode == 3674) ||
(charCode == 3675) ||
((charCode >= 3844) && (charCode <= 3858)) ||
(charCode == 3860) ||
((charCode >= 3898) && (charCode <= 3901)) ||
(charCode == 3973) ||
((charCode >= 4048) && (charCode <= 4052)) ||
(charCode == 4057) || (charCode == 4058) ||
((charCode >= 4170) && (charCode <= 4175)) ||
(charCode == 4347) ||
((charCode >= 4960) && (charCode <= 4968)) ||
(charCode == 5120) || (charCode == 5741) || (charCode == 5742) || (charCode == 5787) ||
(charCode == 5788) ||
((charCode >= 5867) && (charCode <= 5869)) ||
(charCode == 5941) || (charCode == 5942) ||
((charCode >= 6100) && (charCode <= 6102)) ||
((charCode >= 6104) && (charCode <= 6106)) ||
((charCode >= 6144) && (charCode <= 6154)) ||
(charCode == 6468) || (charCode == 6469) || (charCode == 6686) || (charCode == 6687) ||
((charCode >= 6816) && (charCode <= 6822)) ||
((charCode >= 6824) && (charCode <= 6829)) ||
((charCode >= 7002) && (charCode <= 7008)) ||
((charCode >= 7164) && (charCode <= 7167)) ||
((charCode >= 7227) && (charCode <= 7231)) ||
(charCode == 7294) || (charCode == 7295) ||
((charCode >= 7360) && (charCode <= 7367)) ||
(charCode == 7379) ||
((charCode >= 8208) && (charCode <= 8231)) ||
((charCode >= 8240) && (charCode <= 8259)) ||
((charCode >= 8261) && (charCode <= 8273)) ||
((charCode >= 8275) && (charCode <= 8286)) ||
(charCode == 8317) || (charCode == 8318) || (charCode == 8333) || (charCode == 8334) ||
((charCode >= 8968) && (charCode <= 8971)) ||
(charCode == 9001) || (charCode == 9002) ||
((charCode >= 10088) && (charCode <= 10101)) ||
(charCode == 10181) || (charCode == 10182) ||
((charCode >= 10214) && (charCode <= 10223)) ||
((charCode >= 10627) && (charCode <= 10648)) ||
((charCode >= 10712) && (charCode <= 10715)) ||
(charCode == 10748) || (charCode == 10749) ||
((charCode >= 11513) && (charCode <= 11516)) ||
(charCode == 11518) || (charCode == 11519) || (charCode == 11632) ||
((charCode >= 11776) && (charCode <= 11822)) ||
((charCode >= 11824) && (charCode <= 11842)) ||
((charCode >= 12289) && (charCode <= 12291)) ||
((charCode >= 12296) && (charCode <= 12305)) ||
((charCode >= 12308) && (charCode <= 12319)) ||
(charCode == 12336) || (charCode == 12349) || (charCode == 12448) || (charCode == 12539) ||
(charCode == 42238) || (charCode == 42239) ||
((charCode >= 42509) && (charCode <= 42511)) ||
(charCode == 42611) || (charCode == 42622) ||
((charCode >= 42738) && (charCode <= 42743)) ||
((charCode >= 43124) && (charCode <= 43127)) ||
(charCode == 43214) || (charCode == 43215) ||
((charCode >= 43256) && (charCode <= 43258)) ||
(charCode == 43310) || (charCode == 43311) ||
(charCode == 43359) ||
((charCode >= 43457) && (charCode <= 43469)) ||
(charCode == 43486) || (charCode == 43487) ||
((charCode >= 43612) && (charCode <= 43615)) ||
(charCode == 43742) || (charCode == 43743) || (charCode == 43760) || (charCode == 43761) ||
(charCode == 44011) || (charCode == 64830) || (charCode == 64831) ||
((charCode >= 65040) && (charCode <= 65049)) ||
((charCode >= 65072) && (charCode <= 65106)) ||
((charCode >= 65108) && (charCode <= 65121)) ||
(charCode == 65123) || (charCode == 65128) || (charCode == 65130) || (charCode == 65131) ||
((charCode >= 65281) && (charCode <= 65283)) ||
((charCode >= 65285) && (charCode <= 65290)) ||
((charCode >= 65292) && (charCode <= 65295)) ||
(charCode == 65306) || (charCode == 65307) || (charCode == 65311) || (charCode == 65312) ||
((charCode >= 65339) && (charCode <= 65341)) ||
(charCode == 65343) || (charCode == 65371) || (charCode == 65373) ||
((charCode >= 65375) && (charCode <= 65381)) ||
((charCode >= 65792) && (charCode <= 65794)) ||
(charCode == 66463) || (charCode == 66512) || (charCode == 66927) || (charCode == 67671) ||
(charCode == 67871) || (charCode == 67903) ||
((charCode >= 68176) && (charCode <= 68184)) ||
(charCode == 68223) ||
((charCode >= 68336) && (charCode <= 68342)) ||
((charCode >= 68409) && (charCode <= 68415)) ||
((charCode >= 68505) && (charCode <= 68508)) ||
((charCode >= 69703) && (charCode <= 69709)) ||
(charCode == 69819) || (charCode == 69820) ||
((charCode >= 69822) && (charCode <= 69825)) ||
((charCode >= 69952) && (charCode <= 69955)) ||
(charCode == 70004) || (charCode == 70005) ||
((charCode >= 70085) && (charCode <= 70088)) ||
(charCode == 70093) ||
((charCode >= 70200) && (charCode <= 70205)) ||
(charCode == 70854) ||
((charCode >= 71105) && (charCode <= 71113)) ||
((charCode >= 71233) && (charCode <= 71235)) ||
((charCode >= 74864) && (charCode <= 74868)) ||
(charCode == 92782) || (charCode == 92783) || (charCode == 92917) ||
((charCode >= 92983) && (charCode <= 92987)) ||
(charCode == 92996) || (charCode == 113823)
);
}
function init() {
const tableOfContentsEl = $('table-of-contents');
// determine if clipboard is available to browser
if (
document.queryCommandSupported &&
document.queryCommandSupported('copy')
) {
const copyClipboardEl = $('copy-clipboard');
// display copy to clipboard button, add click handler
copyClipboardEl.parentNode.classList.remove('hide');
copyClipboardEl.addEventListener(
'click',
copyFormElementToClipboard.bind(null,tableOfContentsEl)
);
}
// add click handler to 'Generate' button
$('generate').addEventListener('click',() => {
tableOfContentsEl.value = buildTOCMarkdown(
headerListFromMarkdown($('markdown-source').value),
indentWith($('indent-style').value),
$('skip-first-heading').checked
);
});
}
(mainCallback || (() => {
if (document.readyState == 'loading') {
document.addEventListener('DOMContentLoaded',init);
return;
}
// DOM already ready
init();
}))({
stripPunctuation: stripPunctuation
});
};
// export app() for Node.js
if ((typeof module != 'undefined') && module.exports) {
module.exports = app;
}
// test if we are a browser
if (typeof window != 'undefined') {
app();
}