360 lines
20 KiB
TypeScript
360 lines
20 KiB
TypeScript
import path from "node:path";
|
|
import fs from "fs-extra";
|
|
import type { CaptureRow, NormalizedConversation } from "./types.js";
|
|
import { artifactAssetsDir, artifactDir, artifactIndexPath } from "./artifacts.js";
|
|
import { config } from "./config.js";
|
|
|
|
const fallbackChatGptIconUrl = "https://chatgpt.com/cdn/assets/favicon-l4nq08hd.svg";
|
|
|
|
function escapeHtml(value: string): string {
|
|
return value
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """);
|
|
}
|
|
|
|
const icons = {
|
|
download: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 4v10m0 0 4-4m-4 4-4-4"/><path d="M5 18.5h14"/></svg>`,
|
|
image: `<svg viewBox="0 0 24 24" aria-hidden="true"><rect x="4" y="5" width="16" height="14" rx="2"/><path d="m7 16 4-4 3 3 2-2 3 3"/><circle cx="8.5" cy="8.5" r="1.2"/></svg>`,
|
|
share: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M8.5 12.5 15.5 8.5M8.5 11.5 15.5 15.5"/><circle cx="6.5" cy="12" r="2.5"/><circle cx="17.5" cy="7.5" r="2.5"/><circle cx="17.5" cy="16.5" r="2.5"/></svg>`,
|
|
chevron: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="m8 10 4 4 4-4"/></svg>`,
|
|
sun: `<svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="4"/><path d="M12 3v2m0 14v2M3 12h2m14 0h2M5.6 5.6 7 7m10 10 1.4 1.4M18.4 5.6 17 7M7 17l-1.4 1.4"/></svg>`,
|
|
moon: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M20 14.6A7.6 7.6 0 0 1 9.4 4 8 8 0 1 0 20 14.6Z"/></svg>`,
|
|
arrowUp: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="m6 14 6-6 6 6"/><path d="M12 8v12"/></svg>`,
|
|
arrowDown: `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="m6 10 6 6 6-6"/><path d="M12 4v12"/></svg>`
|
|
};
|
|
|
|
function css(): string {
|
|
return `
|
|
:root {
|
|
color-scheme: light dark;
|
|
--bg: #ffffff;
|
|
--panel: #ffffff;
|
|
--text: #1f2328;
|
|
--muted: #6b7280;
|
|
--border: #e5e7eb;
|
|
--soft: #f7f7f8;
|
|
--code: #0f172a;
|
|
--code-bg: #f4f4f5;
|
|
--accent: #10a37f;
|
|
--user-bubble: #f4f4f4;
|
|
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", "Noto Sans CJK SC", "Noto Sans CJK JP", "Noto Sans CJK KR", "Noto Sans Thai", "Helvetica Neue", Arial, sans-serif;
|
|
}
|
|
@media (prefers-color-scheme: dark) {
|
|
:root {
|
|
--bg: #212121;
|
|
--panel: #212121;
|
|
--text: #ececec;
|
|
--muted: #a8a8a8;
|
|
--border: #3f3f46;
|
|
--soft: #2f2f2f;
|
|
--code: #e5e7eb;
|
|
--code-bg: #171717;
|
|
--user-bubble: #303030;
|
|
}
|
|
}
|
|
html[data-theme="light"] {
|
|
--bg: #ffffff; --panel: #ffffff; --text: #1f2328; --muted: #6b7280; --border: #e5e7eb; --soft: #f7f7f8; --code: #0f172a; --code-bg: #f4f4f5; --user-bubble: #f4f4f4;
|
|
}
|
|
html[data-theme="dark"] {
|
|
--bg: #212121; --panel: #212121; --text: #ececec; --muted: #a8a8a8; --border: #3f3f46; --soft: #2f2f2f; --code: #e5e7eb; --code-bg: #171717; --user-bubble: #303030;
|
|
}
|
|
* { box-sizing: border-box; }
|
|
body, .topbar, .message.user .content, .content pre, .content :not(pre) > code, .code-block, .code-block .code-header, .content th, .content td, .content blockquote, .content a[data-citation], .downloads, .icon-button, .shot-menu, .shot-menu button, .toast {
|
|
transition: background-color .22s ease, border-color .22s ease, color .22s ease, box-shadow .22s ease;
|
|
}
|
|
body { margin: 0; background: var(--bg); color: var(--text); font-size: 16px; line-height: 1.65; }
|
|
a { color: inherit; }
|
|
.page { min-height: 100vh; }
|
|
.topbar { position: sticky; top: 0; z-index: 2; border-bottom: 1px solid var(--border); background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(12px); }
|
|
.topbar-inner { max-width: 880px; margin: 0 auto; padding: 14px 20px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
|
|
.topbar-actions { display: inline-flex; align-items: center; gap: 8px; min-width: 0; }
|
|
.snapshot-label { font-size: 13px; color: var(--muted); white-space: nowrap; }
|
|
.theme-toggle .moon-icon { display: none; }
|
|
html[data-theme="dark"] .theme-toggle .sun-icon { display: none; }
|
|
html[data-theme="dark"] .theme-toggle .moon-icon { display: block; }
|
|
.title { max-width: 880px; margin: 0 auto; padding: 34px 20px 18px; }
|
|
h1 { margin: 0; font-size: clamp(28px, 5vw, 44px); line-height: 1.12; letter-spacing: 0; }
|
|
.conversation { max-width: 880px; margin: 0 auto; padding: 4px 20px 36px; }
|
|
.message { padding: 18px 0; }
|
|
.message.assistant { display: block; }
|
|
.message.user { display: flex; justify-content: flex-end; }
|
|
.message.user .content-wrap { max-width: min(72%, 620px); }
|
|
.message.user .content { background: var(--user-bubble); border-radius: 18px; padding: 10px 16px; line-height: 1.55; }
|
|
.message.user .content p { margin: 0; }
|
|
.avatar { width: 28px; height: 28px; display: grid; place-items: center; margin: 0 0 10px; }
|
|
.assistant-icon { width: 28px; height: 28px; display: block; border-radius: 50%; }
|
|
.role { display: none; }
|
|
.content { min-width: 0; overflow-wrap: anywhere; }
|
|
.content h2, .content h3, .content h4 { margin: 1.35em 0 0.55em; line-height: 1.35; font-weight: 700; }
|
|
.content h2 { font-size: 1.28em; }
|
|
.content h3 { font-size: 1.12em; }
|
|
.content h4 { font-size: 1em; }
|
|
.content p { margin: 0 0 0.9em; }
|
|
.content p:last-child { margin-bottom: 0; }
|
|
.content ul, .content ol { margin: 0.6em 0 1em; padding-left: 1.45em; }
|
|
.content li { margin: 0.3em 0; padding-left: 0.1em; }
|
|
.content li > p { margin: 0.2em 0; }
|
|
.content li > ul, .content li > ol { margin: 0.35em 0 0.45em; padding-left: 1.25em; }
|
|
.content strong { font-weight: 700; }
|
|
.content pre { margin: 1em 0; overflow: auto; padding: 14px 16px; border-radius: 8px; background: var(--code-bg); color: var(--code); font-size: 14px; line-height: 1.55; white-space: pre; overflow-wrap: normal; word-break: normal; }
|
|
.content code { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Noto Sans Mono", monospace; }
|
|
.content :not(pre) > code { background: var(--code-bg); border-radius: 5px; padding: 0.1em 0.32em; }
|
|
.code-block { margin: 1em 0; border-radius: 8px; background: var(--code-bg); overflow: hidden; }
|
|
.code-block .code-header { min-height: 34px; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 10px 6px 14px; color: var(--muted); font-size: 12px; border-bottom: 1px solid var(--border); }
|
|
.code-block .code-header span:empty::before { content: "code"; }
|
|
.code-block [data-copy-code] { border: 0; background: transparent; color: var(--muted); font: inherit; font-size: 12px; padding: 4px 6px; cursor: pointer; }
|
|
.code-block pre { margin: 0; border-radius: 0; background: transparent; }
|
|
.content table { width: 100%; border-collapse: collapse; margin: 1em 0; display: block; overflow-x: auto; }
|
|
.content th, .content td { border: 1px solid var(--border); padding: 8px 10px; text-align: left; vertical-align: top; }
|
|
.content blockquote { border-left: 3px solid var(--border); margin: 1em 0; padding-left: 14px; color: var(--muted); }
|
|
.content img { max-width: 100%; height: auto; border-radius: 8px; }
|
|
.content a[data-citation] { display: inline-flex; align-items: center; gap: 5px; max-width: min(100%, 260px); min-height: 24px; margin: 0 2px; padding: 2px 8px 2px 5px; border: 1px solid var(--border); border-radius: 999px; background: var(--soft); color: var(--text); font-size: 0.88em; line-height: 1.25; white-space: nowrap; text-decoration: none; vertical-align: -5px; }
|
|
.content a[data-citation]:hover { background: color-mix(in srgb, var(--soft) 82%, var(--text) 18%); }
|
|
.content a[data-citation] img { width: 16px; height: 16px; max-width: 16px; max-height: 16px; border-radius: 50%; object-fit: cover; flex: 0 0 auto; }
|
|
.content a[data-citation] span { overflow: hidden; text-overflow: ellipsis; }
|
|
.downloads { position: relative; z-index: 3; border-top: 1px solid var(--border); background: var(--soft); }
|
|
.downloads-inner { max-width: 880px; margin: 0 auto; padding: 14px 20px; display: flex; flex-wrap: nowrap; gap: 8px; align-items: center; overflow: visible; }
|
|
.icon-button { width: 36px; height: 36px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); color: var(--text); padding: 0; display: inline-grid; place-items: center; flex: 0 0 auto; cursor: pointer; text-decoration: none; }
|
|
.icon-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
|
|
.icon-button:hover { background: color-mix(in srgb, var(--panel) 88%, var(--text) 12%); }
|
|
.shot-control { display: inline-flex; align-items: stretch; flex: 0 0 auto; position: relative; }
|
|
.shot-control .shot-main { border-radius: 8px 0 0 8px; border-right: 0; }
|
|
.shot-control details { position: static; }
|
|
.shot-control summary { list-style: none; border-radius: 0 8px 8px 0; width: 28px; }
|
|
.shot-control summary::-webkit-details-marker { display: none; }
|
|
.shot-control summary svg { width: 15px; height: 15px; }
|
|
.shot-menu { position: absolute; left: 0; bottom: calc(100% + 8px); z-index: 20; min-width: 150px; padding: 6px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); box-shadow: 0 12px 30px rgba(0,0,0,.12); }
|
|
.shot-menu button { width: 100%; border: 0; border-radius: 6px; background: transparent; color: var(--text); padding: 8px 10px; font: inherit; font-size: 13px; text-align: left; cursor: pointer; }
|
|
.shot-menu button:hover, .shot-menu button[data-current="true"] { background: var(--soft); }
|
|
.toast-region { position: fixed; left: 50%; bottom: 22px; z-index: 40; display: grid; justify-items: center; pointer-events: none; transform: translateX(-50%); }
|
|
.toast { max-width: min(calc(100vw - 32px), 320px); border: 1px solid var(--border); border-radius: 8px; background: var(--panel); color: var(--text); padding: 9px 12px; font-size: 13px; line-height: 1.35; box-shadow: 0 12px 30px rgba(0,0,0,.14); opacity: 0; transform: translateY(8px); transition: opacity .16s ease, transform .16s ease; }
|
|
.toast[data-visible="true"] { opacity: 1; transform: translateY(0); }
|
|
.jump-controls { position: fixed; right: 18px; bottom: 18px; z-index: 30; display: grid; gap: 8px; }
|
|
.jump-controls .icon-button { box-shadow: 0 8px 24px rgba(0,0,0,.12); }
|
|
.source { max-width: 880px; margin: 0 auto; padding: 18px 20px 34px; color: var(--muted); font-size: 12px; }
|
|
.source a { color: var(--muted); }
|
|
.failure { max-width: 720px; margin: 0 auto; padding: 80px 20px; }
|
|
.failure h1 { font-size: 32px; }
|
|
@media (max-width: 640px) {
|
|
body { font-size: 15px; }
|
|
.topbar-inner { padding: 12px 16px; }
|
|
.topbar-actions { gap: 6px; }
|
|
.title { padding: 26px 16px 12px; }
|
|
.conversation { padding: 0 16px 28px; }
|
|
.message { padding: 16px 0; }
|
|
.message.assistant { display: block; }
|
|
.message.user .content-wrap { max-width: 84%; }
|
|
.message.user .content { border-radius: 18px; padding: 9px 14px; }
|
|
.avatar, .assistant-icon { width: 26px; height: 26px; }
|
|
.content ul, .content ol { padding-left: 1.25em; }
|
|
.downloads-inner { padding: 12px 16px; }
|
|
.jump-controls { right: 12px; bottom: 12px; }
|
|
}`;
|
|
}
|
|
|
|
function initialThemeScript(): string {
|
|
return `!function(){var p=new URLSearchParams(location.search),t=p.get("theme");if(t!=="light"&&t!=="dark")t=matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light";document.documentElement.dataset.theme=t}();`;
|
|
}
|
|
|
|
function pageScript(slug: string): string {
|
|
return `
|
|
(function () {
|
|
function currentVariant() {
|
|
var device = matchMedia("(max-width: 640px)").matches ? "mobile" : "desktop";
|
|
var theme = document.documentElement.dataset.theme || (matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
|
|
return device + "-" + theme;
|
|
}
|
|
function downloadShot(variant) {
|
|
location.href = "screenshots/" + variant + ".png";
|
|
}
|
|
var systemTheme = matchMedia("(prefers-color-scheme: dark)");
|
|
var themeToggle = document.querySelector("[data-theme-toggle]");
|
|
var manualTheme = false;
|
|
function preferredTheme() {
|
|
return systemTheme.matches ? "dark" : "light";
|
|
}
|
|
function applyTheme(theme, manual) {
|
|
document.documentElement.dataset.theme = theme;
|
|
if (manual) manualTheme = true;
|
|
if (themeToggle) {
|
|
themeToggle.setAttribute("aria-label", theme === "dark" ? "Switch to light theme" : "Switch to dark theme");
|
|
themeToggle.setAttribute("title", theme === "dark" ? "Switch to light theme" : "Switch to dark theme");
|
|
}
|
|
}
|
|
var toastTimer;
|
|
function showToast(message) {
|
|
var region = document.querySelector("[data-toast-region]");
|
|
if (!region) return;
|
|
var toast = region.querySelector("[data-toast]");
|
|
if (!toast) {
|
|
toast = document.createElement("div");
|
|
toast.className = "toast";
|
|
toast.dataset.toast = "true";
|
|
region.appendChild(toast);
|
|
}
|
|
toast.textContent = message;
|
|
toast.dataset.visible = "true";
|
|
clearTimeout(toastTimer);
|
|
toastTimer = setTimeout(function () {
|
|
delete toast.dataset.visible;
|
|
}, 1600);
|
|
}
|
|
async function copyText(text, successMessage) {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
showToast(successMessage);
|
|
} catch (error) {
|
|
showToast("Copy failed");
|
|
}
|
|
}
|
|
var params = new URLSearchParams(location.search);
|
|
var theme = params.get("theme");
|
|
manualTheme = theme === "light" || theme === "dark";
|
|
applyTheme(theme === "light" || theme === "dark" ? theme : preferredTheme(), theme === "light" || theme === "dark");
|
|
systemTheme.addEventListener("change", function () {
|
|
if (!manualTheme) applyTheme(preferredTheme(), false);
|
|
});
|
|
if (themeToggle) themeToggle.addEventListener("click", function () {
|
|
applyTheme(document.documentElement.dataset.theme === "dark" ? "light" : "dark", true);
|
|
});
|
|
var copy = document.querySelector("[data-copy]");
|
|
if (copy) copy.addEventListener("click", async function () {
|
|
await copyText(location.href.replace(/[?#].*$/, ""), "Link copied");
|
|
});
|
|
var jumpTop = document.querySelector("[data-jump-top]");
|
|
if (jumpTop) jumpTop.addEventListener("click", function () {
|
|
scrollTo({ top: 0, behavior: "smooth" });
|
|
});
|
|
var jumpBottom = document.querySelector("[data-jump-bottom]");
|
|
if (jumpBottom) jumpBottom.addEventListener("click", function () {
|
|
scrollTo({ top: document.documentElement.scrollHeight, behavior: "smooth" });
|
|
});
|
|
document.querySelectorAll("[data-copy-code]").forEach(function (button) {
|
|
button.addEventListener("click", async function () {
|
|
var block = button.closest(".code-block");
|
|
var code = block && block.querySelector("code");
|
|
if (!code) return;
|
|
await copyText(code.textContent || "", "Code copied");
|
|
});
|
|
});
|
|
var defaultShot = document.querySelector("[data-shot-current]");
|
|
if (defaultShot) defaultShot.addEventListener("click", function () {
|
|
downloadShot(currentVariant());
|
|
});
|
|
document.querySelectorAll("[data-shot-variant]").forEach(function (button) {
|
|
if (button.dataset.shotVariant === currentVariant()) button.dataset.current = "true";
|
|
button.addEventListener("click", function () {
|
|
downloadShot(button.dataset.shotVariant);
|
|
});
|
|
});
|
|
})();`;
|
|
}
|
|
|
|
function messageAvatar(role: string, chatGptIconSrc?: string): string {
|
|
if (role === "assistant") {
|
|
return `<div class="avatar"><img class="assistant-icon" src="${escapeHtml(chatGptIconSrc || fallbackChatGptIconUrl)}" alt="ChatGPT"></div>`;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
export function renderConversationPage(slug: string, conversation: NormalizedConversation): string {
|
|
const title = escapeHtml(conversation.title);
|
|
const messages = conversation.messages
|
|
.map((message) => `
|
|
<article class="message ${message.role}">
|
|
${messageAvatar(message.role, conversation.chatGptIconSrc)}
|
|
<div class="content-wrap">
|
|
<div class="role">${message.role === "assistant" ? "Assistant" : message.role === "user" ? "You" : "Message"}</div>
|
|
<div class="content">${message.html}</div>
|
|
</div>
|
|
</article>`)
|
|
.join("\n");
|
|
|
|
return `<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="robots" content="noindex">
|
|
<title>${title}</title>
|
|
<script>${initialThemeScript()}</script>
|
|
<style>${css()}</style>
|
|
</head>
|
|
<body>
|
|
<div class="page">
|
|
<header class="topbar"><div class="topbar-inner"><strong>AI Share</strong><div class="topbar-actions"><span class="snapshot-label">ChatGPT snapshot</span><button class="icon-button theme-toggle" type="button" data-theme-toggle aria-label="Switch theme" title="Switch theme"><span class="sun-icon">${icons.sun}</span><span class="moon-icon">${icons.moon}</span></button></div></div></header>
|
|
<main>
|
|
<section class="title"><h1>${title}</h1></section>
|
|
<section class="conversation">${messages}</section>
|
|
</main>
|
|
<section class="downloads">
|
|
<div class="downloads-inner">
|
|
<a class="icon-button" href="download" aria-label="Download export" title="Download export">${icons.download}</a>
|
|
<div class="shot-control">
|
|
<button class="icon-button shot-main" type="button" data-shot-current aria-label="Download screenshot" title="Download screenshot">${icons.image}</button>
|
|
<details>
|
|
<summary class="icon-button" aria-label="Choose screenshot variant" title="Choose screenshot variant">${icons.chevron}</summary>
|
|
<div class="shot-menu">
|
|
<button type="button" data-shot-variant="desktop-light">Desktop light</button>
|
|
<button type="button" data-shot-variant="desktop-dark">Desktop dark</button>
|
|
<button type="button" data-shot-variant="mobile-light">Mobile light</button>
|
|
<button type="button" data-shot-variant="mobile-dark">Mobile dark</button>
|
|
</div>
|
|
</details>
|
|
</div>
|
|
<button class="icon-button" type="button" data-copy aria-label="Copy link" title="Copy link">${icons.share}</button>
|
|
</div>
|
|
</section>
|
|
<footer class="source">
|
|
Captured from ChatGPT on ${escapeHtml(new Date(conversation.capturedAt).toLocaleString())}.
|
|
Source: <a href="${escapeHtml(conversation.sourceUrl)}" rel="nofollow noreferrer">${escapeHtml(conversation.sourceUrl)}</a>
|
|
</footer>
|
|
<div class="jump-controls" aria-label="Page navigation">
|
|
<button class="icon-button" type="button" data-jump-top aria-label="Jump to top" title="Jump to top">${icons.arrowUp}</button>
|
|
<button class="icon-button" type="button" data-jump-bottom aria-label="Jump to bottom" title="Jump to bottom">${icons.arrowDown}</button>
|
|
</div>
|
|
<div class="toast-region" data-toast-region aria-live="polite" aria-atomic="true"></div>
|
|
</div>
|
|
<script>${pageScript(slug)}</script>
|
|
</body>
|
|
</html>`;
|
|
}
|
|
|
|
export function renderFailurePage(row: CaptureRow): string {
|
|
const title = escapeHtml(row.title || "Capture failed");
|
|
const reason = escapeHtml(row.error_public || "This ChatGPT share could not be captured.");
|
|
return `<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="robots" content="noindex">
|
|
<title>${title}</title>
|
|
<style>${css()}</style>
|
|
</head>
|
|
<body>
|
|
<main class="failure">
|
|
<p class="snapshot-label">AI Share capture</p>
|
|
<h1>${title}</h1>
|
|
<p>${reason}</p>
|
|
</main>
|
|
<footer class="source">
|
|
Source: <a href="${escapeHtml(row.source_url)}" rel="nofollow noreferrer">${escapeHtml(row.source_url)}</a>
|
|
</footer>
|
|
</body>
|
|
</html>`;
|
|
}
|
|
|
|
export async function writeConversationArtifact(slug: string, conversation: NormalizedConversation): Promise<void> {
|
|
await fs.ensureDir(artifactAssetsDir(slug));
|
|
await fs.writeFile(artifactIndexPath(slug), renderConversationPage(slug, conversation), "utf8");
|
|
}
|
|
|
|
export async function writeFailureArtifact(row: CaptureRow): Promise<void> {
|
|
if (!row.slug) return;
|
|
await fs.ensureDir(artifactDir(row.slug));
|
|
await fs.writeFile(path.join(artifactDir(row.slug), "index.html"), renderFailurePage(row), "utf8");
|
|
}
|