2024-02-27 06:15:27 -05:00
|
|
|
import type { VFile, VFileData as Data } from 'vfile';
|
2023-01-03 16:31:19 -05:00
|
|
|
import type { MarkdownAstroData } from './types.js';
|
|
|
|
|
|
|
|
function isValidAstroData(obj: unknown): obj is MarkdownAstroData {
|
|
|
|
if (typeof obj === 'object' && obj !== null && obj.hasOwnProperty('frontmatter')) {
|
|
|
|
const { frontmatter } = obj as any;
|
|
|
|
try {
|
|
|
|
// ensure frontmatter is JSON-serializable
|
|
|
|
JSON.stringify(frontmatter);
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return typeof frontmatter === 'object' && frontmatter !== null;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class InvalidAstroDataError extends TypeError {}
|
|
|
|
|
|
|
|
export function safelyGetAstroData(vfileData: Data): MarkdownAstroData | InvalidAstroDataError {
|
|
|
|
const { astro } = vfileData;
|
|
|
|
|
|
|
|
if (!astro || !isValidAstroData(astro)) {
|
|
|
|
return new InvalidAstroDataError();
|
|
|
|
}
|
|
|
|
|
|
|
|
return astro;
|
|
|
|
}
|
|
|
|
|
2023-09-14 07:05:38 -05:00
|
|
|
export function setVfileFrontmatter(vfile: VFile, frontmatter: Record<string, any>) {
|
|
|
|
vfile.data ??= {};
|
|
|
|
vfile.data.astro ??= {};
|
|
|
|
(vfile.data.astro as any).frontmatter = frontmatter;
|
2023-09-14 05:22:16 -05:00
|
|
|
}
|