Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions apps/www/lib/toc.ts
Original file line number Diff line number Diff line change
@@ -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(``)
}
Expand All @@ -26,13 +26,16 @@ interface Items {
items?: Item[]
}

function getItems(node, current): Items {
function getItems(
node: UnistNode | null | undefined,
current: Partial<Item>
): 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)
Expand All @@ -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)
}

Expand All @@ -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, {})

Expand Down
1 change: 1 addition & 0 deletions apps/www/types/unist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface UnistNode extends Node {
name?: string
tagName?: string
value?: string
url?: string
properties?: {
__rawString__?: string
__className__?: string
Expand Down