Core: Support sync usage of getTableOfContents

This commit is contained in:
Fuma Nama
2024-11-02 17:28:54 +08:00
parent 2949da3cba
commit ca94bfd65b
7 changed files with 34 additions and 33 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
Support sync usage of `getTableOfContents`
@@ -14,7 +14,7 @@ Note: If you're using a CMS, you should use the API provided by the CMS instead.
```ts
import { getTableOfContents } from 'fumadocs-core/server';
const toc = await getTableOfContents('## markdown content');
const toc = getTableOfContents('## markdown content');
```
### Output
+1
View File
@@ -64,6 +64,7 @@
"postcss": "^8.4.47",
"tailwindcss": "^3.4.14",
"tailwindcss-animate": "^1.0.7",
"ts-morph": "^24.0.0",
"tsconfig": "workspace:*",
"tsx": "^4.19.2",
"typescript": "^5.6.3"
+11 -15
View File
@@ -31,24 +31,20 @@ async function checkLinks() {
const scanned = await scanURLs({
populate: {
'(home)/blog/[slug]': blogFiles.map((file) => {
const info = parseFilePath(file.path);
const info = parseFilePath(path.relative('content/blog', file.path));
return { value: getSlugs(info)[2] };
return { value: getSlugs(info)[0] };
}),
'docs/[...slug]': await Promise.all(
docsFiles.map(async (file) => {
const info = parseFilePath(file.path);
'docs/[...slug]': docsFiles.map((file) => {
const info = parseFilePath(path.relative('content/docs', file.path));
return {
value: getSlugs(info).slice(2),
hashes: file.data?._mdx?.mirror
? undefined
: (await getTableOfContents(file.content)).map((item) =>
item.url.slice(1),
),
};
}),
),
return {
value: getSlugs(info),
hashes: file.data?._mdx?.mirror
? undefined
: getTableOfContents(file.content).map((item) => item.url.slice(1)),
};
}),
},
});
+11 -13
View File
@@ -34,22 +34,20 @@ async function checkLinks() {
const scanned = await scanURLs({
populate: {
'(home)/blog/[slug]': blogFiles.map((file) => {
const info = parseFilePath(file.path);
const info = parseFilePath(path.relative('content/blog', file.path));
return { value: getSlugs(info)[2] };
return { value: getSlugs(info)[0] };
}),
'docs/[[...slug]]': await Promise.all(
docsFiles.map(async (file) => {
const info = parseFilePath(file.path);
'docs/[[...slug]]': docsFiles.map((file) => {
const info = parseFilePath(path.relative('content/docs', file.path));
return {
value: getSlugs(info).slice(2),
hashes: (await getTableOfContents(file.content)).map((item) =>
item.url.slice(1),
),
};
}),
),
return {
value: getSlugs(info),
hashes: getTableOfContents(file.content).map((item) =>
item.url.slice(1),
),
};
}),
},
});
+2 -4
View File
@@ -15,10 +15,8 @@ export type TableOfContents = TOCItemType[];
*
* @param content - Markdown content
*/
export async function getTableOfContents(
content: string,
): Promise<TableOfContents> {
const result = await remark().use(remarkHeading).process(content);
export function getTableOfContents(content: string): TableOfContents {
const result = remark().use(remarkHeading).processSync(content);
if ('toc' in result.data) return result.data.toc as TableOfContents;
+3
View File
@@ -207,6 +207,9 @@ importers:
tailwindcss-animate:
specifier: ^1.0.7
version: 1.0.7([email protected])
ts-morph:
specifier: ^24.0.0
version: 24.0.0
tsconfig:
specifier: workspace:*
version: link:../../packages/tsconfig