Skip to content

Commit

Permalink
Improve metadata handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielPower committed May 23, 2024
1 parent b77382b commit aa434dd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
35 changes: 29 additions & 6 deletions src/lib/page.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { open } from "fs/promises";
import { parse, stringify } from "yaml";

export const createFileIfNotExists = async (fileName: string) => {
export const createPageIfNotExists = async (fileName: string) => {
const filePath = `pages/${fileName}.md`;
try {
const date = new Date().toISOString();
const file = await open(filePath, "wx+");
await file.writeFile(
`---
title: ${fileName}
date_created: ${date}
date_modified: ${date}
date_visited: ${date}
---`,
{ encoding: "utf-8" },
);
Expand All @@ -26,10 +27,32 @@ date_modified: ${date}
}
};

export const getFileInfo = async (fileName: string) => {
createFileIfNotExists(fileName);
export const getPageInfo = async (fileName: string) => {
createPageIfNotExists(fileName);
const file = await open(`pages/${fileName}.md`, "r");
const content = await file.readFile({ encoding: "utf-8" });
const body = await file.readFile({ encoding: "utf-8" });
const [, preamble, content] = body.split("---").map((s) => s.trim());
const metadata = parse(preamble);
await file.close();
return { content, metadata };
};

export const writePage = async (
fileName: string,
options: { content?: string; metadata?: object },
) => {
const { content: oldContent, metadata: oldMetadata } = await getPageInfo(fileName);
const file = await open(`pages/${fileName}.md`, "w");
await file.writeFile(
[
"---",
stringify(Object.assign(oldMetadata, options.metadata)).trim(),
"---",
options.content || oldContent,
].join("\n"),
{
encoding: "utf-8",
},
);
await file.close();
return { content };
};
5 changes: 2 additions & 3 deletions src/routes/api/save/[page]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { writeFile } from "fs/promises";
import type { RequestHandler } from "./$types";
import { writePage } from "$lib/page";

export const PUT: RequestHandler = async ({ request, params }) => {
// write fileContents from request to a file with the name fileName from request
const { fileContents } = await request.json();
await writeFile(`pages/${params.page}.md`, fileContents, "utf-8");
await writePage(params.page, { content: fileContents });
return new Response(null, { status: 204 });
};
7 changes: 5 additions & 2 deletions src/routes/page/[page]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { getFileInfo } from "$lib/page";
import { getPageInfo, writePage } from "$lib/page";
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async ({ params }) => {
const { content } = await getFileInfo(params.page);
const { content } = await getPageInfo(params.page);
await writePage(params.page, {
metadata: { date_visited: new Date().toISOString() },
});
return {
fileName: params.page,
content,
Expand Down

0 comments on commit aa434dd

Please sign in to comment.