-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #824 from thefrontside/www-doc-meta
Don't load all docs just to render the sidebar.
- Loading branch information
Showing
18 changed files
with
71 additions
and
202 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -1,20 +1,15 @@ | ||
import { createContext, all, call, spawn, type Operation, type Task } from "effection"; | ||
import { call, spawn, type Operation, type Task } from "effection"; | ||
import structure from "./structure.json" assert { type: "json" }; | ||
|
||
import { basename } from "https://deno.land/[email protected]/path/posix/basename.ts"; | ||
|
||
import remarkFrontmatter from "npm:[email protected]"; | ||
import remarkMdxFrontmatter from "npm:[email protected]"; | ||
import remarkGfm from "npm:[email protected]"; | ||
import rehypePrismPlus from "npm:[email protected]"; | ||
|
||
import { evaluate } from "npm:@mdx-js/[email protected]"; | ||
|
||
import { Fragment, jsx, jsxs } from "hastx/jsx-runtime"; | ||
|
||
export const Docs = createContext<Docs>("docs"); | ||
export const useDocs = () => Docs; | ||
|
||
export interface DocModule { | ||
default: () => JSX.Element; | ||
frontmatter: { | ||
|
@@ -24,22 +19,25 @@ export interface DocModule { | |
} | ||
|
||
export interface Docs { | ||
getTopics(): Operation<Topic[]>; | ||
getDoc(id?: string): Operation<Doc | undefined>; | ||
} | ||
|
||
export interface Topic { | ||
name: string; | ||
items: Doc[]; | ||
items: DocMeta[]; | ||
} | ||
|
||
export interface Doc { | ||
export interface DocMeta { | ||
id: string; | ||
title: string; | ||
MDXContent: () => JSX.Element; | ||
filename: string; | ||
nextId?: string; | ||
previousId?: string; | ||
topics: Topic[]; | ||
next?: DocMeta; | ||
prev?: DocMeta; | ||
} | ||
|
||
export interface Doc extends DocMeta { | ||
MDXContent: () => JSX.Element; | ||
} | ||
|
||
export function* loadDocs(): Operation<Docs> { | ||
|
@@ -48,63 +46,54 @@ export function* loadDocs(): Operation<Docs> { | |
|
||
let entries = Object.entries(structure); | ||
|
||
let topics = entries.map(([name]) => ({ name, items: []}) as Topic) | ||
|
||
let topicsByName = new Map<string, Topic>(topics.map(topic => [topic.name, topic])); | ||
|
||
let files = entries.flatMap(([topicName, files]) => { | ||
return files.map((filename, topicIndex) => ({ topicName, topicIndex, filename, id: basename(filename, ".mdx") })); | ||
}) | ||
|
||
for (let i = 0; i < files.length; i++ ) { | ||
let file = files[i]; | ||
let nextId = files[i + 1]?.id; | ||
let previousId = files[i - 1]?.id; | ||
let { topicName, topicIndex, filename, id } = file; | ||
let location = new URL(filename, import.meta.url); | ||
|
||
loaders.set(id, yield* spawn(function*() { | ||
let source = yield* call(Deno.readTextFile(location)); | ||
let mod = yield* call(evaluate(source, { | ||
jsx, | ||
jsxs, | ||
jsxDEV: jsx, | ||
Fragment, | ||
remarkPlugins: [ | ||
remarkFrontmatter, | ||
remarkMdxFrontmatter, | ||
remarkGfm, | ||
], | ||
rehypePlugins: [ | ||
[rehypePrismPlus, { showLineNumbers: true }], | ||
], | ||
})); | ||
let topics: Topic[] = []; | ||
|
||
let { title } = mod.frontmatter as { id: string; title: string }; | ||
for (let [name, contents] of entries) { | ||
let topic: Topic = { name, items: [] }; | ||
topics.push(topic); | ||
|
||
let doc: Doc = { | ||
id, | ||
nextId, | ||
previousId, | ||
let current: DocMeta | undefined = void(0); | ||
for (let i = 0; i < contents.length; i++) { | ||
let prev: DocMeta | undefined = current; | ||
let [filename, title] = contents[i]; | ||
let meta: DocMeta = current = { | ||
id: basename(filename, ".mdx"), | ||
title, | ||
filename, | ||
MDXContent: () => mod.default({}), | ||
} as Doc; | ||
|
||
let topic = topicsByName.get(topicName); | ||
|
||
topic!.items[topicIndex] = doc; | ||
|
||
return doc; | ||
})); | ||
topics, | ||
prev | ||
}; | ||
if (prev) { | ||
prev.next = current; | ||
} | ||
topic.items.push(current); | ||
|
||
loaders.set(meta.id, yield* spawn(function*() { | ||
let location = new URL(filename, import.meta.url); | ||
let source = yield* call(Deno.readTextFile(location)); | ||
let mod = yield* call(evaluate(source, { | ||
jsx, | ||
jsxs, | ||
jsxDEV: jsx, | ||
Fragment, | ||
remarkPlugins: [ | ||
remarkGfm, | ||
], | ||
rehypePlugins: [ | ||
[rehypePrismPlus, { showLineNumbers: true }], | ||
], | ||
})); | ||
|
||
return { | ||
...meta, | ||
MDXContent: () => mod.default({}), | ||
} as Doc; | ||
|
||
})); | ||
} | ||
} | ||
|
||
return yield* Docs.set({ | ||
*getTopics() { | ||
yield* all([...loaders.values()]); | ||
return topics; | ||
}, | ||
return { | ||
*getDoc(id) { | ||
if (id) { | ||
let task = loaders.get(id); | ||
|
@@ -113,5 +102,5 @@ export function* loadDocs(): Operation<Docs> { | |
} | ||
} | ||
}, | ||
}); | ||
}; | ||
} |
This file contains 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 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 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 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 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 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 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 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 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
Oops, something went wrong.