Add basic support for markdown sources
This commit is contained in:
parent
1848d3cfb6
commit
1c64a941cd
9 changed files with 118 additions and 50 deletions
|
@ -4,9 +4,11 @@ import {
|
|||
AnyResourceSchema,
|
||||
ResourceMap,
|
||||
} from "./playbill-schema";
|
||||
import { extractFrontmatter, extractMarkdown } from "./markdown";
|
||||
|
||||
export class ResourceFileError extends Error { }
|
||||
export class NullResourceError extends Error { }
|
||||
type FileFormat = "yaml" | "markdown";
|
||||
export class ResourceFileError extends Error {}
|
||||
export class NullResourceError extends Error {}
|
||||
|
||||
/**
|
||||
* Load and process all documents in a yaml file at the given path
|
||||
|
@ -20,34 +22,49 @@ export async function loadResourceFile(
|
|||
filePath: string,
|
||||
): Promise<AnyResource[]> {
|
||||
const file = Bun.file(filePath);
|
||||
const format: FileFormat =
|
||||
filePath.split(".").pop() === "md" ? "markdown" : "yaml";
|
||||
const text = await file.text();
|
||||
|
||||
const parsedDocs = YAML.parseAllDocuments(text);
|
||||
if (format === "yaml") {
|
||||
const parsedDocs = YAML.parseAllDocuments(text);
|
||||
|
||||
if (parsedDocs.some((doc) => doc.toJS() === null)) {
|
||||
throw new NullResourceError(`Null resource defined in ${filePath}`);
|
||||
}
|
||||
|
||||
const errors = parsedDocs.flatMap((doc) => doc.errors);
|
||||
|
||||
if (errors.length > 0) {
|
||||
throw new ResourceFileError(`Error parsing ${filePath}: ${errors}`);
|
||||
}
|
||||
|
||||
const collection: AnyResource[] = [];
|
||||
|
||||
for (const doc of parsedDocs) {
|
||||
if (doc.errors.length > 0) {
|
||||
throw new ResourceFileError(`Error parsing ${filePath}: ${doc.errors}`);
|
||||
if (parsedDocs.some((doc) => doc.toJS() === null)) {
|
||||
throw new NullResourceError(`Null resource defined in ${filePath}`);
|
||||
}
|
||||
|
||||
const raw = doc.toJS();
|
||||
const parsed = AnyResourceSchema.parse(raw);
|
||||
const type = parsed.$define;
|
||||
const schemaToUse = ResourceMap[type];
|
||||
const validated = schemaToUse.parse(parsed);
|
||||
collection.push(validated);
|
||||
const errors = parsedDocs.flatMap((doc) => doc.errors);
|
||||
|
||||
if (errors.length > 0) {
|
||||
throw new ResourceFileError(`Error parsing ${filePath}: ${errors}`);
|
||||
}
|
||||
|
||||
const collection: AnyResource[] = [];
|
||||
|
||||
for (const doc of parsedDocs) {
|
||||
if (doc.errors.length > 0) {
|
||||
throw new ResourceFileError(`Error parsing ${filePath}: ${doc.errors}`);
|
||||
}
|
||||
|
||||
const raw = doc.toJS();
|
||||
const parsed = AnyResourceSchema.parse(raw);
|
||||
const type = parsed.$define;
|
||||
const schemaToUse = ResourceMap[type];
|
||||
const validated = schemaToUse.parse(parsed);
|
||||
collection.push(validated);
|
||||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
return collection;
|
||||
const frontmatter = extractFrontmatter(text);
|
||||
const markdown = extractMarkdown(text);
|
||||
|
||||
const together = {
|
||||
...frontmatter,
|
||||
description: markdown,
|
||||
};
|
||||
|
||||
const parsed = AnyResourceSchema.parse(together);
|
||||
return [parsed];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue