Files
2026-06-20 21:53:16 +10:00

221 lines
8.4 KiB
TypeScript

import { marked } from 'marked';
import { escapeHtml } from './responses';
export type CustomLinkLike = {
alias: string;
content_markdown: string | null;
scope: 'public' | 'private';
};
marked.use({
renderer: {
link({ href, title, tokens }) {
const url = String(href ?? '');
if (!/^https?:\/\//i.test(url)) {
return this.parser.parseInline(tokens);
}
const safeTitle = title ? ` title="${escapeHtml(String(title))}"` : '';
return `<a href="${escapeHtml(url)}"${safeTitle} rel="noopener noreferrer" target="_blank">${this.parser.parseInline(tokens)}</a>`;
},
image({ href, title, text }) {
const src = String(href ?? '');
if (!/^https?:\/\//i.test(src)) {
return escapeHtml(String(text ?? ''));
}
const alt = escapeHtml(String(text ?? ''));
const safeTitle = title ? ` title="${escapeHtml(String(title))}"` : '';
return `<img src="${escapeHtml(src)}" alt="${alt}"${safeTitle} loading="lazy">`;
},
},
walkTokens(token) {
// Neutralize raw HTML in markdown by demoting html tokens to text. Marked
// then escapes their content, so <script> tags are shown literally instead
// of being emitted as live HTML.
if (token.type === 'html') {
token.type = 'text';
token.text = token.raw;
}
},
});
export function renderCustomLinkHtml(link: CustomLinkLike): string {
const content = renderMarkdown(link.content_markdown ?? '');
const alias = escapeHtml(link.alias);
const isPrivate = link.scope === 'private';
const scope = isPrivate ? 'private' : 'public';
const scopeIcon = isPrivate ? LOCK_ICON_SVG : GLOBE_ICON_SVG;
return `<!doctype html><html lang="en" data-scope="${scope}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${alias}</title><style>${CUSTOM_PAGE_STYLES}</style></head><body>
<header class="cp-header">
<div class="cp-header__inner">
<span class="cp-scope cp-scope--${scope}">
${scopeIcon}
<span class="cp-alias">/${alias}</span>
</span>
</div>
</header>
<main class="cp-main">
<article class="cp-card">${content}</article>
</main>
<footer class="cp-footer"><span>Published with <strong>Heygo</strong></span></footer>
</body></html>`;
}
export function renderMarkdown(markdown: string): string {
return marked.parse(markdown ?? '', { async: false }) as string;
}
const GLOBE_ICON_SVG = `<svg class="cp-scope__icon" viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18"/></svg>`;
const LOCK_ICON_SVG = `<svg class="cp-scope__icon" viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg>`;
const CUSTOM_PAGE_STYLES = `
:root {
--cp-text: #0F172A;
--cp-muted: #64748B;
--cp-surface: #ffffff;
--cp-border: #E2E8F0;
--cp-code-bg: #F1F5F9;
--cp-link: #4338CA;
--cp-radius: 14px;
color-scheme: light dark;
}
html[data-scope="public"] {
--cp-accent: #15803D;
--cp-accent-soft: #F2FFF6;
--cp-accent-border: rgba(22,163,74,0.18);
}
html[data-scope="private"] {
--cp-accent: #B91C1C;
--cp-accent-soft: #FFF4F4;
--cp-accent-border: rgba(220,38,38,0.16);
}
* { box-sizing: border-box; }
body {
margin: 0;
font: 16px/1.65 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--cp-text);
background: #F8FAFC;
background-attachment: fixed;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
}
/* ── Scope header ── */
.cp-header {
position: sticky;
top: 0;
z-index: 10;
background: color-mix(in srgb, var(--cp-surface) 88%, transparent);
backdrop-filter: saturate(180%) blur(12px);
-webkit-backdrop-filter: saturate(180%) blur(12px);
border-bottom: 1px solid var(--cp-accent-border);
}
.cp-header__inner {
max-width: 760px;
margin: 0 auto;
padding: 0.6rem 1.25rem;
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
}
.cp-scope {
display: inline-flex;
align-items: center;
gap: 0.4rem;
color: var(--cp-accent);
}
.cp-scope__icon { flex-shrink: 0; }
.cp-alias {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.85rem;
font-weight: 600;
color: var(--cp-text);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* ── Content card ── */
.cp-main { max-width: 760px; margin: 0 auto; padding: 2rem 1.25rem 3rem; }
.cp-card {
background: var(--cp-surface);
border: 1px solid var(--cp-border);
border-radius: var(--cp-radius);
box-shadow: 0 1px 2px rgba(15,23,42,0.04), 0 8px 30px rgba(15,23,42,0.06);
padding: 2.5rem 2.75rem;
min-width: 0;
max-width: 100%;
overflow-wrap: anywhere;
word-break: break-word;
}
@media (max-width: 640px) {
.cp-card { padding: 1.75rem 1.25rem; }
.cp-main { padding: 1rem 0.75rem 2rem; }
.cp-header__inner { padding: 0.5rem 0.75rem; }
}
/* ── Markdown typography ── */
.cp-card > :first-child { margin-top: 0; }
.cp-card > :last-child { margin-bottom: 0; }
.cp-card h1, .cp-card h2, .cp-card h3,
.cp-card h4, .cp-card h5, .cp-card h6 {
line-height: 1.25; margin: 1.6em 0 0.55em; font-weight: 700; letter-spacing: -0.015em;
}
.cp-card h1 { font-size: 1.9rem; margin-top: 0; }
.cp-card h2 { font-size: 1.45rem; }
.cp-card h3 { font-size: 1.2rem; }
.cp-card h4 { font-size: 1.02rem; }
.cp-card p { margin: 0.75em 0; overflow-wrap: anywhere; }
.cp-card a { color: var(--cp-link); text-decoration: underline; text-underline-offset: 2px; overflow-wrap: anywhere; }
.cp-card a:hover { color: var(--cp-accent); }
.cp-card img { max-width: 100%; height: auto; border-radius: 10px; margin: 0.75em 0; }
.cp-card ul, .cp-card ol { padding-left: 1.4em; margin: 0.65em 0; }
.cp-card li { margin: 0.22em 0; overflow-wrap: anywhere; }
.cp-card li::marker { color: var(--cp-accent); }
.cp-card code {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.86em; background: var(--cp-code-bg);
padding: 0.15em 0.38em; border-radius: 5px; border: 1px solid var(--cp-border);
overflow-wrap: anywhere;
}
.cp-card pre {
background: #0F172A; color: #E2E8F0; padding: 1.1rem 1.35rem;
border-radius: 10px; overflow-x: auto; margin: 1.1em 0; font-size: 0.875rem;
max-width: 100%;
}
.cp-card pre code { background: transparent; color: inherit; padding: 0; border: none; font-size: inherit; overflow-wrap: normal; }
.cp-card blockquote {
border-left: 3px solid var(--cp-accent); margin: 1.1em 0; padding: 0.3em 1.1em;
color: var(--cp-muted); background: var(--cp-accent-soft); border-radius: 0 8px 8px 0;
overflow-wrap: anywhere;
}
.cp-card hr { border: none; border-top: 1px solid var(--cp-border); margin: 1.75em 0; }
.cp-card table { border-collapse: collapse; width: 100%; margin: 1.1em 0; font-size: 0.9rem; display: block; overflow-x: auto; max-width: 100%; }
.cp-card th, .cp-card td { border: 1px solid var(--cp-border); padding: 0.5em 0.8em; text-align: left; }
.cp-card th { background: var(--cp-code-bg); font-weight: 600; }
.cp-card kbd {
font-family: ui-monospace, monospace; font-size: 0.8em;
border: 1px solid var(--cp-border); border-bottom-width: 2px;
border-radius: 5px; padding: 0.12em 0.42em; background: var(--cp-surface);
}
/* ── Footer ── */
.cp-footer {
max-width: 760px; margin: 0 auto; padding: 0 1.25rem 2.5rem;
text-align: center; color: var(--cp-muted); font-size: 0.78rem;
}
.cp-footer strong { color: var(--cp-accent); font-weight: 700; }
/* ── Dark mode ── */
@media (prefers-color-scheme: dark) {
:root { --cp-text: #E2E8F0; --cp-muted: #94A3B8; --cp-surface: #1E293B; --cp-border: #334155; --cp-code-bg: #334155; --cp-link: #A5B4FC; }
body { background: #0F172A; }
.cp-header { background: color-mix(in srgb, #1E293B 88%, transparent); }
.cp-card { box-shadow: 0 1px 2px rgba(0,0,0,0.2), 0 8px 30px rgba(0,0,0,0.25); }
.cp-card th { background: var(--cp-code-bg); }
}
`;