// shared by the pages functions and the email worker export const SITE = "https://danieldaum.net"; export const REPLY_TO = "reply@replies.danieldaum.net"; // /about/, /now/, /now//, /blog// (/projects/ is showcase, see functions/projects/) export function isLikeable(path) { return /^\/(about|now)\/$|^\/(blog|now)\/[a-z0-9-]+\/$/.test(path); } // ===== hmac ===== export async function hmac(secret, message) { if (!secret) throw new Error("SOCIAL_SECRET is not set"); const enc = new TextEncoder(); const key = await crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]); const sig = await crypto.subtle.sign("HMAC", key, enc.encode(message)); return [...new Uint8Array(sig)].map((b) => b.toString(16).padStart(2, "0")).join(""); } // constant time export async function hmacMatches(secret, message, sig) { const expected = await hmac(secret, message); if (typeof sig !== "string" || sig.length !== expected.length) return false; let diff = 0; for (let i = 0; i < sig.length; i++) diff |= sig.charCodeAt(i) ^ expected.charCodeAt(i); return diff === 0; } // one like per visitor per page per day: ip + browser + date, hashed. // no cookies, nothing reversible, stops matching at utc midnight export function voterId(secret, request) { const ip = request.headers.get("CF-Connecting-IP") || ""; const ua = request.headers.get("User-Agent") || ""; return hmac(secret, `${ip}|${ua}|${new Date().toISOString().slice(0, 10)}`); } // ===== html ===== export function escapeHtml(str) { return String(str).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); } // blank lines split paragraphs, single newlines become
export function bodyHtml(body) { return body .split(/\n{2,}/) .map((para) => `

${escapeHtml(para).replace(/\n/g, "
")}

`) .join(""); } // what goes inside [data-social]. key has passed isLikeable(), so it is safe as-is export function renderSocial(key, likes, rows) { const replies = new Map(); for (const row of rows) { if (row.parent) replies.set(row.parent, [...(replies.get(row.parent) || []), row]); } const top = rows.filter((row) => !row.parent); const title = rows.length === 0 ? "no comments yet" : rows.length === 1 ? "1 comment" : `${rows.length} comments`; const thread = (list) => `
    ${list.map((row) => `
  1. ${comment(row)}
  2. `).join("")}
`; const comment = (row) => { const id = `comment-${row.id}`; const kids = replies.get(row.id); return ``; }; return ` ${top.length ? thread(top) : ""} `; } function likeForm(key, { count, liked }) { const noun = count === 1 ? "like" : "likes"; const label = liked ? `you liked this post, ${count} ${noun}` : `like this post, ${count} ${noun}`; const state = liked ? ` disabled aria-pressed="true"` : ""; return ``; } // feather icons, mit const HEART = ``; // the email worker reads the subject to know where a comment goes function mailto(target) { const subject = encodeURIComponent(`re: ${SITE}${target}`); return `mailto:${REPLY_TO}?subject=${subject}&body=${encodeURIComponent("name: ")}`; } // "4 Sep 2026 ยท 09:12", oakland time function when(iso) { const p = Object.fromEntries( new Intl.DateTimeFormat("en-GB", { timeZone: "America/Los_Angeles", day: "numeric", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit", hourCycle: "h23", }) .formatToParts(new Date(iso)) .map((part) => [part.type, part.value]) ); return `${p.day} ${p.month} ${p.year} · ${p.hour}:${p.minute}`; }