-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoc.js
More file actions
executable file
·33 lines (26 loc) · 795 Bytes
/
toc.js
File metadata and controls
executable file
·33 lines (26 loc) · 795 Bytes
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
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const readme = fs.readFileSync("./README.md").toString().split("\n");
let start;
let end;
const outline = [];
readme.forEach((row, i) => {
if (/toc start/.test(row)) {
start = i + 1;
} else if (/toc end/.test(row)) {
end = i;
} else if (end && /^#/.test(row)) {
const tocRow = row.replace(/^(#+ )(.*)/, (_, hashes, header) => {
const friendlyHeader = header
.toLowerCase()
.replace(/ /g, "-")
.replace(/[^-a-z0-9]/g, "");
const indent = hashes.slice(1).replace(/#/g, " ");
return `${indent}- [${header}](#${friendlyHeader})`;
});
outline.push(tocRow);
}
});
readme.splice(start, end - start, ...outline);
fs.writeFileSync("./README.md", readme.join("\n"));