Skip to content

Commit b07a66c

Browse files
committed
syntax lookup
1 parent 91f9442 commit b07a66c

File tree

104 files changed

+73
-67
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+73
-67
lines changed

app/routes/MdxRoute.res

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ type loaderData = {
66
categories: array<SidebarLayout.Sidebar.Category.t>,
77
entries: array<TableOfContents.entry>,
88
blogPost?: BlogApi.post,
9+
mdxSources?: array<SyntaxLookup.item>,
10+
activeSyntaxItem?: SyntaxLookup.item,
911
}
1012

1113
/**
@@ -144,7 +146,24 @@ let loader: Loader.t<loaderData> = async ({request}) => {
144146
categories: [],
145147
blogPost: mdx.attributes->BlogLoader.transform,
146148
}
149+
res
150+
} else if pathname->String.includes("syntax-lookup") {
151+
let mdxSources =
152+
(await allMdx())
153+
->Array.filter(page => page.path->String.includes("syntax-lookup"))
154+
->Array.map(SyntaxLookupRoute.convert)
155+
156+
let activeSyntaxItem =
157+
mdxSources->Array.find(item => item.id == mdx.attributes.id->Option.getOrThrow)
147158

159+
let res: loaderData = {
160+
__raw: mdx.__raw,
161+
attributes: mdx.attributes,
162+
entries: [],
163+
categories: [],
164+
mdxSources,
165+
?activeSyntaxItem,
166+
}
148167
res
149168
} else {
150169
let categories = {
@@ -229,12 +248,20 @@ let default = () => {
229248
<div className="markdown-body"> {component()} </div>
230249
</CommunityLayout>
231250
</div>
232-
} else {
251+
} else if (pathname :> string)->String.includes("blog") {
233252
switch loaderData.blogPost {
234253
| Some({frontmatter, archived, path}) =>
235254
<BlogArticle frontmatter isArchived=archived path> {component()} </BlogArticle>
236255
| None => React.null // TODO: RR7 show an error
237256
}
257+
} else {
258+
switch loaderData.mdxSources {
259+
| Some(mdxSources) =>
260+
<SyntaxLookup mdxSources activeItem=?loaderData.activeSyntaxItem>
261+
{component()}
262+
</SyntaxLookup>
263+
| None => React.null
264+
}
238265
}}
239266
</>
240267

app/routes/MdxRoute.resi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ type loaderData = {
33
categories: array<SidebarLayout.Sidebar.Category.t>,
44
entries: array<TableOfContents.entry>,
55
blogPost?: BlogApi.post,
6+
mdxSources?: array<SyntaxLookup.item>,
7+
activeSyntaxItem?: SyntaxLookup.item,
68
}
79

810
let loader: ReactRouter.Loader.t<loaderData>

app/routes/SyntaxLookupRoute.res

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ open Mdx
33

44
type loaderData = {mdxSources: array<SyntaxLookup.item>}
55

6-
let loader: Loader.t<loaderData> = async ({request}) => {
7-
let convert = (mdx: Mdx.attributes): SyntaxLookup.item => {
8-
{
9-
category: SyntaxLookup.Category.fromString(mdx.category->Option.getOrThrow),
10-
id: mdx.id->Option.getOrThrow,
11-
keywords: mdx.keywords->Option.getOr([]),
12-
name: mdx.name->Option.getOrThrow,
13-
children: <div> {React.string("TODO: render MDX here")} </div>,
14-
status: SyntaxLookup.Status.fromString(mdx.status->Option.getOr("active")),
15-
summary: mdx.summary->Option.getOrThrow,
16-
}
6+
let convert = (mdx: Mdx.attributes): SyntaxLookup.item => {
7+
{
8+
category: SyntaxLookup.Category.fromString(mdx.category->Option.getOrThrow),
9+
id: mdx.id->Option.getOrThrow,
10+
keywords: mdx.keywords->Option.getOr([]),
11+
name: mdx.name->Option.getOrThrow,
12+
children: <div> {React.string("TODO: render MDX here")} </div>,
13+
status: SyntaxLookup.Status.fromString(mdx.status->Option.getOr("active")),
14+
summary: mdx.summary->Option.getOrThrow,
15+
href: mdx.slug->Option.getOrThrow,
1716
}
17+
}
18+
19+
let loader: Loader.t<loaderData> = async ({request}) => {
1820
let allMdx = await Shims.runWithoutLogging(() => loadAllMdx())
1921

2022
let mdxSources =
2123
allMdx
22-
->Array.filter(page => page.path->String.includes("docs/syntax"))
24+
->Array.filter(page => page.path->String.includes("syntax-lookup"))
2325
->Array.map(convert)
2426

2527
{

app/routes/SyntaxLookupRoute.resi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
type loaderData = {mdxSources: array<SyntaxLookup.item>}
22
let loader: ReactRouter.Loader.t<loaderData>
33
let default: unit => React.element
4+
let convert: Mdx.attributes => SyntaxLookup.item

react-router.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "node:fs";
22
import { init } from "react-router-mdx/server";
33

44

5-
const mdx = init({ paths: ["_blogposts", "docs", "community"], aliases: ["blog", "docs", "community"] });
5+
const mdx = init({ paths: ["_blogposts", "docs", "community", "syntax-lookup"], aliases: ["blog", "docs", "community", "syntax-lookup"] });
66

77
const { stdlibPaths } = await import("./app/routes.mjs");
88

src/SyntaxLookup.res

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module Item = {
7272
category: Category.t,
7373
children: React.element,
7474
status: Status.t,
75+
href: string,
7576
}
7677

7778
let compare = (a, b) =>
@@ -90,30 +91,20 @@ type itemInfo = {
9091
status: Status.t,
9192
}
9293

93-
let getAnchor = path => {
94-
switch String.split(path, "#") {
95-
| [_, anchor] => Some(anchor)
96-
| _ => None
97-
}
98-
}
99-
10094
module Tag = {
10195
@react.component
102-
let make = (~deprecated: bool, ~text: string, ~id: string) => {
103-
let navigate = ReactRouter.useNavigate()
104-
let onClick = evt => {
105-
navigate("#" ++ id)
106-
}
107-
<span
108-
onClick
96+
let make = (~deprecated: bool, ~text: string, ~id: string, ~href) => {
97+
// Console.log(id)
98+
<ReactRouter.Link.String
99+
to={"/syntax-lookup/" ++ href}
109100
className={`
110101
py-1 px-3 rounded text-16
111102
${deprecated
112103
? "hover:bg-gray-30 bg-gray-50 text-gray-80 line-through"
113104
: "hover:bg-fire hover:text-white bg-fire-5 text-fire"}`}
114105
>
115106
{React.string(text)}
116-
</span>
107+
</ReactRouter.Link.String>
117108
}
118109
}
119110

@@ -137,8 +128,7 @@ module DetailBox = {
137128

138129
<div>
139130
<div className="text-24 border-b border-gray-40 pb-4 mb-4 font-semibold"> summaryEl </div>
140-
// TODO: get the actual children
141-
<div className="mt-16"> children </div>
131+
<div className="mt-16 markdown-body"> children </div>
142132
</div>
143133
}
144134
}
@@ -190,21 +180,17 @@ type item = {
190180
category: Category.t,
191181
children: React.element,
192182
status: Status.t,
183+
href: string,
193184
}
194185

195186
@react.component
196-
let make = (~mdxSources: array<item>) => {
187+
let make = (
188+
~mdxSources: array<item>,
189+
~children: option<React.element>=React.null,
190+
~activeItem: option<item>=?,
191+
) => {
197192
let allItems = mdxSources->Array.map(mdxSource => {
198-
let {id, keywords, category, summary, name, status} = mdxSource
199-
200-
// TODO: children?
201-
// let children =
202-
// <MdxRemote
203-
// frontmatter={mdxSource.frontmatter}
204-
// compiledSource={mdxSource.compiledSource}
205-
// scope={mdxSource.scope}
206-
// components={MarkdownComponents.default}
207-
// />
193+
let {id, keywords, category, summary, name, status, href} = mdxSource
208194

209195
{
210196
Item.id,
@@ -213,7 +199,8 @@ let make = (~mdxSources: array<item>) => {
213199
summary,
214200
name,
215201
status,
216-
children: <div> {React.string("Hello!")} </div>,
202+
children,
203+
href,
217204
}
218205
})
219206

@@ -231,7 +218,12 @@ let make = (~mdxSources: array<item>) => {
231218
let fuse: Fuse.t<Item.t> = Fuse.make(allItems, fuseOpts)
232219

233220
let location = ReactRouter.useLocation()
234-
let (state, setState) = React.useState(_ => ShowAll)
221+
let (state, setState) = React.useState(_ => {
222+
switch activeItem {
223+
| Some(item) => ShowDetails((item :> Item.t))
224+
| None => ShowAll
225+
}
226+
})
235227

236228
let findItemById = id => allItems->Array.find(item => "#" ++ item.id === id)
237229

@@ -250,26 +242,6 @@ let make = (~mdxSources: array<item>) => {
250242

251243
let navigate = ReactRouter.useNavigate()
252244

253-
// This effect is responsible for updating the view state when the router anchor changes.
254-
// This effect is triggered when:
255-
// [A] The page first loads.
256-
// [B] The search box is cleared.
257-
// [C] The search box value exactly matches an item name.
258-
React.useEffect(() => {
259-
switch location.hash {
260-
| None => setState(_ => ShowAll)
261-
| Some(anchor) =>
262-
switch findItemById(anchor) {
263-
| None => setState(_ => ShowAll)
264-
| Some(item) => {
265-
setState(_ => ShowDetails(item))
266-
scrollToTop()
267-
}
268-
}
269-
}
270-
None
271-
}, [location.hash])
272-
273245
// onSearchValueChange() is called when:
274246
// [A] The search value changes.
275247
// [B] The search is cleared.
@@ -288,7 +260,7 @@ let make = (~mdxSources: array<item>) => {
288260
let filtered = searchItems(value)
289261
setState(_ => ShowFiltered(value, filtered))
290262
}
291-
| Some(item) => ReactRouter.navigate("/syntax-lookup#" ++ item.id)
263+
| Some(item) => ReactRouter.navigate("/syntax-lookup/" ++ item.id)
292264
}
293265
}
294266
}
@@ -298,7 +270,7 @@ let make = (~mdxSources: array<item>) => {
298270
| ShowAll => React.null
299271
| ShowDetails(item) =>
300272
<div className="mb-16">
301-
<DetailBox summary={item.summary}> item.children </DetailBox>
273+
<DetailBox summary={item.summary}> children </DetailBox>
302274
</div>
303275
}
304276

@@ -347,7 +319,9 @@ let make = (~mdxSources: array<item>) => {
347319
onSearchValueChange(item.name)
348320
}
349321
<span className="mr-2 mb-2 cursor-pointer" onMouseDown key=item.name>
350-
<Tag text={item.name} deprecated={item.status == Deprecated} id=item.id />
322+
<Tag
323+
text={item.name} deprecated={item.status == Deprecated} id=item.id href=item.href
324+
/>
351325
</span>
352326
})
353327
let el =

0 commit comments

Comments
 (0)