← Files DXIRARCHIVED FILE

scripts/document-template.mjs

10.7 KB · Nov 29, 2026 · 18:15 UTC

↓ Download file

import{createHash as x}from"node:crypto";const k="action-room-html-v1",A=8*1024*1024,u=t=>String(t).replace(/[&<>"']/g,a=>({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"})[a]),r=(t,a)=>{if(!t)throw new Error(a)},i=(t,a,e=!1)=>(r(typeof t=="string"&&(e||t.trim().length>0),`${a} must be text.`),u(t));function z(t,a=!0){if(!t||typeof t.name!="string"||!t.name.trim())return null;const e={name:t.name.trim()};for(const o of["primary","accent"])/^#[\da-f]{6}$/i.test(t[o]??"")&&(e[o]=t[o].toUpperCase());typeof t.font_family=="string"&&/^[\p{L}\p{M}\p{N} ._-]{1,80}$/u.test(t.font_family)&&(e.font_family=t.font_family);const n=/^data:image\/(?:png|jpeg);base64,[A-Za-z0-9+/]+={0,2}$/;return typeof t.logo=="string"&&n.test(t.logo)&&(e.logo=t.logo),a&&typeof t.cover_image=="string"&&n.test(t.cover_image)&&(e.cover_image=t.cover_image),t.logo_position==="end"&&(e.logo_position="end"),e}function w(t){const a=t.map(e=>{const n=e/255;return n<=.04045?n/12.92:((n+.055)/1.055)**2.4});return .2126*a[0]+.7152*a[1]+.0722*a[2]}function j(t,a=!0){const e=z(t,a);if(!e)return{css:"",mark:"",name:"",cover:""};const n=e.primary||e.accent||"#173E59",o=e.accent||n,p=n.slice(1).match(/../g).map(m=>parseInt(m,16)),s=w(p),f=(s+.05)/.05>1.05/(s+.05)?"#000000":"#FFFFFF";let c=p;for(;1.05/(w(c)+.05)<4.5;)c=c.map(m=>Math.floor(m*.9));const $="#"+c.map(m=>m.toString(16).padStart(2,"0")).join(""),y=e.font_family?`html,body {font-family:"${e.font_family}","Malgun Gothic","Apple SD Gothic Neo",Arial,sans-serif;}`:"",b=`
:root {--brand-primary:${n};--brand-accent:${o};--brand-ink:${$};--brand-on-primary:${f};}
${y}
h1,h2,h3,a {color:var(--brand-ink);}
.toolbar button,th {background:var(--brand-primary);color:var(--brand-on-primary);}
.document-title,.slide-header,blockquote {border-color:var(--brand-accent);}
.brand-mark {display:flex;align-items:center;justify-content:${e.logo_position==="end"?"flex-end":"flex-start"};margin:0 0 4mm;font-weight:650;font-size:11pt;line-height:1.2;color:var(--brand-ink);}
.brand-mark img {display:block;max-width:46mm;max-height:12mm;width:auto;height:auto;object-fit:contain;margin:0;}
.slide-header .brand-mark {margin:0;}
.slide-header {align-items:center;flex-wrap:wrap;}
.slide-header .slide-title {flex:1;min-width:0;}
.slide-header .brand-end {order:1;}
.brand-cover {position:relative;padding-inline-end:30%;}
.brand-cover img {position:absolute;inset-block:0;inset-inline-end:0;width:26%;height:100%;max-height:30mm;margin:0;object-fit:contain;object-position:right center;}
.slide .brand-cover {margin-bottom:7mm;}
.slide .brand-cover h1 {margin-bottom:0;}
.tracker-header {border-bottom:2px solid var(--brand-accent);padding-bottom:.75rem;}
`,g=e.logo_position==="end"?"brand-mark brand-end":"brand-mark",d=`<span class="${g}">${u(e.name)}</span>`,h=e.logo?`<span class="${g}"><img src="${e.logo}" alt="${u(e.name)}"></span>`:d,l=e.cover_image?`<img src="${e.cover_image}" alt="">`:"";return{css:b,mark:h,name:d,cover:l}}function _(t){switch(r(t&&typeof t=="object","Document blocks must be objects."),t.type){case"paragraph":return`<p>${i(t.text,"Paragraph")}</p>`;case"heading":return r([2,3].includes(t.level),"Heading level must be 2 or 3."),`<h${t.level}>${i(t.text,"Heading")}</h${t.level}>`;case"quote":return`<blockquote>${i(t.text,"Quote")}</blockquote>`;case"list":{r(Array.isArray(t.items)&&t.items.length>0,"Lists need items.");const a=t.ordered?"ol":"ul";return`<${a}>${t.items.map(e=>`<li>${i(e,"List item")}</li>`).join("")}</${a}>`}case"table":{r(Array.isArray(t.columns)&&t.columns.length>0&&Array.isArray(t.rows),"Tables need columns and rows.");const a=t.columns.map(n=>`<th scope="col">${i(n,"Column")}</th>`).join(""),e=t.rows.map(n=>(r(Array.isArray(n)&&n.length===t.columns.length,"Every table row must preserve all columns."),`<tr>${n.map(o=>`<td>${i(o,"Cell",!0)}</td>`).join("")}</tr>`)).join("");return`<table>${t.caption?`<caption>${i(t.caption,"Caption")}</caption>`:""}<thead><tr>${a}</tr></thead><tbody>${e}</tbody></table>`}case"image":return r(typeof t.src=="string"&&/^data:image\/(?:png|jpeg|webp|gif);base64,[A-Za-z0-9+/]+={0,2}$/.test(t.src),"Embed images as PNG, JPEG, WebP or GIF data URLs."),`<figure><img src="${t.src}" alt="${i(t.alt,"Image description")}">${t.caption?`<figcaption>${i(t.caption,"Caption")}</figcaption>`:""}</figure>`;case"links":return r(Array.isArray(t.items)&&t.items.length>0,"Sources need links."),`<ul class="sources">${t.items.map(a=>{let e;try{e=new URL(a.url)}catch{throw new Error("Sources must use public HTTP(S) URLs.")}return r(["https:","http:"].includes(e.protocol)&&!e.username&&!e.password,"Sources must use public HTTP(S) URLs."),`<li><a href="${u(e.href)}" target="_blank" rel="noopener noreferrer">${i(a.label,"Source label")}</a><span class="source-url">${u(e.href)}</span></li>`}).join("")}</ul>`;default:throw new Error("Unsupported document block. Preserve its content using a supported block.")}}function P(t,a=null){r(t&&["document","presentation"].includes(t.layout),"Choose document or presentation layout.");const e=i(t.title,"Document title"),n=t.language??"ko";r(typeof n=="string"&&/^[a-z]{2,3}(?:-[a-z0-9]{2,8})*$/i.test(n),"Use a language tag such as ko or en."),r(Array.isArray(t.sections)&&t.sections.length>0&&t.sections.length<=200,"Provide 1 to 200 sections.");const o=t.layout==="presentation";r(!o||!t.subtitle,"Put presentation subtitles in the first slide so their content is preserved.");const p=n.toLowerCase().startsWith("ko"),s=j(a),f=t.sections.map((d,h)=>{r(Array.isArray(d.blocks)&&d.blocks.length>0,"Every section needs content.");const l=d.heading?i(d.heading,"Section heading"):"";r(!o||l,"Each slide needs a heading.");const m=d.blocks.map(_).join(`
`),v=o&&h===0&&s.cover?`<div class="brand-cover"><h1>${l}</h1>${s.cover}</div>`:`<h1>${l}</h1>`;return o?`<section class="slide"><header class="slide-header">${h===0?s.mark:s.name}<span${s.css?' class="slide-title"':""}>${e}</span><span>${h+1} / ${t.sections.length}</span></header>${v}<div class="slide-body">${m}</div></section>`:`<section>${l?`<h2>${l}</h2>`:""}${m}</section>`}).join(`
`),c="document.getElementById('print-document').addEventListener('click',()=>window.print());",y=`default-src 'none'; script-src 'sha256-${x("sha256").update(c).digest("base64")}'; style-src 'unsafe-inline'; img-src data:; font-src data:; base-uri 'none'; form-action 'none'`,b=`<h1>${e}</h1>${t.subtitle?`<p class="subtitle">${i(t.subtitle,"Subtitle")}</p>`:""}`,g=`<!doctype html>
<html lang="${n}" data-action-room-document="${k}" data-action-room-layout="${t.layout}">
<head><meta charset="utf-8"><meta http-equiv="Content-Security-Policy" content="${y}"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${e}</title>
<style>
@page { size: A4 ${o?"landscape":"portrait"}; margin: ${o?"10mm":"18mm"}; }
* { box-sizing: border-box; }
html { color: #172b3a; background: #edf1f5; font-family: "Malgun Gothic", "Apple SD Gothic Neo", "Noto Sans CJK KR", Arial, sans-serif; font-size: 11pt; line-height: 1.65; }
body { margin: 0; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 16px; max-width: 1120px; margin: 20px auto; padding: 0 20px; font-size: 10pt; color: #475569; }
.toolbar button { background: #173e59; color: white; border: 0; border-radius: 6px; padding: 10px 18px; font: inherit; cursor: pointer; }
main { margin: 0 auto 28px; }
.document { width: 210mm; padding: 18mm; background: white; box-shadow: 0 3px 18px #173e5918; }
.document-title { border-bottom: 2px solid #2b6474; padding-bottom: 9mm; margin-bottom: 9mm; }
.document-title h1 { margin-top: 0; }
.subtitle { color: #526674; }
h1, h2, h3 { line-height: 1.35; color: #173e59; overflow-wrap: anywhere; break-after: avoid; page-break-after: avoid; }
h1 { font-size: 25pt; margin: 0 0 7mm; }
h2 { font-size: 17pt; margin: 9mm 0 4mm; }
h3 { font-size: 13pt; margin: 6mm 0 3mm; }
p, li, td, th, blockquote, figcaption { overflow-wrap: anywhere; }
p { white-space: pre-wrap; margin: 0 0 3.5mm; orphans: 3; widows: 3; }
ul, ol { margin: 0 0 5mm; padding-left: 7mm; }
li { margin: 0 0 2mm; }
blockquote { border-left: 3px solid #69909a; margin: 5mm 0; padding: 2mm 5mm; color: #455c6c; white-space: pre-wrap; }
table { width: 100%; border-collapse: collapse; table-layout: fixed; margin: 5mm 0; font-size: 10pt; }
caption { text-align: left; font-weight: 700; padding-bottom: 2mm; }
th, td { border: 1px solid #cad5de; padding: 2.5mm 3mm; text-align: left; vertical-align: top; white-space: pre-wrap; }
th { background: #e9f0f4; color: #173e59; }
thead { display: table-header-group; }
tr { break-inside: avoid; page-break-inside: avoid; }
figure { margin: 5mm 0; break-inside: avoid; }
img { display: block; max-width: 100%; max-height: 210mm; height: auto; margin: auto; object-fit: contain; }
figcaption { margin-top: 2mm; font-size: 9pt; color: #526674; }
a { color: #195c78; }
.source-url { display: block; font-size: 9pt; color: #526674; overflow-wrap: anywhere; }
.deck { width: 277mm; }
.slide { width: 277mm; height: 189mm; padding: 10mm 12mm; margin: 0 0 8mm; background: white; box-shadow: 0 3px 18px #173e5918; break-inside: avoid; break-after: page; page-break-after: always; }
.slide:last-child { break-after: auto; page-break-after: auto; }
.slide-header { display: flex; justify-content: space-between; gap: 8mm; border-bottom: 1px solid #cad5de; padding-bottom: 3mm; margin-bottom: 7mm; font-size: 9pt; color: #526674; }
.slide-header span:first-child { overflow-wrap: anywhere; }
.slide-header span:last-child { white-space: nowrap; }
.slide h1 { font-size: 27pt; margin-bottom: 7mm; }
.slide-body { font-size: 15pt; line-height: 1.55; }
.slide h2 { font-size: 20pt; margin-top: 5mm; }
.slide h3 { font-size: 17pt; }
.slide table { font-size: 12pt; }
.slide img { max-height: 95mm; }
@media screen and (max-width: 820px) { .document { width: 100%; padding: 7vw; } }
@media print {
  html, body { background: white; print-color-adjust: exact; -webkit-print-color-adjust: exact; }
  .toolbar { display: none !important; }
  main, .document, .deck { width: auto; margin: 0; padding: 0; box-shadow: none; }
  .slide { margin: 0; box-shadow: none; }
  a { color: inherit; text-decoration: none; }
}
${s.css}</style></head><body>
<div class="toolbar"><span>${o?p?"발표형 · A4 가로":"Presentation · A4 landscape":p?"문서형 · A4 세로":"Document · A4 portrait"}</span><button id="print-document" type="button">${p?"인쇄 / PDF로 저장":"Print / Save as PDF"}</button></div>
<main class="${o?"deck":"document"}">${o?"":`<header class="document-title">${s.mark}${s.cover?`<div class="brand-cover">${b}${s.cover}</div>`:b}</header>`}${f}</main>
<script>${c}<\/script></body></html>
`;return r(Buffer.byteLength(g)<=A,"HTML exceeds 8 MiB. Reduce embedded image sizes or split the deliverable."),g}export{A as DOCUMENT_LIMIT,k as DOCUMENT_VERSION,j as brandAppearance,_ as documentBlock,z as printBrand,P as renderDocument};

SHA-256: 1e573b837c07114538ea0d49f005000af42246f53a4c1b959cbacaceb55ad291