@@ -6,7 +6,11 @@ module Sidebar = SidebarLayout.Sidebar
6
6
module NavItem = Sidebar .NavItem
7
7
module Category = Sidebar .Category
8
8
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
+ }
10
14
11
15
/**
12
16
This configures the MDX component to use our custom markdown components
@@ -45,18 +49,38 @@ let components = {
45
49
46
50
let loader : Loader .t <loaderData > = async ({request }) => {
47
51
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
+
53
76
let res : loaderData = {
54
77
__raw : mdx .__raw ,
55
78
attributes : mdx .attributes ,
79
+ entries ,
56
80
categories : [
57
81
{
58
82
name : "overview" ,
59
- items : [{name : "Introduction" , href : # "/docs/manual/installation " }],
83
+ items : [{name : "Introduction" , href : # "/docs/manual/introduction " }],
60
84
},
61
85
],
62
86
}
@@ -68,23 +92,17 @@ let default = () => {
68
92
let component = useMdxComponent (~components )
69
93
let attributes = useMdxAttributes ()
70
94
71
- let {categories } = useLoaderData ()
95
+ let {categories , entries } = useLoaderData ()
96
+
97
+ Console .log (entries )
72
98
73
99
let metaTitleCategory =
74
100
(pathname :> string )-> String .includes ("docs/manual" )
75
101
? "ReScript Language Manual"
76
102
: "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
+
86
104
<div >
87
- <DocsLayout metaTitleCategory categories >
105
+ <DocsLayout metaTitleCategory categories activeToc = { title : "Introduction" , entries } >
88
106
<div className = "markdown-body" > {component ()} </div >
89
107
</DocsLayout >
90
108
</div >
0 commit comments