@file-viewer/renderer-text 2.0.11
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/LICENSE +160 -0
- package/README.en.md +40 -0
- package/README.md +40 -0
- package/dist/code.d.ts +11 -0
- package/dist/code.js +243 -0
- package/dist/gitBundle.d.ts +2 -0
- package/dist/gitBundle.js +580 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +25 -0
- package/dist/markdown.d.ts +2 -0
- package/dist/markdown.js +81 -0
- package/dist/patch.d.ts +2 -0
- package/dist/patch.js +123 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work by
|
|
47
|
+
the copyright owner or by an individual or Legal Entity authorized to submit on
|
|
48
|
+
behalf of the copyright owner. For the purposes of this definition, "submitted"
|
|
49
|
+
means any form of electronic, verbal, or written communication sent to the
|
|
50
|
+
Licensor or its representatives, including but not limited to communication on
|
|
51
|
+
electronic mailing lists, source code control systems, and issue tracking systems
|
|
52
|
+
that are managed by, or on behalf of, the Licensor for the purpose of discussing
|
|
53
|
+
and improving the Work, but excluding communication that is conspicuously marked
|
|
54
|
+
or otherwise designated in writing by the copyright owner as "Not a
|
|
55
|
+
Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
66
|
+
Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
71
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
72
|
+
transfer the Work, where such license applies only to those patent claims
|
|
73
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
74
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work to
|
|
75
|
+
which such Contribution(s) was submitted. If You institute patent litigation
|
|
76
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
77
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
78
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
79
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
80
|
+
such litigation is filed.
|
|
81
|
+
|
|
82
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
83
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
84
|
+
Source or Object form, provided that You meet the following conditions:
|
|
85
|
+
|
|
86
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
87
|
+
this License; and
|
|
88
|
+
|
|
89
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
90
|
+
You changed the files; and
|
|
91
|
+
|
|
92
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
93
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
94
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
95
|
+
of the Derivative Works; and
|
|
96
|
+
|
|
97
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
98
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
99
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
100
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
101
|
+
following places: within a NOTICE text file distributed as part of the Derivative
|
|
102
|
+
Works; within the Source form or documentation, if provided along with the
|
|
103
|
+
Derivative Works; or, within a display generated by the Derivative Works, if and
|
|
104
|
+
wherever such third-party notices normally appear. The contents of the NOTICE
|
|
105
|
+
file are for informational purposes only and do not modify the License. You may
|
|
106
|
+
add Your own attribution notices within Derivative Works that You distribute,
|
|
107
|
+
alongside or as an addendum to the NOTICE text from the Work, provided that such
|
|
108
|
+
additional attribution notices cannot be construed as modifying the License.
|
|
109
|
+
|
|
110
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
111
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
112
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
113
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
114
|
+
with the conditions stated in this License.
|
|
115
|
+
|
|
116
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
117
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
118
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
119
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
120
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
121
|
+
executed with Licensor regarding such Contributions.
|
|
122
|
+
|
|
123
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
124
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
125
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
126
|
+
reproducing the content of the NOTICE file.
|
|
127
|
+
|
|
128
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
129
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
130
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
131
|
+
KIND, either express or implied, including, without limitation, any warranties or
|
|
132
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
133
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
134
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
135
|
+
associated with Your exercise of permissions under this License.
|
|
136
|
+
|
|
137
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
138
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
139
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
140
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
141
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
142
|
+
arising as a result of this License or out of the use or inability to use the
|
|
143
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
144
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
145
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
146
|
+
damages.
|
|
147
|
+
|
|
148
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
149
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
150
|
+
acceptance of support, warranty, indemnity, or other liability obligations and/or
|
|
151
|
+
rights consistent with this License. However, in accepting such obligations, You
|
|
152
|
+
may act only on Your own behalf and on Your sole responsibility, not on behalf of
|
|
153
|
+
any other Contributor, and only if You agree to indemnify, defend, and hold each
|
|
154
|
+
Contributor harmless for any liability incurred by, or claims asserted against,
|
|
155
|
+
such Contributor by reason of your accepting any such warranty or additional
|
|
156
|
+
liability.
|
|
157
|
+
|
|
158
|
+
END OF TERMS AND CONDITIONS
|
|
159
|
+
|
|
160
|
+
Copyright 2026 Flyfish Viewer
|
package/README.en.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# @file-viewer/renderer-text
|
|
2
|
+
|
|
3
|
+
Standalone code, text, Markdown, patch, and git bundle renderer package for Flyfish File Viewer. It handles source highlighting, Markdown reading surfaces, side-by-side patch diffs, git bundle inspection, and unified zoom for `.txt`, `.json`, `.ts`, `.vue`, `.log`, `.md`, `.markdown`, `.patch`, `.bundle`, and other code delivery formats.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
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
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
You can also compose it with other renderers:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { textRenderer } from '@file-viewer/renderer-text'
|
|
21
|
+
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
22
|
+
|
|
23
|
+
const options = {
|
|
24
|
+
builtinRenderers: 'none',
|
|
25
|
+
renderers: [pdfRenderer, textRenderer],
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Capabilities
|
|
30
|
+
|
|
31
|
+
- Code and text preview uses `highlight.js` core with per-language dynamic imports instead of registering every language up front.
|
|
32
|
+
- `patch` uses `diff2html` on demand for side-by-side review.
|
|
33
|
+
- `bundle` / `bdl` parses Git bundle headers, refs, commit history, file trees, and readable blobs; regular OFS_DELTA / REF_DELTA objects are resolved in the browser, while very large packs or bundles that depend on external prerequisites surface a clear boundary notice.
|
|
34
|
+
- HTML, XML, Vue, and similar files are escaped and shown as source, never executed.
|
|
35
|
+
- Markdown uses `marked` for a read-only reading surface with dark/light theme support, table scrolling, and a unified zoom provider.
|
|
36
|
+
- Does not depend on any online service or public CDN, making it suitable for intranet logs, configs, snippets, README files, and knowledge-base attachments.
|
|
37
|
+
|
|
38
|
+
## Migration Note
|
|
39
|
+
|
|
40
|
+
`@file-viewer/core` no longer bundles code / markdown renderers and no longer depends directly on `highlight.js`, `marked`, `diff2html`, or git bundle parsing dependencies. Install this package explicitly for code, text, Markdown, patch, or git bundle preview, or use `@file-viewer/preset-all`, which aggregates this renderer automatically.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# @file-viewer/renderer-text
|
|
2
|
+
|
|
3
|
+
Flyfish File Viewer 的独立代码、文本、Markdown、patch 和 git bundle renderer 包。它负责 `.txt`、`.json`、`.ts`、`.vue`、`.log`、`.md`、`.markdown`、`.patch`、`.bundle` 等文本与代码交付文件的源码高亮、Markdown 阅读面、左右比对、bundle 结构查看和统一缩放。
|
|
4
|
+
|
|
5
|
+
## 用法
|
|
6
|
+
|
|
7
|
+
```ts
|
|
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
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
也可以与其他 renderer 组合:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { textRenderer } from '@file-viewer/renderer-text'
|
|
21
|
+
import { pdfRenderer } from '@file-viewer/renderer-pdf'
|
|
22
|
+
|
|
23
|
+
const options = {
|
|
24
|
+
builtinRenderers: 'none',
|
|
25
|
+
renderers: [pdfRenderer, textRenderer],
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 能力边界
|
|
30
|
+
|
|
31
|
+
- 代码和文本使用 `highlight.js` core + 按语言动态加载,避免一次性注册全部语言。
|
|
32
|
+
- `patch` 使用 `diff2html` 按需渲染左右比对视图。
|
|
33
|
+
- `bundle` / `bdl` 会解析 Git bundle header、refs、commit 历史、文件树和可读 blob;常规 OFS_DELTA / REF_DELTA 会在浏览器端还原,超大包或依赖外部 prerequisite 的包会给出清晰边界提示。
|
|
34
|
+
- HTML / XML / Vue 等文件按源码方式转义展示,不执行脚本。
|
|
35
|
+
- Markdown 使用 `marked` 输出只读阅读面,并保留明暗主题、表格滚动和统一缩放 provider。
|
|
36
|
+
- 不绑定任何在线服务或公共 CDN,适合内网日志、配置、代码片段、README 和知识库附件预览。
|
|
37
|
+
|
|
38
|
+
## 迁移说明
|
|
39
|
+
|
|
40
|
+
`@file-viewer/core` 已不再内置 code / markdown renderer,也不再直接依赖 `highlight.js`、`marked`、`diff2html` 或 Git bundle 解析依赖。需要代码、文本、Markdown、patch 或 git bundle 预览时,请显式安装本包,或使用会自动聚合本包的 `@file-viewer/preset-all`。
|
package/dist/code.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type FileViewerRenderedInstance } from '@file-viewer/core';
|
|
2
|
+
/**
|
|
3
|
+
* Framework-neutral text/code renderer.
|
|
4
|
+
*
|
|
5
|
+
* highlight.js core and language definitions are loaded lazily by format. HTML
|
|
6
|
+
* and XML are highlighted as escaped source text, never executed as real DOM.
|
|
7
|
+
* @param buffer 文本二进制内容
|
|
8
|
+
* @param target 目标
|
|
9
|
+
* @param type 文件扩展名,用于选择 highlight.js 语言
|
|
10
|
+
*/
|
|
11
|
+
export default function renderText(buffer: ArrayBuffer, target: HTMLDivElement, type?: string): Promise<FileViewerRenderedInstance>;
|
package/dist/code.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { createFileViewerZoomChangeEmitter as createZoomChangeEmitter, readFileViewerText as readText, registerFileViewerZoomProvider, unregisterFileViewerZoomProvider } from '@file-viewer/core';
|
|
2
|
+
const languageMap = {
|
|
3
|
+
bash: 'bash',
|
|
4
|
+
c: 'cpp',
|
|
5
|
+
cc: 'cpp',
|
|
6
|
+
cjs: 'javascript',
|
|
7
|
+
cpp: 'cpp',
|
|
8
|
+
cs: 'csharp',
|
|
9
|
+
css: 'css',
|
|
10
|
+
diff: 'diff',
|
|
11
|
+
patch: 'diff',
|
|
12
|
+
bundle: 'plaintext',
|
|
13
|
+
bdl: 'plaintext',
|
|
14
|
+
gv: 'plaintext',
|
|
15
|
+
go: 'go',
|
|
16
|
+
h: 'cpp',
|
|
17
|
+
hcl: 'plaintext',
|
|
18
|
+
hpp: 'cpp',
|
|
19
|
+
html: 'xml',
|
|
20
|
+
htm: 'xml',
|
|
21
|
+
http: 'http',
|
|
22
|
+
ini: 'ini',
|
|
23
|
+
ipynb: 'json',
|
|
24
|
+
java: 'java',
|
|
25
|
+
js: 'javascript',
|
|
26
|
+
json: 'json',
|
|
27
|
+
json5: 'json',
|
|
28
|
+
jsonc: 'json',
|
|
29
|
+
jsx: 'javascript',
|
|
30
|
+
kt: 'kotlin',
|
|
31
|
+
log: 'plaintext',
|
|
32
|
+
md: 'markdown',
|
|
33
|
+
markdown: 'markdown',
|
|
34
|
+
mjs: 'javascript',
|
|
35
|
+
php: 'php',
|
|
36
|
+
proto: 'protobuf',
|
|
37
|
+
py: 'python',
|
|
38
|
+
rb: 'ruby',
|
|
39
|
+
react: 'javascript',
|
|
40
|
+
rs: 'rust',
|
|
41
|
+
sh: 'bash',
|
|
42
|
+
sql: 'sql',
|
|
43
|
+
swift: 'swift',
|
|
44
|
+
tex: 'latex',
|
|
45
|
+
toml: 'ini',
|
|
46
|
+
ts: 'typescript',
|
|
47
|
+
tsx: 'typescript',
|
|
48
|
+
txt: 'plaintext',
|
|
49
|
+
vue: 'xml',
|
|
50
|
+
xml: 'xml',
|
|
51
|
+
yaml: 'yaml',
|
|
52
|
+
yml: 'yaml'
|
|
53
|
+
};
|
|
54
|
+
const languageLoaders = {
|
|
55
|
+
bash: () => import('highlight.js/lib/languages/bash'),
|
|
56
|
+
cpp: () => import('highlight.js/lib/languages/cpp'),
|
|
57
|
+
csharp: () => import('highlight.js/lib/languages/csharp'),
|
|
58
|
+
css: () => import('highlight.js/lib/languages/css'),
|
|
59
|
+
diff: () => import('highlight.js/lib/languages/diff'),
|
|
60
|
+
go: () => import('highlight.js/lib/languages/go'),
|
|
61
|
+
http: () => import('highlight.js/lib/languages/http'),
|
|
62
|
+
ini: () => import('highlight.js/lib/languages/ini'),
|
|
63
|
+
java: () => import('highlight.js/lib/languages/java'),
|
|
64
|
+
javascript: () => import('highlight.js/lib/languages/javascript'),
|
|
65
|
+
json: () => import('highlight.js/lib/languages/json'),
|
|
66
|
+
kotlin: () => import('highlight.js/lib/languages/kotlin'),
|
|
67
|
+
latex: () => import('highlight.js/lib/languages/latex'),
|
|
68
|
+
markdown: () => import('highlight.js/lib/languages/markdown'),
|
|
69
|
+
php: () => import('highlight.js/lib/languages/php'),
|
|
70
|
+
protobuf: () => import('highlight.js/lib/languages/protobuf'),
|
|
71
|
+
python: () => import('highlight.js/lib/languages/python'),
|
|
72
|
+
ruby: () => import('highlight.js/lib/languages/ruby'),
|
|
73
|
+
rust: () => import('highlight.js/lib/languages/rust'),
|
|
74
|
+
sql: () => import('highlight.js/lib/languages/sql'),
|
|
75
|
+
swift: () => import('highlight.js/lib/languages/swift'),
|
|
76
|
+
typescript: () => import('highlight.js/lib/languages/typescript'),
|
|
77
|
+
xml: () => import('highlight.js/lib/languages/xml'),
|
|
78
|
+
yaml: () => import('highlight.js/lib/languages/yaml')
|
|
79
|
+
};
|
|
80
|
+
const codeStyle = `
|
|
81
|
+
.code-viewer{min-height:100%;--code-bg:#f6f8fa;--code-toolbar-bg:rgba(255,255,255,.92);--code-border:rgba(31,35,40,.12);--code-text:#24292f;--code-muted:#57606a;--code-keyword:#cf222e;--code-title:#8250df;--code-string:#0a3069;--code-number:#0550ae;--code-comment:#6e7781;--code-attr:#953800;--code-built-in:#116329;background:var(--code-bg);color:var(--code-text);box-sizing:border-box}
|
|
82
|
+
.code-toolbar{position:sticky;top:0;z-index:1;display:flex;height:42px;align-items:center;justify-content:space-between;gap:16px;padding:0 16px;border-bottom:1px solid var(--code-border);background:var(--code-toolbar-bg);backdrop-filter:blur(12px);box-sizing:border-box}
|
|
83
|
+
.code-toolbar span,.code-toolbar strong{color:var(--code-muted);font-size:12px;font-weight:700;letter-spacing:0}
|
|
84
|
+
.code-area{display:block;min-width:min-content;margin:0;padding:18px 20px 28px;overflow:auto;background:transparent;box-sizing:border-box}
|
|
85
|
+
.code-area code{display:block;padding:0;overflow:visible;background:transparent;color:inherit;font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,'Liberation Mono',monospace;font-size:var(--code-font-size,13px);line-height:1.7;tab-size:2;white-space:pre}
|
|
86
|
+
.code-area .hljs-comment,.code-area .hljs-quote{color:var(--code-comment)}
|
|
87
|
+
.code-area .hljs-keyword,.code-area .hljs-selector-tag,.code-area .hljs-subst{color:var(--code-keyword)}
|
|
88
|
+
.code-area .hljs-string,.code-area .hljs-doctag,.code-area .hljs-regexp{color:var(--code-string)}
|
|
89
|
+
.code-area .hljs-title,.code-area .hljs-section,.code-area .hljs-selector-id{color:var(--code-title);font-weight:700}
|
|
90
|
+
.code-area .hljs-number,.code-area .hljs-literal,.code-area .hljs-variable,.code-area .hljs-template-variable{color:var(--code-number)}
|
|
91
|
+
.code-area .hljs-attr,.code-area .hljs-attribute,.code-area .hljs-name,.code-area .hljs-selector-class{color:var(--code-attr)}
|
|
92
|
+
.code-area .hljs-built_in,.code-area .hljs-type,.code-area .hljs-class .hljs-title{color:var(--code-built-in)}
|
|
93
|
+
.file-viewer[data-viewer-theme='dark'] .code-viewer{--code-bg:#0d1117;--code-toolbar-bg:rgba(13,17,23,.92);--code-border:rgba(139,148,158,.24);--code-text:#e6edf3;--code-muted:#8b949e;--code-keyword:#ff7b72;--code-title:#d2a8ff;--code-string:#a5d6ff;--code-number:#79c0ff;--code-comment:#8b949e;--code-attr:#ffa657;--code-built-in:#7ee787}
|
|
94
|
+
@media (prefers-color-scheme:dark){.file-viewer[data-viewer-theme='system'] .code-viewer{--code-bg:#0d1117;--code-toolbar-bg:rgba(13,17,23,.92);--code-border:rgba(139,148,158,.24);--code-text:#e6edf3;--code-muted:#8b949e;--code-keyword:#ff7b72;--code-title:#d2a8ff;--code-string:#a5d6ff;--code-number:#79c0ff;--code-comment:#8b949e;--code-attr:#ffa657;--code-built-in:#7ee787}}
|
|
95
|
+
`;
|
|
96
|
+
let highlighterPromise = null;
|
|
97
|
+
const registeredLanguages = new Set();
|
|
98
|
+
const createElement = (tagName, className, text) => {
|
|
99
|
+
const element = document.createElement(tagName);
|
|
100
|
+
if (className) {
|
|
101
|
+
element.className = className;
|
|
102
|
+
}
|
|
103
|
+
if (typeof text === 'string') {
|
|
104
|
+
element.textContent = text;
|
|
105
|
+
}
|
|
106
|
+
return element;
|
|
107
|
+
};
|
|
108
|
+
const createStyle = () => {
|
|
109
|
+
const style = document.createElement('style');
|
|
110
|
+
style.textContent = codeStyle;
|
|
111
|
+
return style;
|
|
112
|
+
};
|
|
113
|
+
const resolveLanguage = (type) => {
|
|
114
|
+
return languageMap[type.trim().toLowerCase()] || 'plaintext';
|
|
115
|
+
};
|
|
116
|
+
const escapeHtml = (value) => {
|
|
117
|
+
return value.replace(/[&<>"']/g, char => {
|
|
118
|
+
const entities = {
|
|
119
|
+
'&': '&',
|
|
120
|
+
'<': '<',
|
|
121
|
+
'>': '>',
|
|
122
|
+
'"': '"',
|
|
123
|
+
"'": '''
|
|
124
|
+
};
|
|
125
|
+
return entities[char];
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
const loadHighlighter = async () => {
|
|
129
|
+
if (!highlighterPromise) {
|
|
130
|
+
highlighterPromise = import('highlight.js/lib/core').then(module => module.default);
|
|
131
|
+
}
|
|
132
|
+
return highlighterPromise;
|
|
133
|
+
};
|
|
134
|
+
const registerLanguageOnce = async (hljs, name) => {
|
|
135
|
+
if (registeredLanguages.has(name)) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
const loader = languageLoaders[name];
|
|
139
|
+
if (!loader) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
const { default: language } = await loader();
|
|
143
|
+
hljs.registerLanguage(name, language);
|
|
144
|
+
registeredLanguages.add(name);
|
|
145
|
+
return true;
|
|
146
|
+
};
|
|
147
|
+
const clampZoom = (value) => {
|
|
148
|
+
return Math.min(2.6, Math.max(0.6, Number(value.toFixed(2))));
|
|
149
|
+
};
|
|
150
|
+
const lineCountOf = (value) => {
|
|
151
|
+
return value.split(/\r\n|\r|\n/).length;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Framework-neutral text/code renderer.
|
|
155
|
+
*
|
|
156
|
+
* highlight.js core and language definitions are loaded lazily by format. HTML
|
|
157
|
+
* and XML are highlighted as escaped source text, never executed as real DOM.
|
|
158
|
+
* @param buffer 文本二进制内容
|
|
159
|
+
* @param target 目标
|
|
160
|
+
* @param type 文件扩展名,用于选择 highlight.js 语言
|
|
161
|
+
*/
|
|
162
|
+
export default async function renderText(buffer, target, type) {
|
|
163
|
+
const extension = type || 'txt';
|
|
164
|
+
const normalizedExtension = extension.trim().toLowerCase();
|
|
165
|
+
if (normalizedExtension === 'patch') {
|
|
166
|
+
const { default: renderPatch } = await import('./patch.js');
|
|
167
|
+
return renderPatch(buffer, target, extension);
|
|
168
|
+
}
|
|
169
|
+
if (normalizedExtension === 'bundle' || normalizedExtension === 'bdl') {
|
|
170
|
+
const { default: renderGitBundle } = await import('./gitBundle.js');
|
|
171
|
+
return renderGitBundle(buffer, target, extension);
|
|
172
|
+
}
|
|
173
|
+
const text = await readText(buffer);
|
|
174
|
+
const language = resolveLanguage(extension);
|
|
175
|
+
let disposed = false;
|
|
176
|
+
let zoom = 1;
|
|
177
|
+
const zoomEmitter = createZoomChangeEmitter();
|
|
178
|
+
const root = createElement('div', 'code-viewer');
|
|
179
|
+
root.dataset.viewerZoomProvider = 'code';
|
|
180
|
+
const toolbar = createElement('div', 'code-toolbar');
|
|
181
|
+
toolbar.append(createElement('span', '', extension.toUpperCase()), createElement('strong', '', `${lineCountOf(text)} lines`));
|
|
182
|
+
const pre = createElement('pre', 'code-area');
|
|
183
|
+
const code = createElement('code', `hljs language-${language}`);
|
|
184
|
+
code.innerHTML = language === 'plaintext'
|
|
185
|
+
? escapeHtml(text)
|
|
186
|
+
: '正在加载高亮...';
|
|
187
|
+
pre.append(code);
|
|
188
|
+
root.append(toolbar, pre);
|
|
189
|
+
root.style.setProperty('--code-font-size', `${13 * zoom}px`);
|
|
190
|
+
target.replaceChildren(createStyle(), root);
|
|
191
|
+
const updateHighlighted = async () => {
|
|
192
|
+
if (language === 'plaintext') {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
const hljs = await loadHighlighter();
|
|
197
|
+
const hasLanguage = await registerLanguageOnce(hljs, language);
|
|
198
|
+
if (disposed) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
code.innerHTML = hasLanguage
|
|
202
|
+
? hljs.highlight(text, { language, ignoreIllegals: true }).value
|
|
203
|
+
: escapeHtml(text);
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
if (!disposed) {
|
|
207
|
+
code.innerHTML = escapeHtml(text);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
void updateHighlighted();
|
|
212
|
+
const getZoomState = () => ({
|
|
213
|
+
scale: zoom,
|
|
214
|
+
label: `${Math.round(zoom * 100)}%`,
|
|
215
|
+
canZoomIn: zoom < 2.6,
|
|
216
|
+
canZoomOut: zoom > 0.6,
|
|
217
|
+
canReset: zoom !== 1,
|
|
218
|
+
minScale: 0.6,
|
|
219
|
+
maxScale: 2.6
|
|
220
|
+
});
|
|
221
|
+
const setZoom = (scale) => {
|
|
222
|
+
zoom = clampZoom(scale);
|
|
223
|
+
root.style.setProperty('--code-font-size', `${13 * zoom}px`);
|
|
224
|
+
zoomEmitter.emit();
|
|
225
|
+
return getZoomState();
|
|
226
|
+
};
|
|
227
|
+
registerFileViewerZoomProvider(root, {
|
|
228
|
+
zoomIn: () => setZoom(zoom + 0.1),
|
|
229
|
+
zoomOut: () => setZoom(zoom - 0.1),
|
|
230
|
+
resetZoom: () => setZoom(1),
|
|
231
|
+
setZoom,
|
|
232
|
+
getState: getZoomState,
|
|
233
|
+
subscribe: zoomEmitter.subscribe
|
|
234
|
+
});
|
|
235
|
+
return {
|
|
236
|
+
$el: target,
|
|
237
|
+
unmount() {
|
|
238
|
+
disposed = true;
|
|
239
|
+
unregisterFileViewerZoomProvider(root);
|
|
240
|
+
target.replaceChildren();
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|