| |
| import { $typst, TypstSnippet } from '@myriaddreamin/typst.ts/dist/esm/contrib/snippet.mjs'; |
|
|
| let inited = false; |
| const PROJECT_ROOT = '/proj'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function ensureTypstInited() { |
| if (inited) return; |
|
|
| |
| |
| const fontUrls = [ |
| '/fonts/NotoSerifCJKtc-Medium.otf', |
| |
| |
| |
| |
| |
| |
| |
| ]; |
|
|
| |
| await Promise.all( |
| fontUrls.map(async (url) => { |
| try { |
| const resp = await fetch(url); |
| if (!resp.ok) throw new Error(`HTTP ${resp.status}`); |
| const fontBytes = new Uint8Array(await resp.arrayBuffer()); |
| $typst.use(TypstSnippet.preloadFontData(fontBytes)); |
| } catch (e) { |
| console.warn(`[typst] 字体加载失败:${url},中文可能显示异常:`, e); |
| } |
| }), |
| ); |
|
|
| |
| $typst.setCompilerInitOptions({ |
| getModule: () => ({ |
| module_or_path: 'https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-web-compiler/pkg/typst_ts_web_compiler_bg.wasm' |
| }) as any, |
| }); |
|
|
| $typst.setRendererInitOptions({ |
| getModule: () => ({ |
| module_or_path: 'https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm' |
| }) as any, |
| }); |
|
|
| inited = true; |
| } |
|
|
| export async function compileTypstToSvg(mainContent: string): Promise<string> { |
| await ensureTypstInited(); |
| return await $typst.svg({ mainContent }); |
| } |
|
|
| function isTypFilePath(p: string) { |
| return p.endsWith('.typ'); |
| } |
|
|
| function extractTypDependencies(content: string): string[] { |
| |
| const deps: string[] = []; |
| const re = /#(?:import|include)\s+(?:"([^"]+)"|'([^']+)')/g; |
| let m: RegExpExecArray | null; |
| while ((m = re.exec(content))) { |
| const p = (m[1] ?? m[2] ?? '').trim(); |
| if (!p) continue; |
| |
| if (p.startsWith('@')) continue; |
| if (!isTypFilePath(p)) continue; |
| deps.push(p); |
| } |
| return deps; |
| } |
|
|
| function resolveUrl(fromUrl: string, rel: string): string { |
| |
| |
| const base = new URL(fromUrl, 'http://local'); |
| const next = new URL(rel, base); |
| return next.pathname; |
| } |
|
|
| function toProjectPath(urlPath: string): string { |
| |
| |
| return `${PROJECT_ROOT}${urlPath}`; |
| } |
|
|
| export type TypstProject = { |
| entryUrl: string; |
| entryPath: string; |
| files: Map<string, string>; |
| }; |
|
|
| export async function loadTypstProjectFromPublic(entryUrl: string): Promise<TypstProject> { |
| await ensureTypstInited(); |
|
|
| const entry = entryUrl.startsWith('/') ? entryUrl : `/${entryUrl}`; |
| const visited = new Set<string>(); |
| const files = new Map<string, string>(); |
|
|
| const queue: Array<{ urlPath: string }> = [{ urlPath: entry }]; |
|
|
| while (queue.length) { |
| const { urlPath } = queue.shift()!; |
| if (visited.has(urlPath)) continue; |
| visited.add(urlPath); |
|
|
| const resp = await fetch(urlPath); |
| if (!resp.ok) { |
| throw new Error(`加载 typ 文件失败:${urlPath}(HTTP ${resp.status})`); |
| } |
| const text = await resp.text(); |
| files.set(toProjectPath(urlPath), text); |
|
|
| for (const dep of extractTypDependencies(text)) { |
| const depUrl = resolveUrl(urlPath, dep); |
| queue.push({ urlPath: depUrl }); |
| } |
| } |
|
|
| return { |
| entryUrl: entry, |
| entryPath: toProjectPath(entry), |
| files, |
| }; |
| } |
|
|
| export async function compileTypstProjectToSvg( |
| project: TypstProject, |
| overrides?: Record<string, string>, |
| ) { |
| await ensureTypstInited(); |
|
|
| |
| await $typst.resetShadow(); |
|
|
| |
| for (const [projectPath, content] of project.files.entries()) { |
| const override = overrides?.[projectPath]; |
| await $typst.addSource(projectPath, override ?? content); |
| } |
|
|
| |
| if (overrides) { |
| for (const [projectPath, content] of Object.entries(overrides)) { |
| if (!project.files.has(projectPath)) { |
| await $typst.addSource(projectPath, content); |
| } |
| } |
| } |
|
|
| |
| return await $typst.svg({ mainFilePath: project.entryPath }); |
| } |
|
|