import * as yaml from "@std/yaml"; import { Command } from "@cliffy/command"; import { Hsh } from "../hsh.ts"; import { ensureSuffix } from "../utils.ts"; import { toSnakeCase } from "@std/text/to-snake-case"; import { format } from "@std/datetime/format"; function register(cmd: Command, hsh: Hsh) { cmd .command("bookmark", "add a bookmark") .alias("book") .description("manage bookmarks") .arguments(" [path:string]") .option("--skip-fetch", "do not fetch the page") .action(async ({ skipFetch }, url, path) => { const p = path != null ? hsh.parse(path) : hsh.getNotebook().newPath(); let md = ""; let title: string | undefined; if (!skipFetch) { const { Readability } = await import("@mozilla/readability"); const { DOMParser } = await import("@b-fuze/deno-dom"); const { default: TurndownService } = await import("turndown"); const u = new URL(url); const content = await (await fetch(u)).text(); const dom = new DOMParser().parseFromString(content, "text/html"); const readable = new Readability(dom); const parsed = readable.parse(); if (parsed == null) { console.error("WARNING: could not parse the upstream HTML page"); } else { md = new TurndownService().turndown(parsed.content); title = dom.title; } } const frontMatter = { title, type: "bookmark", url, }; p.entry = ensureSuffix( toSnakeCase(title ?? format(new Date(), "yyyyMMddHHmmss")), ".bookmark.md", ); p.write(`---\n${yaml.stringify(frontMatter)}---\n\n${md}`); await hsh.emitNotebookChanged?.(p.notebook, `[hsh]: Bookmarked ${url}`); }); } export default { register };