Skip to content

Commit 269accb

Browse files
author
ComputelessComputer
committed
persist city in note frontmatter
1 parent 47af2ec commit 269accb

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

src-tauri/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/storage.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ import { DailyNote, getDaysAgo, } from "../types/note";
33
import { resolveMarkdownImages, unresolveMarkdownImages, } from "./images";
44
import { getNoteDir, getNotePath, } from "./paths";
55

6+
const FRONTMATTER_RE = /^---\n([\s\S]*?)\n---\n?/;
7+
8+
function parseFrontmatter(raw: string,): { city: string | null; body: string; } {
9+
const match = raw.match(FRONTMATTER_RE,);
10+
if (!match) return { city: null, body: raw, };
11+
const cityMatch = match[1].match(/^city:\s*(.+)$/m,);
12+
return {
13+
city: cityMatch ? cityMatch[1].trim() : null,
14+
body: raw.slice(match[0].length,),
15+
};
16+
}
17+
18+
function buildFrontmatter(city: string | null | undefined, body: string,): string {
19+
if (!city) return body;
20+
return `---\ncity: ${city}\n---\n${body}`;
21+
}
22+
623
async function ensureDir(dir: string,) {
724
const dirExists = await exists(dir,);
825
if (!dirExists) {
@@ -42,9 +59,9 @@ export async function saveDailyNote(note: DailyNote,): Promise<void> {
4259
await ensureDir(noteDir,);
4360
const filepath = await getNotePath(note.date,);
4461
// Convert asset:// URLs back to relative paths, strip ZWSP, normalize blanks
45-
let markdown = normalizeForSave(stripNbsp(unresolveMarkdownImages(note.content,),),);
46-
if (!markdown.endsWith("\n",)) markdown += "\n";
47-
await writeTextFile(filepath, markdown,);
62+
let body = normalizeForSave(stripNbsp(unresolveMarkdownImages(note.content,),),);
63+
if (!body.endsWith("\n",)) body += "\n";
64+
await writeTextFile(filepath, buildFrontmatter(note.city, body,),);
4865
}
4966

5067
export async function loadDailyNote(date: string,): Promise<DailyNote | null> {
@@ -56,11 +73,12 @@ export async function loadDailyNote(date: string,): Promise<DailyNote | null> {
5673
}
5774

5875
const raw = await readTextFile(filepath,);
76+
const { city, body, } = parseFrontmatter(raw,);
5977
// Strip &nbsp; from existing files, resolve asset paths, then
6078
// convert blank lines into ZWSP paragraphs so TipTap shows them.
61-
const resolved = await resolveMarkdownImages(stripNbsp(raw,),);
79+
const resolved = await resolveMarkdownImages(stripNbsp(body,),);
6280
const content = preserveBlankLines(resolved,);
63-
return { date, content, };
81+
return { date, content, city, };
6482
}
6583

6684
export async function createEmptyDailyNote(date: string,): Promise<DailyNote> {

src/types/note.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface DailyNote {
22
date: string; // ISO date string (YYYY-MM-DD)
33
content: string; // markdown
4+
city?: string | null;
45
}
56

67
function toLocalDateString(d: Date,): string {

0 commit comments

Comments
 (0)