-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmake-table-of-contents.ts
36 lines (27 loc) · 1.06 KB
/
make-table-of-contents.ts
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
// A quick tool to generate the table of contents in the README
// Usage: 'esrun make-table-of-contents.ts'
import { exec as oldExec } from "node:child_process";
import { promisify } from "node:util";
const exec = promisify(oldExec);
const log = console.log.bind(console);
// Get all the headings from the README
const command = `git grep -h '^### ' README.md`;
// @ts-ignore - not adding "type": "module" to package.json as it breaks imports
// in the rest of the code.
const { stdout } = await exec(command);
const headings = stdout.split("\n");
const headingToLink = (markdownHeading: string) => {
// A markdown heading like:
// ### Get the logs for a transaction
// becomes a link like:
// #get-the-logs-for-a-transaction
const heading = markdownHeading.slice(4);
// replace all the spaces with a dash
// and remove any commas
const link = heading.toLowerCase().replaceAll(/ /g, "-").replaceAll(/,/g, "");
return `[${heading}](#${link})`;
};
const links = headings.map((heading) => {
return headingToLink(heading);
});
log(links.join("\n\n"));