Skip to content

Commit 9a98259

Browse files
committed
feat: correct toc
1 parent b916f18 commit 9a98259

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

apps/docs/src/app/docs/[[...slug]]/page.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { DocsLayout } from '@/app/docs/[[...slug]]/docs.layout';
22
import { getPage } from '@/lib/docs';
33
import { createMetadata } from '@/lib/metadata';
44
import { getPageTreePeers } from '@/lib/pageTree';
5+
import { getToc } from '@/lib/remark-plugins/toc';
56
import { type DocsPage, type DocsSource, docs } from '@/lib/source';
7+
import { filterCotent } from '@/lib/toc';
68
import { getDocId, getDocUrl, parseDocId } from '@/lib/utils';
79
import { getMDXComponents } from '@/mdx-components';
810
import { Identity } from '@/mdx/Identity';
@@ -25,25 +27,28 @@ export default async function Docs(props: {
2527
const page = getPage(docUrl);
2628
if (!page) notFound();
2729
const { isApp, isPages, isVLatest, version } = parseDocId(docId);
30+
let content = page.data.content;
2831

2932
let { body: MdxContent, toc } = await page.data.load();
30-
31-
// source: app/getting-started/installation
3233
const ref = page.data.source;
3334
if (ref) {
3435
const refUrl = getDocUrl(isVLatest ? ref : `${version}/${ref}`);
3536
const refPage = getPage(refUrl);
3637
if (!refPage) notFound();
37-
3838
const { body: MdxContent2, toc: toc2 } = await refPage.data.load();
3939

40+
content = refPage.data.content;
4041
MdxContent = MdxContent2;
4142
toc = toc2;
4243
}
4344

4445
const hasRelated = page.data.related;
4546
const isIndex = page.file.name === 'index';
4647

48+
if (isApp || isPages) {
49+
toc = await getToc(filterCotent({ content, isApp, isPages }));
50+
}
51+
4752
return (
4853
<DocsLayout docId={docId} pageTree={docs.pageTree}>
4954
<UiDocsPage
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { remarkHeading } from 'fumadocs-core/mdx-plugins';
2+
import type { TableOfContents } from 'fumadocs-core/server';
3+
import { remark } from 'remark';
4+
5+
export async function getToc(content: string) {
6+
const result = remark().use(remarkHeading).process(content);
7+
8+
return (await result).data.toc as TableOfContents;
9+
}

apps/docs/src/lib/toc.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { TableOfContents } from 'fumadocs-core/server';
2+
3+
export function filterCotent({
4+
isApp,
5+
isPages,
6+
content,
7+
}: {
8+
isApp: boolean;
9+
isPages: boolean;
10+
content: string;
11+
}) {
12+
if (!isApp && !isPages) return content;
13+
let finalContent = content;
14+
if (isApp) {
15+
finalContent = finalContent.replace(
16+
/<PagesOnly>([\s\S]*?)<\/PagesOnly>/g,
17+
'',
18+
);
19+
}
20+
if (isPages) {
21+
finalContent = finalContent.replace(/<AppOnly>([\s\S]*?)<\/AppOnly>/g, '');
22+
}
23+
return finalContent;
24+
}
25+
26+
export function filterToc({
27+
toc,
28+
isApp,
29+
isPages,
30+
content,
31+
}: {
32+
toc: TableOfContents;
33+
isApp: boolean;
34+
isPages: boolean;
35+
content: string;
36+
}): TableOfContents {
37+
if (!isApp && !isPages) return toc;
38+
let finalContent = content;
39+
if (isApp) {
40+
finalContent = finalContent.replace(
41+
/<PagesOnly>([\s\S]*?)<\/PagesOnly>/g,
42+
'',
43+
);
44+
}
45+
if (isPages) {
46+
finalContent = finalContent.replace(/<AppOnly>([\s\S]*?)<\/AppOnly>/g, '');
47+
}
48+
49+
console.log('filterToc finalContent', finalContent);
50+
return toc.filter((item) => {
51+
const heading = `${'#'.repeat(item.depth)} ${item.url.slice(1)}`;
52+
return finalContent.includes(heading);
53+
});
54+
}

0 commit comments

Comments
 (0)