6
scanURLs,
7
validateFiles,
8
} from 'next-validate-link';
9
-import type { InferPageType } from 'fumadocs-core/source';
9
import {
10
source,
11
referenceSource,
19
| typeof cookbooksSource
20
| typeof toolkitsSource;
21
22
+type PageOf = ReturnType<AnySource['getPages']>[number];
23
+
24
+/**
25
+ * Slugify a heading the same way rehype-slug does.
26
+ */
27
+function slugify(text: string): string {
28
+ return text
29
+ .toLowerCase()
30
+ .replace(/[^\w\s-]/g, '')
31
+ .replace(/[\s]+/g, '-')
32
+ .replace(/-+/g, '-')
33
+ .replace(/^-|-$/g, '');
34
+}
35
+
36
+/**
37
+ * Extract heading anchors from raw MDX/markdown content.
38
+ * Falls back to this when data.toc is unavailable (outside Next.js runtime).
39
+ */
40
+function extractHeadingsFromContent(content: string): string[] {
41
+ const headings: string[] = [];
42
+ for (const line of content.split('\n')) {
43
+ const match = line.match(/^#{1,6}\s+(.+)$/);
44
+ if (match) {
45
+ headings.push(slugify(match[1]));
46
+ }
47
+ }
48
+ return headings;
49
+}
50
+
51
+/**
52
+ * Get headings for a page, trying data.toc first then falling back to raw content parsing.
53
+ */
54
+async function getHeadingsForPage(page: PageOf): Promise<string[]> {
55
+ if (page.data.toc?.length) {
56
+ return page.data.toc.map((item: { url: string }) => item.url.slice(1));
57
+ }
58
+ if ('getText' in page.data) {
59
+ try {
60
+ const content = await (page.data as { getText: (mode: string) => Promise<string> }).getText('raw');
61
+ return extractHeadingsFromContent(content);
62
+ } catch {
63
+ // fall through
64
+ }
65
+ }
66
+ if (page.absolutePath) {
67
+ try {
68
+ const content = await readFile(page.absolutePath, 'utf-8');
69
+ return extractHeadingsFromContent(content);
70
+ } catch {
71
+ // fall through
72
+ }
73
+ }
74
+ return [];
75
+}
76
+
77
+/**
78
+ * Build populate entries for a source, resolving headings asynchronously.
79
+ */
80
+async function buildPopulateEntries(src: AnySource) {
81
+ return Promise.all(
82
+ src.getPages().map(async (page) => ({
83
+ value: { slug: page.slugs },
84
+ hashes: await getHeadingsForPage(page),
85
+ })),
86
+ );
87
+}
88
+
89
async function checkLinks() {
90
+ const [docsEntries, refEntries, cookbookEntries, toolkitEntries] = await Promise.all([
91
+ buildPopulateEntries(source),
92
+ buildPopulateEntries(referenceSource),
93
+ buildPopulateEntries(cookbooksSource),
94
+ buildPopulateEntries(toolkitsSource),
95
+ ]);
96
+
97
const scanned = await scanURLs({
98
preset: 'next',
99
populate: {
27
- // Dynamic routes (keys must include (home) route group to match app directory structure)
28
- '(home)/docs/[[...slug]]': source.getPages().map((page) => ({
29
- value: { slug: page.slugs },
30
- hashes: getHeadings(page),
31
- })),
32
- '(home)/reference/[[...slug]]': referenceSource.getPages().map((page) => ({
33
- value: { slug: page.slugs },
34
- hashes: getHeadings(page),
35
- })),
36
- '(home)/cookbooks/[[...slug]]': cookbooksSource.getPages().map((page) => ({
37
- value: { slug: page.slugs },
38
- hashes: getHeadings(page),
39
- })),
40
- '(home)/toolkits/[[...slug]]': toolkitsSource.getPages().map((page) => ({
41
- value: { slug: page.slugs },
42
- hashes: getHeadings(page),
43
- })),
100
+ // Keys must include (home) route group to match app directory structure
101
+ '(home)/docs/[[...slug]]': docsEntries,
102
+ '(home)/reference/[[...slug]]': refEntries,
103
+ '(home)/cookbooks/[[...slug]]': cookbookEntries,
104
+ '(home)/toolkits/[[...slug]]': toolkitEntries,
105
},
106
});
107
131
}
132
}
133
73
-function getHeadings({ data }: InferPageType<AnySource>): string[] {
74
- if (!data.toc) return [];
75
- return data.toc.map((item) => item.url.slice(1));
76
-}
77
-
134
async function getFiles(): Promise<FileObject[]> {
135
const sources = [source, referenceSource, cookbooksSource, toolkitsSource];
136
const allFiles: FileObject[] = [];