import * as frontMatter from "@std/front-matter"; import { Notebook } from "./notebook.ts"; export interface BaseEntry { notebook: Notebook; folders: string[]; filename: string; title: string; lastModified: Date; pinned: boolean; } export interface TodoEntry extends BaseEntry { type: "todo"; status: "open" | "closed"; } export type GenericEntryType = | "note" | "directory" | "bookmark" | "image" | "file"; export interface GenericEntry extends BaseEntry { type: GenericEntryType; } export type EntryType = GenericEntryType | "todo"; export type Entry = TodoEntry | GenericEntry; export interface NoteContents { // deno-lint-ignore no-explicit-any attrs?: any; body: string; } export function suffixFor(filename: string): string { switch (inferredType(filename)) { case "bookmark": return ".bookmark.md"; case "todo": return ".todo.md"; default: return ".md"; } } export function inferredType(filename: string): EntryType { if (filename.endsWith(".bookmark.md")) { return "bookmark"; } else if (filename.endsWith(".todo.md")) { return "todo"; } else if ( [".jpg", ".jpeg", ".png", ".gif"].some((ext) => filename.endsWith(ext)) ) { return "image"; } else { return "note"; } } export function getRawTitle(content: string): string | null { if (frontMatter.test(content)) { const extract = frontMatter.extractYaml(content); const title = (extract.attrs as { title?: string }).title; if (title != null) return title; } const [firstLine] = content.split("\n", 1); if (firstLine.startsWith("# ")) { return firstLine.slice(2); } return null; } export function getTitleFor(item: string, content: string): string { return getRawTitle(content) ?? `${item} ยท "${content.split("\n", 1)[0]}"`; } export function iconForEntry(entry: Entry): string { let icon = ""; if (entry.pinned) { icon = "๐Ÿ“Œ "; } switch (entry.type) { case "bookmark": icon += "๐Ÿ”– "; break; case "directory": icon += "๐Ÿ“‚ "; break; case "todo": icon += entry.status === "open" ? "โœ”๏ธ " : "โœ… "; break; case "note": break; case "image": icon += "๐Ÿ–ผ๏ธ "; break; case "file": icon += "๐Ÿ“„ "; break; default: entry satisfies never; } return icon; }