-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeScript: Support overriding
renderMarkdown
function
- Loading branch information
Showing
6 changed files
with
56 additions
and
77 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'fumadocs-typescript': patch | ||
--- | ||
|
||
Support overriding `renderMarkdown` function |
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,64 +1,36 @@ | ||
import { fromMarkdown } from 'mdast-util-from-markdown'; | ||
import { gfmFromMarkdown } from 'mdast-util-gfm'; | ||
import { toHast } from 'mdast-util-to-hast'; | ||
import { getSingletonHighlighter, type LanguageRegistration } from 'shiki'; | ||
import type { Nodes } from 'hast'; | ||
import { type Code, type RootContent } from 'mdast'; | ||
import { remark } from 'remark'; | ||
import { | ||
remarkGfm, | ||
rehypeCode, | ||
type RehypeCodeOptions, | ||
} from 'fumadocs-core/mdx-plugins'; | ||
import remarkRehype from 'remark-rehype'; | ||
|
||
export async function renderMarkdownToHast(md: string): Promise<Nodes> { | ||
const mdast = fromMarkdown( | ||
md.replace(/{@link (?<link>[^}]*)}/g, '$1'), // replace jsdoc links | ||
{ mdastExtensions: [gfmFromMarkdown()] }, | ||
); | ||
|
||
const highlighter = await getSingletonHighlighter({ | ||
themes: ['vitesse-light', 'vitesse-dark'], | ||
}); | ||
const processor = remark() | ||
.use(remarkGfm) | ||
.use(remarkRehype) | ||
.use(rehypeCode, { | ||
lazy: true, | ||
|
||
async function preload(contents: RootContent[]): Promise<void> { | ||
await Promise.all( | ||
contents.map(async (c) => { | ||
if ('children' in c) await preload(c.children); | ||
themes: { | ||
light: 'github-light', | ||
dark: 'github-dark', | ||
}, | ||
} satisfies RehypeCodeOptions) | ||
// @ts-expect-error -- safe | ||
.use(() => { | ||
return (tree, file: { data: Record<string, unknown> }) => { | ||
file.data.tree = tree; | ||
|
||
if (c.type === 'code' && c.lang) { | ||
await highlighter.loadLanguage( | ||
c.lang as unknown as LanguageRegistration, | ||
); | ||
} | ||
}), | ||
); | ||
} | ||
return ''; | ||
}; | ||
}); | ||
|
||
await preload(mdast.children); | ||
export async function renderMarkdownToHast(md: string): Promise<Nodes> { | ||
md = md.replace(/{@link (?<link>[^}]*)}/g, '$1'); // replace jsdoc links | ||
|
||
return toHast(mdast, { | ||
handlers: { | ||
// @ts-expect-error hast with mdx | ||
code(_, node: Code) { | ||
const lang = node.lang ?? 'plaintext'; | ||
const out = await processor.process(md); | ||
|
||
return highlighter.codeToHast(node.value, { | ||
lang, | ||
themes: { | ||
light: 'vitesse-light', | ||
dark: 'vitesse-dark', | ||
}, | ||
defaultColor: false, | ||
transformers: [ | ||
{ | ||
name: 'rehype-code:pre-process', | ||
line(hast) { | ||
if (hast.children.length > 0) return; | ||
// Keep the empty lines when using grid layout | ||
hast.children.push({ | ||
type: 'text', | ||
value: ' ', | ||
}); | ||
}, | ||
}, | ||
], | ||
}).children; | ||
}, | ||
}, | ||
}); | ||
return out.data.tree as Nodes; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.