diff --git a/apps/www/lib/toc.ts b/apps/www/lib/toc.ts index 6e7de5680c4..02718293466 100644 --- a/apps/www/lib/toc.ts +++ b/apps/www/lib/toc.ts @@ -1,17 +1,17 @@ -// @ts-nocheck -// TODO: I'll fix this later. - import { toc } from "mdast-util-toc" import { remark } from "remark" +import { UnistNode } from "types/unist" import { visit } from "unist-util-visit" const textTypes = ["text", "emphasis", "strong", "inlineCode"] -function flattenNode(node) { - const p = [] - visit(node, (node) => { +function flattenNode(node: UnistNode) { + const p: string[] = [] + visit(node, (node: UnistNode) => { if (!textTypes.includes(node.type)) return - p.push(node.value) + if (node.value) { + p.push(node.value) + } }) return p.join(``) } @@ -26,13 +26,16 @@ interface Items { items?: Item[] } -function getItems(node, current): Items { +function getItems( + node: UnistNode | null | undefined, + current: Partial +): Items { if (!node) { return {} } if (node.type === "paragraph") { - visit(node, (item) => { + visit(node, (item: UnistNode) => { if (item.type === "link") { current.url = item.url current.title = flattenNode(node) @@ -47,13 +50,14 @@ function getItems(node, current): Items { } if (node.type === "list") { - current.items = node.children.map((i) => getItems(i, {})) + current.items = + node.children?.map((i: UnistNode) => getItems(i, {}) as Item) ?? [] return current } else if (node.type === "listItem") { - const heading = getItems(node.children[0], {}) + const heading = getItems(node.children?.[0], {}) - if (node.children.length > 1) { + if (node.children && node.children.length > 1) { getItems(node.children[1], heading) } @@ -63,7 +67,7 @@ function getItems(node, current): Items { return {} } -const getToc = () => (node, file) => { +const getToc = () => (node: any, file: any) => { const table = toc(node) const items = getItems(table.map, {}) diff --git a/apps/www/types/unist.ts b/apps/www/types/unist.ts index 179b447daa5..cfcd556fcad 100644 --- a/apps/www/types/unist.ts +++ b/apps/www/types/unist.ts @@ -5,6 +5,7 @@ export interface UnistNode extends Node { name?: string tagName?: string value?: string + url?: string properties?: { __rawString__?: string __className__?: string