Refactor: Extract shared utils

This commit is contained in:
2026-02-26 17:38:58 -08:00
parent 32419c3db9
commit 27d75121f3

40
scripts/lib/utils.mjs Normal file
View File

@@ -0,0 +1,40 @@
export const fmtDate = (date) =>
new Date(date).toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" });
export const toISODate = (d) => new Date(d).toISOString().split("T")[0];
export const escapeHtml = (s = "") =>
s.replace(/[&<>"']/g, (ch) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[ch]));
export const escapeXml = (s = "") =>
s.replace(/[<>&'"]/g, (ch) => ({ "<": "&lt;", ">": "&gt;", "&": "&amp;", "'": "&apos;", '"': "&quot;" }[ch]));
export const fixInternalLinks = (html) =>
html
.replace(/href="\.\//g, 'href="/')
.replace(/href="([^"]+)"(?=[^>]*>)/g, (_, href) =>
href.startsWith("http") || href.startsWith("#") || href.endsWith("/") || href.endsWith(".md")
? `href="${href}"`
: `href="${href}/"`
);
export const renderAuthorInline = (author = "Apophenia") => {
const src = String(author || "Apophenia");
const re = /\[([^\]]+)\]\(([^)]+)\)/g;
let out = "";
let last = 0;
let match;
while ((match = re.exec(src))) {
out += escapeHtml(src.slice(last, match.index));
const text = escapeHtml(match[1].trim());
const href = match[2].trim();
const safeHref = /^(https?:\/\/|\/)/.test(href) ? href : "#";
const normalized = safeHref === "#" || safeHref.endsWith("/") || safeHref.startsWith("http") ? safeHref : `${safeHref}/`;
out += `<a href="${escapeHtml(normalized)}">${text}</a>`;
last = re.lastIndex;
}
out += escapeHtml(src.slice(last));
return out;
};