@file-viewer/renderer-text 3.1.1 → 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 -50
- package/README.md +3 -50
- package/bin/copy-xml-profile-assets.mjs +59 -0
- package/dist/code.d.ts +0 -7
- package/dist/code.js +6 -138
- package/dist/html.js +2 -4
- package/dist/index.js +30 -3
- 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 +11 -4
- 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 +3 -0
- package/dist/xml-profiles.js +4 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +169 -0
- package/dist/xmlEngines.d.ts +9 -0
- package/dist/xmlEngines.js +157 -0
- package/dist/xmlProfiles.d.ts +7 -0
- package/dist/xmlProfiles.js +145 -0
- package/dist/xmlRegistration.d.ts +8 -0
- package/dist/xmlRegistration.js +13 -0
- package/dist/xmlSafety.d.ts +18 -0
- package/dist/xmlSafety.js +143 -0
- package/dist/xmlWasmMemory.d.ts +1 -0
- package/dist/xmlWasmMemory.js +71 -0
- package/file-viewer.capability.json +1 -1
- package/licenses/xml-profiles/NOTICE.md +31 -0
- package/licenses/xml-profiles/libxml2-xsd-Copyright.txt +23 -0
- package/licenses/xml-profiles/libxml2-xslt-Copyright.txt +24 -0
- package/licenses/xml-profiles/libxslt-Copyright.txt +53 -0
- package/licenses/xml-profiles/xmllint-wasm-COPYING.txt +21 -0
- package/licenses/xml-profiles/xslt-polyfill-LICENSE.txt +28 -0
- package/licenses/xml-profiles/zlib-LICENSE.txt +22 -0
- package/package.json +104 -23
package/dist/lrc.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { createFileViewerZoomChangeEmitter, decodeFileViewerTextBuffer, registerFileViewerZoomProvider, resolveFileViewerLocale, unregisterFileViewerZoomProvider } from '@file-viewer/core';
|
|
2
|
+
import { formatLrcTimestamp, parseLrc } from './lrcParser.js';
|
|
3
|
+
import renderCode from './code.js';
|
|
4
|
+
const labels = {
|
|
5
|
+
'en-US': ['Annotated', 'Lyrics', 'Source', 'Wrap lines', 'Static lyrics preview; no audio playback.'],
|
|
6
|
+
'zh-CN': ['时间标注', '歌词', '原文', '自动换行', '静态歌词预览,不播放音频。'],
|
|
7
|
+
'ja-JP': ['時間付き', '歌詞', '原文', '折り返し', '歌詞の静的プレビューです。音声は再生しません。'],
|
|
8
|
+
'de-DE': ['Mit Zeitangaben', 'Liedtext', 'Quelltext', 'Zeilenumbruch', 'Statische Liedtextvorschau ohne Audiowiedergabe.']
|
|
9
|
+
};
|
|
10
|
+
const css = `
|
|
11
|
+
.fv-lrc{--lrc-bg:#fff;--lrc-fg:#1f2937;height:100%;min-height:0;display:flex;flex-direction:column;color:var(--lrc-fg);background:var(--lrc-bg);font:14px/1.65 system-ui,sans-serif}
|
|
12
|
+
.fv-lrc .lrc-toolbar{display:flex;flex-wrap:wrap;gap:6px;padding:10px;border-bottom:1px solid #8885;align-items:center}
|
|
13
|
+
.fv-lrc .lrc-toolbar[hidden]{display:none}
|
|
14
|
+
.fv-lrc button{font:inherit;color:inherit;background:transparent;border:1px solid #8887;border-radius:5px;padding:4px 10px;cursor:pointer}
|
|
15
|
+
.fv-lrc button[aria-pressed=true]{background:#8883;font-weight:600}
|
|
16
|
+
.fv-lrc .lrc-scroll{flex:1;min-height:0;overflow:auto;padding:14px;scrollbar-gutter:stable}
|
|
17
|
+
.fv-lrc .lrc-content{font-size:var(--lrc-font-size,16px);min-width:0}
|
|
18
|
+
.fv-lrc .lrc-cue{display:flex;gap:1em;min-height:1.8em;align-items:baseline}
|
|
19
|
+
.fv-lrc .lrc-time{font:0.8em ui-monospace,monospace;white-space:nowrap;flex:0 0 8.5em;position:sticky;left:0;background:var(--lrc-bg);z-index:1}
|
|
20
|
+
.fv-lrc .lrc-phrase{white-space:pre-wrap;overflow-wrap:anywhere;min-width:0;flex:1}
|
|
21
|
+
.fv-lrc .lrc-role{font-size:.65em;margin-right:.6em}
|
|
22
|
+
.fv-lrc ruby{display:inline-flex;flex-direction:column-reverse;vertical-align:bottom;white-space:pre}.fv-lrc rt{display:block;font:0.55em ui-monospace,monospace;opacity:.72}
|
|
23
|
+
.fv-lrc .lrc-source{font:inherit;white-space:pre-wrap;overflow-wrap:anywhere;margin:0}
|
|
24
|
+
.fv-lrc[data-wrap=false] .lrc-content{width:max-content;min-width:100%}
|
|
25
|
+
.fv-lrc[data-wrap=false] .lrc-source,.fv-lrc[data-wrap=false] .lrc-phrase{white-space:pre;overflow-wrap:normal}
|
|
26
|
+
.fv-lrc .lrc-metadata{white-space:pre-wrap;overflow-wrap:anywhere;font-size:.85em;opacity:.8;margin-bottom:1em}
|
|
27
|
+
.fv-lrc .lrc-note{font-size:.85em;opacity:.75}
|
|
28
|
+
[data-viewer-theme=dark] .fv-lrc,.fv-lrc[data-viewer-theme=dark]{--lrc-bg:#101820;--lrc-fg:#e5e7eb;color-scheme:dark}
|
|
29
|
+
@media (prefers-color-scheme:dark){[data-viewer-theme=system] .fv-lrc,.fv-lrc[data-viewer-theme=system]{--lrc-bg:#101820;--lrc-fg:#e5e7eb;color-scheme:dark}}
|
|
30
|
+
@media print{.fv-lrc{--lrc-bg:#fff;--lrc-fg:#1f2937;height:auto}.fv-lrc .lrc-toolbar{display:none}.fv-lrc .lrc-scroll{overflow:visible}.fv-lrc .lrc-time{position:static}}
|
|
31
|
+
`;
|
|
32
|
+
export default async function renderLrc(buffer, target, type, context) {
|
|
33
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
34
|
+
if ((_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.aborted)
|
|
35
|
+
throw new DOMException('Aborted', 'AbortError');
|
|
36
|
+
if (buffer.byteLength > 4000000)
|
|
37
|
+
return renderCode(buffer, target, type, context);
|
|
38
|
+
const source = decodeFileViewerTextBuffer(buffer, (_c = (_b = context === null || context === void 0 ? void 0 : context.options) === null || _b === void 0 ? void 0 : _b.text) === null || _c === void 0 ? void 0 : _c.encoding, (_e = (_d = context === null || context === void 0 ? void 0 : context.options) === null || _d === void 0 ? void 0 : _d.text) === null || _e === void 0 ? void 0 : _e.fallbackEncoding).text;
|
|
39
|
+
let model;
|
|
40
|
+
try {
|
|
41
|
+
model = parseLrc(source);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (!(error instanceof RangeError))
|
|
45
|
+
throw error;
|
|
46
|
+
return renderCode(buffer, target, type, context);
|
|
47
|
+
}
|
|
48
|
+
const doc = target.ownerDocument;
|
|
49
|
+
const words = labels[resolveFileViewerLocale(context === null || context === void 0 ? void 0 : context.options)];
|
|
50
|
+
const root = doc.createElement('div');
|
|
51
|
+
root.className = 'fv-lrc';
|
|
52
|
+
root.dataset.wrap = String(((_g = (_f = context === null || context === void 0 ? void 0 : context.options) === null || _f === void 0 ? void 0 : _f.text) === null || _g === void 0 ? void 0 : _g.wrapLongLines) !== false);
|
|
53
|
+
root.dataset.viewerTheme = ((_h = context === null || context === void 0 ? void 0 : context.options) === null || _h === void 0 ? void 0 : _h.theme) || 'system';
|
|
54
|
+
const style = doc.createElement('style');
|
|
55
|
+
style.textContent = css;
|
|
56
|
+
const toolbar = doc.createElement('div');
|
|
57
|
+
toolbar.className = 'lrc-toolbar';
|
|
58
|
+
toolbar.hidden = ((_k = (_j = context === null || context === void 0 ? void 0 : context.options) === null || _j === void 0 ? void 0 : _j.text) === null || _k === void 0 ? void 0 : _k.toolbar) === false;
|
|
59
|
+
const scroller = doc.createElement('div');
|
|
60
|
+
scroller.className = 'lrc-scroll';
|
|
61
|
+
const content = doc.createElement('div');
|
|
62
|
+
content.className = 'lrc-content';
|
|
63
|
+
scroller.append(content);
|
|
64
|
+
const buttons = words.slice(0, 3).map((text, index) => {
|
|
65
|
+
const button = doc.createElement('button');
|
|
66
|
+
button.type = 'button';
|
|
67
|
+
button.textContent = text;
|
|
68
|
+
button.dataset.lrcMode = String(index);
|
|
69
|
+
toolbar.append(button);
|
|
70
|
+
return button;
|
|
71
|
+
});
|
|
72
|
+
const wrap = doc.createElement('button');
|
|
73
|
+
wrap.type = 'button';
|
|
74
|
+
wrap.textContent = words[3];
|
|
75
|
+
wrap.dataset.lrcWrap = '';
|
|
76
|
+
wrap.setAttribute('aria-pressed', root.dataset.wrap);
|
|
77
|
+
toolbar.append(wrap);
|
|
78
|
+
const note = doc.createElement('span');
|
|
79
|
+
note.className = 'lrc-note';
|
|
80
|
+
note.textContent = words[4];
|
|
81
|
+
toolbar.append(note);
|
|
82
|
+
root.append(style, toolbar, scroller);
|
|
83
|
+
target.replaceChildren(root);
|
|
84
|
+
let disposed = false;
|
|
85
|
+
let mode = 0;
|
|
86
|
+
let zoom = 1;
|
|
87
|
+
const emitter = createFileViewerZoomChangeEmitter();
|
|
88
|
+
function getState() {
|
|
89
|
+
return { scale: zoom, label: `${Math.round(zoom * 100)}%`, canZoomIn: zoom < 2.6, canZoomOut: zoom > 0.6, canReset: zoom !== 1, minScale: 0.6, maxScale: 2.6 };
|
|
90
|
+
}
|
|
91
|
+
function setZoom(scale) {
|
|
92
|
+
if (!disposed && Number.isFinite(scale)) {
|
|
93
|
+
zoom = Math.max(0.6, Math.min(2.6, Math.round(scale * 100) / 100));
|
|
94
|
+
root.style.setProperty('--lrc-font-size', `${16 * zoom}px`);
|
|
95
|
+
emitter.emit();
|
|
96
|
+
}
|
|
97
|
+
return getState();
|
|
98
|
+
}
|
|
99
|
+
function show() {
|
|
100
|
+
if (disposed)
|
|
101
|
+
return;
|
|
102
|
+
buttons.forEach((button, index) => button.setAttribute('aria-pressed', String(mode === index)));
|
|
103
|
+
const fragment = doc.createDocumentFragment();
|
|
104
|
+
if (mode === 2) {
|
|
105
|
+
const pre = doc.createElement('pre');
|
|
106
|
+
pre.className = 'lrc-source';
|
|
107
|
+
pre.textContent = source;
|
|
108
|
+
fragment.append(pre);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const metadata = doc.createElement('div');
|
|
112
|
+
metadata.className = 'lrc-metadata';
|
|
113
|
+
metadata.textContent = model.metadata.map(entry => `${entry.key}: ${entry.value}`).join('\n');
|
|
114
|
+
fragment.append(metadata);
|
|
115
|
+
for (const cue of model.cues) {
|
|
116
|
+
const row = doc.createElement('div');
|
|
117
|
+
row.className = 'lrc-cue';
|
|
118
|
+
row.dataset.sourceLine = String(cue.line);
|
|
119
|
+
if (cue.timeMs !== null)
|
|
120
|
+
row.dataset.timeMs = String(cue.timeMs);
|
|
121
|
+
if (mode === 0) {
|
|
122
|
+
const time = doc.createElement('span');
|
|
123
|
+
time.className = 'lrc-time';
|
|
124
|
+
time.textContent = cue.timeMs === null ? '·' : formatLrcTimestamp(cue.timeMs);
|
|
125
|
+
row.append(time);
|
|
126
|
+
}
|
|
127
|
+
const phrase = doc.createElement('span');
|
|
128
|
+
phrase.className = 'lrc-phrase';
|
|
129
|
+
phrase.dir = 'auto';
|
|
130
|
+
if (cue.role) {
|
|
131
|
+
const role = doc.createElement('sup');
|
|
132
|
+
role.className = 'lrc-role';
|
|
133
|
+
role.textContent = cue.role;
|
|
134
|
+
phrase.append(role);
|
|
135
|
+
}
|
|
136
|
+
for (const word of cue.words) {
|
|
137
|
+
if (mode === 0 && word.timeMs !== null) {
|
|
138
|
+
const ruby = doc.createElement('ruby');
|
|
139
|
+
ruby.dataset.wordTimeMs = String(word.timeMs);
|
|
140
|
+
ruby.append(doc.createTextNode(word.text));
|
|
141
|
+
const rt = doc.createElement('rt');
|
|
142
|
+
rt.textContent = formatLrcTimestamp(word.timeMs);
|
|
143
|
+
ruby.append(rt);
|
|
144
|
+
phrase.append(ruby);
|
|
145
|
+
}
|
|
146
|
+
else
|
|
147
|
+
phrase.append(doc.createTextNode(word.text));
|
|
148
|
+
}
|
|
149
|
+
row.append(phrase);
|
|
150
|
+
fragment.append(row);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
content.replaceChildren(fragment);
|
|
154
|
+
}
|
|
155
|
+
function click(event) {
|
|
156
|
+
const element = event.target;
|
|
157
|
+
const button = element === null || element === void 0 ? void 0 : element.closest('button');
|
|
158
|
+
if (!button || !toolbar.contains(button))
|
|
159
|
+
return;
|
|
160
|
+
if (button === wrap) {
|
|
161
|
+
root.dataset.wrap = String(root.dataset.wrap !== 'true');
|
|
162
|
+
wrap.setAttribute('aria-pressed', root.dataset.wrap);
|
|
163
|
+
}
|
|
164
|
+
else if (button.dataset.lrcMode !== undefined) {
|
|
165
|
+
mode = Number(button.dataset.lrcMode);
|
|
166
|
+
show();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function unmount() {
|
|
170
|
+
var _a;
|
|
171
|
+
if (disposed)
|
|
172
|
+
return;
|
|
173
|
+
disposed = true;
|
|
174
|
+
toolbar.removeEventListener('click', click);
|
|
175
|
+
(_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener('abort', unmount);
|
|
176
|
+
unregisterFileViewerZoomProvider(root);
|
|
177
|
+
emitter.clear();
|
|
178
|
+
root.remove();
|
|
179
|
+
}
|
|
180
|
+
toolbar.addEventListener('click', click);
|
|
181
|
+
(_l = context === null || context === void 0 ? void 0 : context.signal) === null || _l === void 0 ? void 0 : _l.addEventListener('abort', unmount, { once: true });
|
|
182
|
+
registerFileViewerZoomProvider(root, { getState, setZoom, zoomIn: () => setZoom(zoom + .1), zoomOut: () => setZoom(zoom - .1), resetZoom: () => setZoom(1), subscribe: emitter.subscribe });
|
|
183
|
+
show();
|
|
184
|
+
return { $el: root, unmount };
|
|
185
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface LrcWord {
|
|
2
|
+
text: string;
|
|
3
|
+
timeMs: number | null;
|
|
4
|
+
}
|
|
5
|
+
export interface LrcCue {
|
|
6
|
+
line: number;
|
|
7
|
+
timeMs: number | null;
|
|
8
|
+
text: string;
|
|
9
|
+
role: 'M' | 'F' | 'D' | null;
|
|
10
|
+
words: LrcWord[];
|
|
11
|
+
}
|
|
12
|
+
export interface LrcDocument {
|
|
13
|
+
source: string;
|
|
14
|
+
offsetMs: number;
|
|
15
|
+
metadata: {
|
|
16
|
+
key: string;
|
|
17
|
+
value: string;
|
|
18
|
+
}[];
|
|
19
|
+
cues: LrcCue[];
|
|
20
|
+
}
|
|
21
|
+
export declare function parseLrcTimestamp(value: string): number | null;
|
|
22
|
+
export declare function formatLrcTimestamp(ms: number): string;
|
|
23
|
+
export declare function parseLrc(source: string): LrcDocument;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const STAMP = /^(-?)(\d{1,8}):([0-5]\d)(?:[.:](\d{1,3}))?$/;
|
|
2
|
+
export function parseLrcTimestamp(value) {
|
|
3
|
+
const match = STAMP.exec(value);
|
|
4
|
+
if (!match)
|
|
5
|
+
return null;
|
|
6
|
+
const ms = Number(match[2]) * 60000 + Number(match[3]) * 1000 + Number((match[4] || '').padEnd(3, '0'));
|
|
7
|
+
return (match[1] ? -1 : 1) * ms;
|
|
8
|
+
}
|
|
9
|
+
export function formatLrcTimestamp(ms) {
|
|
10
|
+
const absolute = Math.abs(ms);
|
|
11
|
+
return `${ms < 0 ? '-' : ''}${String(Math.floor(absolute / 60000)).padStart(2, '0')}:${String(Math.floor(absolute / 1000) % 60).padStart(2, '0')}.${String(absolute % 1000).padStart(3, '0')}`;
|
|
12
|
+
}
|
|
13
|
+
export function parseLrc(source) {
|
|
14
|
+
if (source.length > 2000000)
|
|
15
|
+
throw new RangeError('LRC source preview required');
|
|
16
|
+
const document = { source, offsetMs: 0, metadata: [], cues: [] };
|
|
17
|
+
let role = null;
|
|
18
|
+
let wordCount = 0;
|
|
19
|
+
const lines = source.replace(/^\uFEFF/, '').split(/\r\n|\r|\n/);
|
|
20
|
+
for (let index = 0; index < lines.length; index++) {
|
|
21
|
+
const line = lines[index];
|
|
22
|
+
const metadata = /^\[([a-z][a-z0-9_-]*):(.*)\]\s*$/i.exec(line);
|
|
23
|
+
if (metadata) {
|
|
24
|
+
document.metadata.push({ key: metadata[1], value: metadata[2] });
|
|
25
|
+
if (metadata[1].toLowerCase() === 'offset' && /^[+-]?\d+$/.test(metadata[2].trim())) {
|
|
26
|
+
const offset = Number(metadata[2]);
|
|
27
|
+
if (Number.isSafeInteger(offset) && Math.abs(offset) <= 1e12)
|
|
28
|
+
document.offsetMs = offset;
|
|
29
|
+
}
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
let cursor = 0;
|
|
33
|
+
const timestamps = [];
|
|
34
|
+
while (line[cursor] === '[') {
|
|
35
|
+
const end = line.indexOf(']', cursor + 1);
|
|
36
|
+
if (end < 0 || end - cursor > 24)
|
|
37
|
+
break;
|
|
38
|
+
const timestamp = parseLrcTimestamp(line.slice(cursor + 1, end));
|
|
39
|
+
if (timestamp === null)
|
|
40
|
+
break;
|
|
41
|
+
timestamps.push(timestamp);
|
|
42
|
+
cursor = end + 1;
|
|
43
|
+
if (timestamps.length + document.cues.length > 10000)
|
|
44
|
+
throw new RangeError('Too many LRC cues');
|
|
45
|
+
}
|
|
46
|
+
let body = line.slice(cursor);
|
|
47
|
+
const speaker = timestamps.length ? /^([MFD]):/.exec(body) : null;
|
|
48
|
+
if (speaker) {
|
|
49
|
+
role = speaker[1];
|
|
50
|
+
body = body.slice(2);
|
|
51
|
+
}
|
|
52
|
+
const words = [];
|
|
53
|
+
const marker = /<(-?\d{1,8}:[0-5]\d(?:[.:]\d{1,3})?)>/g;
|
|
54
|
+
let wordStart = 0;
|
|
55
|
+
let timeMs = null;
|
|
56
|
+
for (let match = marker.exec(body); match; match = marker.exec(body)) {
|
|
57
|
+
if (match.index > wordStart)
|
|
58
|
+
words.push({ text: body.slice(wordStart, match.index), timeMs });
|
|
59
|
+
timeMs = parseLrcTimestamp(match[1]);
|
|
60
|
+
wordStart = match.index + match[0].length;
|
|
61
|
+
}
|
|
62
|
+
if (wordStart < body.length || !words.length)
|
|
63
|
+
words.push({ text: body.slice(wordStart), timeMs });
|
|
64
|
+
wordCount += words.length * Math.max(1, timestamps.length);
|
|
65
|
+
if (wordCount > 50000 || document.cues.length >= 10000)
|
|
66
|
+
throw new RangeError('LRC source preview required');
|
|
67
|
+
const text = words.map(word => word.text).join('');
|
|
68
|
+
const times = timestamps.length ? timestamps : [null];
|
|
69
|
+
for (const timestamp of times) {
|
|
70
|
+
const shift = timestamp === null ? 0 : timestamp - timestamps[0];
|
|
71
|
+
document.cues.push({ line: index + 1, timeMs: timestamp, text, role: timestamps.length ? role : null,
|
|
72
|
+
words: words.map(word => ({ text: word.text, timeMs: word.timeMs === null ? null : word.timeMs + shift })) });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const cue of document.cues) {
|
|
76
|
+
if (cue.timeMs !== null)
|
|
77
|
+
cue.timeMs -= document.offsetMs;
|
|
78
|
+
for (const word of cue.words)
|
|
79
|
+
if (word.timeMs !== null)
|
|
80
|
+
word.timeMs -= document.offsetMs;
|
|
81
|
+
}
|
|
82
|
+
return document;
|
|
83
|
+
}
|
package/dist/markdown.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { type FileRenderContext, type FileViewerRenderedInstance } from '@file-viewer/core';
|
|
2
|
-
export declare const sanitizeMermaidSvg: (documentRef: Document, svg: string) => SVGSVGElement;
|
|
2
|
+
export declare const sanitizeMermaidSvg: (documentRef: Document, svg: string, mathLabels?: boolean) => SVGSVGElement;
|
|
3
3
|
export declare const stripMarkdownFrontmatter: (text: string) => string;
|
|
4
4
|
export default function renderMarkdown(buffer: ArrayBuffer, target: HTMLDivElement, context?: FileRenderContext): Promise<FileViewerRenderedInstance>;
|
package/dist/markdown.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { installMarkdownAnchors } from './markdownAnchors.js';
|
|
1
2
|
import { marked } from 'marked';
|
|
2
3
|
import createDOMPurify from 'dompurify';
|
|
3
|
-
import { createFileViewerZoomChangeEmitter as createZoomChangeEmitter, assertFileViewerMermaidSourceHasNoExternalResources, decodeFileViewerTextBuffer, registerFileViewerZoomProvider, resolveFileViewerFitScale, unregisterFileViewerZoomProvider, sanitizeFileViewerSvgResources
|
|
4
|
+
import { createFileViewerZoomChangeEmitter as createZoomChangeEmitter, assertFileViewerMermaidSourceHasNoExternalResources, decodeFileViewerTextBuffer, registerFileViewerZoomProvider, resolveFileViewerFitScale, unregisterFileViewerZoomProvider, sanitizeFileViewerSvgResources } from '@file-viewer/core';
|
|
4
5
|
import { getFileViewerMermaidLoader } from './optionalCapabilities.js';
|
|
5
6
|
import { sanitizeFileViewerRichHtml } from './sanitizeHtml.js';
|
|
7
|
+
import { loadHighlighter, registerLanguageOnce, resolveLanguage } from './syntaxHighlight.js';
|
|
6
8
|
const sanitizeMarkdownHtml = sanitizeFileViewerRichHtml;
|
|
7
9
|
const purifierByDocument = new WeakMap();
|
|
8
10
|
const getMarkdownPurifier = (documentRef) => {
|
|
@@ -19,14 +21,14 @@ const getMarkdownPurifier = (documentRef) => {
|
|
|
19
21
|
return purifier;
|
|
20
22
|
};
|
|
21
23
|
const hardenMarkdownLinks = (root) => {
|
|
22
|
-
root.querySelectorAll('a[target]').forEach(anchor => {
|
|
24
|
+
root.querySelectorAll('a[target]').forEach((anchor) => {
|
|
23
25
|
if ((anchor.getAttribute('target') || '').trim().toLowerCase() === '_blank') {
|
|
24
26
|
anchor.rel = 'noopener noreferrer';
|
|
25
27
|
}
|
|
26
28
|
});
|
|
27
29
|
};
|
|
28
30
|
const markdownStyle = `
|
|
29
|
-
.markdown-viewer{min-height:
|
|
31
|
+
.markdown-viewer{height:100%;min-height:0;padding:28px 16px 48px;background:var(--file-viewer-render-surface-background,#eef1f4);overflow:auto;box-sizing:border-box}
|
|
30
32
|
.markdown-body{color-scheme:light;--bgColor-default:#fff;--bgColor-muted:#f6f8fa;--bgColor-neutral-muted:#818b981f;--borderColor-default:#d1d9e0;--borderColor-muted:#d1d9e0b3;--borderColor-neutral-muted:#d1d9e0b3;--fgColor-default:#1f2328;--fgColor-muted:#59636e;--fgColor-accent:#0969da;background:var(--bgColor-default);border:1px solid rgba(20,35,53,.1);border-radius:12px;margin:0 auto;box-sizing:border-box;min-width:200px;max-width:var(--markdown-max-width,980px);padding:var(--markdown-padding,45px);color:var(--fgColor-default);font-size:var(--markdown-font-size,16px);box-shadow:0 18px 42px rgba(15,23,42,.1)}
|
|
31
33
|
.markdown-body h1,.markdown-body h2,.markdown-body h3{margin-top:24px;margin-bottom:16px;font-weight:700;line-height:1.25}
|
|
32
34
|
.markdown-body h1{padding-bottom:.3em;border-bottom:1px solid var(--borderColor-muted);font-size:2em}
|
|
@@ -37,6 +39,16 @@ const markdownStyle = `
|
|
|
37
39
|
.markdown-body code{padding:.2em .4em;border-radius:6px;background:var(--bgColor-neutral-muted);font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,'Liberation Mono',monospace;font-size:85%}
|
|
38
40
|
.markdown-body pre{padding:16px;overflow:auto;border-radius:8px;background:var(--bgColor-muted)}
|
|
39
41
|
.markdown-body pre code{padding:0;background:transparent;font-size:100%}
|
|
42
|
+
.markdown-body{--code-keyword:#cf222e;--code-title:#8250df;--code-string:#0a3069;--code-number:#0550ae;--code-comment:#6e7781;--code-attr:#953800;--code-built-in:#116329}
|
|
43
|
+
.markdown-body pre .hljs-comment,.markdown-body pre .hljs-quote{color:var(--code-comment)}
|
|
44
|
+
.markdown-body pre .hljs-keyword,.markdown-body pre .hljs-selector-tag,.markdown-body pre .hljs-subst{color:var(--code-keyword)}
|
|
45
|
+
.markdown-body pre .hljs-string,.markdown-body pre .hljs-doctag,.markdown-body pre .hljs-regexp{color:var(--code-string)}
|
|
46
|
+
.markdown-body pre .hljs-title,.markdown-body pre .hljs-section,.markdown-body pre .hljs-selector-id{color:var(--code-title);font-weight:700}
|
|
47
|
+
.markdown-body pre .hljs-number,.markdown-body pre .hljs-literal,.markdown-body pre .hljs-variable,.markdown-body pre .hljs-template-variable{color:var(--code-number)}
|
|
48
|
+
.markdown-body pre .hljs-attr,.markdown-body pre .hljs-attribute,.markdown-body pre .hljs-name,.markdown-body pre .hljs-selector-class{color:var(--code-attr)}
|
|
49
|
+
.markdown-body pre .hljs-built_in,.markdown-body pre .hljs-type,.markdown-body pre .hljs-class .hljs-title{color:var(--code-built-in)}
|
|
50
|
+
[data-viewer-theme='dark'] .markdown-body{--code-keyword:#ff7b72;--code-title:#d2a8ff;--code-string:#a5d6ff;--code-number:#79c0ff;--code-comment:#8b949e;--code-attr:#ffa657;--code-built-in:#7ee787}
|
|
51
|
+
@media (prefers-color-scheme:dark){[data-viewer-theme='system'] .markdown-body{--code-keyword:#ff7b72;--code-title:#d2a8ff;--code-string:#a5d6ff;--code-number:#79c0ff;--code-comment:#8b949e;--code-attr:#ffa657;--code-built-in:#7ee787}}
|
|
40
52
|
.markdown-body table{display:block;width:max-content;max-width:100%;overflow:auto;border-spacing:0;border-collapse:collapse}
|
|
41
53
|
.markdown-body th,.markdown-body td{padding:6px 13px;border:1px solid var(--borderColor-default)}
|
|
42
54
|
.markdown-body tr{background:var(--bgColor-default);border-top:1px solid var(--borderColor-muted)}
|
|
@@ -61,6 +73,49 @@ const MARKDOWN_BASE_PADDING = 45;
|
|
|
61
73
|
const MARKDOWN_BASE_FONT_SIZE = 16;
|
|
62
74
|
const MARKDOWN_MIN_SCALE = 0.6;
|
|
63
75
|
const MARKDOWN_MAX_SCALE = 2.4;
|
|
76
|
+
const highlightMarkdownCode = async (article, context) => {
|
|
77
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
78
|
+
if (((_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.markdownHighlight) === false)
|
|
79
|
+
return;
|
|
80
|
+
let remainingBytes = 512 * 1024;
|
|
81
|
+
const encoder = new TextEncoder();
|
|
82
|
+
for (const code of Array.from(article.querySelectorAll('pre > code'))) {
|
|
83
|
+
const declared = (_c = /(?:^|\s)(?:language|lang)-([^\s]+)/i.exec(code.className)) === null || _c === void 0 ? void 0 : _c[1];
|
|
84
|
+
if (!declared || declared.toLowerCase() === 'mermaid')
|
|
85
|
+
continue;
|
|
86
|
+
const language = resolveLanguage(declared);
|
|
87
|
+
if (language === 'plaintext')
|
|
88
|
+
continue;
|
|
89
|
+
const source = code.textContent || '';
|
|
90
|
+
const bytes = encoder.encode(source).byteLength;
|
|
91
|
+
if (bytes > remainingBytes)
|
|
92
|
+
continue;
|
|
93
|
+
remainingBytes -= bytes;
|
|
94
|
+
try {
|
|
95
|
+
const hljs = await loadHighlighter();
|
|
96
|
+
const registered = await registerLanguageOnce(hljs, language);
|
|
97
|
+
if ((_d = context === null || context === void 0 ? void 0 : context.signal) === null || _d === void 0 ? void 0 : _d.aborted)
|
|
98
|
+
throw context.signal.reason || new DOMException('Markdown rendering aborted.', 'AbortError');
|
|
99
|
+
if (!registered)
|
|
100
|
+
continue;
|
|
101
|
+
code.replaceChildren(sanitizeMarkdownHtml(article.ownerDocument, hljs.highlight(source, { language, ignoreIllegals: true }).value));
|
|
102
|
+
code.classList.add('hljs');
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
if ((_e = context === null || context === void 0 ? void 0 : context.signal) === null || _e === void 0 ? void 0 : _e.aborted)
|
|
106
|
+
throw error;
|
|
107
|
+
code.textContent = source;
|
|
108
|
+
try {
|
|
109
|
+
(_g = (_f = context === null || context === void 0 ? void 0 : context.options) === null || _f === void 0 ? void 0 : _f.onDiagnostic) === null || _g === void 0 ? void 0 : _g.call(_f, {
|
|
110
|
+
code: 'markdown-highlight-unavailable',
|
|
111
|
+
level: 'warning',
|
|
112
|
+
message: 'Markdown code blocks kept as plain text because local highlighting could not load.'
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
catch { }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
64
119
|
const clampZoom = (value) => {
|
|
65
120
|
return Math.min(MARKDOWN_MAX_SCALE, Math.max(MARKDOWN_MIN_SCALE, Number(value.toFixed(2))));
|
|
66
121
|
};
|
|
@@ -82,13 +137,17 @@ const readMarkdownViewport = (root, request) => {
|
|
|
82
137
|
const mobile = viewportWidth <= 767;
|
|
83
138
|
const horizontalPadding = computed
|
|
84
139
|
? readCssPixels(computed.paddingLeft) + readCssPixels(computed.paddingRight)
|
|
85
|
-
: mobile
|
|
140
|
+
: mobile
|
|
141
|
+
? 20
|
|
142
|
+
: 32;
|
|
86
143
|
const verticalPadding = computed
|
|
87
144
|
? readCssPixels(computed.paddingTop) + readCssPixels(computed.paddingBottom)
|
|
88
|
-
: mobile
|
|
145
|
+
: mobile
|
|
146
|
+
? 42
|
|
147
|
+
: 76;
|
|
89
148
|
return {
|
|
90
149
|
width: Math.max(1, viewportWidth - horizontalPadding),
|
|
91
|
-
height: Math.max(1, viewportHeight - verticalPadding)
|
|
150
|
+
height: Math.max(1, viewportHeight - verticalPadding)
|
|
92
151
|
};
|
|
93
152
|
};
|
|
94
153
|
let mermaidRenderSequence = 0;
|
|
@@ -103,16 +162,18 @@ const isDarkTheme = (documentRef, theme) => {
|
|
|
103
162
|
}
|
|
104
163
|
return Boolean((_b = (_a = documentRef.defaultView) === null || _a === void 0 ? void 0 : _a.matchMedia) === null || _b === void 0 ? void 0 : _b.call(_a, '(prefers-color-scheme: dark)').matches);
|
|
105
164
|
};
|
|
106
|
-
export const sanitizeMermaidSvg = (documentRef, svg) => {
|
|
165
|
+
export const sanitizeMermaidSvg = (documentRef, svg, mathLabels = false) => {
|
|
107
166
|
const purifier = getMarkdownPurifier(documentRef);
|
|
108
167
|
if (!purifier) {
|
|
109
168
|
throw new Error('Unable to initialize the Mermaid SVG sanitizer.');
|
|
110
169
|
}
|
|
111
170
|
const fragment = purifier.sanitize(svg, {
|
|
112
171
|
RETURN_DOM_FRAGMENT: true,
|
|
113
|
-
USE_PROFILES: { html:
|
|
114
|
-
|
|
115
|
-
|
|
172
|
+
USE_PROFILES: { html: mathLabels, svg: true, svgFilters: true, mathMl: mathLabels },
|
|
173
|
+
ADD_TAGS: mathLabels ? ['foreignObject'] : [],
|
|
174
|
+
HTML_INTEGRATION_POINTS: mathLabels ? { foreignobject: true } : {},
|
|
175
|
+
FORBID_TAGS: ['script', 'iframe', 'object', 'embed', 'form', ...(mathLabels ? [] : ['foreignObject'])],
|
|
176
|
+
FORBID_ATTR: ['srcdoc']
|
|
116
177
|
});
|
|
117
178
|
const root = fragment.querySelector('svg');
|
|
118
179
|
if (!root) {
|
|
@@ -130,15 +191,16 @@ const renderMermaidSvg = async (documentRef, source, theme) => {
|
|
|
130
191
|
}
|
|
131
192
|
const mermaidModule = await loadMermaid();
|
|
132
193
|
const mermaid = mermaidModule.default;
|
|
133
|
-
const id = `file-viewer-markdown-mermaid-${Date.now()}-${mermaidRenderSequence += 1}`;
|
|
194
|
+
const id = `file-viewer-markdown-mermaid-${Date.now()}-${(mermaidRenderSequence += 1)}`;
|
|
195
|
+
const mathLabels = source.includes('$$');
|
|
134
196
|
mermaid.initialize({
|
|
135
197
|
startOnLoad: false,
|
|
136
198
|
securityLevel: 'strict',
|
|
137
|
-
htmlLabels:
|
|
138
|
-
theme: isDarkTheme(documentRef, theme) ? 'dark' : 'default'
|
|
199
|
+
htmlLabels: mathLabels,
|
|
200
|
+
theme: isDarkTheme(documentRef, theme) ? 'dark' : 'default'
|
|
139
201
|
});
|
|
140
202
|
const rendered = await mermaid.render(id, source);
|
|
141
|
-
return sanitizeMermaidSvg(documentRef, rendered.svg);
|
|
203
|
+
return sanitizeMermaidSvg(documentRef, rendered.svg, mathLabels);
|
|
142
204
|
};
|
|
143
205
|
const result = mermaidRenderQueue.then(render, render);
|
|
144
206
|
mermaidRenderQueue = result.then(() => undefined, () => undefined);
|
|
@@ -146,7 +208,7 @@ const renderMermaidSvg = async (documentRef, source, theme) => {
|
|
|
146
208
|
};
|
|
147
209
|
const renderEmbeddedMermaid = async (article, theme) => {
|
|
148
210
|
const documentRef = article.ownerDocument;
|
|
149
|
-
const mermaidBlocks = Array.from(article.querySelectorAll('pre > code')).filter(code => /(?:^|\s)(?:language|lang)-mermaid(?:\s|$)/i.test(code.className));
|
|
211
|
+
const mermaidBlocks = Array.from(article.querySelectorAll('pre > code')).filter((code) => /(?:^|\s)(?:language|lang)-mermaid(?:\s|$)/i.test(code.className));
|
|
150
212
|
for (const code of mermaidBlocks) {
|
|
151
213
|
const pre = code.parentElement;
|
|
152
214
|
if (!pre) {
|
|
@@ -169,9 +231,10 @@ const renderEmbeddedMermaid = async (article, theme) => {
|
|
|
169
231
|
const message = documentRef.createElement('p');
|
|
170
232
|
message.className = 'markdown-mermaid-error';
|
|
171
233
|
message.setAttribute('role', 'alert');
|
|
172
|
-
message.textContent =
|
|
173
|
-
|
|
174
|
-
|
|
234
|
+
message.textContent =
|
|
235
|
+
error instanceof Error && error.message.includes('npx file-viewer-cli')
|
|
236
|
+
? error.message
|
|
237
|
+
: 'Mermaid diagram could not be rendered. The source is shown above.';
|
|
175
238
|
pre.after(message);
|
|
176
239
|
console.warn('[file-viewer] Unable to render embedded Mermaid diagram.', error);
|
|
177
240
|
}
|
|
@@ -196,8 +259,8 @@ export const stripMarkdownFrontmatter = (text) => {
|
|
|
196
259
|
return input.slice(match.index + match[0].length).replace(/^\r?\n/, '');
|
|
197
260
|
};
|
|
198
261
|
export default async function renderMarkdown(buffer, target, context) {
|
|
199
|
-
var _a, _b, _c;
|
|
200
|
-
const text = 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;
|
|
262
|
+
var _a, _b, _c, _d, _e, _f;
|
|
263
|
+
const text = 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;
|
|
201
264
|
let zoom = 1;
|
|
202
265
|
const zoomEmitter = createZoomChangeEmitter();
|
|
203
266
|
const root = document.createElement('div');
|
|
@@ -206,12 +269,16 @@ export default async function renderMarkdown(buffer, target, context) {
|
|
|
206
269
|
const article = document.createElement('article');
|
|
207
270
|
article.className = 'markdown-body';
|
|
208
271
|
const renderedHtml = await marked(stripMarkdownFrontmatter(text));
|
|
272
|
+
if ((_e = context === null || context === void 0 ? void 0 : context.signal) === null || _e === void 0 ? void 0 : _e.aborted)
|
|
273
|
+
throw context.signal.reason || new DOMException('Markdown rendering aborted.', 'AbortError');
|
|
209
274
|
article.append(sanitizeMarkdownHtml(article.ownerDocument, renderedHtml));
|
|
210
275
|
hardenMarkdownLinks(article);
|
|
276
|
+
await highlightMarkdownCode(article, context);
|
|
277
|
+
const disposeAnchors = installMarkdownAnchors(root, article);
|
|
211
278
|
applyMarkdownZoom(root, zoom);
|
|
212
279
|
root.append(article);
|
|
213
280
|
target.replaceChildren(createStyle(), root);
|
|
214
|
-
await renderEmbeddedMermaid(article, (
|
|
281
|
+
await renderEmbeddedMermaid(article, (_f = context === null || context === void 0 ? void 0 : context.options) === null || _f === void 0 ? void 0 : _f.theme);
|
|
215
282
|
const baseContentHeight = Math.max(article.scrollHeight || 0, article.offsetHeight || 0, 0);
|
|
216
283
|
const getZoomState = () => ({
|
|
217
284
|
scale: zoom,
|
|
@@ -220,7 +287,7 @@ export default async function renderMarkdown(buffer, target, context) {
|
|
|
220
287
|
canZoomOut: zoom > MARKDOWN_MIN_SCALE,
|
|
221
288
|
canReset: zoom !== 1,
|
|
222
289
|
minScale: MARKDOWN_MIN_SCALE,
|
|
223
|
-
maxScale: MARKDOWN_MAX_SCALE
|
|
290
|
+
maxScale: MARKDOWN_MAX_SCALE
|
|
224
291
|
});
|
|
225
292
|
const setZoom = (scale) => {
|
|
226
293
|
zoom = clampZoom(scale);
|
|
@@ -233,10 +300,6 @@ export default async function renderMarkdown(buffer, target, context) {
|
|
|
233
300
|
const viewport = readMarkdownViewport(root, request);
|
|
234
301
|
const minScale = (_a = request.minScale) !== null && _a !== void 0 ? _a : MARKDOWN_MIN_SCALE;
|
|
235
302
|
const requestedMaxScale = (_b = request.maxScale) !== null && _b !== void 0 ? _b : MARKDOWN_MAX_SCALE;
|
|
236
|
-
// A flowing document must not be fitted against its full scroll height.
|
|
237
|
-
// Auto therefore follows the readable content width. It stays at 1:1 by
|
|
238
|
-
// default, but an explicit maxScale can opt into the wider DOCX-like fit
|
|
239
|
-
// requested by the caller.
|
|
240
303
|
const maxScale = request.mode === 'auto' && request.maxScale == null
|
|
241
304
|
? Math.max(minScale, Math.min(1, requestedMaxScale))
|
|
242
305
|
: requestedMaxScale;
|
|
@@ -248,7 +311,7 @@ export default async function renderMarkdown(buffer, target, context) {
|
|
|
248
311
|
contentHeight: baseContentHeight || viewport.height,
|
|
249
312
|
currentScale: zoom,
|
|
250
313
|
minScale,
|
|
251
|
-
maxScale
|
|
314
|
+
maxScale
|
|
252
315
|
});
|
|
253
316
|
if (!scale) {
|
|
254
317
|
return {
|
|
@@ -257,7 +320,7 @@ export default async function renderMarkdown(buffer, target, context) {
|
|
|
257
320
|
resize: request.resize,
|
|
258
321
|
source: request.source,
|
|
259
322
|
reason: 'unmeasurable',
|
|
260
|
-
provider: 'zoom'
|
|
323
|
+
provider: 'zoom'
|
|
261
324
|
};
|
|
262
325
|
}
|
|
263
326
|
const state = setZoom(scale);
|
|
@@ -267,7 +330,7 @@ export default async function renderMarkdown(buffer, target, context) {
|
|
|
267
330
|
resize: request.resize,
|
|
268
331
|
scale: state.scale,
|
|
269
332
|
source: request.source,
|
|
270
|
-
provider: 'zoom'
|
|
333
|
+
provider: 'zoom'
|
|
271
334
|
};
|
|
272
335
|
};
|
|
273
336
|
registerFileViewerZoomProvider(root, {
|
|
@@ -277,13 +340,14 @@ export default async function renderMarkdown(buffer, target, context) {
|
|
|
277
340
|
setZoom,
|
|
278
341
|
fit: fitMarkdown,
|
|
279
342
|
getState: getZoomState,
|
|
280
|
-
subscribe: zoomEmitter.subscribe
|
|
343
|
+
subscribe: zoomEmitter.subscribe
|
|
281
344
|
});
|
|
282
345
|
return {
|
|
283
346
|
$el: target,
|
|
284
347
|
unmount() {
|
|
348
|
+
disposeAnchors();
|
|
285
349
|
unregisterFileViewerZoomProvider(root);
|
|
286
350
|
target.replaceChildren();
|
|
287
|
-
}
|
|
351
|
+
}
|
|
288
352
|
};
|
|
289
353
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function installMarkdownAnchors(root: HTMLElement, article: HTMLElement): () => void;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export function installMarkdownAnchors(root, article) {
|
|
2
|
+
const used = new Set(Array.from(article.querySelectorAll('[id]'), (element) => element.id));
|
|
3
|
+
for (const heading of Array.from(article.querySelectorAll('h1,h2,h3,h4,h5,h6'))) {
|
|
4
|
+
if (heading.id)
|
|
5
|
+
continue;
|
|
6
|
+
const slug = (heading.textContent || '')
|
|
7
|
+
.trim()
|
|
8
|
+
.toLowerCase()
|
|
9
|
+
.replace(/[^\p{L}\p{M}\p{N}_\-\s]/gu, '')
|
|
10
|
+
.replace(/\s/g, '-') || 'section';
|
|
11
|
+
let id = slug;
|
|
12
|
+
for (let suffix = 1; used.has(id); suffix++)
|
|
13
|
+
id = `${slug}-${suffix}`;
|
|
14
|
+
heading.id = id;
|
|
15
|
+
used.add(id);
|
|
16
|
+
}
|
|
17
|
+
const focusTargets = new Set();
|
|
18
|
+
const onClick = (event) => {
|
|
19
|
+
var _a;
|
|
20
|
+
if (event.defaultPrevented ||
|
|
21
|
+
event.button !== 0 ||
|
|
22
|
+
event.metaKey ||
|
|
23
|
+
event.ctrlKey ||
|
|
24
|
+
event.shiftKey ||
|
|
25
|
+
event.altKey)
|
|
26
|
+
return;
|
|
27
|
+
const source = event.target;
|
|
28
|
+
const anchor = (_a = source === null || source === void 0 ? void 0 : source.closest) === null || _a === void 0 ? void 0 : _a.call(source, 'a[href]');
|
|
29
|
+
if (!anchor || !article.contains(anchor) || anchor.hasAttribute('download'))
|
|
30
|
+
return;
|
|
31
|
+
const target = anchor.getAttribute('target');
|
|
32
|
+
if (target && target.toLowerCase() !== '_self')
|
|
33
|
+
return;
|
|
34
|
+
const href = (anchor.getAttribute('href') || '').trim();
|
|
35
|
+
if (!href.startsWith('#') || href === '#')
|
|
36
|
+
return;
|
|
37
|
+
let id;
|
|
38
|
+
try {
|
|
39
|
+
id = decodeURIComponent(href.slice(1));
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const destination = Array.from(article.querySelectorAll('[id]')).find((element) => element.id === id);
|
|
45
|
+
if (!destination)
|
|
46
|
+
return;
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
const rootRect = root.getBoundingClientRect();
|
|
49
|
+
const ratio = root.offsetHeight > 0 ? rootRect.height / root.offsetHeight : 1;
|
|
50
|
+
const top = (destination.getBoundingClientRect().top - rootRect.top) / (ratio || 1);
|
|
51
|
+
root.scrollTop += top - root.clientTop;
|
|
52
|
+
if (!destination.hasAttribute('tabindex')) {
|
|
53
|
+
destination.setAttribute('tabindex', '-1');
|
|
54
|
+
focusTargets.add(destination);
|
|
55
|
+
}
|
|
56
|
+
destination.focus({ preventScroll: true });
|
|
57
|
+
};
|
|
58
|
+
article.addEventListener('click', onClick);
|
|
59
|
+
return () => {
|
|
60
|
+
article.removeEventListener('click', onClick);
|
|
61
|
+
for (const element of focusTargets) {
|
|
62
|
+
if (element.getAttribute('tabindex') === '-1')
|
|
63
|
+
element.removeAttribute('tabindex');
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -9,10 +9,6 @@ export type FileViewerMermaidModule = {
|
|
|
9
9
|
export type FileViewerMermaidLoader = () => Promise<FileViewerMermaidModule>;
|
|
10
10
|
export type FileViewerDiffToHtml = (input: string, options: Record<string, unknown>) => string;
|
|
11
11
|
export type FileViewerPakoModule = typeof import('pako');
|
|
12
|
-
/**
|
|
13
|
-
* Enables embedded Mermaid blocks without making Mermaid part of the base text
|
|
14
|
-
* renderer dependency closure. Capability packs call this once at startup.
|
|
15
|
-
*/
|
|
16
12
|
export declare const registerFileViewerMermaidLoader: (loader: FileViewerMermaidLoader | null) => void;
|
|
17
13
|
export declare const getFileViewerMermaidLoader: () => FileViewerMermaidLoader | null;
|
|
18
14
|
export declare const registerFileViewerDiffToHtml: (renderer: FileViewerDiffToHtml | null) => void;
|