@file-viewer/renderer-text 3.1.2 → 3.2.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 +3 -135
- package/README.md +3 -124
- package/bin/copy-xml-profile-assets.mjs +3 -3
- package/dist/code.d.ts +0 -7
- package/dist/code.js +6 -138
- package/dist/html.js +2 -4
- package/dist/index.js +6 -2
- package/dist/largeText.js +39 -24
- package/dist/lrc.d.ts +2 -0
- package/dist/lrc.js +185 -0
- package/dist/lrcParser.d.ts +23 -0
- package/dist/lrcParser.js +83 -0
- package/dist/markdown.d.ts +1 -1
- package/dist/markdown.js +95 -31
- package/dist/markdownAnchors.d.ts +1 -0
- package/dist/markdownAnchors.js +66 -0
- package/dist/optionalCapabilities.d.ts +0 -4
- package/dist/optionalCapabilities.js +0 -4
- package/dist/patch.js +2 -2
- package/dist/prettyPrint.d.ts +0 -7
- package/dist/prettyPrint.js +0 -9
- package/dist/sanitizeHtml.d.ts +0 -1
- package/dist/sanitizeHtml.js +0 -1
- package/dist/syntaxGrammars/data.d.ts +10 -0
- package/dist/syntaxGrammars/data.js +8 -0
- package/dist/syntaxGrammars/documents.d.ts +5 -0
- package/dist/syntaxGrammars/documents.js +3 -0
- package/dist/syntaxGrammars/managed.d.ts +7 -0
- package/dist/syntaxGrammars/managed.js +5 -0
- package/dist/syntaxGrammars/native.d.ts +10 -0
- package/dist/syntaxGrammars/native.js +8 -0
- package/dist/syntaxGrammars/php.d.ts +5 -0
- package/dist/syntaxGrammars/php.js +3 -0
- package/dist/syntaxGrammars/scripting.d.ts +9 -0
- package/dist/syntaxGrammars/scripting.js +7 -0
- package/dist/syntaxGrammars/tooling.d.ts +8 -0
- package/dist/syntaxGrammars/tooling.js +6 -0
- package/dist/syntaxGrammars/web.d.ts +8 -0
- package/dist/syntaxGrammars/web.js +6 -0
- package/dist/syntaxHighlight.d.ts +5 -0
- package/dist/syntaxHighlight.js +167 -0
- package/dist/xml-profiles.d.ts +0 -1
- package/dist/xml-profiles.js +0 -1
- package/dist/xml.js +6 -11
- package/dist/xmlEngines.js +3 -4
- package/dist/xmlSafety.d.ts +0 -1
- package/dist/xmlSafety.js +0 -3
- package/dist/xmlWasmMemory.d.ts +0 -6
- package/dist/xmlWasmMemory.js +0 -6
- package/file-viewer.capability.json +1 -1
- package/licenses/xml-profiles/NOTICE.md +1 -1
- package/package.json +84 -29
package/README.en.md
CHANGED
|
@@ -1,139 +1,7 @@
|
|
|
1
1
|
# @file-viewer/renderer-text
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Runtime package version 3.2.0.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Usage and API documentation: https://doc.file-viewer.app/guide/on-demand-renderers
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
import FileViewer from '@file-viewer/vue3'
|
|
9
|
-
import { textRenderer } from '@file-viewer/renderer-text'
|
|
10
|
-
|
|
11
|
-
const options = {
|
|
12
|
-
builtinRenderers: 'none',
|
|
13
|
-
renderers: textRenderer,
|
|
14
|
-
text: {
|
|
15
|
-
lineNumbers: true,
|
|
16
|
-
wrapLongLines: true,
|
|
17
|
-
prettyPrint: true,
|
|
18
|
-
prettyPrintMaxBytes: 512 * 1024,
|
|
19
|
-
},
|
|
20
|
-
}
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
You can also compose it with other renderers:
|
|
24
|
-
|
|
25
|
-
```ts
|
|
26
|
-
import { textRenderer } from '@file-viewer/renderer-text'
|
|
27
|
-
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
28
|
-
|
|
29
|
-
const options = {
|
|
30
|
-
builtinRenderers: 'none',
|
|
31
|
-
renderers: [pdfRenderer, textRenderer],
|
|
32
|
-
}
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## Capabilities
|
|
36
|
-
|
|
37
|
-
- Code and text preview uses `highlight.js` core with per-language dynamic imports instead of registering every language up front.
|
|
38
|
-
- Code, text, and virtualized Markdown source views show their file type, indexing status, and line-count metadata bar by default. Set `options.text.toolbar: false` to hide this renderer-local bar without hiding the viewer-level download, search, or zoom toolbar.
|
|
39
|
-
- Regular code and text previews can show a line-number gutter with `options.text.lineNumbers: true`. The gutter is excluded from copied source, search matches, and assistive reading. Virtual large-text views keep their existing gutter unless it is explicitly set to `false`.
|
|
40
|
-
- `options.text.wrapLongLines: true` changes layout only and never inserts source newlines. Regular previews keep one gutter entry per logical line; large files remain bounded in the virtual window while wrapping to the available width.
|
|
41
|
-
- `options.text.prettyPrint: true` lazily loads Prettier and only the parser plugins needed by a supported structured format. It formats a display copy, labels the toolbar as a formatted preview, and provides a switch back to the original source. JSON/JSONC/JSON5, JavaScript/TypeScript, HTML/Vue, CSS, YAML, Markdown, GraphQL, and XML share this path.
|
|
42
|
-
- `prettyPrintMaxBytes` limits only Prettier and defaults to the effective `virtualizeAboveBytes` value (512 KiB when omitted). Oversized, malformed, and unsupported inputs fall back to the original source, which continues through the existing regular or virtual renderer. XML uses a conservative whitespace-preserving mode; mixed content and `xml:space="preserve"` stay on the original source path.
|
|
43
|
-
- The legacy `*-full` script-tag IIFE assets do not bundle Prettier, so `prettyPrint` falls back to the original source there. Use the ESM integration (standard component packages or `@file-viewer/preset-*`) for formatted previews.
|
|
44
|
-
- With the text-tools capability installed, `patch` uses `diff2html` for side-by-side review and `bundle` / `bdl` enables Git bundle inspection.
|
|
45
|
-
- With the Mermaid capability installed, fenced Mermaid blocks render as diagrams. Without it, the source stays visible with the exact CLI enablement command.
|
|
46
|
-
- HTML/HTM opens a static page preview with a source-view toggle. `options.text.htmlView: 'source'` starts with the original source. Inline CSS and embedded images are preserved; scripts, forms, external navigation, and external resource requests are blocked by sanitization, CSP, and an opaque sandbox. This is not a website runtime.
|
|
47
|
-
- XML, Vue, and similar files default to escaped source previews. XML can opt into the XSD/XSLT profiles below. HTML source supports the same highlighting, formatting, and large-text virtualization options.
|
|
48
|
-
- Markdown uses `marked` for a read-only reading surface with dark/light theme support, table scrolling, and a unified zoom provider.
|
|
49
|
-
- Markdown no longer falls back to source because of the general large-text threshold. Set `options.text.markdownVirtualizeAboveBytes` only when an application must bound exceptionally large Markdown files.
|
|
50
|
-
- Does not depend on any online service or public CDN, making it suitable for intranet logs, configs, snippets, README files, and knowledge-base attachments.
|
|
51
|
-
|
|
52
|
-
## Migration Note
|
|
53
|
-
|
|
54
|
-
Standard/full includes base code, text, and Markdown without installing `diff2html`, `pako`, or Mermaid. Optional uploads show `npx file-viewer-cli add text-tools --write` or `add mermaid-markdown --write`; `preset-all` is only for explicit all/debug use.
|
|
55
|
-
|
|
56
|
-
## Optional XML Profiles
|
|
57
|
-
|
|
58
|
-
XML profiles use real libxml2 XML Schema 1.0 and libxslt XSLT 1.0 WASM engines.
|
|
59
|
-
The ordinary text/standard/full installation does not install these optional peers
|
|
60
|
-
or replace the browser's `XSLTProcessor`. To enable profiles, install the pinned
|
|
61
|
-
engines and copy their assets:
|
|
62
|
-
|
|
63
|
-
```sh
|
|
64
|
-
pnpm add xmllint-wasm@5.3.0 xslt-polyfill@1.0.29
|
|
65
|
-
pnpm exec file-viewer-xml-assets public/file-viewer/xml
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
The asset helper copies installed files without downloading or installing anything.
|
|
69
|
-
It preserves unrelated destination files and includes JS/WASM, `licenses/`, and a
|
|
70
|
-
SHA-256 `manifest.json`. Keep the notices with redistributed assets.
|
|
71
|
-
|
|
72
|
-
Register once, then pass the options to Vanilla/Web Component or any standard component:
|
|
73
|
-
|
|
74
|
-
```ts
|
|
75
|
-
import { enableFileViewerXmlProfiles } from '@file-viewer/renderer-text/xml-profiles'
|
|
76
|
-
|
|
77
|
-
const disableXmlProfiles = enableFileViewerXmlProfiles()
|
|
78
|
-
const options = {
|
|
79
|
-
xml: {
|
|
80
|
-
profilesUrl: '/xml-profiles/profiles.json',
|
|
81
|
-
runtime: {
|
|
82
|
-
xsdWorkerUrl: '/file-viewer/xml/xmllint-browser.mjs',
|
|
83
|
-
xsltModuleUrl: '/file-viewer/xml/xslt-wasm.js',
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
}
|
|
87
|
-
// Call disableXmlProfiles() when registration is no longer needed.
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
Keep `xmllint.wasm` beside `xmllint-browser.mjs`. Serve JS/MJS as `text/javascript`
|
|
91
|
-
and WASM as `application/wasm`. Omitted runtime URLs resolve under
|
|
92
|
-
`xml/` in the configured asset base, or relative to the page when no asset base is
|
|
93
|
-
configured. The host CSP must permit `worker-src blob:` and WASM compilation
|
|
94
|
-
(`script-src 'wasm-unsafe-eval'` where supported). All profile and runtime resources
|
|
95
|
-
must use same-origin HTTP(S) without redirects. Serve them with the application
|
|
96
|
-
for offline/intranet use; no public CDN or document upload is involved.
|
|
97
|
-
|
|
98
|
-
Example manifest:
|
|
99
|
-
|
|
100
|
-
```json
|
|
101
|
-
{
|
|
102
|
-
"profiles": [{
|
|
103
|
-
"id": "invoice-v1",
|
|
104
|
-
"match": {
|
|
105
|
-
"rootNamespace": { "enabled": true, "root": "invoice", "namespace": "urn:example:invoice:v1" },
|
|
106
|
-
"xsd": { "enabled": true }
|
|
107
|
-
},
|
|
108
|
-
"xsd": "./invoice.xsd",
|
|
109
|
-
"xslt": "./invoice.xsl"
|
|
110
|
-
}]
|
|
111
|
-
}
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
Resource paths resolve against the manifest directory. Alternatively, supply
|
|
115
|
-
`xml.profiles` and `xml.baseUrl` instead of `profilesUrl`. Each profile must enable
|
|
116
|
-
at least one check. Root/namespace and XSD checks are independent; all enabled
|
|
117
|
-
checks must pass. Exactly one matching profile triggers transformation. No match,
|
|
118
|
-
ambiguity, validation errors and processing failures retain the XML source and
|
|
119
|
-
report diagnostics through `xml.onDiagnostic`, the standard `onDiagnostic`, and
|
|
120
|
-
the diagnostics panel.
|
|
121
|
-
|
|
122
|
-
Generated HTML is sanitized and displayed in an opaque iframe with an empty
|
|
123
|
-
sandbox and a CSP blocking scripts and external resources. `View Source` and
|
|
124
|
-
`View Rendered` reuse the original input buffer and cached result without another
|
|
125
|
-
download, validation or transformation. Source is never DOM serialization and
|
|
126
|
-
pretty printing is disabled for this view. The original remains the download
|
|
127
|
-
source. Use `xml.initialView: 'source'` or customize button copy with `xml.labels`.
|
|
128
|
-
|
|
129
|
-
DTD/entity declarations, XInclude, schema include/import/redefine/override,
|
|
130
|
-
XSLT include/import, `document()` and extension instructions fail closed. There
|
|
131
|
-
is no external-resource resolver in this version; such documents fall back to source.
|
|
132
|
-
The default operation deadline is 15 seconds, capped at 60 seconds. Hard ceilings
|
|
133
|
-
are 4 MiB XML, 2 MiB per schema/stylesheet, 8 MiB output, 16 MiB total resources,
|
|
134
|
-
and 32 profiles; `xml.limits` may lower them. Each engine's WASM linear memory is
|
|
135
|
-
capped at 64 MiB. Timeout, cancellation and unload terminate workers and revoke Blob URLs.
|
|
136
|
-
|
|
137
|
-
The source repository's `test/fixtures/issue-305/` contains redistributable
|
|
138
|
-
valid/invalid XML, XSD, XSLT and a manifest. See [XML profiles notices](./licenses/xml-profiles/NOTICE.md)
|
|
139
|
-
for verified licenses and upstream sources.
|
|
7
|
+
The package's own license is Apache-2.0. Included third-party notices apply to their respective components.
|
package/README.md
CHANGED
|
@@ -1,128 +1,7 @@
|
|
|
1
1
|
# @file-viewer/renderer-text
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Runtime package version 3.2.0.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Usage and API documentation: https://doc.file-viewer.app/guide/on-demand-renderers
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
import FileViewer from '@file-viewer/vue3'
|
|
9
|
-
import { textRenderer } from '@file-viewer/renderer-text'
|
|
10
|
-
|
|
11
|
-
const options = {
|
|
12
|
-
builtinRenderers: 'none',
|
|
13
|
-
renderers: textRenderer,
|
|
14
|
-
text: {
|
|
15
|
-
lineNumbers: true,
|
|
16
|
-
wrapLongLines: true,
|
|
17
|
-
prettyPrint: true,
|
|
18
|
-
prettyPrintMaxBytes: 512 * 1024,
|
|
19
|
-
},
|
|
20
|
-
}
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
也可以与其他 renderer 组合:
|
|
24
|
-
|
|
25
|
-
```ts
|
|
26
|
-
import { textRenderer } from '@file-viewer/renderer-text'
|
|
27
|
-
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
28
|
-
|
|
29
|
-
const options = {
|
|
30
|
-
builtinRenderers: 'none',
|
|
31
|
-
renderers: [pdfRenderer, textRenderer],
|
|
32
|
-
}
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## 能力边界
|
|
36
|
-
|
|
37
|
-
- 代码和文本使用 `highlight.js` core + 按语言动态加载,避免一次性注册全部语言。
|
|
38
|
-
- 代码、文本和超大 Markdown 源码视图默认显示文件类型、索引状态和行数元信息栏;传入 `options.text.toolbar: false` 可隐藏该 renderer 内部栏,不影响 Viewer 的下载、搜索、缩放等全局工具栏。
|
|
39
|
-
- 普通代码和文本可通过 `options.text.lineNumbers: true` 显示行号;行号不会进入复制内容、搜索结果或无障碍朗读。超大文本保留原有的虚拟行号栏,可显式传 `false` 隐藏。
|
|
40
|
-
- `options.text.wrapLongLines: true` 仅改变布局,不向源码插入换行。普通预览会按逻辑行维护行号,超大文本仍使用有界虚拟窗口并按可用宽度换行。
|
|
41
|
-
- `options.text.prettyPrint: true` 会在支持的结构化文本上按需加载 Prettier 与对应 parser,仅格式化显示副本;工具栏会标明“格式化预览”,并可切回原始源码。JSON/JSONC/JSON5、JavaScript/TypeScript、HTML/Vue、CSS、YAML、Markdown、GraphQL 和 XML 共用同一路径。
|
|
42
|
-
- `prettyPrintMaxBytes` 只限制 Prettier,默认继承 `virtualizeAboveBytes`(未配置时为 512 KiB)。超限、语法错误或不支持的格式直接回退原始源码,之后仍由普通或虚拟文本 renderer 决定展示方式。XML 使用保守的 whitespace-preserving 模式;检测到混合内容或 `xml:space="preserve"` 时直接保留原文。
|
|
43
|
-
- 历史 `*-full` 包的 script 标签 IIFE 资源不打包 Prettier,该路径下 `prettyPrint` 无错误回退到原始源码;需要格式化预览时使用 ESM 集成(标准组件包或 `@file-viewer/preset-*`)。
|
|
44
|
-
- 安装 text-tools capability 后,`patch` 使用 `diff2html` 渲染左右比对视图,`bundle` / `bdl` 才启用 Git bundle 结构检查。
|
|
45
|
-
- 安装 Mermaid capability 后,Markdown 内嵌 Mermaid 图才会渲染;未安装时保留源码并显示精确 CLI 启用命令。
|
|
46
|
-
- HTML / HTM 默认显示静态页面,提供页面与源码切换;`options.text.htmlView: 'source'` 可默认查看原始源码。页面保留内联 CSS 和内嵌图片,通过净化、CSP 和独立沙箱阻止脚本、表单、外链跳转和外部资源请求,不用于运行完整网站。
|
|
47
|
-
- XML / Vue 等默认按源码转义展示。XML 可显式启用下方的 XSD/XSLT profiles;HTML 源码视图继续支持高亮、格式化和大文本虚拟化。
|
|
48
|
-
- Markdown 使用 `marked` 输出只读阅读面,并保留明暗主题、表格滚动和统一缩放 provider。
|
|
49
|
-
- Markdown 不再因为通用大文本阈值自动退化成源码;如业务必须限制超大 Markdown,可单独设置 `options.text.markdownVirtualizeAboveBytes`。
|
|
50
|
-
- 不绑定任何在线服务或公共 CDN,适合内网日志、配置、代码片段、README 和知识库附件预览。
|
|
51
|
-
|
|
52
|
-
## 迁移说明
|
|
53
|
-
|
|
54
|
-
standard/full 默认包含基础代码、文本和 Markdown,不安装 `diff2html`、`pako` 或 Mermaid。打开可选格式时会提示运行 `npx file-viewer-cli add text-tools --write` 或 `add mermaid-markdown --write`;`preset-all` 仅用于显式全量/调试。
|
|
55
|
-
|
|
56
|
-
## 可选 XML Profiles
|
|
57
|
-
|
|
58
|
-
XML profiles 使用真实的 libxml2 XML Schema 1.0 和 libxslt XSLT 1.0 WASM 引擎。
|
|
59
|
-
普通 text/standard/full 不安装这两个可选引擎,也不会替换浏览器的 `XSLTProcessor`。
|
|
60
|
-
需要此功能时,安装固定版本并复制资源:
|
|
61
|
-
|
|
62
|
-
```sh
|
|
63
|
-
pnpm add xmllint-wasm@5.3.0 xslt-polyfill@1.0.29
|
|
64
|
-
pnpm exec file-viewer-xml-assets public/file-viewer/xml
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
复制命令不联网、不安装依赖。它会保留目标目录的其他文件,并写入两个引擎的
|
|
68
|
-
JS/WASM、`licenses/` 和带 SHA-256 的 `manifest.json`。部署时一并保留许可证。
|
|
69
|
-
|
|
70
|
-
先注册实现,再把 `xml` 配置传给 Vanilla/Web Component 或其他标准组件:
|
|
71
|
-
|
|
72
|
-
```ts
|
|
73
|
-
import { enableFileViewerXmlProfiles } from '@file-viewer/renderer-text/xml-profiles'
|
|
74
|
-
|
|
75
|
-
const disableXmlProfiles = enableFileViewerXmlProfiles()
|
|
76
|
-
const options = {
|
|
77
|
-
xml: {
|
|
78
|
-
profilesUrl: '/xml-profiles/profiles.json',
|
|
79
|
-
runtime: {
|
|
80
|
-
xsdWorkerUrl: '/file-viewer/xml/xmllint-browser.mjs',
|
|
81
|
-
xsltModuleUrl: '/file-viewer/xml/xslt-wasm.js',
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
}
|
|
85
|
-
// 不再需要全局注册时调用 disableXmlProfiles()。
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
`xmllint.wasm` 必须与 `xmllint-browser.mjs` 位于同一目录。JS/MJS 使用
|
|
89
|
-
`text/javascript`,WASM 使用 `application/wasm`。省略 runtime URL 时,
|
|
90
|
-
默认从配置的 asset base 下的 `xml/` 查找;未配置 asset base 则相对于页面 URL。
|
|
91
|
-
页面 CSP 需允许 `worker-src blob:` 和 WASM 编译(支持该指令的浏览器可使用
|
|
92
|
-
`script-src 'wasm-unsafe-eval'`)。引擎和 profiles 仅允许同源 HTTP(S),不跟随重定向。
|
|
93
|
-
所有资源可随应用部署,运行时不依赖外网。
|
|
94
|
-
|
|
95
|
-
Manifest 示例:
|
|
96
|
-
|
|
97
|
-
```json
|
|
98
|
-
{
|
|
99
|
-
"profiles": [{
|
|
100
|
-
"id": "invoice-v1",
|
|
101
|
-
"match": {
|
|
102
|
-
"rootNamespace": { "enabled": true, "root": "invoice", "namespace": "urn:example:invoice:v1" },
|
|
103
|
-
"xsd": { "enabled": true }
|
|
104
|
-
},
|
|
105
|
-
"xsd": "./invoice.xsd",
|
|
106
|
-
"xslt": "./invoice.xsl"
|
|
107
|
-
}]
|
|
108
|
-
}
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
相对路径以 manifest 所在目录为基准。也可以传 `xml.profiles` 数组和 `xml.baseUrl`,
|
|
112
|
-
替代 `profilesUrl`。每个 profile 至少启用一项检查;root/namespace 和 XSD 可各自启用,
|
|
113
|
-
同时启用时必须全部通过。仅有一个 profile 通过时才转换;多个匹配、无匹配或任何处理错误
|
|
114
|
-
都保留 XML 源码,并通过 `xml.onDiagnostic`、标准 `onDiagnostic` 和界面诊断栏说明原因。
|
|
115
|
-
|
|
116
|
-
转换后的 HTML 经过净化,放入无 sandbox 权限的隔离 iframe,并附带禁止脚本和外网的 CSP。
|
|
117
|
-
`View Source` / `View Rendered` 切换使用已保留的原始 buffer,不重新下载、校验或转换,
|
|
118
|
-
也不使用解析后重新序列化的 XML。源码视图强制关闭 pretty print;下载仍使用原文件。
|
|
119
|
-
可通过 `xml.initialView: 'source'` 默认查看源码,通过 `xml.labels` 定制按钮文字。
|
|
120
|
-
|
|
121
|
-
DTD、实体声明、XInclude、XSD include/import/redefine/override、XSLT include/import、
|
|
122
|
-
`document()` 和扩展指令默认拒绝。当前没有开放外部资源 resolver;需要这些机制的文件会回退源码。
|
|
123
|
-
默认总耗时上限 15 秒(最多 60 秒),XML 4 MiB、单个 schema/style 2 MiB、输出 8 MiB、
|
|
124
|
-
资源总计 16 MiB、最多 32 个 profiles。`xml.limits` 可降低这些上限,不能取消。
|
|
125
|
-
两个引擎的 WASM 线性内存均硬限 64 MiB;超时、取消和卸载会终止 Worker 并释放 Blob URL。
|
|
126
|
-
|
|
127
|
-
源仓库的 `test/fixtures/issue-305/` 提供可再分发的 valid/invalid XML、XSD、XSLT 和 manifest。
|
|
128
|
-
引擎许可证与具体上游来源见 [XML profiles notices](./licenses/xml-profiles/NOTICE.md)。
|
|
7
|
+
The package's own license is Apache-2.0. Included third-party notices apply to their respective components.
|
|
@@ -10,7 +10,7 @@ const require = createRequire(import.meta.url)
|
|
|
10
10
|
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
11
11
|
const engines = [
|
|
12
12
|
{ name: 'xmllint-wasm', version: '5.3.0', files: ['xmllint-browser.mjs', 'xmllint.wasm'] },
|
|
13
|
-
{ name: 'xslt-polyfill', version: '1.0.
|
|
13
|
+
{ name: 'xslt-polyfill', version: '1.0.31', files: ['dist/xslt-wasm.js'] }
|
|
14
14
|
]
|
|
15
15
|
|
|
16
16
|
/** Copy pinned optional runtimes and notices; never download or install packages. */
|
|
@@ -19,7 +19,7 @@ export async function copyXmlProfileAssets(destination = 'public/file-viewer/xml
|
|
|
19
19
|
for (const engine of engines) {
|
|
20
20
|
let manifest
|
|
21
21
|
try { manifest = require.resolve(`${engine.name}/package.json`) } catch {
|
|
22
|
-
throw new Error(`Install the optional XML engines first: pnpm add xmllint-wasm@5.3.0 xslt-polyfill@1.0.
|
|
22
|
+
throw new Error(`Install the optional XML engines first: pnpm add xmllint-wasm@5.3.0 xslt-polyfill@1.0.31 (missing ${engine.name}).`)
|
|
23
23
|
}
|
|
24
24
|
const metadata = JSON.parse(await readFile(manifest, 'utf8'))
|
|
25
25
|
if (metadata.version !== engine.version) throw new Error(`Expected ${engine.name}@${engine.version}; found ${metadata.version}.`)
|
|
@@ -47,7 +47,7 @@ let entryPoint = false
|
|
|
47
47
|
try { entryPoint = !!process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url) } catch { /* Imported helper. */ }
|
|
48
48
|
if (entryPoint) {
|
|
49
49
|
if (process.argv.includes('--help')) {
|
|
50
|
-
console.log('Usage: file-viewer-xml-assets [destination-directory]\nDefault: public/file-viewer/xml\nRequires xmllint-wasm@5.3.0 and xslt-polyfill@1.0.
|
|
50
|
+
console.log('Usage: file-viewer-xml-assets [destination-directory]\nDefault: public/file-viewer/xml\nRequires xmllint-wasm@5.3.0 and xslt-polyfill@1.0.31 installed by the host.')
|
|
51
51
|
} else if (process.argv.length > 3 || process.argv[2]?.startsWith('-')) {
|
|
52
52
|
console.error('Expected one destination directory.'); process.exitCode = 1
|
|
53
53
|
} else {
|
package/dist/code.d.ts
CHANGED
|
@@ -1,9 +1,2 @@
|
|
|
1
1
|
import { type FileRenderContext, type FileViewerRenderedInstance } from '@file-viewer/core';
|
|
2
|
-
/**
|
|
3
|
-
* Framework-neutral text/code renderer.
|
|
4
|
-
*
|
|
5
|
-
* highlight.js core, Prettier standalone, and parser definitions are loaded
|
|
6
|
-
* lazily by format. HTML and XML are always mounted as escaped source text and
|
|
7
|
-
* never executed as real DOM.
|
|
8
|
-
*/
|
|
9
2
|
export default function renderText(buffer: ArrayBuffer, target: HTMLDivElement, type?: string, context?: FileRenderContext): Promise<FileViewerRenderedInstance>;
|
package/dist/code.js
CHANGED
|
@@ -1,90 +1,8 @@
|
|
|
1
1
|
import { createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, decodeFileViewerTextBuffer, registerFileViewerZoomProvider, unregisterFileViewerZoomProvider } from '@file-viewer/core';
|
|
2
|
+
import { escapeHtml, loadHighlighter, registerLanguageOnce, resolveLanguage } from './syntaxHighlight.js';
|
|
2
3
|
import { codeStyle } from './codeStyle.js';
|
|
3
4
|
import renderLargeText, { shouldVirtualizeTextBuffer } from './largeText.js';
|
|
4
5
|
import { formatFileViewerTextForDisplay, resolveFileViewerPrettyPrintMaxBytes, supportsFileViewerPrettyPrint } from './prettyPrint.js';
|
|
5
|
-
const languageMap = {
|
|
6
|
-
bash: 'bash',
|
|
7
|
-
c: 'cpp',
|
|
8
|
-
cc: 'cpp',
|
|
9
|
-
cjs: 'javascript',
|
|
10
|
-
cpp: 'cpp',
|
|
11
|
-
cs: 'csharp',
|
|
12
|
-
css: 'css',
|
|
13
|
-
diff: 'diff',
|
|
14
|
-
patch: 'diff',
|
|
15
|
-
bundle: 'plaintext',
|
|
16
|
-
bdl: 'plaintext',
|
|
17
|
-
gv: 'plaintext',
|
|
18
|
-
go: 'go',
|
|
19
|
-
graphql: 'graphql',
|
|
20
|
-
gql: 'graphql',
|
|
21
|
-
h: 'cpp',
|
|
22
|
-
hcl: 'plaintext',
|
|
23
|
-
hpp: 'cpp',
|
|
24
|
-
html: 'xml',
|
|
25
|
-
htm: 'xml',
|
|
26
|
-
http: 'http',
|
|
27
|
-
ini: 'ini',
|
|
28
|
-
ipynb: 'json',
|
|
29
|
-
java: 'java',
|
|
30
|
-
js: 'javascript',
|
|
31
|
-
json: 'json',
|
|
32
|
-
json5: 'json',
|
|
33
|
-
jsonc: 'json',
|
|
34
|
-
jsx: 'javascript',
|
|
35
|
-
kt: 'kotlin',
|
|
36
|
-
log: 'plaintext',
|
|
37
|
-
md: 'markdown',
|
|
38
|
-
markdown: 'markdown',
|
|
39
|
-
mjs: 'javascript',
|
|
40
|
-
php: 'php',
|
|
41
|
-
proto: 'protobuf',
|
|
42
|
-
py: 'python',
|
|
43
|
-
rb: 'ruby',
|
|
44
|
-
react: 'javascript',
|
|
45
|
-
rs: 'rust',
|
|
46
|
-
sh: 'bash',
|
|
47
|
-
sql: 'sql',
|
|
48
|
-
swift: 'swift',
|
|
49
|
-
tex: 'latex',
|
|
50
|
-
toml: 'ini',
|
|
51
|
-
ts: 'typescript',
|
|
52
|
-
tsx: 'typescript',
|
|
53
|
-
txt: 'plaintext',
|
|
54
|
-
vue: 'xml',
|
|
55
|
-
xml: 'xml',
|
|
56
|
-
yaml: 'yaml',
|
|
57
|
-
yml: 'yaml'
|
|
58
|
-
};
|
|
59
|
-
const languageLoaders = {
|
|
60
|
-
bash: () => import('highlight.js/lib/languages/bash'),
|
|
61
|
-
cpp: () => import('highlight.js/lib/languages/cpp'),
|
|
62
|
-
csharp: () => import('highlight.js/lib/languages/csharp'),
|
|
63
|
-
css: () => import('highlight.js/lib/languages/css'),
|
|
64
|
-
diff: () => import('highlight.js/lib/languages/diff'),
|
|
65
|
-
go: () => import('highlight.js/lib/languages/go'),
|
|
66
|
-
graphql: () => import('highlight.js/lib/languages/graphql'),
|
|
67
|
-
http: () => import('highlight.js/lib/languages/http'),
|
|
68
|
-
ini: () => import('highlight.js/lib/languages/ini'),
|
|
69
|
-
java: () => import('highlight.js/lib/languages/java'),
|
|
70
|
-
javascript: () => import('highlight.js/lib/languages/javascript'),
|
|
71
|
-
json: () => import('highlight.js/lib/languages/json'),
|
|
72
|
-
kotlin: () => import('highlight.js/lib/languages/kotlin'),
|
|
73
|
-
latex: () => import('highlight.js/lib/languages/latex'),
|
|
74
|
-
markdown: () => import('highlight.js/lib/languages/markdown'),
|
|
75
|
-
php: () => import('highlight.js/lib/languages/php'),
|
|
76
|
-
protobuf: () => import('highlight.js/lib/languages/protobuf'),
|
|
77
|
-
python: () => import('highlight.js/lib/languages/python'),
|
|
78
|
-
ruby: () => import('highlight.js/lib/languages/ruby'),
|
|
79
|
-
rust: () => import('highlight.js/lib/languages/rust'),
|
|
80
|
-
sql: () => import('highlight.js/lib/languages/sql'),
|
|
81
|
-
swift: () => import('highlight.js/lib/languages/swift'),
|
|
82
|
-
typescript: () => import('highlight.js/lib/languages/typescript'),
|
|
83
|
-
xml: () => import('highlight.js/lib/languages/xml'),
|
|
84
|
-
yaml: () => import('highlight.js/lib/languages/yaml')
|
|
85
|
-
};
|
|
86
|
-
let highlighterPromise = null;
|
|
87
|
-
const registeredLanguages = new Set();
|
|
88
6
|
const createElement = (documentRef, tagName, className, text) => {
|
|
89
7
|
const element = documentRef.createElement(tagName);
|
|
90
8
|
if (className) {
|
|
@@ -100,40 +18,6 @@ const createStyle = (documentRef) => {
|
|
|
100
18
|
style.textContent = codeStyle;
|
|
101
19
|
return style;
|
|
102
20
|
};
|
|
103
|
-
const resolveLanguage = (type) => {
|
|
104
|
-
return languageMap[type.trim().toLowerCase()] || 'plaintext';
|
|
105
|
-
};
|
|
106
|
-
const escapeHtml = (value) => {
|
|
107
|
-
return value.replace(/[&<>"']/g, char => {
|
|
108
|
-
const entities = {
|
|
109
|
-
'&': '&',
|
|
110
|
-
'<': '<',
|
|
111
|
-
'>': '>',
|
|
112
|
-
'"': '"',
|
|
113
|
-
"'": '''
|
|
114
|
-
};
|
|
115
|
-
return entities[char];
|
|
116
|
-
});
|
|
117
|
-
};
|
|
118
|
-
const loadHighlighter = async () => {
|
|
119
|
-
if (!highlighterPromise) {
|
|
120
|
-
highlighterPromise = import('highlight.js/lib/core').then(module => module.default);
|
|
121
|
-
}
|
|
122
|
-
return highlighterPromise;
|
|
123
|
-
};
|
|
124
|
-
const registerLanguageOnce = async (hljs, name) => {
|
|
125
|
-
if (registeredLanguages.has(name)) {
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
const loader = languageLoaders[name];
|
|
129
|
-
if (!loader) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
const { default: language } = await loader();
|
|
133
|
-
hljs.registerLanguage(name, language);
|
|
134
|
-
registeredLanguages.add(name);
|
|
135
|
-
return true;
|
|
136
|
-
};
|
|
137
21
|
const clampZoom = (value) => {
|
|
138
22
|
return Math.min(2.6, Math.max(0.6, Number(value.toFixed(2))));
|
|
139
23
|
};
|
|
@@ -152,12 +36,6 @@ const createWrappedSourceLine = (documentRef, lineNumber) => {
|
|
|
152
36
|
row.append(number, content);
|
|
153
37
|
return { row, content };
|
|
154
38
|
};
|
|
155
|
-
/**
|
|
156
|
-
* Splits trusted highlight.js markup into logical source-line wrappers while
|
|
157
|
-
* recreating active token spans after every newline. This keeps multiline
|
|
158
|
-
* comments and strings highlighted without coupling visual wrapping to source
|
|
159
|
-
* line numbers.
|
|
160
|
-
*/
|
|
161
39
|
const mountWrappedHighlightedLines = (code, highlightedHtml, expectedLineCount) => {
|
|
162
40
|
const documentRef = code.ownerDocument;
|
|
163
41
|
const staging = createElement(documentRef, 'span');
|
|
@@ -172,7 +50,7 @@ const mountWrappedHighlightedLines = (code, highlightedHtml, expectedLineCount)
|
|
|
172
50
|
lineIndex += 1;
|
|
173
51
|
lines.push(createWrappedSourceLine(documentRef, lineIndex + 1));
|
|
174
52
|
let parent = currentContent();
|
|
175
|
-
activeClones = activeElements.map(element => {
|
|
53
|
+
activeClones = activeElements.map((element) => {
|
|
176
54
|
const clone = element.cloneNode(false);
|
|
177
55
|
parent.append(clone);
|
|
178
56
|
parent = clone;
|
|
@@ -209,7 +87,7 @@ const mountWrappedHighlightedLines = (code, highlightedHtml, expectedLineCount)
|
|
|
209
87
|
while (lines.length < expectedLineCount) {
|
|
210
88
|
lines.push(createWrappedSourceLine(documentRef, lines.length + 1));
|
|
211
89
|
}
|
|
212
|
-
code.replaceChildren(...lines.map(line => line.row));
|
|
90
|
+
code.replaceChildren(...lines.map((line) => line.row));
|
|
213
91
|
};
|
|
214
92
|
const mountCodeMarkup = (pre, code, highlightedHtml, lineCount, showLineNumbers, wrapLongLines) => {
|
|
215
93
|
code.replaceChildren();
|
|
@@ -231,18 +109,8 @@ const mountCodeMarkup = (pre, code, highlightedHtml, lineCount, showLineNumbers,
|
|
|
231
109
|
code.innerHTML = highlightedHtml;
|
|
232
110
|
};
|
|
233
111
|
const canPossiblyFitDecodedPrettyPrintLimit = (buffer, maxBytes) => {
|
|
234
|
-
|
|
235
|
-
// decoded UTF-8 representation. This fast guard avoids decoding enormous
|
|
236
|
-
// structured files that cannot possibly fit the configured decoded limit.
|
|
237
|
-
return buffer.byteLength <= (maxBytes * 2) + 4;
|
|
112
|
+
return buffer.byteLength <= maxBytes * 2 + 4;
|
|
238
113
|
};
|
|
239
|
-
/**
|
|
240
|
-
* Framework-neutral text/code renderer.
|
|
241
|
-
*
|
|
242
|
-
* highlight.js core, Prettier standalone, and parser definitions are loaded
|
|
243
|
-
* lazily by format. HTML and XML are always mounted as escaped source text and
|
|
244
|
-
* never executed as real DOM.
|
|
245
|
-
*/
|
|
246
114
|
export default async function renderText(buffer, target, type, context) {
|
|
247
115
|
var _a, _b;
|
|
248
116
|
const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
|
|
@@ -254,7 +122,7 @@ export default async function renderText(buffer, target, type, context) {
|
|
|
254
122
|
if ((textOptions === null || textOptions === void 0 ? void 0 : textOptions.prettyPrint) === true &&
|
|
255
123
|
supportsFileViewerPrettyPrint(normalizedExtension) &&
|
|
256
124
|
canPossiblyFitDecodedPrettyPrintLimit(buffer, resolveFileViewerPrettyPrintMaxBytes(textOptions))) {
|
|
257
|
-
sourceText = decodeFileViewerTextBuffer(buffer, textOptions.encoding).text;
|
|
125
|
+
sourceText = decodeFileViewerTextBuffer(buffer, textOptions.encoding, textOptions.fallbackEncoding).text;
|
|
258
126
|
prettyPrintResult = await formatFileViewerTextForDisplay(sourceText, normalizedExtension, textOptions, context === null || context === void 0 ? void 0 : context.signal);
|
|
259
127
|
}
|
|
260
128
|
if (!(prettyPrintResult === null || prettyPrintResult === void 0 ? void 0 : prettyPrintResult.formatted) &&
|
|
@@ -271,7 +139,7 @@ export default async function renderText(buffer, target, type, context) {
|
|
|
271
139
|
const { default: renderGitBundle } = await import('./gitBundle.js');
|
|
272
140
|
return renderGitBundle(buffer, target, extension, context);
|
|
273
141
|
}
|
|
274
|
-
const originalText = sourceText !== null && sourceText !== void 0 ? sourceText : decodeFileViewerTextBuffer(buffer, textOptions === null || textOptions === void 0 ? void 0 : textOptions.encoding).text;
|
|
142
|
+
const originalText = sourceText !== null && sourceText !== void 0 ? sourceText : decodeFileViewerTextBuffer(buffer, textOptions === null || textOptions === void 0 ? void 0 : textOptions.encoding, textOptions === null || textOptions === void 0 ? void 0 : textOptions.fallbackEncoding).text;
|
|
275
143
|
const formattedText = (prettyPrintResult === null || prettyPrintResult === void 0 ? void 0 : prettyPrintResult.formatted) ? prettyPrintResult.text : null;
|
|
276
144
|
const language = resolveLanguage(extension);
|
|
277
145
|
const showToolbar = (textOptions === null || textOptions === void 0 ? void 0 : textOptions.toolbar) !== false;
|
package/dist/html.js
CHANGED
|
@@ -40,8 +40,6 @@ export function createHtmlPreviewDocument(documentRef, source) {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
-
// CSP is first, before any retained inline CSS. The containing iframe also has
|
|
44
|
-
// an opaque origin and no sandbox permissions, even if sanitization regresses.
|
|
45
43
|
const policy = preview.createElement('meta');
|
|
46
44
|
policy.httpEquiv = 'Content-Security-Policy';
|
|
47
45
|
policy.content = previewPolicy;
|
|
@@ -99,7 +97,7 @@ export default async function renderHtml(buffer, target, type, context) {
|
|
|
99
97
|
let sourcePending;
|
|
100
98
|
let disposed = false;
|
|
101
99
|
const select = async (view) => {
|
|
102
|
-
var _a, _b;
|
|
100
|
+
var _a, _b, _c, _d;
|
|
103
101
|
if (disposed)
|
|
104
102
|
return;
|
|
105
103
|
root.dataset.htmlView = view;
|
|
@@ -109,7 +107,7 @@ export default async function renderHtml(buffer, target, type, context) {
|
|
|
109
107
|
sourceTarget.hidden = view !== 'source';
|
|
110
108
|
hint.hidden = view !== 'preview';
|
|
111
109
|
if (view === 'preview' && !frame.hasAttribute('srcdoc')) {
|
|
112
|
-
frame.srcdoc = createHtmlPreviewDocument(documentRef, decodeFileViewerTextBuffer(buffer, (_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.encoding).text);
|
|
110
|
+
frame.srcdoc = createHtmlPreviewDocument(documentRef, decodeFileViewerTextBuffer(buffer, (_b = (_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.encoding, (_d = (_c = context === null || context === void 0 ? void 0 : context.options) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.fallbackEncoding).text);
|
|
113
111
|
}
|
|
114
112
|
if (view === 'source' && !sourcePending) {
|
|
115
113
|
sourcePending = renderCode(buffer, sourceTarget, type, context).then((instance) => {
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,10 @@ if (textDefinitions.length !== textRendererIds.length) {
|
|
|
10
10
|
export const textRendererDefinitions = textDefinitions;
|
|
11
11
|
export const renderFileViewerCode = async (buffer, target, type, context) => {
|
|
12
12
|
var _a, _b, _c, _d, _e;
|
|
13
|
+
if ((type === null || type === void 0 ? void 0 : type.trim().toLowerCase()) === 'lrc') {
|
|
14
|
+
const { default: renderLrc } = await import('./lrc.js');
|
|
15
|
+
return renderLrc(buffer, target, type, context);
|
|
16
|
+
}
|
|
13
17
|
if (/^(?:html|htm)$/i.test(type || '')) {
|
|
14
18
|
const { default: renderHtml } = await import('./html.js');
|
|
15
19
|
return renderHtml(buffer, target, type, context);
|
|
@@ -24,11 +28,11 @@ export const renderFileViewerCode = async (buffer, target, type, context) => {
|
|
|
24
28
|
try {
|
|
25
29
|
(_c = (_b = context.options.xml).onDiagnostic) === null || _c === void 0 ? void 0 : _c.call(_b, { code: 'capability-unavailable', message });
|
|
26
30
|
}
|
|
27
|
-
catch {
|
|
31
|
+
catch { }
|
|
28
32
|
try {
|
|
29
33
|
(_e = (_d = context.options).onDiagnostic) === null || _e === void 0 ? void 0 : _e.call(_d, { code: 'xml-profile-capability-unavailable', level: 'warning', message });
|
|
30
34
|
}
|
|
31
|
-
catch {
|
|
35
|
+
catch { }
|
|
32
36
|
}
|
|
33
37
|
const { default: renderCode } = await import('./code.js');
|
|
34
38
|
return renderCode(buffer, target, type, context);
|