mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-26 23:49:51 +00:00
MDX: Improve error handling logic on parsing meta entries
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'fumadocs-mdx': patch
|
||||
---
|
||||
|
||||
Improve error handling logic on parsing meta entries
|
||||
@@ -57,6 +57,7 @@
|
||||
"estree-util-value-to-estree": "^3.3.3",
|
||||
"fast-glob": "^3.3.3",
|
||||
"gray-matter": "^4.0.3",
|
||||
"js-yaml": "^4.1.0",
|
||||
"lru-cache": "^11.1.0",
|
||||
"picocolors": "^1.1.1",
|
||||
"unist-util-visit": "^5.0.0",
|
||||
@@ -65,6 +66,7 @@
|
||||
"devDependencies": {
|
||||
"@fumadocs/mdx-remote": "workspace:*",
|
||||
"@types/cross-spawn": "^6.0.6",
|
||||
"@types/js-yaml": "^4.0.9",
|
||||
"@types/mdast": "^4.0.3",
|
||||
"@types/mdx": "^2.0.13",
|
||||
"@types/react": "^19.1.2",
|
||||
|
||||
@@ -131,6 +131,7 @@ export function defineDocs<
|
||||
// @ts-expect-error -- internal type inferring
|
||||
meta: defineCollections({
|
||||
type: 'meta',
|
||||
files: ['**/meta.{json,yaml}'],
|
||||
dir,
|
||||
schema: metaSchema,
|
||||
...options?.meta,
|
||||
|
||||
@@ -3,12 +3,13 @@ import * as fs from 'node:fs/promises';
|
||||
import fg from 'fast-glob';
|
||||
import { getTypeFromPath } from '@/utils/get-type-from-path';
|
||||
import type { FileInfo } from '@/config/types';
|
||||
import { type LoadedConfig } from '@/utils/config';
|
||||
import type { LoadedConfig } from '@/utils/config';
|
||||
import type { DocCollection, MetaCollection } from '@/config';
|
||||
import { validate } from '@/utils/schema';
|
||||
import { fileCache } from '@/map/file-cache';
|
||||
import matter from 'gray-matter';
|
||||
import type { AsyncRuntimeFile } from '@/runtime/types';
|
||||
import { load } from 'js-yaml';
|
||||
|
||||
async function readFileWithCache(file: string): Promise<string> {
|
||||
const cached = fileCache.read<string>('read-file', file);
|
||||
@@ -60,7 +61,8 @@ export async function generateJS(
|
||||
async function getMetaEntries(collection: MetaCollection, files: FileInfo[]) {
|
||||
const items = files.map(async (file) => {
|
||||
const source = await readFileWithCache(file.absolutePath).catch(() => '');
|
||||
let data = source.length === 0 ? {} : JSON.parse(source);
|
||||
let data =
|
||||
source.length === 0 ? {} : parseMetaEntry(file.absolutePath, source);
|
||||
|
||||
if (collection?.schema) {
|
||||
data = await validate(
|
||||
@@ -237,3 +239,17 @@ export function toImportPath(file: string, dir: string): string {
|
||||
|
||||
return importPath.replaceAll(path.sep, '/');
|
||||
}
|
||||
|
||||
function parseMetaEntry(file: string, content: string) {
|
||||
const extname = path.extname(file);
|
||||
try {
|
||||
if (extname === '.json') return JSON.parse(content);
|
||||
if (extname === '.yaml') return load(content);
|
||||
} catch (e) {
|
||||
throw new Error(`Failed to parse meta file: ${file}.`, {
|
||||
cause: e,
|
||||
});
|
||||
}
|
||||
|
||||
throw new Error(`Unknown meta file format: ${extname}, in ${file}.`);
|
||||
}
|
||||
|
||||
Generated
+9
-3
Reference in New Issue
Block a user