@file-viewer/renderer-typst 2.0.11 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md CHANGED
@@ -31,7 +31,7 @@ const options = {
31
31
  - Supports `.typ` and `.typst`.
32
32
  - Compiles Typst source in the browser and renders SVG pages.
33
33
  - Preserves page size metadata for preview, zoom, print, and HTML export.
34
- - Supports `options.typst.renderTimeoutMs` for long-running documents.
34
+ - Supports `options.typst.renderTimeoutMs` for slow asset loading and long-running documents.
35
35
  - Reports clear diagnostics for missing WASM assets, wrong MIME types, network failures, and Typst compile errors.
36
36
 
37
37
  ## Offline Assets
@@ -40,8 +40,9 @@ The default asset paths are:
40
40
 
41
41
  - `wasm/typst/typst_ts_web_compiler_bg.wasm`
42
42
  - `wasm/typst/typst_ts_renderer_bg.wasm`
43
+ - `wasm/typst/fonts/`
43
44
 
44
- For private deployments, override them with `options.typst.compilerWasmUrl` and `options.typst.rendererWasmUrl`. The runtime does not depend on public CDNs.
45
+ For private deployments, override them with `options.typst.compilerWasmUrl`, `options.typst.rendererWasmUrl`, and `options.typst.fontAssetsUrl`. The default text fonts ship with this package and are copied by `file-viewer-copy-assets` / `@file-viewer/vite-plugin`, so the runtime does not depend on public CDNs.
45
46
 
46
47
  ## Migration Note
47
48
 
package/README.md CHANGED
@@ -31,7 +31,7 @@ const options = {
31
31
  - 支持 `.typ`、`.typst`。
32
32
  - 浏览器端真实编译 Typst 源文件并输出 SVG 页面。
33
33
  - 支持页面尺寸识别、缩放、打印和 HTML 导出。
34
- - 支持 `options.typst.renderTimeoutMs` 编译超时控制。
34
+ - 支持 `options.typst.renderTimeoutMs` 加载 / 编译超时控制。
35
35
  - WASM 缺失、MIME 错误、网络错误或编译错误会给出明确诊断。
36
36
 
37
37
  ## 离线资产
@@ -40,8 +40,9 @@ const options = {
40
40
 
41
41
  - `wasm/typst/typst_ts_web_compiler_bg.wasm`
42
42
  - `wasm/typst/typst_ts_renderer_bg.wasm`
43
+ - `wasm/typst/fonts/`
43
44
 
44
- 私有化部署时可以通过 `options.typst.compilerWasmUrl` 和 `options.typst.rendererWasmUrl` 覆盖。预览运行时不会访问公共 CDN。
45
+ 私有化部署时可以通过 `options.typst.compilerWasmUrl`、`options.typst.rendererWasmUrl` 和 `options.typst.fontAssetsUrl` 覆盖。默认字体资产随本包发布并由 `file-viewer-copy-assets` / `@file-viewer/vite-plugin` 复制到本地静态目录,预览运行时不会访问公共 CDN。
45
46
 
46
47
  ## 迁移说明
47
48
 
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,7 @@
1
+ # Typst Font Assets
2
+
3
+ This directory vendors the default `text` font assets used by `@myriaddreamin/typst.ts`.
4
+
5
+ Source: `typst-assets@v0.13.1`, `files/fonts/`
6
+
7
+ The files are copied into `wasm/typst/fonts/` by File Viewer asset tooling so Typst previews do not fetch fonts from public CDNs at runtime. Keep the filenames stable because `typst.ts` resolves them by name.
package/dist/typst.d.ts CHANGED
@@ -2,6 +2,7 @@ import { type FileRenderContext, type FileViewerRenderedInstance } from '@file-v
2
2
  declare global {
3
3
  interface Window {
4
4
  __FLYFISH_TYPST_COMPILER_WASM_URL__?: string;
5
+ __FLYFISH_TYPST_FONT_ASSETS_URL__?: string;
5
6
  __FLYFISH_TYPST_RENDERER_WASM_URL__?: string;
6
7
  }
7
8
  }
package/dist/typst.js CHANGED
@@ -1,5 +1,6 @@
1
- import { $typst } from '@myriaddreamin/typst.ts';
2
- import { resolveFileViewerTypstCompilerWasmUrl, resolveFileViewerTypstRendererWasmUrl, } from '@file-viewer/core/assets';
1
+ import { $typst, MemoryAccessModel } from '@myriaddreamin/typst.ts';
2
+ import { TypstSnippet } from '@myriaddreamin/typst.ts/contrib/snippet';
3
+ import { resolveFileViewerTypstCompilerWasmUrl, resolveFileViewerTypstFontAssetsUrl, resolveFileViewerTypstRendererWasmUrl, } from '@file-viewer/core/assets';
3
4
  import { createFileViewerZoomChangeEmitter, formatCssPixels, registerFileViewerZoomProvider, readFileViewerText, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
4
5
  const typstStyle = `
5
6
  .typst-viewer{min-height:100%;overflow:auto;background:#eef1f4;color:#172033}
@@ -30,10 +31,10 @@ const typstStyle = `
30
31
  @media (prefers-color-scheme:dark){.file-viewer[data-viewer-theme='system'] .typst-viewer{background:#101820;color:#e6edf3}.file-viewer[data-viewer-theme='system'] .typst-toolbar{border-bottom-color:rgba(139,148,158,.22);background:rgba(15,23,42,.9)}.file-viewer[data-viewer-theme='system'] .typst-toolbar strong{color:#f8fafc}.file-viewer[data-viewer-theme='system'] .typst-toolbar span,.file-viewer[data-viewer-theme='system'] .typst-toolbar em{color:#9aa7b8}.file-viewer[data-viewer-theme='system'] .typst-page-shell{border-color:rgba(139,148,158,.26);box-shadow:0 24px 56px rgba(0,0,0,.38)}.file-viewer[data-viewer-theme='system'] .typst-loading,.file-viewer[data-viewer-theme='system'] .typst-error{border-color:rgba(139,148,158,.22);background:#151b23;box-shadow:0 24px 56px rgba(0,0,0,.32)}.file-viewer[data-viewer-theme='system'] .typst-loading strong,.file-viewer[data-viewer-theme='system'] .typst-error strong{color:#f8fafc}}
31
32
  `;
32
33
  let typstEngineConfigKey = '';
33
- const DEFAULT_TYPST_RENDER_TIMEOUT_MS = 60000;
34
+ const DEFAULT_TYPST_RENDER_TIMEOUT_MS = 180000;
34
35
  class TypstRenderTimeoutError extends Error {
35
36
  constructor(timeoutMs) {
36
- super(`Typst 编译超过 ${Math.round(timeoutMs / 1000)} 秒`);
37
+ super(`Typst WASM / 字体加载或编译超过 ${Math.round(timeoutMs / 1000)} 秒`);
37
38
  this.name = 'TypstRenderTimeoutError';
38
39
  }
39
40
  }
@@ -58,11 +59,21 @@ const getWindowOverride = (key) => {
58
59
  }
59
60
  return window[key];
60
61
  };
61
- const configureTypstEngine = (compilerWasmUrl, rendererWasmUrl) => {
62
- const configKey = `${compilerWasmUrl}\n${rendererWasmUrl}`;
62
+ const configureTypstEngine = (compilerWasmUrl, rendererWasmUrl, fontAssetsUrl) => {
63
+ const normalizedFontAssetsUrl = fontAssetsUrl.endsWith('/') ? fontAssetsUrl : `${fontAssetsUrl}/`;
64
+ const configKey = `${compilerWasmUrl}\n${rendererWasmUrl}\n${normalizedFontAssetsUrl}`;
63
65
  if (typstEngineConfigKey === configKey) {
64
66
  return;
65
67
  }
68
+ // typst.ts otherwise installs public registry/font fetchers. File Viewer keeps
69
+ // Typst deterministic for enterprise/offline deployments by pinning both.
70
+ $typst.use(TypstSnippet.withAccessModel(new MemoryAccessModel()), TypstSnippet.preloadFontAssets({
71
+ assets: ['text'],
72
+ assetUrlPrefix: {
73
+ text: normalizedFontAssetsUrl,
74
+ _: normalizedFontAssetsUrl,
75
+ },
76
+ }));
66
77
  $typst.setCompilerInitOptions({
67
78
  getModule: () => compilerWasmUrl,
68
79
  });
@@ -73,6 +84,7 @@ const configureTypstEngine = (compilerWasmUrl, rendererWasmUrl) => {
73
84
  };
74
85
  const pushUniqueTypstCandidate = (candidates, candidate) => {
75
86
  if (candidates.some(item => item.compilerWasmUrl === candidate.compilerWasmUrl &&
87
+ item.fontAssetsUrl === candidate.fontAssetsUrl &&
76
88
  item.rendererWasmUrl === candidate.rendererWasmUrl)) {
77
89
  return;
78
90
  }
@@ -82,20 +94,27 @@ const resolveTypstEngineCandidates = (context, documentBaseUrl) => {
82
94
  var _a;
83
95
  const typstOptions = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.typst;
84
96
  const compilerOverride = getWindowOverride('__FLYFISH_TYPST_COMPILER_WASM_URL__');
97
+ const fontAssetsOverride = getWindowOverride('__FLYFISH_TYPST_FONT_ASSETS_URL__');
85
98
  const rendererOverride = getWindowOverride('__FLYFISH_TYPST_RENDERER_WASM_URL__');
86
99
  const compilerWasmUrl = resolveFileViewerTypstCompilerWasmUrl(typstOptions, [
87
100
  compilerOverride,
88
101
  ], documentBaseUrl);
102
+ const fontAssetsUrl = resolveFileViewerTypstFontAssetsUrl(typstOptions, [
103
+ fontAssetsOverride,
104
+ ], documentBaseUrl);
89
105
  const rendererWasmUrl = resolveFileViewerTypstRendererWasmUrl(typstOptions, [
90
106
  rendererOverride,
91
107
  ], documentBaseUrl);
92
108
  const hasConfiguredAsset = Boolean((typstOptions === null || typstOptions === void 0 ? void 0 : typstOptions.compilerWasmUrl) ||
109
+ (typstOptions === null || typstOptions === void 0 ? void 0 : typstOptions.fontAssetsUrl) ||
93
110
  (typstOptions === null || typstOptions === void 0 ? void 0 : typstOptions.rendererWasmUrl) ||
94
111
  compilerOverride ||
112
+ fontAssetsOverride ||
95
113
  rendererOverride);
96
114
  const candidates = [];
97
115
  pushUniqueTypstCandidate(candidates, {
98
116
  compilerWasmUrl,
117
+ fontAssetsUrl,
99
118
  rendererWasmUrl,
100
119
  source: hasConfiguredAsset ? 'configured' : 'local',
101
120
  preflight: !hasConfiguredAsset,
@@ -232,13 +251,13 @@ const formatTypstRuntimeError = (error) => {
232
251
  if (error instanceof TypstRenderTimeoutError) {
233
252
  return [
234
253
  message,
235
- '请检查 Typst 源文件复杂度,或通过 options.typst.renderTimeoutMs 调大浏览器端编译超时。'
254
+ '请检查 Typst compiler WASM、renderer WASM、字体目录的下载速度和缓存策略;弱网或跨境部署可通过 options.typst.renderTimeoutMs 调大浏览器端加载/编译超时。'
236
255
  ].join('\n\n');
237
256
  }
238
257
  if (isTypstAssetLoadError(error)) {
239
258
  return [
240
259
  message,
241
- 'Typst 需要本地 compiler / renderer WASM。请运行 file-viewer-copy-assets,或配置 options.typst.compilerWasmUrl / options.typst.rendererWasmUrl,并确认服务器以 application/wasm 返回资源。'
260
+ 'Typst 需要本地 compiler / renderer WASM 和字体目录。请运行 file-viewer-copy-assets,或配置 options.typst.compilerWasmUrl / options.typst.rendererWasmUrl / options.typst.fontAssetsUrl,并确认服务器以 application/wasm 返回 WASM。'
242
261
  ].join('\n\n');
243
262
  }
244
263
  return message;
@@ -481,7 +500,7 @@ export default async function renderTypst(buffer, target, _type, context) {
481
500
  continue;
482
501
  }
483
502
  try {
484
- configureTypstEngine(candidate.compilerWasmUrl, candidate.rendererWasmUrl);
503
+ configureTypstEngine(candidate.compilerWasmUrl, candidate.rendererWasmUrl, candidate.fontAssetsUrl);
485
504
  return await withRenderTimeout($typst.svg({
486
505
  mainContent: source,
487
506
  data_selection: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/renderer-typst",
3
- "version": "2.0.11",
3
+ "version": "2.1.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Standalone Typst renderer plugin for Flyfish File Viewer powered by browser WASM.",
@@ -47,13 +47,14 @@
47
47
  "./package.json": "./package.json"
48
48
  },
49
49
  "files": [
50
+ "assets",
50
51
  "dist",
51
52
  "README.md",
52
53
  "README.en.md",
53
54
  "LICENSE"
54
55
  ],
55
56
  "dependencies": {
56
- "@file-viewer/core": "^2.0.11",
57
+ "@file-viewer/core": "^2.1.0",
57
58
  "@myriaddreamin/typst-ts-renderer": "0.7.0",
58
59
  "@myriaddreamin/typst-ts-web-compiler": "0.7.0",
59
60
  "@myriaddreamin/typst.ts": "0.7.0"