fix: add null checks to parsers and fix docs path

This commit is contained in:
2026-03-18 16:13:57 +08:00
parent ff48936402
commit 4898ee5434
8 changed files with 20 additions and 4 deletions

View File

@@ -5,12 +5,13 @@ import { parseMarkdown, buildFileTree } from '@/lib/parser'
import type { DocFile, ParsedDoc } from '@/lib/types' import type { DocFile, ParsedDoc } from '@/lib/types'
import { config } from '@/config' import { config } from '@/config'
const modules = import.meta.glob('../docs/**/*.md', { as: 'raw', eager: true }) const modules = import.meta.glob('../docs/api/**/*.md', { as: 'raw', eager: true })
const DOCS_FILES: Record<string, string> = Object.fromEntries( const DOCS_FILES: Record<string, string> = Object.fromEntries(
Object.entries(modules).map(([path, content]) => { Object.entries(modules).map(([path, content]) => {
const relativePath = path.replace('../docs/', '') const relativePath = path.replace('../docs/api/', '')
return [relativePath, content as string] const fileContent = typeof content === 'string' ? content : ''
return [relativePath, fileContent]
}) })
) )

View File

@@ -41,12 +41,14 @@ export interface BlueprintData {
} }
function extractYamlBlock(markdown: string, sectionName: string): string { function extractYamlBlock(markdown: string, sectionName: string): string {
if (!markdown || typeof markdown !== 'string') return '';
const pattern = new RegExp(`# ${sectionName}[\\s\\S]*?\`\`\`yaml\\n([\\s\\S]*?)\n\`\`\``, 'i'); const pattern = new RegExp(`# ${sectionName}[\\s\\S]*?\`\`\`yaml\\n([\\s\\S]*?)\n\`\`\``, 'i');
const match = markdown.match(pattern); const match = markdown.match(pattern);
return match ? match[1].trim() : ''; return match ? (match[1] || '').trim() : '';
} }
function parseSubsystems(yaml: string): Subsystem[] { function parseSubsystems(yaml: string): Subsystem[] {
if (!yaml || typeof yaml !== 'string') return [];
const subsystems: Subsystem[] = []; const subsystems: Subsystem[] = [];
const lines = yaml.split('\n'); const lines = yaml.split('\n');
@@ -159,6 +161,7 @@ function parseSubsystems(yaml: string): Subsystem[] {
} }
function parseModules(yaml: string): Module[] { function parseModules(yaml: string): Module[] {
if (!yaml || typeof yaml !== 'string') return [];
const modules: Module[] = []; const modules: Module[] = [];
const lines = yaml.split('\n'); const lines = yaml.split('\n');
@@ -270,6 +273,14 @@ function parseModules(yaml: string): Module[] {
} }
export function parseBlueprintFromMd(markdown: string): BlueprintData { export function parseBlueprintFromMd(markdown: string): BlueprintData {
if (!markdown || typeof markdown !== 'string') {
return {
meta: { name: 'Unknown', version: '0.0.0', type: 'unknown', description: '', target_runtime: '' },
subsystems: [],
modules: []
};
}
const metaYaml = extractYamlBlock(markdown, 'SYSTEM_META'); const metaYaml = extractYamlBlock(markdown, 'SYSTEM_META');
const structureYaml = extractYamlBlock(markdown, 'SYSTEM_STRUCTURE'); const structureYaml = extractYamlBlock(markdown, 'SYSTEM_STRUCTURE');

View File

@@ -8,6 +8,10 @@ const HR_REGEX = /^---+$/
const REFERENCE_REGEX = /@see\s+([^\s]+)/g const REFERENCE_REGEX = /@see\s+([^\s]+)/g
export function parseMarkdown(content: string): ParsedDoc { export function parseMarkdown(content: string): ParsedDoc {
if (!content || typeof content !== 'string') {
return { title: '', metadata: {}, sections: [], references: [] };
}
const lines = content.split('\n') const lines = content.split('\n')
const result: ParsedDoc = { const result: ParsedDoc = {
title: '', title: '',