forked from thi-ng/umbrella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoc.ts
25 lines (24 loc) · 739 Bytes
/
toc.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
import { slugifyGH } from "@thi.ng/strings";
import {
PATTERN_NO_TOC,
PATTERN_TOC,
RE_HEADING,
RE_IS_HEADING
} from "./api";
import { link } from "./partials/link";
export const injectTOC = (readme: string) => {
const toc = readme
.split("\n")
.filter(
(line) =>
RE_IS_HEADING.test(line) && line.indexOf(PATTERN_NO_TOC) === -1
)
.map((hd) => {
const [_, level, title] = RE_HEADING.exec(hd)!;
const indent = " ".substr(0, (level.length - 2) * 2);
const href = "#" + slugifyGH(title);
return `${indent}- ${link(title, href)}`;
})
.join("\n");
return readme.replace(PATTERN_TOC, toc);
};