@@ -42,15 +42,72 @@ let components = {
4242 "Warn" : Markdown .Warn .make ,
4343}
4444
45- // the loadAllMdx function logs out all of the file contents as it reads them, which is noisy and not useful.
45+ // The loadAllMdx function logs out all of the file contents as it reads them, which is noisy and not useful.
4646// We can suppress that logging with this helper function.
4747let allMdx = await Shims .runWithoutLogging (() => loadAllMdx ())
4848
49+ let sortSection = mdxPages =>
50+ Array .toSorted (mdxPages , (a : Mdx .attributes , b : Mdx .attributes ) =>
51+ switch (a .order , b .order ) {
52+ | (Some (a ), Some (b )) => a > b ? 1.0 : - 1.0
53+ | _ => - 1.0
54+ }
55+ )
56+
57+ let groupBySection = mdxPages =>
58+ Array .reduce (mdxPages , (Dict .make () :> Dict .t <array <Mdx .attributes >>), (acc , item ) => {
59+ let section = item .section -> Option .flatMap (Dict .get (acc , _ ))
60+ switch section {
61+ // If the section already exists, add this item to it
62+ | Some (section ) => section -> Array .push (item )
63+ // otherwise create a new section with this item
64+ | None => item .section -> Option .forEach (section => acc -> Dict .set (section , [item ]))
65+ }
66+ acc
67+ })
68+
69+ let convertToNavItems = items =>
70+ Array .map (items , (item ): SidebarLayout .Sidebar .NavItem .t => {
71+ {
72+ name : item .title ,
73+ href : item .canonical , // TODO: RR7 - canonical works for now, but we should make this more robust so that it's not required
74+ }
75+ })
76+
77+ let filterMdxPages = (mdxPages , path ) =>
78+ Array .filter (mdxPages , mdx => (mdx .path :> string )-> String .includes (path ))
79+
80+ // These are the pages for the language manual, sorted by their "order" field in the frontmatter
81+ let manualTableOfContents = () => {
82+ let groups =
83+ allMdx
84+ -> filterMdxPages ("docs/manual" )
85+ -> groupBySection
86+ -> Dict .mapValues (values => values -> sortSection -> convertToNavItems )
87+
88+ // Console.log(groups)
89+
90+ // these are the categories that appear in the sidebar
91+ let categories : array <SidebarLayout .Sidebar .Category .t > = [
92+ {name : "Overview" , items : groups -> Dict .getUnsafe ("Overview" )},
93+ {name : "Guides" , items : groups -> Dict .getUnsafe ("Guides" )},
94+ {name : "Language Features" , items : groups -> Dict .getUnsafe ("Language Features" )},
95+ {name : "JavaScript Interop" , items : groups -> Dict .getUnsafe ("JavaScript Interop" )},
96+ {name : "Build System" , items : groups -> Dict .getUnsafe ("Build System" )},
97+ {name : "Advanced Features" , items : groups -> Dict .getUnsafe ("Advanced Features" )},
98+ ]
99+ categories
100+ }
101+
49102let loader : Loader .t <loaderData > = async ({request }) => {
103+ let {pathname } = WebAPI .URL .make (~url = request .url )
104+
50105 let mdx = await loadMdx (request )
51106
107+ let categories = manualTableOfContents ()
108+
52109 let fileContents = await allMdx
53- -> Array .filter (mdx => (mdx .path :> string )-> String .includes ("docs/manual/introduction" ))
110+ -> Array .filter (mdx => (mdx .path :> string )-> String .includes (pathname ))
54111 -> Array .get (0 )
55112 -> Option .map (mdx => mdx .path )
56113 -> Option .map (path => Node .Fs .readFile ((path :> string ), "utf-8" ))
@@ -70,18 +127,13 @@ let loader: Loader.t<loaderData> = async ({request}) => {
70127 header ,
71128 href : (url :> string ),
72129 })
73- -> Array .slice (~start = 2 ) // skip first two entries which are "Introduction" and "Getting Started"
130+ -> Array .slice (~start = 2 ) // skip first two entries which are the document entry and the H1 title for the page, we just want the h2 sections
74131
75132 let res : loaderData = {
76133 __raw : mdx .__raw ,
77134 attributes : mdx .attributes ,
78135 entries ,
79- categories : [
80- {
81- name : "overview" ,
82- items : [{name : "Introduction" , href : # "/docs/manual/introduction" }],
83- },
84- ],
136+ categories ,
85137 }
86138 res
87139}
0 commit comments