Skip to content

Commit

Permalink
Add fallback for metadata extraction (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
spring-raining authored Nov 18, 2024
2 parents 7043187 + 1b5e15c commit 8647d50
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/processor/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }),
Expand All @@ -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'));
}

0 comments on commit 8647d50

Please sign in to comment.