From 1b5e15c1a21c71278c9363af12ab320a9e59d88e Mon Sep 17 00:00:00 2001 From: Koutaro Mukai Date: Mon, 18 Nov 2024 12:29:28 +0900 Subject: [PATCH] Add fallback for metadata extraction --- src/processor/markdown.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/processor/markdown.ts b/src/processor/markdown.ts index 6848d3d2..3d553219 100644 --- a/src/processor/markdown.ts +++ b/src/processor/markdown.ts @@ -14,6 +14,16 @@ export interface VSFile extends VFile { }; } +function safeReadMetadata(content: string): Metadata { + // The input assumes VFM format, but errors during metadata extraction + // should be suppressed to allow processing of non-VFM files as well. + try { + return readMetadata(content); + } catch { + return {}; + } +} + export async function processMarkdown( documentProcessorFactory: DocumentProcessorFactory, filepath: string, @@ -22,7 +32,7 @@ export async function processMarkdown( const markdownString = fs.readFileSync(filepath, 'utf8'); const processor = documentProcessorFactory( options, - readMetadata(markdownString), + safeReadMetadata(markdownString), ); const processed = (await processor.process( vfile({ path: filepath, contents: markdownString }), @@ -31,5 +41,5 @@ export async function processMarkdown( } export function readMarkdownMetadata(filepath: string): Metadata { - return readMetadata(fs.readFileSync(filepath, 'utf8')); + return safeReadMetadata(fs.readFileSync(filepath, 'utf8')); }