minecodex 2.0.12 → 2.0.13
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.md +12 -0
- package/apps/dashboard/app.js +37 -4
- package/apps/dashboard/assets/data-transfer.js +129 -0
- package/apps/dashboard/assets/download.svg +15 -0
- package/apps/dashboard/assets/upload.svg +15 -0
- package/apps/dashboard/index.html +10 -0
- package/apps/dashboard/styles.css +52 -1
- package/features/file-navigation/assets/book-open.svg +16 -0
- package/features/file-navigation/codex-feature.json +27 -0
- package/features/file-navigation/src/adapters/codex/modules.mjs +67 -0
- package/features/file-navigation/src/adapters/codex/navigation.mjs +84 -0
- package/features/file-navigation/src/domain/navigation.mjs +12 -0
- package/features/file-navigation/src/entry.mjs +34 -0
- package/features/file-navigation/src/frontend/navigation.mjs +140 -0
- package/features/file-navigation/src/injection-source.mjs +9 -0
- package/features/file-navigation/src/navigation.css +16 -0
- package/features/file-navigation/vendor/lucide-LICENSE +43 -0
- package/features/markdown-reading/assets/book-open.svg +16 -0
- package/features/markdown-reading/codex-feature.json +27 -0
- package/features/markdown-reading/src/adapters/codex/links.mjs +280 -0
- package/features/markdown-reading/src/adapters/codex/lists.mjs +168 -0
- package/features/markdown-reading/src/adapters/codex/markdown-native.mjs +28 -0
- package/features/markdown-reading/src/adapters/codex/preview.mjs +38 -0
- package/features/markdown-reading/src/adapters/codex/providers.mjs +18 -0
- package/features/markdown-reading/src/adapters/codex/reading-runtime.mjs +152 -0
- package/features/markdown-reading/src/adapters/codex/rich-text.mjs +561 -0
- package/features/markdown-reading/src/adapters/codex/table-actions.mjs +270 -0
- package/features/markdown-reading/src/adapters/codex/tables.mjs +493 -0
- package/features/markdown-reading/src/adapters/codex/toc-native.mjs +131 -0
- package/features/markdown-reading/src/adapters/codex/toc.mjs +334 -0
- package/features/markdown-reading/src/domain/footnotes.mjs +56 -0
- package/features/markdown-reading/src/injection-source.mjs +43 -0
- package/features/markdown-reading/src/markdown-reading.mjs +82 -0
- package/features/markdown-reading/src/reading.css +346 -0
- package/features/markdown-reading/vendor/lucide-LICENSE +43 -0
- package/features/model-slider/codex-feature.json +1 -0
- package/features/model-slider/src/backend/favorites.mjs +54 -0
- package/features/model-slider/src/domain/model-rules.mjs +6 -1
- package/features/model-slider/src/frontend/install.mjs +24 -3
- package/features/notes/assets/prompt-previews.json +26 -0
- package/features/notes/codex-feature.json +2 -1
- package/features/notes/src/backend/transfer.mjs +97 -0
- package/features/notes/src/frontend/entry.mjs +380 -10
- package/features/notes/src/frontend/icons.json +4 -1
- package/features/notes/src/frontend/styles.css +63 -0
- package/features/session-status/src/domain/session-stats.mjs +22 -8
- package/features/session-status/src/session-status.mjs +2 -2
- package/features/sites/src/backend/sites-service.mjs +20 -5
- package/features/sites/src/backend/transfer.mjs +43 -0
- package/package.json +11 -3
- package/packages/cli/src/config.mjs +4 -2
- package/packages/cli/src/control-server.mjs +23 -4
- package/packages/cli/src/data-transfer/service.mjs +42 -0
- package/packages/cli/src/runtime-manager.mjs +3 -0
- package/packages/runtime-host/src/codex-injection.mjs +1 -0
- package/packages/runtime-host/src/codex-runtime.mjs +11 -0
- package/packages/runtime-host/src/feature-registry.mjs +1 -0
- package/packages/ui/src/native/theme.mjs +22 -1
- package/packages/ui/src/native/tokens.mjs +4 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { fileNavigationPath, selectFileNavigationDirectory } from '../domain/navigation.mjs';
|
|
2
|
+
|
|
3
|
+
export function installFileNavigation(view, ui, locale) {
|
|
4
|
+
const copy = locale === 'zh-CN'
|
|
5
|
+
? { browse: '浏览并切换文件', loading: '正在加载…', empty: '此文件夹为空', error: '无法读取文件夹,点击重试' }
|
|
6
|
+
: { browse: 'Browse and switch files', loading: 'Loading…', empty: 'This folder is empty', error: 'Could not load folder. Click to retry.' };
|
|
7
|
+
const unbind = ui.theme.bindScope(view.capsule, ['--font-sans-default', '--text-sm', '--font-weight-medium', '--color-text-primary', '--color-text-secondary', '--color-ring', '--spacing', '--radius-full']);
|
|
8
|
+
const life = new AbortController();
|
|
9
|
+
const path = fileNavigationPath(view.rootName, view.path);
|
|
10
|
+
const trigger = document.createElement('button');
|
|
11
|
+
trigger.type = 'button'; trigger.className = 'mcx-file-navigation-trigger cursor-interaction';
|
|
12
|
+
trigger.setAttribute('aria-label', copy.browse); trigger.setAttribute('aria-haspopup', 'dialog');
|
|
13
|
+
trigger.setAttribute('aria-expanded', 'false');
|
|
14
|
+
trigger.dataset.parts = path.labels.length;
|
|
15
|
+
trigger.setAttribute('data-minecodex-tooltip', [view.rootName, view.path].join('/'));
|
|
16
|
+
path.labels.forEach((label, index) => {
|
|
17
|
+
if (index && view.separator) {
|
|
18
|
+
const separator = view.separator.cloneNode(true); separator.classList.add('mcx-file-navigation-separator');
|
|
19
|
+
trigger.append(separator);
|
|
20
|
+
}
|
|
21
|
+
const span = document.createElement('span'); span.textContent = label;
|
|
22
|
+
span.className = index === path.labels.length - 1 ? 'mcx-file-navigation-name' : 'mcx-file-navigation-parent';
|
|
23
|
+
trigger.append(span);
|
|
24
|
+
});
|
|
25
|
+
view.nav.setAttribute('data-mcx-file-navigation-original', '');
|
|
26
|
+
view.capsule.append(trigger);
|
|
27
|
+
let popover = null, session = null, columns = [], directories = [], native = null;
|
|
28
|
+
let disposed = false, suppressEscapeRelease = false;
|
|
29
|
+
function close(focus = false) {
|
|
30
|
+
session?.abort(); session = null;
|
|
31
|
+
for (const column of columns) column.mount?.dispose();
|
|
32
|
+
columns = []; popover?.remove(); popover = null;
|
|
33
|
+
trigger.setAttribute('aria-expanded', 'false');
|
|
34
|
+
if (focus && trigger.isConnected) {
|
|
35
|
+
trigger.focus();
|
|
36
|
+
// 文件树卸载也会恢复焦点;等该轮清理结束,再交还当前路径入口。
|
|
37
|
+
requestAnimationFrame(() => { if (!disposed && !session && trigger.isConnected) trigger.focus(); });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function position() {
|
|
41
|
+
trigger.toggleAttribute('data-compact', view.capsule.parentElement.getBoundingClientRect().width < 330);
|
|
42
|
+
if (!popover) return;
|
|
43
|
+
const anchor = view.capsule.getBoundingClientRect(), bounds = view.boundary.getBoundingClientRect();
|
|
44
|
+
const zoom = Number.parseFloat(getComputedStyle(document.body).zoom) || 1;
|
|
45
|
+
const leftEdge = Math.max(8, bounds.left + 8), rightEdge = Math.min(innerWidth - 8, bounds.right - 8);
|
|
46
|
+
const width = Math.max(0, Math.min(744, rightEdge - leftEdge));
|
|
47
|
+
const below = Math.min(innerHeight, bounds.bottom) - anchor.bottom - 24;
|
|
48
|
+
const above = anchor.top - Math.max(0, bounds.top) - 24;
|
|
49
|
+
const down = below >= 180 || below >= above;
|
|
50
|
+
const height = Math.max(60, Math.min(320, down ? below : above));
|
|
51
|
+
popover.style.width = `${width / zoom}px`; popover.style.height = `${height / zoom}px`;
|
|
52
|
+
popover.style.left = `${Math.max(leftEdge, Math.min(anchor.left, rightEdge - width)) / zoom}px`;
|
|
53
|
+
popover.dataset.side = down ? 'bottom' : 'top';
|
|
54
|
+
popover.style.top = `${(down ? anchor.bottom : anchor.top - height) / zoom}px`;
|
|
55
|
+
}
|
|
56
|
+
async function loadColumn(directory, index, current) {
|
|
57
|
+
const element = document.createElement('div'); element.className = 'mcx-file-navigation-column';
|
|
58
|
+
element.setAttribute('aria-label', directory || view.rootName);
|
|
59
|
+
const column = { element, directory, mount: null, alive: true };
|
|
60
|
+
columns.push(column); popover.append(element);
|
|
61
|
+
const message = document.createElement('div'); message.className = 'mcx-file-navigation-message';
|
|
62
|
+
message.setAttribute('role', 'status'); message.textContent = copy.loading; element.append(message);
|
|
63
|
+
const valid = () => !current.aborted && column.alive;
|
|
64
|
+
async function load() {
|
|
65
|
+
try {
|
|
66
|
+
const entries = await view.list(directory, current);
|
|
67
|
+
if (!valid()) return;
|
|
68
|
+
element.replaceChildren();
|
|
69
|
+
if (!entries.length) { message.textContent = copy.empty; element.append(message); return; }
|
|
70
|
+
column.mount = native.mountColumn(element, entries, directories[index + 1] ?? view.path, entry => {
|
|
71
|
+
if (!valid()) return;
|
|
72
|
+
if (!entry.directory) { close(); view.open(entry); return; }
|
|
73
|
+
for (const old of columns.splice(index + 1)) { old.alive = false; old.mount?.dispose(); old.element.remove(); }
|
|
74
|
+
directories = selectFileNavigationDirectory(directories, index, entry.path);
|
|
75
|
+
loadColumn(entry.path, index + 1, current);
|
|
76
|
+
requestAnimationFrame(() => { if (popover) popover.scrollLeft = popover.scrollWidth; });
|
|
77
|
+
}, () => {
|
|
78
|
+
const previous = columns[index - 1];
|
|
79
|
+
if (previous) { previous.element.scrollIntoView({ block: 'nearest', inline: 'nearest' }); previous.mount?.focus(); }
|
|
80
|
+
else trigger.focus();
|
|
81
|
+
});
|
|
82
|
+
} catch {
|
|
83
|
+
if (!valid()) return;
|
|
84
|
+
const retry = document.createElement('button'); retry.type = 'button';
|
|
85
|
+
retry.className = 'mcx-file-navigation-message cursor-interaction'; retry.textContent = copy.error;
|
|
86
|
+
retry.onclick = () => { element.replaceChildren(message); message.textContent = copy.loading; load(); };
|
|
87
|
+
element.replaceChildren(retry);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
await load();
|
|
91
|
+
}
|
|
92
|
+
async function open() {
|
|
93
|
+
if (session) { close(true); return; }
|
|
94
|
+
session = new AbortController(); const current = session.signal;
|
|
95
|
+
try {
|
|
96
|
+
native = await view.prepare();
|
|
97
|
+
if (current.aborted || disposed) return;
|
|
98
|
+
popover = document.createElement('div'); popover.className = 'mcx-file-navigation-popover';
|
|
99
|
+
popover.setAttribute('role', 'dialog'); popover.setAttribute('aria-label', copy.browse);
|
|
100
|
+
ui.surface(popover, 'popover'); ui.mount(popover, 'floating');
|
|
101
|
+
trigger.setAttribute('aria-expanded', 'true');
|
|
102
|
+
directories = path.directories.slice(); position();
|
|
103
|
+
await Promise.all(directories.map((directory, index) => loadColumn(directory, index, current)));
|
|
104
|
+
if (!current.aborted && popover) {
|
|
105
|
+
popover.scrollLeft = popover.scrollWidth;
|
|
106
|
+
requestAnimationFrame(() => { if (!current.aborted) columns.at(-1)?.mount?.focus(); });
|
|
107
|
+
}
|
|
108
|
+
} catch {
|
|
109
|
+
if (current.aborted) return;
|
|
110
|
+
close();
|
|
111
|
+
// 原生能力失效时立即交还已有导航,不能留一个无响应的替身入口。
|
|
112
|
+
dispose();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
trigger.addEventListener('click', open, { signal: life.signal });
|
|
116
|
+
document.addEventListener('pointerdown', event => {
|
|
117
|
+
if (session && !event.composedPath().includes(trigger) && !event.composedPath().includes(popover)) close();
|
|
118
|
+
}, { capture: true, signal: life.signal });
|
|
119
|
+
window.addEventListener('keydown', event => {
|
|
120
|
+
if (!popover) return;
|
|
121
|
+
if (event.key === 'Escape') { suppressEscapeRelease = true; event.preventDefault(); event.stopImmediatePropagation(); close(true); }
|
|
122
|
+
if (event.key === 'Tab') close();
|
|
123
|
+
}, { capture: true, signal: life.signal });
|
|
124
|
+
// 原生全局 Escape 在释放阶段也处理焦点;同一次关闭需消费完整按键周期。
|
|
125
|
+
window.addEventListener('keyup', event => {
|
|
126
|
+
if (event.key !== 'Escape' || !suppressEscapeRelease) return;
|
|
127
|
+
suppressEscapeRelease = false;
|
|
128
|
+
event.preventDefault(); event.stopImmediatePropagation();
|
|
129
|
+
if (!disposed && trigger.isConnected) trigger.focus();
|
|
130
|
+
}, { capture: true, signal: life.signal });
|
|
131
|
+
window.addEventListener('resize', position, { signal: life.signal });
|
|
132
|
+
window.addEventListener('blur', () => close(), { signal: life.signal });
|
|
133
|
+
const resize = new ResizeObserver(position); resize.observe(view.capsule); resize.observe(view.boundary); resize.observe(view.capsule.parentElement);
|
|
134
|
+
function dispose() {
|
|
135
|
+
if (disposed) return;
|
|
136
|
+
disposed = true; close(); life.abort(); resize.disconnect(); trigger.remove();
|
|
137
|
+
view.nav.removeAttribute('data-mcx-file-navigation-original'); unbind();
|
|
138
|
+
}
|
|
139
|
+
return { dispose, available: () => !disposed && trigger.isConnected };
|
|
140
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { fileNavigationPath, selectFileNavigationDirectory } from './domain/navigation.mjs';
|
|
3
|
+
import { loadFileNavigationModules } from './adapters/codex/modules.mjs';
|
|
4
|
+
import { createFileNavigationAdapter } from './adapters/codex/navigation.mjs';
|
|
5
|
+
import { installFileNavigation } from './frontend/navigation.mjs';
|
|
6
|
+
export async function createPageScriptSource() {
|
|
7
|
+
const bindings = [fileNavigationPath, selectFileNavigationDirectory, loadFileNavigationModules, createFileNavigationAdapter, installFileNavigation];
|
|
8
|
+
return bindings.map(fn => `const ${fn.name} = ${fn.toString()};`).join('\n') + '\n' + await readFile(new URL('./entry.mjs', import.meta.url), 'utf8');
|
|
9
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[data-mcx-file-navigation-original] { display:none !important; }
|
|
2
|
+
.mcx-file-navigation-trigger { display:flex; align-items:center; gap:calc(var(--mcx-spacing)*1); min-width:0; max-width:100%; height:100%; padding:0 calc(var(--mcx-spacing)*2); border:0; background:transparent; font-family:var(--mcx-font-sans-default); font-size:var(--mcx-text-sm); color:var(--mcx-color-text-secondary); white-space:nowrap; }
|
|
3
|
+
.mcx-file-navigation-trigger:hover { color:var(--mcx-color-text-primary); }
|
|
4
|
+
.mcx-file-navigation-trigger:focus-visible { outline:2px solid var(--mcx-color-ring); outline-offset:-2px; border-radius:var(--mcx-radius-full); }
|
|
5
|
+
.mcx-file-navigation-parent { overflow:hidden; text-overflow:ellipsis; flex:0 1 auto; }
|
|
6
|
+
.mcx-file-navigation-name { min-width:0; overflow:hidden; text-overflow:ellipsis; font-weight:var(--mcx-font-weight-medium); color:var(--mcx-color-text-primary); }
|
|
7
|
+
.mcx-file-navigation-separator { flex-shrink:0; }
|
|
8
|
+
.mcx-file-navigation-popover { display:flex; overflow-x:auto; overscroll-behavior:contain; border-radius:var(--mcx-radius-xl); font-family:var(--mcx-font-sans-default); font-size:var(--mcx-text-sm); color:var(--mcx-color-text-primary); }
|
|
9
|
+
.mcx-file-navigation-column { flex:0 0 248px; min-width:0; height:100%; padding:4px; overflow:hidden; border-inline-end:1px solid var(--mcx-color-border); }
|
|
10
|
+
.mcx-file-navigation-column:last-child { flex-grow:1; border-inline-end:0; }
|
|
11
|
+
.mcx-file-navigation-message { padding:calc(var(--mcx-spacing)*3); color:var(--mcx-color-text-secondary); }
|
|
12
|
+
|
|
13
|
+
.mcx-file-navigation-trigger[data-compact][data-parts="3"] > :nth-child(2), .mcx-file-navigation-trigger[data-compact][data-parts="3"] > :nth-child(3), .mcx-file-navigation-trigger[data-compact][data-parts="2"] > :nth-child(-n+2) { display:none; }
|
|
14
|
+
|
|
15
|
+
.mcx-file-navigation-popover[data-side="bottom"] { translate:0 calc(var(--mcx-spacing) * 2); }
|
|
16
|
+
.mcx-file-navigation-popover[data-side="top"] { translate:0 calc(var(--mcx-spacing) * -2); }
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lucide Icons and Contributors
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
The following Lucide icons are derived from the Feather project:
|
|
20
|
+
|
|
21
|
+
airplay, alert-circle, alert-octagon, alert-triangle, aperture, arrow-down-circle, arrow-down-left, arrow-down-right, arrow-down, arrow-left-circle, arrow-left, arrow-right-circle, arrow-right, arrow-up-circle, arrow-up-left, arrow-up-right, arrow-up, at-sign, calendar, cast, check, chevron-down, chevron-left, chevron-right, chevron-up, chevrons-down, chevrons-left, chevrons-right, chevrons-up, circle, clipboard, clock, code, columns, command, compass, corner-down-left, corner-down-right, corner-left-down, corner-left-up, corner-right-down, corner-right-up, corner-up-left, corner-up-right, crosshair, database, divide-circle, divide-square, dollar-sign, download, external-link, feather, frown, hash, headphones, help-circle, info, italic, key, layout, life-buoy, link-2, link, loader, lock, log-in, log-out, maximize, meh, minimize, minimize-2, minus-circle, minus-square, minus, monitor, moon, more-horizontal, more-vertical, move, music, navigation-2, navigation, octagon, pause-circle, percent, plus-circle, plus-square, plus, power, radio, rss, search, server, share, shopping-bag, sidebar, smartphone, smile, square, table-2, tablet, target, terminal, trash-2, trash, triangle, tv, type, upload, x-circle, x-octagon, x-square, x, zoom-in, zoom-out
|
|
22
|
+
|
|
23
|
+
The MIT License (MIT) (for the icons listed above)
|
|
24
|
+
|
|
25
|
+
Copyright (c) 2013-present Cole Bemis
|
|
26
|
+
|
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
28
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
29
|
+
in the Software without restriction, including without limitation the rights
|
|
30
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
31
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
32
|
+
furnished to do so, subject to the following conditions:
|
|
33
|
+
|
|
34
|
+
The above copyright notice and this permission notice shall be included in all
|
|
35
|
+
copies or substantial portions of the Software.
|
|
36
|
+
|
|
37
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
38
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
39
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
40
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
41
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
42
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
43
|
+
SOFTWARE.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!-- @license lucide-static v0.577.0 - ISC -->
|
|
2
|
+
<svg
|
|
3
|
+
class="lucide lucide-book-open"
|
|
4
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
5
|
+
width="24"
|
|
6
|
+
height="24"
|
|
7
|
+
viewBox="0 0 24 24"
|
|
8
|
+
fill="none"
|
|
9
|
+
stroke="currentColor"
|
|
10
|
+
stroke-width="2.1"
|
|
11
|
+
stroke-linecap="round"
|
|
12
|
+
stroke-linejoin="round"
|
|
13
|
+
>
|
|
14
|
+
<path d="M12 7v14" />
|
|
15
|
+
<path d="M3 18a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h5a4 4 0 0 1 4 4 4 4 0 0 1 4-4h5a1 1 0 0 1 1 1v13a1 1 0 0 1-1 1h-6a3 3 0 0 0-3 3 3 3 0 0 0-3-3z" />
|
|
16
|
+
</svg>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 1,
|
|
3
|
+
"id": "markdown-reading",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"label": {
|
|
6
|
+
"en": "Markdown Reading",
|
|
7
|
+
"zh-CN": "Markdown 阅读排版"
|
|
8
|
+
},
|
|
9
|
+
"description": {
|
|
10
|
+
"en": "Read Markdown previews with Pages typography, spacing, and native outline.",
|
|
11
|
+
"zh-CN": "用 Pages 的正文排版、舒适留白与原生目录阅读 Markdown 预览。"
|
|
12
|
+
},
|
|
13
|
+
"entry": {
|
|
14
|
+
"kind": "page-script",
|
|
15
|
+
"script": "src/markdown-reading.mjs",
|
|
16
|
+
"sourceFactory": "src/injection-source.mjs",
|
|
17
|
+
"resources": {
|
|
18
|
+
"styles": "src/reading.css"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"icon": {
|
|
22
|
+
"resource": "assets/book-open.svg"
|
|
23
|
+
},
|
|
24
|
+
"placement": {
|
|
25
|
+
"order": 90
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// 修补原生 Preview 的链接装饰;源码、语法树与编辑器实例继续归 Codex。
|
|
2
|
+
export async function loadNativeMarkdownLinks() {
|
|
3
|
+
const url = [...document.querySelectorAll('link[rel="modulepreload"]')]
|
|
4
|
+
.map((n) => n.href)
|
|
5
|
+
.find((u) => /\/text-file-editor-extensions-[^/]+\.js$/.test(u));
|
|
6
|
+
if (!url) throw new Error('native-markdown-link-module-unavailable');
|
|
7
|
+
const source = await (await fetch(url)).text();
|
|
8
|
+
const opener = source.match(/([\w$]+)\(\{event:[\w$]+,href:[\w$]+,initiator:`markdown_link_click`/)?.[1];
|
|
9
|
+
let openHref;
|
|
10
|
+
for (const match of source.matchAll(/import\{([^}]+)\}from["']([^"']+)["']/g))
|
|
11
|
+
for (const entry of match[1].split(',')) {
|
|
12
|
+
const parts = entry.trim().split(/\s+as\s+/);
|
|
13
|
+
if (parts.at(-1) === opener) openHref = (await import(new URL(match[2], url).href))[parts[0]];
|
|
14
|
+
}
|
|
15
|
+
if (typeof openHref !== 'function') throw new Error('native-markdown-link-opener-unavailable');
|
|
16
|
+
return openHref;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function installMarkdownLinks(view, api, openHref) {
|
|
20
|
+
const { Decoration, ViewPlugin, StateEffect, Compartment, WidgetType, Prec } = api;
|
|
21
|
+
const normalize = (s) =>
|
|
22
|
+
s
|
|
23
|
+
.replace(/\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~])/g, '$1')
|
|
24
|
+
.replace(/\s+/g, ' ')
|
|
25
|
+
.trim()
|
|
26
|
+
.toLowerCase();
|
|
27
|
+
const unescape = (s) => {
|
|
28
|
+
const node = document.createElement('textarea');
|
|
29
|
+
node.innerHTML = s;
|
|
30
|
+
return node.value.replace(/\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~])/g, '$1');
|
|
31
|
+
};
|
|
32
|
+
class Link extends WidgetType {
|
|
33
|
+
constructor(label, href, title, format = []) {
|
|
34
|
+
super();
|
|
35
|
+
Object.assign(this, { label, href, title, format });
|
|
36
|
+
}
|
|
37
|
+
eq(other) {
|
|
38
|
+
return (
|
|
39
|
+
this.label === other.label &&
|
|
40
|
+
this.href === other.href &&
|
|
41
|
+
this.title === other.title &&
|
|
42
|
+
JSON.stringify(this.format) === JSON.stringify(other.format)
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
toDOM() {
|
|
46
|
+
const span = document.createElement('span');
|
|
47
|
+
span.className = 'mcx-reading-link';
|
|
48
|
+
const append = (parent, parts) => {
|
|
49
|
+
for (const part of parts) {
|
|
50
|
+
if (typeof part === 'string') parent.append(document.createTextNode(part));
|
|
51
|
+
else {
|
|
52
|
+
const el = document.createElement(part.tag);
|
|
53
|
+
append(el, part.children);
|
|
54
|
+
parent.append(el);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
if (this.format.length) append(span, this.format);
|
|
59
|
+
else span.textContent = this.label;
|
|
60
|
+
span.setAttribute('role', 'link');
|
|
61
|
+
span.tabIndex = 0;
|
|
62
|
+
if (this.title) span.title = this.title;
|
|
63
|
+
span.dataset.href = this.href;
|
|
64
|
+
const activate = (event) => {
|
|
65
|
+
// 普通本地链接仍交给原生编辑器的文件打开回调。
|
|
66
|
+
if (!/^(?:https?:|mailto:)/i.test(this.href)) return;
|
|
67
|
+
event.preventDefault();
|
|
68
|
+
event.stopPropagation();
|
|
69
|
+
openHref({ event, href: this.href, initiator: 'markdown_link_click' });
|
|
70
|
+
};
|
|
71
|
+
span.addEventListener('click', (event) => {
|
|
72
|
+
if (event.button === 0 && !event.shiftKey && event.detail === 1) activate(event);
|
|
73
|
+
});
|
|
74
|
+
span.addEventListener('keydown', (event) => {
|
|
75
|
+
if (event.key === 'Enter') activate(event);
|
|
76
|
+
});
|
|
77
|
+
return span;
|
|
78
|
+
}
|
|
79
|
+
ignoreEvent() {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
class AutoLinks extends WidgetType {
|
|
84
|
+
constructor(parts) {
|
|
85
|
+
super();
|
|
86
|
+
this.parts = parts;
|
|
87
|
+
}
|
|
88
|
+
eq(other) {
|
|
89
|
+
return JSON.stringify(this.parts) === JSON.stringify(other.parts);
|
|
90
|
+
}
|
|
91
|
+
toDOM() {
|
|
92
|
+
const span = document.createElement('span');
|
|
93
|
+
for (const part of this.parts)
|
|
94
|
+
span.append(
|
|
95
|
+
part.href ? new Link(part.label, part.href, '').toDOM() : document.createTextNode(part.label),
|
|
96
|
+
);
|
|
97
|
+
return span;
|
|
98
|
+
}
|
|
99
|
+
ignoreEvent() {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
let cachedTree,
|
|
104
|
+
cachedDoc,
|
|
105
|
+
references = new Map(),
|
|
106
|
+
definitions = [];
|
|
107
|
+
function build(editor) {
|
|
108
|
+
const text = (node) => editor.state.sliceDoc(node.from, node.to);
|
|
109
|
+
if (cachedTree !== editor.state.tree || cachedDoc !== editor.state.doc) {
|
|
110
|
+
cachedTree = editor.state.tree;
|
|
111
|
+
cachedDoc = editor.state.doc;
|
|
112
|
+
references = new Map();
|
|
113
|
+
definitions = [];
|
|
114
|
+
editor.state.tree.iterate({
|
|
115
|
+
enter(n) {
|
|
116
|
+
if (n.name !== 'LinkReference') return;
|
|
117
|
+
const node = n.node,
|
|
118
|
+
label = node.getChild('LinkLabel'),
|
|
119
|
+
url = node.getChild('URL'),
|
|
120
|
+
title = node.getChild('LinkTitle');
|
|
121
|
+
if (label && url) {
|
|
122
|
+
const key = normalize(text(label).slice(1, -1));
|
|
123
|
+
// 原生把脚注也解析成 LinkReference;保留脚注源码,不能隐藏正文。
|
|
124
|
+
if (key.startsWith('^')) return false;
|
|
125
|
+
if (!references.has(key))
|
|
126
|
+
references.set(key, {
|
|
127
|
+
href: unescape(text(url).replace(/^<|>$/g, '')),
|
|
128
|
+
title: title ? unescape(text(title).slice(1, -1)) : '',
|
|
129
|
+
});
|
|
130
|
+
definitions.push(node);
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
const ranges = [];
|
|
137
|
+
const selected = (node) =>
|
|
138
|
+
editor.hasFocus &&
|
|
139
|
+
editor.state.selection.ranges.some((r) =>
|
|
140
|
+
r.empty ? r.from >= node.from && r.from <= node.to : r.from < node.to && r.to > node.from,
|
|
141
|
+
);
|
|
142
|
+
for (const node of definitions) {
|
|
143
|
+
if (selected(node)) continue;
|
|
144
|
+
for (
|
|
145
|
+
let number = editor.state.doc.lineAt(node.from).number;
|
|
146
|
+
number <= editor.state.doc.lineAt(node.to).number;
|
|
147
|
+
number++
|
|
148
|
+
) {
|
|
149
|
+
const line = editor.state.doc.line(number);
|
|
150
|
+
if (line.to < editor.viewport.from || line.from > editor.viewport.to) continue;
|
|
151
|
+
ranges.push(Decoration.line({ class: 'mcx-reading-link-definition' }).range(line.from));
|
|
152
|
+
if (line.to > line.from) ranges.push(Decoration.replace({}).range(line.from, line.to));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
editor.state.tree.iterate({
|
|
156
|
+
from: editor.viewport.from,
|
|
157
|
+
to: editor.viewport.to,
|
|
158
|
+
enter(n) {
|
|
159
|
+
if (n.name === 'LinkReference') return false;
|
|
160
|
+
if (n.name === 'URL' && n.node.parent?.name === 'Paragraph') {
|
|
161
|
+
const raw = text(n.node),
|
|
162
|
+
parts = [];
|
|
163
|
+
let cursor = 0;
|
|
164
|
+
for (const match of raw.matchAll(/(?:https?:\/\/|www\.)[^\s<>、,。;!?]+/gi)) {
|
|
165
|
+
const label = match[0].replace(/[.,;:!?]+$/g, '');
|
|
166
|
+
if (!label) continue;
|
|
167
|
+
if (match.index > cursor) parts.push({ label: raw.slice(cursor, match.index) });
|
|
168
|
+
parts.push({ label, href: label.startsWith('www.') ? `https://${label}` : unescape(label) });
|
|
169
|
+
cursor = match.index + label.length;
|
|
170
|
+
}
|
|
171
|
+
if (cursor < raw.length) parts.push({ label: raw.slice(cursor) });
|
|
172
|
+
if (parts.some((part) => part.href))
|
|
173
|
+
ranges.push(Decoration.replace({ widget: new AutoLinks(parts) }).range(n.from, n.to));
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
if (n.name !== 'Link') return;
|
|
177
|
+
const node = n.node;
|
|
178
|
+
if (
|
|
179
|
+
(selected(node) && !editor.state.selection.main.empty) ||
|
|
180
|
+
editor.state.doc.lineAt(node.from).number !== editor.state.doc.lineAt(node.to).number
|
|
181
|
+
)
|
|
182
|
+
return false;
|
|
183
|
+
const marks = node.getChildren('LinkMark'),
|
|
184
|
+
start = marks[0],
|
|
185
|
+
end = marks.find((m) => text(m) === ']');
|
|
186
|
+
if (!start || !end) return false;
|
|
187
|
+
const labelFrom = start.to,
|
|
188
|
+
labelTo = end.from;
|
|
189
|
+
const urls = node.getChildren('URL'),
|
|
190
|
+
url = urls.find((u) => u.from > labelTo),
|
|
191
|
+
title = node.getChild('LinkTitle'),
|
|
192
|
+
reference = node.getChild('LinkLabel');
|
|
193
|
+
if (url && !title && !urls.some((u) => u.from >= labelFrom && u.to <= labelTo)) return false;
|
|
194
|
+
const key = normalize(
|
|
195
|
+
reference
|
|
196
|
+
? text(reference).slice(1, -1) || editor.state.sliceDoc(labelFrom, labelTo)
|
|
197
|
+
: editor.state.sliceDoc(labelFrom, labelTo),
|
|
198
|
+
);
|
|
199
|
+
const target = url
|
|
200
|
+
? {
|
|
201
|
+
href: unescape(text(url).replace(/^<|>$/g, '')),
|
|
202
|
+
title: title ? unescape(text(title).slice(1, -1)) : '',
|
|
203
|
+
}
|
|
204
|
+
: references.get(key);
|
|
205
|
+
if (!target || /^(?:javascript|data|vbscript):/i.test(target.href)) return false;
|
|
206
|
+
const omit = [];
|
|
207
|
+
node.toTree().iterate({
|
|
208
|
+
enter(child) {
|
|
209
|
+
if (/^(EmphasisMark|CodeMark|StrikethroughMark)$/.test(child.name)) {
|
|
210
|
+
const from = node.from + child.from,
|
|
211
|
+
to = node.from + child.to;
|
|
212
|
+
if (from >= labelFrom && to <= labelTo) omit.push([from, to]);
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
let label = '',
|
|
217
|
+
cursor = labelFrom;
|
|
218
|
+
for (const [from, to] of omit.sort((a, b) => a[0] - b[0])) {
|
|
219
|
+
label += editor.state.sliceDoc(cursor, from);
|
|
220
|
+
cursor = to;
|
|
221
|
+
}
|
|
222
|
+
label = unescape(label + editor.state.sliceDoc(cursor, labelTo));
|
|
223
|
+
const format = (node) => {
|
|
224
|
+
const parts = [];
|
|
225
|
+
let cursor = Math.max(node.from, labelFrom);
|
|
226
|
+
for (let child = node.firstChild; child; child = child.nextSibling) {
|
|
227
|
+
if (child.to <= labelFrom || child.from >= labelTo) continue;
|
|
228
|
+
if (child.from > cursor) parts.push(unescape(editor.state.sliceDoc(cursor, child.from)));
|
|
229
|
+
const tag = {
|
|
230
|
+
StrongEmphasis: 'strong',
|
|
231
|
+
Emphasis: 'em',
|
|
232
|
+
Strikethrough: 'del',
|
|
233
|
+
InlineCode: 'code',
|
|
234
|
+
}[child.name];
|
|
235
|
+
if (tag) parts.push({ tag, children: format(child) });
|
|
236
|
+
else if (!/^(?:LinkMark|EmphasisMark|CodeMark|StrikethroughMark)$/.test(child.name))
|
|
237
|
+
parts.push(
|
|
238
|
+
unescape(editor.state.sliceDoc(Math.max(child.from, labelFrom), Math.min(child.to, labelTo))),
|
|
239
|
+
);
|
|
240
|
+
cursor = Math.min(child.to, labelTo);
|
|
241
|
+
}
|
|
242
|
+
if (cursor < Math.min(node.to, labelTo))
|
|
243
|
+
parts.push(unescape(editor.state.sliceDoc(cursor, Math.min(node.to, labelTo))));
|
|
244
|
+
return parts;
|
|
245
|
+
};
|
|
246
|
+
ranges.push(
|
|
247
|
+
Decoration.replace({ widget: new Link(label, target.href, target.title, format(node)) }).range(
|
|
248
|
+
node.from,
|
|
249
|
+
node.to,
|
|
250
|
+
),
|
|
251
|
+
);
|
|
252
|
+
return false;
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
return Decoration.set(ranges, true);
|
|
256
|
+
}
|
|
257
|
+
const plugin = ViewPlugin.define(
|
|
258
|
+
(editor) => ({
|
|
259
|
+
decorations: build(editor),
|
|
260
|
+
update(update) {
|
|
261
|
+
if (
|
|
262
|
+
update.docChanged ||
|
|
263
|
+
update.selectionSet ||
|
|
264
|
+
update.focusChanged ||
|
|
265
|
+
update.viewportChanged ||
|
|
266
|
+
update.startState.tree !== update.state.tree
|
|
267
|
+
)
|
|
268
|
+
this.decorations = build(update.view);
|
|
269
|
+
},
|
|
270
|
+
}),
|
|
271
|
+
{ decorations: (value) => value.decorations },
|
|
272
|
+
);
|
|
273
|
+
const compartment = new Compartment();
|
|
274
|
+
view.dispatch({
|
|
275
|
+
effects: StateEffect.appendConfig.of(compartment.of(Prec ? Prec.highest(plugin) : plugin)),
|
|
276
|
+
});
|
|
277
|
+
return () => {
|
|
278
|
+
if (!view.destroyed) view.dispatch({ effects: compartment.reconfigure([]) });
|
|
279
|
+
};
|
|
280
|
+
}
|