mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
2dd00a0024
* chore: import sort source code, exception for the `astro` package * fix import sorting bug * Update packages/integrations/lit/server.js Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> --------- Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
34 lines
982 B
TypeScript
34 lines
982 B
TypeScript
import type { VFile, VFileData as Data } from 'vfile';
|
|
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;
|
|
}
|
|
|
|
export function setVfileFrontmatter(vfile: VFile, frontmatter: Record<string, any>) {
|
|
vfile.data ??= {};
|
|
vfile.data.astro ??= {};
|
|
(vfile.data.astro as any).frontmatter = frontmatter;
|
|
}
|