Files
docs/examples/tanstack-start/lib/source.ts
T
a6c909b618 Deps: Reducing deps (#1873)
* Deps: Reducing deps

* Changeset

* Reverting the changes for `generate-docs` script as it shuold work as expected without any additional workarounds

* Returning `isbot` dep

* clean code

---------

Co-authored-by: Fuma Nama <[email protected]>
2025-05-19 20:32:47 +08:00

71 lines
1.4 KiB
TypeScript

import {
loader,
type MetaData,
type PageData,
type Source,
type VirtualFile,
} from 'fumadocs-core/source';
import matter from 'gray-matter';
import * as path from 'node:path';
import { globSync } from 'tinyglobby';
import * as fs from 'node:fs';
let files: [string, string][];
if (typeof import.meta.glob === 'function') {
files = Object.entries(
import.meta.glob<true, 'raw'>('/content/docs/**/*', {
eager: true,
query: '?raw',
import: 'default',
}),
);
} else {
files = globSync('content/docs/**/*').map((file) => {
return [file, fs.readFileSync(file).toString()];
});
}
const virtualFiles: VirtualFile[] = files.flatMap(([file, content]) => {
const ext = path.extname(file);
const virtualPath = path.relative(
'content/docs',
path.join(process.cwd(), file),
);
if (ext === '.mdx' || ext === '.md') {
const parsed = matter(content);
return {
type: 'page',
path: virtualPath,
data: {
...parsed.data,
content: parsed.content,
},
};
}
if (ext === '.json') {
return {
type: 'meta',
path: virtualPath,
data: JSON.parse(content),
};
}
return [];
});
export const source = loader({
source: {
files: virtualFiles,
} as Source<{
pageData: PageData & {
content: string;
};
metaData: MetaData;
}>,
baseUrl: '/docs',
});