refactor: commands from menu

This commit is contained in:
2023-08-04 13:48:12 +02:00
parent b95cfcc5b4
commit f9638c35fc
12 changed files with 317 additions and 114 deletions

View File

@ -1,7 +1,8 @@
import { parseDocument, renderMarkdown } from "@lib/documents.ts";
import { parse } from "yaml";
import { parse, stringify } from "yaml";
import { createCrud } from "@lib/crud.ts";
import { extractHashTags } from "@lib/string.ts";
import { extractHashTags, formatDate } from "@lib/string.ts";
import { fixRenderedMarkdown } from "@lib/helpers.ts";
export type Movie = {
id: string;
@ -18,6 +19,27 @@ export type Movie = {
};
};
function renderMovie(movie: Movie) {
const meta = movie.meta;
if ("date" in meta) {
meta.date = formatDate(meta.date);
}
return fixRenderedMarkdown(`${
meta
? `---
${stringify(meta)}
---`
: `---
---`
}
# ${movie.name}
${movie.meta.image ? `![](${movie.meta.image})` : ""}
${movie.tags.map((t) => `#${t}`).join(" ")}
${movie.description}
`);
}
export function parseMovie(original: string, id: string): Movie {
const doc = parseDocument(original);
@ -80,3 +102,8 @@ const crud = createCrud<Movie>({
export const getMovie = crud.read;
export const getAllMovies = crud.readAll;
export const createMovie = (movie: Movie) => {
console.log("creating movie", { movie });
const content = renderMovie(movie);
return crud.create(movie.id, content);
};