/** * Browser previews of Word and PowerPoint uploads, so the original can be read next to the * extracted text. The result is a self-contained HTML page (pictures inlined as data URIs) * that the server sends with a sandboxing Content-Security-Policy. */ import { zipSync } from 'fflate'; import mammoth from 'mammoth'; import { pptxSlidesForPreview, WEB_IMAGE_MIME } from './pptx.ts'; import { ZipReader, type ZipLimits } from './zip.ts'; const LIMITS: ZipLimits = { maxEntryBytes: 64 * 1024 * 1024, maxTotalBytes: 300 * 1024 * 1024 }; const IMAGE_BUDGET = 40 * 1024 * 1024; const escapeHtml = (text: string) => text.replace(/[&<>"']/g, (ch) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[ch]!); const STYLE = ` :root { color-scheme: light dark; --ink: #1b1a17; --ink-2: #57534a; --line: #e4e0d6; --paper: #f7f5ef; --surface: #fffefa; } @media (prefers-color-scheme: dark) { :root { --ink: #ece9e1; --ink-2: #a8a396; --line: #34322d; --paper: #161512; --surface: #1e1d19; } } * { box-sizing: border-box; } body { margin: 0; padding: 20px; background: var(--paper); color: var(--ink); font: 15px/1.6 ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif; } img { max-width: 100%; height: auto; } .page { max-width: 820px; margin: 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px 32px; } .page h1, .page h2, .page h3 { line-height: 1.25; } .page table { border-collapse: collapse; width: 100%; } .page td, .page th { border: 1px solid var(--line); padding: 4px 8px; vertical-align: top; } .slide { max-width: 900px; margin: 0 auto 18px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 18px 22px; } .slide-no { font: 600 11px/1 ui-monospace, Menlo, Consolas, monospace; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-2); } .slide h2 { margin: 8px 0 10px; font-size: 20px; } .block { white-space: pre-wrap; margin: 0 0 8px; } .pics { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; } .pics img { max-height: 420px; border-radius: 8px; border: 1px solid var(--line); background: #fff; } .notes { margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--line); color: var(--ink-2); font-size: 13.5px; white-space: pre-wrap; } .muted { color: var(--ink-2); font-size: 13px; } `; function page(title: string, body: string): string { return `
${escapeHtml(b.replace(/^#{1,6}\s+/, ''))}
`).join(''); const pics = s.images.length ? `${s.skippedImages} picture${s.skippedImages === 1 ? '' : 's'} on this slide can't be shown here.
` : ''; const notes = s.notes ? `This deck has no slides.
'); } async function docxHtml(bytes: Uint8Array, title: string): PromiseThis document has no content to show.
'}