The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { pipe } from "effect/Function"; |
| 2 | import { |
| 3 | IMAGE_DIMENSIONS_HEADER_BYTES, |
| 4 | readImageDimensions, |
| 5 | } from "@t3tools/shared/imageDimensions"; |
| 6 | import { XMLParser, XMLValidator } from "fast-xml-parser"; |
| 7 | import { decodeHTMLAttribute, decodeHTMLStrict as decodeEntities } from "entities"; |
| 8 | import type JSZip from "jszip"; |
| 9 | import { terminalOutputPlainText } from "@t3tools/shared/terminalOutput"; |
| 10 | |
| 11 | import { type OfficeWorkerResult, type NotebookCell } from "./officePreviewProtocol"; |
| 12 | import { PreviewError } from "./previewFailure"; |
| 13 | |
| 14 | const TABLE_PREVIEW_MAX_ROWS = 500; |
| 15 | const TABLE_PREVIEW_MAX_COLUMNS = 50; |
| 16 | |
| 17 | // `text` and `attrs` hold decoded values: escaped text is decoded once, CDATA stays literal. |
| 18 | type XmlNode = { name: string; attrs: Record<string, string>; children: XmlNode[]; text: string }; |
| 19 | type OrderedXml = { [key: string]: OrderedXml[] | Record<string, string> | string }; |
| 20 | const escape = (value: string) => |
| 21 | value |
| 22 | .replaceAll("&", "&") |
| 23 | .replaceAll("<", "<") |
| 24 | .replaceAll(">", ">") |
| 25 | .replaceAll('"', """); |
| 26 | |
| 27 | const namespacePrefixes: Record<string, string> = { |
| 28 | "urn:oasis:names:tc:opendocument:xmlns:office:1.0": "office", |
| 29 | "urn:oasis:names:tc:opendocument:xmlns:table:1.0": "table", |
| 30 | "urn:oasis:names:tc:opendocument:xmlns:text:1.0": "text", |
| 31 | "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0": "draw", |
| 32 | "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0": "presentation", |
| 33 | "http://www.w3.org/1999/xlink": "xlink", |
| 34 | "http://www.w3.org/1999/xhtml": "", |
| 35 | "http://www.idpf.org/2007/opf": "", |
| 36 | "urn:oasis:names:tc:opendocument:xmlns:container": "", |
| 37 | }; |
| 38 | function parseXml(source: string): XmlNode { |
| 39 | // EPUB XHTML commonly declares an external DTD: discard it without resolving it. Any other |
| 40 | // declaration is rejected; CDATA, comments and processing instructions pass through untouched. |
| 41 | let declares = false; |
| 42 | source = source.replaceAll( |
| 43 | /<\?[\s\S]*?(?:\?>|$)|<!\[CDATA\[[\s\S]*?(?:]]>|$)|<!--[\s\S]*?(?:-->|$)|(<!DOCTYPE\s+[^<>[\]]*>)|(<!\s*(?:DOCTYPE|ENTITY))/gi, |
| 44 | (markup, externalDoctype?: string, declaration?: string) => { |
| 45 | if (declaration) declares = true; |
| 46 | return externalDoctype ? " " : markup; |
| 47 | }, |
| 48 | ); |
| 49 | if (source.length > 8 * 1024 * 1024 || declares || XMLValidator.validate(source) !== true) |
| 50 | throw new PreviewError({ code: "xml-invalid" }); |
| 51 | const parser = new XMLParser({ |
| 52 | preserveOrder: true, |
| 53 | ignoreAttributes: false, |
| 54 | attributeNamePrefix: "", |
| 55 | processEntities: false, |
| 56 | trimValues: false, |
| 57 | parseTagValue: false, |
| 58 | cdataPropName: "#cdata", |
| 59 | }); |
| 60 | const ordered = parser.parse(source) as OrderedXml[]; |
| 61 | let count = 0; |
| 62 | function convert( |
| 63 | items: OrderedXml[], |
| 64 | depth: number, |
| 65 | inherited: Record<string, string>, |
| 66 | ): XmlNode[] { |
| 67 | if (depth > 100) throw new PreviewError({ code: "xml-too-deep" }); |
| 68 | return items.map((item) => { |
| 69 | if (++count > 100_000) throw new PreviewError({ code: "xml-too-much-content" }); |
| 70 | const name = Object.keys(item).find((key) => key !== ":@") ?? "#text"; |
| 71 | const value = item[name]; |
| 72 | if (name === "#cdata") { |
| 73 | const literal = Array.isArray(value) |
| 74 | ? value.map((part) => part["#text"] ?? "").join("") |
| 75 | : ""; |
| 76 | return { name: "#text", attrs: {}, children: [], text: literal }; |
| 77 | } |
| 78 | const attrs = Object.fromEntries( |
| 79 | Object.entries((item[":@"] ?? {}) as Record<string, string>).map(([key, value]) => { |
| 80 | // fast-xml-parser accepts raw '<' in element attributes, which can mask declarations |
| 81 | // above. Processing instructions end at their first '?>', so their data may hold '<'. |
| 82 | if (!name.startsWith("?") && value.includes("<")) |
| 83 | throw new PreviewError({ code: "xml-invalid" }); |
| 84 | return [key, decodeEntities(value)]; |
| 85 | }), |
| 86 | ); |
| 87 | const namespaces = Object.create(inherited) as Record<string, string>; |
| 88 | for (const [key, value] of Object.entries(attrs)) { |
| 89 | if (key === "xmlns") namespaces[""] = value; |
| 90 | else if (key.startsWith("xmlns:")) namespaces[key.slice(6)] = value; |
| 91 | } |
| 92 | const expanded = (name: string, attribute = false) => { |
| 93 | if (name.startsWith("#")) return name; |
| 94 | const colon = name.indexOf(":"); |
| 95 | if (colon < 0 && attribute) return name; |
| 96 | const uri = namespaces[colon < 0 ? "" : name.slice(0, colon)]; |
| 97 | if (!uri) return name; |
| 98 | const prefix = Object.hasOwn(namespacePrefixes, uri) ? namespacePrefixes[uri] : undefined; |
| 99 | const local = name.slice(colon + 1); |
| 100 | return prefix === undefined |
| 101 | ? "{" + uri + "}" + local |
| 102 | : prefix |
| 103 | ? prefix + ":" + local |
| 104 | : local; |
| 105 | }; |
| 106 | return { |
| 107 | name: expanded(name), |
| 108 | attrs: Object.fromEntries( |
| 109 | Object.entries(attrs).map(([key, value]) => [expanded(key, true), value]), |
| 110 | ), |
| 111 | children: Array.isArray(value) ? convert(value, depth + 1, namespaces) : [], |
| 112 | text: typeof value === "string" ? decodeEntities(value) : "", |
| 113 | }; |
| 114 | }); |
| 115 | } |
| 116 | return { |
| 117 | name: "root", |
| 118 | attrs: {}, |
| 119 | children: convert(ordered, 0, Object.create(null) as Record<string, string>), |
| 120 | text: "", |
| 121 | }; |
| 122 | } |
| 123 | function find(node: XmlNode, name: string, skip?: string): XmlNode[] { |
| 124 | return node.children.flatMap((child) => |
| 125 | child.name === skip |
| 126 | ? [] |
| 127 | : child.name === name || (!name.includes(":") && child.name.endsWith(`:${name}`)) |
| 128 | ? [child] |
| 129 | : find(child, name, skip), |
| 130 | ); |
| 131 | } |
| 132 | function text(node: XmlNode): string { |
| 133 | if (node.name === "office:annotation") return ""; |
| 134 | if (node.name === "text:s") return " ".repeat(repeat(node.attrs["text:c"], 100)); |
| 135 | if (node.name === "text:tab") return "\t"; |
| 136 | if (node.name === "text:line-break") return "\n"; |
| 137 | return node.name === "#text" ? node.text : node.children.map(text).join(""); |
| 138 | } |
| 139 | function archivePath(base: string, href: string): string | null { |
| 140 | if (/^(?:[a-z][a-z\d+.-]*:|\/|\\)/i.test(href)) return null; |
| 141 | const parts = base.split("/").slice(0, -1); |
| 142 | let decoded: string; |
| 143 | try { |
| 144 | decoded = decodeURIComponent(href.split(/[?#]/, 1)[0] ?? ""); |
| 145 | } catch { |
| 146 | return null; |
| 147 | } |
| 148 | for (const part of decoded.split("/")) { |
| 149 | if (part === "..") { |
| 150 | if (!parts.length) return null; |
| 151 | parts.pop(); |
| 152 | } else if (part && part !== ".") parts.push(part); |
| 153 | } |
| 154 | return parts.join("/"); |
| 155 | } |
| 156 | function imageFits(bytes: Uint8Array) { |
| 157 | const size = readImageDimensions(bytes.subarray(0, IMAGE_DIMENSIONS_HEADER_BYTES)); |
| 158 | return size !== null && size.width <= 16_777_216 / size.height; |
| 159 | } |
| 160 | function base64ImageFits(base64: string) { |
| 161 | return ( |
| 162 | /^[a-z\d+/]*={0,2}$/i.test(base64) && |
| 163 | base64.length % 4 !== 1 && |
| 164 | (!base64.endsWith("=") || base64.length % 4 === 0) && |
| 165 | imageFits( |
| 166 | Uint8Array.from( |
| 167 | atob(base64.slice(0, 4 * Math.ceil(IMAGE_DIMENSIONS_HEADER_BYTES / 3))), |
| 168 | (char) => char.charCodeAt(0), |
| 169 | ), |
| 170 | ) |
| 171 | ); |
| 172 | } |
| 173 | function htmlImagesFit(html: string) { |
| 174 | // HTML attribute entities can obscure a data URL until the client parses it. |
| 175 | for (const match of decodeHTMLAttribute(html).matchAll( |
| 176 | /data:image\/(?:png|jpeg|gif|webp);base64,([a-z\d+/]*={0,2})/gi, |
| 177 | )) |
| 178 | if (!base64ImageFits(match[1]!)) return false; |
| 179 | return true; |
| 180 | } |
| 181 | async function imageData(archive: JSZip, path: string | null): Promise<string> { |
| 182 | if (!path) return ""; |
| 183 | const mime = ( |
| 184 | { png: "png", jpg: "jpeg", jpeg: "jpeg", gif: "gif", webp: "webp" } as Record<string, string> |
| 185 | )[path.split(".").at(-1)?.toLowerCase() ?? ""]; |
| 186 | const entry = archive.file(path); |
| 187 | if (!mime || !entry) return ""; |
| 188 | const bytes = await entry.async("uint8array"); |
| 189 | if (bytes.length > 4 * 1024 * 1024 || !imageFits(bytes)) return ""; |
| 190 | reserveMarkup(4 * Math.ceil(bytes.length / 3) + 100); |
| 191 | return `data:image/${mime};base64,${await entry.async("base64")}`; |
| 192 | } |
| 193 | let markupBytes = 0; |
| 194 | function reserveMarkup(size: number) { |
| 195 | markupBytes += size; |
| 196 | if (markupBytes > 16 * 1024 * 1024) throw new PreviewError({ code: "rendered-too-large" }); |
| 197 | } |
| 198 | function boundedMarkup(value: string): string { |
| 199 | reserveMarkup(value.length); |
| 200 | return value; |
| 201 | } |
| 202 | const HTML_TAGS = new Set([ |
| 203 | "p", |
| 204 | "h1", |
| 205 | "h2", |
| 206 | "h3", |
| 207 | "h4", |
| 208 | "h5", |
| 209 | "h6", |
| 210 | "div", |
| 211 | "span", |
| 212 | "strong", |
| 213 | "b", |
| 214 | "em", |
| 215 | "i", |
| 216 | "u", |
| 217 | "s", |
| 218 | "ul", |
| 219 | "ol", |
| 220 | "li", |
| 221 | "blockquote", |
| 222 | "pre", |
| 223 | "code", |
| 224 | "table", |
| 225 | "thead", |
| 226 | "tbody", |
| 227 | "tr", |
| 228 | "th", |
| 229 | "td", |
| 230 | "caption", |
| 231 | "figure", |
| 232 | "figcaption", |
| 233 | "br", |
| 234 | "hr", |
| 235 | "sub", |
| 236 | "sup", |
| 237 | "dl", |
| 238 | "dt", |
| 239 | "dd", |
| 240 | ]); |
| 241 | async function epubHtml(node: XmlNode, archive: JSZip, base: string): Promise<string> { |
| 242 | if (node.name === "#text") return pipe(text(node), escape, boundedMarkup); |
| 243 | const tag = |
| 244 | node.name |
| 245 | .replace(/^\{[^}]*\}/, "") |
| 246 | .split(":") |
| 247 | .at(-1) ?? ""; |
| 248 | if (["head", "script", "style", "iframe", "object", "embed", "svg", "math", "form"].includes(tag)) |
| 249 | return ""; |
| 250 | if (tag === "br" || tag === "hr") return boundedMarkup(`<${tag}>`); |
| 251 | if (tag === "img") { |
| 252 | const src = await imageData(archive, archivePath(base, node.attrs.src ?? "")); |
| 253 | return src ? `<img src="${src}" alt="${boundedMarkup(escape(node.attrs.alt ?? ""))}">` : ""; |
| 254 | } |
| 255 | let children = ""; |
| 256 | for (const child of node.children) children += await epubHtml(child, archive, base); |
| 257 | if (!HTML_TAGS.has(tag)) return children; |
| 258 | const spans = |
| 259 | tag === "td" || tag === "th" |
| 260 | ? ["colspan", "rowspan"] |
| 261 | .map((name) => |
| 262 | /^\d{1,4}$/.test(node.attrs[name] ?? "") ? ` ${name}="${node.attrs[name]}"` : "", |
| 263 | ) |
| 264 | .join("") |
| 265 | : ""; |
| 266 | reserveMarkup(tag.length * 2 + spans.length + 5); |
| 267 | return `<${tag}${spans}>${children}</${tag}>`; |
| 268 | } |
| 269 | async function odfHtml(node: XmlNode, archive: JSZip): Promise<string> { |
| 270 | if (node.name === "#text") return pipe(text(node), escape, boundedMarkup); |
| 271 | if (node.name === "text:s" || node.name === "text:tab") |
| 272 | return boundedMarkup( |
| 273 | `<span data-odf-whitespace>${node.name === "text:tab" ? "	" : " ".repeat(repeat(node.attrs["text:c"], 100))}</span>`, |
| 274 | ); |
| 275 | if (node.name === "text:line-break") return boundedMarkup("<br>"); |
| 276 | if (node.name === "draw:image") { |
| 277 | const src = await imageData( |
| 278 | archive, |
| 279 | archivePath("content.xml", node.attrs["xlink:href"] ?? ""), |
| 280 | ); |
| 281 | return src ? `<img src="${src}" alt="">` : ""; |
| 282 | } |
| 283 | if ( |
| 284 | [ |
| 285 | "office:annotation", |
| 286 | "presentation:notes", |
| 287 | "text:tracked-changes", |
| 288 | "office:scripts", |
| 289 | "draw:object", |
| 290 | "draw:object-ole", |
| 291 | ].includes(node.name) |
| 292 | ) |
| 293 | return ""; |
| 294 | let children = ""; |
| 295 | for (const child of node.children) children += await odfHtml(child, archive); |
| 296 | const tags: Record<string, string> = { |
| 297 | "text:p": "p", |
| 298 | "text:h": "h2", |
| 299 | "text:span": "span", |
| 300 | "text:list": "ul", |
| 301 | "text:list-item": "li", |
| 302 | "table:table": "table", |
| 303 | "table:table-row": "tr", |
| 304 | "table:table-cell": "td", |
| 305 | }; |
| 306 | const tag = tags[node.name]; |
| 307 | if (!tag) return children; |
| 308 | const count = |
| 309 | tag === "tr" |
| 310 | ? repeat(node.attrs["table:number-rows-repeated"], TABLE_PREVIEW_MAX_ROWS) |
| 311 | : tag === "td" |
| 312 | ? repeat(node.attrs["table:number-columns-repeated"], TABLE_PREVIEW_MAX_COLUMNS) |
| 313 | : 1; |
| 314 | const markup = `<${tag}>${children}</${tag}>`; |
| 315 | reserveMarkup(count * markup.length - children.length); |
| 316 | return markup.repeat(count); |
| 317 | } |
| 318 | function repeat(value: string | undefined, max: number) { |
| 319 | const count = Number(value ?? 1); |
| 320 | if (!Number.isSafeInteger(count) || count < 1) |
| 321 | throw new PreviewError({ code: "repeated-content-invalid" }); |
| 322 | return Math.min(count, max); |
| 323 | } |
| 324 | export async function openDocument( |
| 325 | archive: JSZip, |
| 326 | kind: "odt" | "ods" | "odp", |
| 327 | ): Promise<OfficeWorkerResult> { |
| 328 | markupBytes = 0; |
| 329 | const source = await archive.file("content.xml")?.async("string"); |
| 330 | if (!source) throw new PreviewError({ code: "odf-no-content" }); |
| 331 | const root = parseXml(source); |
| 332 | if (kind === "ods") { |
| 333 | const tables = find(root, "table:table"); |
| 334 | if (!tables.length || tables.length > 100) throw new PreviewError({ code: "ods-sheet-count" }); |
| 335 | let cellBytes = 0; |
| 336 | return { |
| 337 | type: "ods", |
| 338 | sheets: tables.map((table) => { |
| 339 | const rows: string[][] = []; |
| 340 | let truncated = false; |
| 341 | let pendingEmptyRows = 0; |
| 342 | for (const row of find(table, "table:table-row")) { |
| 343 | const cells: string[] = []; |
| 344 | for (const cell of row.children.filter((child) => |
| 345 | ["table:table-cell", "table:covered-table-cell"].includes(child.name), |
| 346 | )) { |
| 347 | const value = |
| 348 | find(cell, "text:p", "office:annotation").map(text).join("\n") || |
| 349 | cell.attrs["office:value"] || |
| 350 | cell.attrs["office:date-value"] || |
| 351 | cell.attrs["office:time-value"] || |
| 352 | cell.attrs["office:boolean-value"] || |
| 353 | cell.attrs["office:string-value"] || |
| 354 | ""; |
| 355 | const count = repeat( |
| 356 | cell.attrs["table:number-columns-repeated"], |
| 357 | TABLE_PREVIEW_MAX_COLUMNS + 1, |
| 358 | ); |
| 359 | if (value && cells.length + count > TABLE_PREVIEW_MAX_COLUMNS) truncated = true; |
| 360 | for (let i = 0; i < count && cells.length < TABLE_PREVIEW_MAX_COLUMNS; i++) { |
| 361 | if (value.length > 10_000) throw new PreviewError({ code: "ods-cell-too-large" }); |
| 362 | cells.push(value); |
| 363 | } |
| 364 | } |
| 365 | while (cells.at(-1) === "") cells.pop(); |
| 366 | const count = repeat(row.attrs["table:number-rows-repeated"], TABLE_PREVIEW_MAX_ROWS + 1); |
| 367 | if (!cells.length) { |
| 368 | pendingEmptyRows = Math.min(TABLE_PREVIEW_MAX_ROWS + 1, pendingEmptyRows + count); |
| 369 | continue; |
| 370 | } |
| 371 | while (pendingEmptyRows > 0 && rows.length < TABLE_PREVIEW_MAX_ROWS) { |
| 372 | rows.push([]); |
| 373 | pendingEmptyRows--; |
| 374 | } |
| 375 | pendingEmptyRows = 0; |
| 376 | if (rows.length + count > TABLE_PREVIEW_MAX_ROWS) truncated = true; |
| 377 | if (rows.length >= TABLE_PREVIEW_MAX_ROWS) break; |
| 378 | for (let i = 0; i < count && rows.length < TABLE_PREVIEW_MAX_ROWS; i++) { |
| 379 | cellBytes += cells.reduce((size, cell) => size + cell.length, 0); |
| 380 | if (cellBytes > 8 * 1024 * 1024) throw new PreviewError({ code: "ods-too-much-text" }); |
| 381 | rows.push(cells); |
| 382 | } |
| 383 | } |
| 384 | return { |
| 385 | name: table.attrs["table:name"] || undefined, |
| 386 | rows, |
| 387 | rowCount: rows.length, |
| 388 | columns: rows.reduce((columns, row) => Math.max(columns, row.length), 1), |
| 389 | truncated, |
| 390 | }; |
| 391 | }), |
| 392 | }; |
| 393 | } |
| 394 | const pages = kind === "odp" ? find(root, "draw:page") : find(root, "office:text"); |
| 395 | if (!pages.length || pages.length > 500) throw new PreviewError({ code: "odf-page-count" }); |
| 396 | const rendered = []; |
| 397 | for (const page of pages) { |
| 398 | rendered.push({ |
| 399 | title: page.attrs["draw:name"] || undefined, |
| 400 | html: await odfHtml(page, archive), |
| 401 | }); |
| 402 | } |
| 403 | return { type: "document", navigation: kind === "odp" ? "slide" : "page", pages: rendered }; |
| 404 | } |
| 405 | export async function openEpub(archive: JSZip): Promise<OfficeWorkerResult> { |
| 406 | markupBytes = 0; |
| 407 | const container = parseXml((await archive.file("META-INF/container.xml")?.async("string")) ?? ""); |
| 408 | const rootfile = find(container, "rootfile")[0]?.attrs["full-path"]; |
| 409 | if (!rootfile || !archive.file(rootfile)) throw new PreviewError({ code: "epub-no-package" }); |
| 410 | const packageRoot = parseXml(await archive.file(rootfile)!.async("string")); |
| 411 | const manifest = new Map(find(packageRoot, "item").map((item) => [item.attrs.id, item])); |
| 412 | const spine = find(packageRoot, "itemref"); |
| 413 | if (!spine.length || spine.length > 500) throw new PreviewError({ code: "epub-chapter-count" }); |
| 414 | const pages = []; |
| 415 | for (const itemref of spine) { |
| 416 | const item = manifest.get(itemref.attrs.idref); |
| 417 | if (!item || item.attrs["media-type"] !== "application/xhtml+xml") continue; |
| 418 | const path = archivePath(rootfile, item.attrs.href ?? ""); |
| 419 | const entry = path ? archive.file(path) : null; |
| 420 | if (!entry || !path) continue; |
| 421 | const chapter = parseXml(await entry.async("string")); |
| 422 | pages.push({ |
| 423 | title: |
| 424 | text(find(chapter, "title")[0] ?? find(chapter, "h1")[0] ?? chapter) |
| 425 | .trim() |
| 426 | .slice(0, 120) || undefined, |
| 427 | html: await epubHtml(chapter, archive, path), |
| 428 | }); |
| 429 | } |
| 430 | if (!pages.length) throw new PreviewError({ code: "epub-no-chapters" }); |
| 431 | return { type: "document", navigation: "chapter", pages }; |
| 432 | } |
| 433 | function record(value: unknown): Record<string, unknown> { |
| 434 | return value !== null && typeof value === "object" && !Array.isArray(value) |
| 435 | ? (value as Record<string, unknown>) |
| 436 | : {}; |
| 437 | } |
| 438 | function notebookText(value: unknown, limit = 256_000): string { |
| 439 | const result = |
| 440 | typeof value === "string" |
| 441 | ? value |
| 442 | : Array.isArray(value) |
| 443 | ? value.filter((part) => typeof part === "string").join("") |
| 444 | : ""; |
| 445 | if (result.length > limit) throw new PreviewError({ code: "notebook-cell-too-large" }); |
| 446 | return result; |
| 447 | } |
| 448 | // IPython traceback frames carry ANSI SGR colors and may omit a final newline. |
| 449 | function tracebackLines(value: unknown): unknown { |
| 450 | if (!Array.isArray(value)) return value; |
| 451 | const frames = value |
| 452 | .filter((frame): frame is string => typeof frame === "string") |
| 453 | .map(terminalOutputPlainText); |
| 454 | return frames.map((frame, index) => |
| 455 | index && !frames[index - 1]!.endsWith("\n") ? `\n${frame}` : frame, |
| 456 | ); |
| 457 | } |
| 458 | export function openNotebook(bytes: ArrayBuffer): OfficeWorkerResult { |
| 459 | if (bytes.byteLength > 8 * 1024 * 1024) throw new PreviewError({ code: "notebook-too-large" }); |
| 460 | const notebook = record(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes))); |
| 461 | if (notebook.nbformat !== 4 || !Array.isArray(notebook.cells) || notebook.cells.length > 500) |
| 462 | throw new PreviewError({ code: "notebook-unsupported" }); |
| 463 | const language = String(record(record(notebook.metadata).language_info).name ?? "text") |
| 464 | .replaceAll(/[^a-z\d+#-]/gi, "") |
| 465 | .slice(0, 40); |
| 466 | const cells: NotebookCell[] = notebook.cells.map((value) => { |
| 467 | const cell = record(value); |
| 468 | const outputs = Array.isArray(cell.outputs) ? cell.outputs : []; |
| 469 | if (outputs.length > 100) throw new PreviewError({ code: "notebook-too-many-outputs" }); |
| 470 | return { |
| 471 | kind: cell.cell_type === "markdown" ? "markdown" : cell.cell_type === "code" ? "code" : "raw", |
| 472 | source: notebookText(cell.source), |
| 473 | outputs: outputs |
| 474 | .map((value) => { |
| 475 | const output = record(value); |
| 476 | const data = record(output.data); |
| 477 | for (const mime of ["image/png", "image/jpeg", "image/gif", "image/webp"]) { |
| 478 | if (data[mime]) { |
| 479 | const base64 = notebookText(data[mime], 4 * 1024 * 1024).replaceAll(/\s/g, ""); |
| 480 | if (base64ImageFits(base64)) |
| 481 | return { kind: "image" as const, text: `data:${mime};base64,${base64}` }; |
| 482 | } |
| 483 | } |
| 484 | const html = notebookText(data["text/html"]); |
| 485 | if (html.trim() && htmlImagesFit(html)) return { kind: "html" as const, text: html }; |
| 486 | return { |
| 487 | kind: "text" as const, |
| 488 | text: terminalOutputPlainText( |
| 489 | notebookText(output.text ?? data["text/plain"] ?? tracebackLines(output.traceback)), |
| 490 | ), |
| 491 | }; |
| 492 | }) |
| 493 | .filter((output) => output.text.trim().length > 0), |
| 494 | }; |
| 495 | }); |
| 496 | return { type: "notebook", language, cells }; |
| 497 | } |
| 498 | |
| 499 | // Characters RTF writes as control symbols (`\~`) and control words (`\emspace`). |
| 500 | const RTF_CHARACTERS = new Map([ |
| 501 | ["~", "\u00a0"], |
| 502 | ["_", "\u2011"], |
| 503 | ["-", "\u00ad"], |
| 504 | ["tab", "\t"], |
| 505 | ["cell", "\t"], |
| 506 | ["endash", "–"], |
| 507 | // oxlint-disable-next-line t3code/no-em-dash-copy -- Preserve the RTF document's punctuation. |
| 508 | ["emdash", "—"], |
| 509 | ["bullet", "•"], |
| 510 | ["lquote", "‘"], |
| 511 | ["rquote", "’"], |
| 512 | ["ldblquote", "“"], |
| 513 | ["rdblquote", "”"], |
| 514 | ["enspace", "\u2002"], |
| 515 | ["emspace", "\u2003"], |
| 516 | ["qmspace", "\u2005"], |
| 517 | ["zwbo", "\u200b"], |
| 518 | ["zwnj", "\u200c"], |
| 519 | ["zwj", "\u200d"], |
| 520 | ["ltrmark", "\u200e"], |
| 521 | ["rtlmark", "\u200f"], |
| 522 | ]); |
| 523 | |
| 524 | export function openRtf(bytes: ArrayBuffer): OfficeWorkerResult { |
| 525 | if (bytes.byteLength > 8 * 1024 * 1024) throw new PreviewError({ code: "rtf-too-large" }); |
| 526 | // Keep byte offsets intact for binary destinations and multibyte code pages. |
| 527 | const input = new Uint8Array(bytes); |
| 528 | let source = ""; |
| 529 | for (let offset = 0; offset < input.length; offset += 8192) |
| 530 | source += String.fromCharCode(...input.subarray(offset, offset + 8192)); |
| 531 | if (!source.startsWith("{\\rtf")) throw new PreviewError({ code: "rtf-invalid" }); |
| 532 | let state = { |
| 533 | skip: false, |
| 534 | bold: false, |
| 535 | italic: false, |
| 536 | underline: false, |
| 537 | uc: 1, |
| 538 | font: 0, |
| 539 | fontTable: false, |
| 540 | decoder: new TextDecoder("windows-1252"), |
| 541 | }; |
| 542 | const fonts = new Map<number, TextDecoder>(); |
| 543 | let defaultFont = 0; |
| 544 | const stack: (typeof state)[] = []; |
| 545 | let html = ""; |
| 546 | const pages: { title: undefined; html: string }[] = []; |
| 547 | let completedLength = 0; |
| 548 | const finishPage = () => { |
| 549 | if (pages.length >= 500) throw new PreviewError({ code: "rtf-too-many-pages" }); |
| 550 | pages.push({ title: undefined, html: `<p>${html}</p>` }); |
| 551 | completedLength += html.length; |
| 552 | html = ""; |
| 553 | }; |
| 554 | let run = ""; |
| 555 | let fallback = 0; |
| 556 | const encoded = new Uint8Array(bytes.byteLength); |
| 557 | let encodedLength = 0; |
| 558 | const flush = () => { |
| 559 | if (encodedLength) { |
| 560 | run += escape( |
| 561 | (fonts.get(state.font) ?? state.decoder).decode(encoded.subarray(0, encodedLength)), |
| 562 | ); |
| 563 | encodedLength = 0; |
| 564 | } |
| 565 | if (completedLength + html.length + run.length > 8 * 1024 * 1024) |
| 566 | throw new PreviewError({ code: "rtf-too-much-content" }); |
| 567 | if (!run) return; |
| 568 | let span = run; |
| 569 | if (state.bold) span = `<strong>${span}</strong>`; |
| 570 | if (state.italic) span = `<em>${span}</em>`; |
| 571 | if (state.underline) span = `<u>${span}</u>`; |
| 572 | html += span; |
| 573 | run = ""; |
| 574 | }; |
| 575 | const emit = (value: string) => { |
| 576 | if (fallback > 0) { |
| 577 | fallback--; |
| 578 | return; |
| 579 | } |
| 580 | if (state.skip) return; |
| 581 | run += escape(value); |
| 582 | if (completedLength + html.length + run.length > 8 * 1024 * 1024) |
| 583 | throw new PreviewError({ code: "rtf-too-much-content" }); |
| 584 | }; |
| 585 | const emitByte = (value: number) => { |
| 586 | if (fallback > 0) fallback--; |
| 587 | else if (!state.skip) encoded[encodedLength++] = value; |
| 588 | }; |
| 589 | for (let i = 0; i < source.length;) { |
| 590 | const char = source[i++]; |
| 591 | if (char === "{") { |
| 592 | flush(); |
| 593 | fallback = 0; |
| 594 | if (stack.length > 100) throw new PreviewError({ code: "rtf-too-deep" }); |
| 595 | stack.push({ ...state }); |
| 596 | } else if (char === "}") { |
| 597 | flush(); |
| 598 | fallback = 0; |
| 599 | const parent = stack.pop(); |
| 600 | if (!parent) throw new PreviewError({ code: "rtf-damaged" }); |
| 601 | state = parent; |
| 602 | } else if (char === "\\") { |
| 603 | const next = source[i]; |
| 604 | if (next === "'") { |
| 605 | const hex = source.slice(i + 1, i + 3); |
| 606 | if (!/^[\da-f]{2}$/i.test(hex)) throw new PreviewError({ code: "rtf-byte-escape" }); |
| 607 | i += 3; |
| 608 | emitByte(Number.parseInt(hex, 16)); |
| 609 | continue; |
| 610 | } |
| 611 | flush(); |
| 612 | if (next === "\\" || next === "{" || next === "}") { |
| 613 | i++; |
| 614 | emit(next); |
| 615 | continue; |
| 616 | } |
| 617 | if (next === "*") { |
| 618 | state.skip = true; |
| 619 | i++; |
| 620 | continue; |
| 621 | } |
| 622 | const match = /^([a-z]+)(-?\d+)? ?/i.exec(source.slice(i)); |
| 623 | if (!match) { |
| 624 | const character = RTF_CHARACTERS.get(next!); |
| 625 | if (character) emit(character); |
| 626 | // A backslash before a line break is \par; TextEdit ends paragraphs this way. |
| 627 | else if ((next === "\n" || next === "\r") && !state.skip) html += "<br>"; |
| 628 | i++; |
| 629 | continue; |
| 630 | } |
| 631 | i += match[0].length; |
| 632 | const word = match[1]; |
| 633 | const arg = match[2] === undefined ? 1 : Number(match[2]); |
| 634 | if (word === "fonttbl") { |
| 635 | state.fontTable = true; |
| 636 | state.skip = true; |
| 637 | } else if (word === "deff") state.font = defaultFont = arg; |
| 638 | else if (word === "f") state.font = arg; |
| 639 | else if (word === "fcharset" && state.fontTable) { |
| 640 | const label = ( |
| 641 | { |
| 642 | 0: state.decoder.encoding, |
| 643 | 1: state.decoder.encoding, |
| 644 | 77: "macintosh", |
| 645 | 128: "shift_jis", |
| 646 | 129: "euc-kr", |
| 647 | 134: "gbk", |
| 648 | 136: "big5", |
| 649 | 161: "windows-1253", |
| 650 | 162: "windows-1254", |
| 651 | 163: "windows-1258", |
| 652 | 177: "windows-1255", |
| 653 | 178: "windows-1256", |
| 654 | 186: "windows-1257", |
| 655 | 204: "windows-1251", |
| 656 | 222: "windows-874", |
| 657 | 238: "windows-1250", |
| 658 | 255: "ibm866", |
| 659 | } as Record<number, string> |
| 660 | )[arg]; |
| 661 | if (label) fonts.set(state.font, new TextDecoder(label)); |
| 662 | } else if ( |
| 663 | [ |
| 664 | "colortbl", |
| 665 | "stylesheet", |
| 666 | "info", |
| 667 | "pict", |
| 668 | "object", |
| 669 | "fldinst", |
| 670 | "header", |
| 671 | "headerl", |
| 672 | "headerr", |
| 673 | "headerf", |
| 674 | "footer", |
| 675 | "footerl", |
| 676 | "footerr", |
| 677 | "footerf", |
| 678 | "filetbl", |
| 679 | "datastore", |
| 680 | "themedata", |
| 681 | ].includes(word!) |
| 682 | ) |
| 683 | state.skip = true; |
| 684 | else if (word === "bin") { |
| 685 | if (!Number.isSafeInteger(arg) || arg < 0 || arg > source.length - i) |
| 686 | throw new PreviewError({ code: "rtf-binary-data" }); |
| 687 | i += arg; |
| 688 | } else if (word === "mac") state.decoder = new TextDecoder("macintosh"); |
| 689 | else if (word === "ansicpg") { |
| 690 | const label = |
| 691 | ( |
| 692 | { |
| 693 | 65001: "utf-8", |
| 694 | 932: "shift_jis", |
| 695 | 936: "gbk", |
| 696 | 949: "euc-kr", |
| 697 | 950: "big5", |
| 698 | 874: "windows-874", |
| 699 | 10000: "macintosh", |
| 700 | } as Record<number, string> |
| 701 | )[arg] ?? `windows-${arg}`; |
| 702 | try { |
| 703 | state.decoder = new TextDecoder(label); |
| 704 | } catch { |
| 705 | throw new PreviewError({ code: "rtf-code-page" }); |
| 706 | } |
| 707 | } else if (word === "b") state.bold = arg !== 0; |
| 708 | else if (word === "i") state.italic = arg !== 0; |
| 709 | else if (word === "ul") state.underline = arg !== 0; |
| 710 | else if (word === "ulnone") state.underline = false; |
| 711 | else if (word === "plain") |
| 712 | state = { ...state, bold: false, italic: false, underline: false, font: defaultFont }; |
| 713 | else if (word === "uc") state.uc = Math.max(0, Math.min(10, arg)); |
| 714 | else if (word === "u") { |
| 715 | fallback = 0; |
| 716 | emit(String.fromCharCode(arg < 0 ? arg + 65536 : arg)); |
| 717 | fallback = state.uc; |
| 718 | } else if (!state.skip && word === "page") finishPage(); |
| 719 | else if (!state.skip && (word === "par" || word === "line" || word === "row")) html += "<br>"; |
| 720 | else { |
| 721 | const character = RTF_CHARACTERS.get(word!); |
| 722 | if (character) emit(character); |
| 723 | } |
| 724 | } else if (char !== "\r" && char !== "\n" && char) emitByte(char.charCodeAt(0)); |
| 725 | } |
| 726 | if (stack.length) throw new PreviewError({ code: "rtf-damaged" }); |
| 727 | flush(); |
| 728 | if (html || !pages.length) finishPage(); |
| 729 | return { type: "document", navigation: "page", pages }; |
| 730 | } |