-
-
Notifications
You must be signed in to change notification settings - Fork 258
refactor: split out docs/manual routes from shared MdxRoute file #1221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jderochervlk
wants to merge
4
commits into
master
Choose a base branch
from
vlk/split-out-docs-manual
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1b8f46d
refactor: split out docs/manual routes from shared MdxRoute file
jderochervlk a2120c9
Update src/MdxFile.res
jderochervlk 7656ee6
fix docs/manual/api route
jderochervlk eb9a434
Merge branch 'master' of github.com:rescript-lang/rescript-lang.org i…
jderochervlk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| type loaderData = { | ||
| compiledMdx: CompiledMdx.t, | ||
| categories: array<SidebarLayout.Sidebar.Category.t>, | ||
| entries: array<TableOfContents.entry>, | ||
| title: string, | ||
| description: string, | ||
| filePath: string, | ||
| } | ||
|
|
||
| // Build sidebar categories from all manual docs, sorted by their "order" field in frontmatter | ||
| let manualTableOfContents = async () => { | ||
| let groups = | ||
| (await MdxFile.loadAllAttributes(~dir="markdown-pages/docs")) | ||
| ->Mdx.filterMdxPages("docs/manual") | ||
| ->Mdx.groupBySection | ||
| ->Dict.mapValues(values => | ||
| values->Mdx.sortSection->SidebarHelpers.convertToNavItems("/docs/manual") | ||
| ) | ||
|
|
||
| SidebarHelpers.getAllGroups( | ||
| groups, | ||
| [ | ||
| "Overview", | ||
| "Guides", | ||
| "Language Features", | ||
| "JavaScript Interop", | ||
| "Build System", | ||
| "Advanced Features", | ||
| ], | ||
| ) | ||
| } | ||
|
|
||
| let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => { | ||
| let {pathname} = WebAPI.URL.make(~url=request.url) | ||
| let filePath = MdxFile.resolveFilePath( | ||
| (pathname :> string), | ||
| ~dir="markdown-pages/docs/manual", | ||
| ~alias="docs/manual", | ||
| ) | ||
|
|
||
| let raw = await Node.Fs.readFile(filePath, "utf-8") | ||
| let {frontmatter}: MarkdownParser.result = MarkdownParser.parseSync(raw) | ||
|
|
||
| let description = switch frontmatter { | ||
| | Object(dict) => | ||
| switch dict->Dict.get("description") { | ||
| | Some(String(s)) => s | ||
| | _ => "" | ||
| } | ||
| | _ => "" | ||
| } | ||
|
|
||
| let title = switch frontmatter { | ||
| | Object(dict) => | ||
| switch dict->Dict.get("title") { | ||
| | Some(String(s)) => s | ||
| | _ => "" | ||
| } | ||
| | _ => "" | ||
| } | ||
|
|
||
| let categories = await manualTableOfContents() | ||
|
|
||
| let compiledMdx = await MdxFile.compileMdx(raw, ~filePath, ~remarkPlugins=Mdx.plugins) | ||
|
|
||
| // Build table of contents entries from markdown headings | ||
| let markdownTree = Mdast.fromMarkdown(raw) | ||
| let tocResult = Mdast.toc(markdownTree, {maxDepth: 2}) | ||
|
|
||
| let headers = Dict.make() | ||
| Mdast.reduceHeaders(tocResult.map, headers) | ||
|
|
||
| let entries = | ||
| headers | ||
| ->Dict.toArray | ||
| ->Array.map(((header, url)): TableOfContents.entry => { | ||
| header, | ||
| href: (url :> string), | ||
| }) | ||
| ->Array.slice(~start=2) // skip document entry and H1 title, keep h2 sections | ||
|
|
||
| { | ||
| compiledMdx, | ||
| categories, | ||
| entries, | ||
| title: `${title} | ReScript Language Manual`, | ||
| description, | ||
| filePath, | ||
| } | ||
| } | ||
|
|
||
| let default = () => { | ||
| let {pathname} = ReactRouter.useLocation() | ||
| let {compiledMdx, categories, entries, title, description, filePath} = ReactRouter.useLoaderData() | ||
|
|
||
| let breadcrumbs = list{ | ||
| {Url.name: "Docs", href: "/docs/manual/introduction"}, | ||
| { | ||
| Url.name: "Language Manual", | ||
| href: "/docs/manual/introduction", | ||
| }, | ||
| } | ||
|
|
||
| let editHref = `https://github.com/rescript-lang/rescript-lang.org/blob/master/${filePath}` | ||
|
|
||
| let sidebarContent = | ||
| <aside className="px-4 w-full block"> | ||
| <div className="flex justify-between items-baseline"> | ||
| <div className="flex flex-col text-fire font-medium"> | ||
| <VersionSelect /> | ||
| </div> | ||
| <button | ||
| className="flex items-center" onClick={_ => NavbarUtils.closeMobileTertiaryDrawer()} | ||
| > | ||
| <Icon.Close /> | ||
| </button> | ||
| </div> | ||
| <div className="mb-56"> | ||
| {categories | ||
| ->Array.map(category => { | ||
| let isItemActive = (navItem: SidebarLayout.Sidebar.NavItem.t) => | ||
| navItem.href === (pathname :> string) | ||
| let getActiveToc = (navItem: SidebarLayout.Sidebar.NavItem.t) => | ||
| if navItem.href === (pathname :> string) { | ||
| Some({TableOfContents.title, entries}) | ||
| } else { | ||
| None | ||
| } | ||
| <div key=category.name> | ||
| <SidebarLayout.Sidebar.Category | ||
| isItemActive | ||
| getActiveToc | ||
| category | ||
| onClick={_ => NavbarUtils.closeMobileTertiaryDrawer()} | ||
| /> | ||
| </div> | ||
| }) | ||
| ->React.array} | ||
| </div> | ||
| </aside> | ||
|
|
||
| <> | ||
| <Meta title description /> | ||
| <NavbarSecondary /> | ||
| <NavbarTertiary sidebar=sidebarContent> | ||
| <SidebarLayout.BreadCrumbs crumbs=breadcrumbs /> | ||
| <a | ||
| href=editHref className="inline text-14 hover:underline text-fire" rel="noopener noreferrer" | ||
| > | ||
| {React.string("Edit")} | ||
| </a> | ||
| </NavbarTertiary> | ||
| <DocsLayout categories activeToc={title, entries}> | ||
| <div className="markdown-body"> | ||
| <MdxContent compiledMdx /> | ||
| </div> | ||
| </DocsLayout> | ||
| </> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| type loaderData = { | ||
| compiledMdx: CompiledMdx.t, | ||
| categories: array<SidebarLayout.Sidebar.Category.t>, | ||
| entries: array<TableOfContents.entry>, | ||
| title: string, | ||
| description: string, | ||
| filePath: string, | ||
| } | ||
|
|
||
| let loader: ReactRouter.Loader.t<loaderData> | ||
|
|
||
| let default: unit => React.element |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| let convertToNavItems = (items, rootPath) => | ||
| Array.map(items, (item): SidebarLayout.Sidebar.NavItem.t => { | ||
| let href = switch item.Mdx.slug { | ||
| | Some(slug) => `${rootPath}/${slug}` | ||
| | None => rootPath | ||
| } | ||
| { | ||
| name: item.title, | ||
| href, | ||
| } | ||
| }) | ||
|
|
||
| let getGroup = (groups, groupName): SidebarLayout.Sidebar.Category.t => { | ||
| { | ||
| name: groupName, | ||
| items: groups | ||
| ->Dict.get(groupName) | ||
| ->Option.getOr([]), | ||
| } | ||
| } | ||
|
|
||
| let getAllGroups = (groups, groupNames): array<SidebarLayout.Sidebar.Category.t> => | ||
| groupNames->Array.map(item => getGroup(groups, item)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** Convert Mdx.attributes to sidebar nav items, building hrefs from rootPath + slug. */ | ||
| let convertToNavItems: (array<Mdx.attributes>, string) => array<SidebarLayout.Sidebar.NavItem.t> | ||
|
|
||
| /** Get a single sidebar category by name from a dict of grouped nav items. */ | ||
| let getGroup: ( | ||
| Dict.t<array<SidebarLayout.Sidebar.NavItem.t>>, | ||
| string, | ||
| ) => SidebarLayout.Sidebar.Category.t | ||
|
|
||
| /** Get multiple sidebar categories by name from a dict of grouped nav items. */ | ||
| let getAllGroups: ( | ||
| Dict.t<array<SidebarLayout.Sidebar.NavItem.t>>, | ||
| array<string>, | ||
| ) => array<SidebarLayout.Sidebar.Category.t> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.