2023-08-01 17:50:00 +02:00
|
|
|
import { parseDocument, renderMarkdown } from "@lib/documents.ts";
|
|
|
|
import { parse } from "yaml";
|
|
|
|
import { createCrud } from "@lib/crud.ts";
|
2023-08-02 01:58:03 +02:00
|
|
|
import { extractHashTags } from "@lib/string.ts";
|
2023-07-30 23:55:51 +02:00
|
|
|
|
|
|
|
export type Movie = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
2023-08-02 01:58:03 +02:00
|
|
|
tags: string[];
|
2023-07-30 23:55:51 +02:00
|
|
|
meta: {
|
2023-07-31 17:21:17 +02:00
|
|
|
date: Date;
|
2023-07-30 23:55:51 +02:00
|
|
|
image: string;
|
|
|
|
author: string;
|
|
|
|
rating: number;
|
|
|
|
status: "not-seen" | "watch-again" | "finished";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export function parseMovie(original: string, id: string): Movie {
|
|
|
|
const doc = parseDocument(original);
|
|
|
|
|
|
|
|
let meta = {} as Movie["meta"];
|
|
|
|
let name = "";
|
|
|
|
|
|
|
|
const range = [Infinity, -Infinity];
|
|
|
|
|
|
|
|
for (const child of doc.children) {
|
|
|
|
if (child.type === "yaml") {
|
2023-08-01 17:50:00 +02:00
|
|
|
meta = parse(child.value) as Movie["meta"];
|
2023-07-30 23:55:51 +02:00
|
|
|
|
|
|
|
if (meta["rating"] && typeof meta["rating"] === "string") {
|
|
|
|
meta.rating = [...meta.rating?.matchAll("⭐")].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
child.type === "heading" && child.depth === 1 && !name &&
|
|
|
|
child.children.length === 1 && child.children[0].type === "text"
|
|
|
|
) {
|
|
|
|
name = child.children[0].value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
const start = child.position?.start.offset || Infinity;
|
|
|
|
const end = child.position?.end.offset || -Infinity;
|
|
|
|
if (start < range[0]) range[0] = start;
|
|
|
|
if (end > range[1]) range[1] = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let description = original.slice(range[0], range[1]);
|
2023-08-02 01:58:03 +02:00
|
|
|
const tags = extractHashTags(description);
|
|
|
|
for (const tag of tags) {
|
|
|
|
description = description.replace("#" + tag, "");
|
2023-07-30 23:55:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
name,
|
2023-08-02 01:58:03 +02:00
|
|
|
tags,
|
2023-07-30 23:55:51 +02:00
|
|
|
description: renderMarkdown(description),
|
|
|
|
meta,
|
|
|
|
};
|
|
|
|
}
|
2023-08-01 17:50:00 +02:00
|
|
|
|
|
|
|
const crud = createCrud<Movie>({
|
|
|
|
prefix: "Media/movies/",
|
|
|
|
parse: parseMovie,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const getMovie = crud.read;
|
|
|
|
export const getAllMovies = crud.readAll;
|