Skip to content

Commit b3abd1f

Browse files
committed
toc is working
1 parent a7009f3 commit b3abd1f

File tree

15 files changed

+4755
-7483
lines changed

15 files changed

+4755
-7483
lines changed

app/routes/MdxRoute.res

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ module Sidebar = SidebarLayout.Sidebar
66
module NavItem = Sidebar.NavItem
77
module Category = Sidebar.Category
88

9-
type loaderData = {...Mdx.t, categories: array<SidebarLayout.Sidebar.Category.t>}
9+
type loaderData = {
10+
...Mdx.t,
11+
categories: array<SidebarLayout.Sidebar.Category.t>,
12+
entries: array<TableOfContents.entry>,
13+
}
1014

1115
/**
1216
This configures the MDX component to use our custom markdown components
@@ -45,18 +49,38 @@ let components = {
4549

4650
let loader: Loader.t<loaderData> = async ({request}) => {
4751
let mdx = await loadMdx(request)
48-
Console.log(
49-
(await loadAllMdx())->Array.filter(page =>
50-
(page.path :> string)->String.includes("docs/manual")
51-
),
52-
)
52+
53+
let fileContents = await (await loadAllMdx(~filterByPaths=["docs"]))
54+
->Array.filter(mdx => (mdx.path :> string)->String.includes("docs/manual/introduction"))
55+
->Array.get(0)
56+
->Option.map(mdx => mdx.path)
57+
->Option.map(path => Node.Fs.readFile((path :> string), "utf-8"))
58+
->Option.getOrThrow
59+
60+
let markdownTree = Mdast.fromMarkdown(fileContents)
61+
let tocResult = Mdast.toc(markdownTree, {maxDepth: 2})
62+
63+
let headers = Js.Dict.empty()
64+
65+
Mdast.reduceHeaders(tocResult.map, headers)
66+
67+
let entries =
68+
headers
69+
->Dict.toArray
70+
->Array.map(((header, url)): TableOfContents.entry => {
71+
header,
72+
href: (url :> string),
73+
})
74+
->Array.slice(~start=2) // skip first two entries which are "Introduction" and "Getting Started"
75+
5376
let res: loaderData = {
5477
__raw: mdx.__raw,
5578
attributes: mdx.attributes,
79+
entries,
5680
categories: [
5781
{
5882
name: "overview",
59-
items: [{name: "Introduction", href: #"/docs/manual/installation"}],
83+
items: [{name: "Introduction", href: #"/docs/manual/introduction"}],
6084
},
6185
],
6286
}
@@ -68,23 +92,17 @@ let default = () => {
6892
let component = useMdxComponent(~components)
6993
let attributes = useMdxAttributes()
7094

71-
let {categories} = useLoaderData()
95+
let {categories, entries} = useLoaderData()
96+
97+
Console.log(entries)
7298

7399
let metaTitleCategory =
74100
(pathname :> string)->String.includes("docs/manual")
75101
? "ReScript Language Manual"
76102
: "Some other page"
77-
// TODO directly use layout and pass props
78-
79-
// Console.log(attributes)
80-
// <ManualDocsLayout.V1200Layout
81-
// metaTitleCategory="ReScript Language Manual"
82-
// version="latest"
83-
// availableVersions=Constants.allManualVersions
84-
// nextVersion=?Constants.nextVersion>
85-
// // {React.string(attributes.title)} </h1>
103+
86104
<div>
87-
<DocsLayout metaTitleCategory categories>
105+
<DocsLayout metaTitleCategory categories activeToc={title: "Introduction", entries}>
88106
<div className="markdown-body"> {component()} </div>
89107
</DocsLayout>
90108
</div>

0 commit comments

Comments
 (0)