// Name: Format HTML Diff from Clipboard JSON // Description: Format before/after HTML from clipboard JSON into a Markdown doc and alert if they differ. // Author: tayiorbeii // GitHub: tayiorbeii import "@johnlindquist/kit"; const raw = await clipboard.readText(); if (!raw?.trim()) { await notify({ title: "Clipboard Error", body: "Clipboard is empty or not text." }); exit(); } let data: any; try { data = JSON.parse(raw); } catch { await notify({ title: "Clipboard Error", body: "Clipboard does not contain valid JSON." }); exit(); } const keys = Object.keys(data || {}); if (!keys.length) { await notify({ title: "JSON Error", body: "Parsed JSON has no keys." }); exit(); } const norm = (s: string) => s.toLowerCase().replace(/[^a-z0-9]/g, ""); const findKey = (candidates: string[]) => { for (const k of keys) { const nk = norm(k); if (candidates.includes(nk)) return k; } return undefined; }; const beforeCandidates = [ "beforehtml", "before", "htmlbefore", "before_innerhtml", "beforeinnerhtml", "previoushtml", "oldhtml", ]; const afterCandidates = [ "afterhtml", "after", "htmlafter", "after_innerhtml", "afterinnerhtml", "newhtml", ]; let beforeKey = findKey(beforeCandidates); let afterKey = findKey(afterCandidates); if (!beforeKey) { beforeKey = await arg("Select the Before HTML key", keys); } if (!afterKey) { afterKey = await arg("Select the After HTML key", keys.filter(k => k !== beforeKey)); } const toStr = (v: any) => (typeof v === "string" ? v : v == null ? "" : String(v)); const normalizeEol = (s: string) => s.replace(/\r\n/g, "\n"); const beforeHtml = normalizeEol(toStr(data[beforeKey])); const afterHtml = normalizeEol(toStr(data[afterKey])); const formatted = [ "## Before HTML", "", beforeHtml, "", "", "## After HTML", "", afterHtml, "", ].join("\n"); await clipboard.writeText(formatted); const different = beforeHtml.trim() !== afterHtml.trim(); await notify({ title: "HTML Comparison", body: different ? "Before and After HTML are different." : "Before and After HTML are identical.", }); await toast("Formatted Markdown copied to clipboard.", { autoClose: 2500 });