-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
147 additions
and
75 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-core': patch | ||
--- | ||
|
||
Support remark plugins & vfile input on `getTableOfContents` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Processor, Transformer } from 'unified'; | ||
import { visit } from 'unist-util-visit'; | ||
import type { Literal, Root } from 'mdast'; | ||
import * as path from 'node:path'; | ||
import * as fs from 'node:fs/promises'; | ||
import matter from 'gray-matter'; | ||
|
||
export function remarkInclude(this: Processor): Transformer<Root, Root> { | ||
return async (tree, file) => { | ||
const queue: Promise<void>[] = []; | ||
|
||
visit(tree, ['mdxJsxFlowElement', 'paragraph'] as const, (node) => { | ||
let specifier: string | undefined; | ||
|
||
// without MDX, parse from paragraph nodes | ||
if (node.type === 'paragraph' && node.children.length === 3) { | ||
const [open, content, closure] = node.children; | ||
|
||
if ( | ||
open.type === 'html' && | ||
open.value === '<include>' && | ||
content.type === 'text' && | ||
closure.type === 'html' && | ||
closure.value === '</include>' | ||
) { | ||
specifier = content.value.trim(); | ||
} | ||
} | ||
|
||
if (node.type === 'mdxJsxFlowElement' && node.name === 'include') { | ||
const child = node.children.at(0) as Literal | undefined; | ||
|
||
if (!child || child.type !== 'text') return; | ||
specifier = child.value; | ||
} | ||
|
||
if (!specifier) return 'skip'; | ||
const targetPath = path.resolve(path.dirname(file.path), specifier); | ||
|
||
queue.push( | ||
fs.readFile(targetPath).then((content) => { | ||
const parsed = this.parse(matter(content).content); | ||
|
||
if (file.data._compiler) { | ||
file.data._compiler.addDependency(targetPath); | ||
} | ||
Object.assign(node, parsed); | ||
}), | ||
); | ||
|
||
return 'skip'; | ||
}); | ||
|
||
await Promise.all(queue); | ||
}; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.