feat: implement table of contents

This commit is contained in:
Jean-Philippe Sirois
2020-05-12 00:05:17 -04:00
parent 465d026392
commit b17527cd6c
12 changed files with 194 additions and 14 deletions
+20 -2
View File
@@ -1,9 +1,11 @@
import kebabCase from "lodash/kebabCase"
import chain from "ramda/es/chain"
import partition from "ramda/es/partition"
import pipe from "ramda/es/pipe"
import sort from "ramda/es/sort"
import { sort } from "ramda"
import { IFile, IFileOrFolder, IFolder } from "../types"
import { IFile, IFileOrFolder, IFolder, ITocItem } from "../types"
import { MarkdownRemark } from "../../generated/graphql"
function specificWordsToUpper(str: string): string {
const wordsToUpper = ["pdo", "c"]
@@ -139,3 +141,19 @@ export function join([head, ...tail]: IFileOrFolder[]): IFileOrFolder[] {
return [current, ...join(remaining)]
}
export function buildToc(headings: MarkdownRemark["headings"]): ITocItem[] {
if (!headings) {
return []
}
return headings
.filter(h => h?.depth === 2)
.map(h => {
return {
depth: h!.depth!,
link: `#${kebabCase(h!.value!)}`,
title: h!.value!,
}
})
}