Integrations
Validate Links
Ensure your links are correct.
Setup
You can use next-validate-link
to validate your links in content files.
This guide is mainly for Fumadocs MDX, see the docs of next-validate-link
for other setups.
Create a script for Bun:
import {
type FileObject,
printErrors,
scanURLs,
validateFiles,
} from 'next-validate-link';
import type { InferPageType } from 'fumadocs-core/source';
import { source } from '@/lib/source';
async function checkLinks() {
const scanned = await scanURLs({
// pick a preset for your React framework
preset: 'next',
populate: {
'docs/[[...slug]]': source.getPages().map((page) => {
return {
value: {
slug: page.slugs,
},
hashes: getHeadings(page),
};
}),
},
});
printErrors(
await validateFiles(source.getPages().map(toFile), {
scanned,
// check `href` attributes in different MDX components
markdown: {
components: {
Card: { attributes: ['href'] },
},
},
// check relative paths
checkRelativePaths: 'as-url',
}),
true,
);
}
function getHeadings({ data }: InferPageType<typeof source>): string[] {
return data.toc.map((item) => item.url.slice(1));
}
function toFile(page: InferPageType<typeof source>): FileObject {
return {
data: page.data,
url: page.url,
path: page.absolutePath,
content: page.data.content,
};
}
void checkLinks();
To access the source
object outside of app runtime, you'll need a runtime loader:
preload = ["./scripts/preload.ts"]
Run the script to validate links:
bun ./scripts/lint.ts
How is this guide?
Last updated on