From 1e1f976eed1133c21a3adddbc7024d8a506b8ac9 Mon Sep 17 00:00:00 2001 From: "lithe-support[bot]" <356900028+lithe-support[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 07:38:34 +0000 Subject: [PATCH 1/3] fix(desktop): resolve standalone Markdown references per document --- .../2026-11-20-cli-and-standalone-editor.md | 5 + desktop/internal/workbench/markdown_images.go | 2 +- .../internal/workbench/markdown_preview.go | 2 +- desktop/internal/workbench/markdown_web.go | 2 +- desktop/internal/workbench/preview_pages.go | 10 ++ .../standalone_markdown_references_test.go | 155 ++++++++++++++++++ .../features/cli-single-file-mode.json | 10 +- .../features/markdown-rendering.json | 10 +- 8 files changed, 185 insertions(+), 11 deletions(-) create mode 100644 desktop/internal/workbench/standalone_markdown_references_test.go diff --git a/.agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md b/.agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md index 6e4230a9bf..df320aa79d 100644 --- a/.agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md +++ b/.agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md @@ -14,6 +14,7 @@ Lithe 默认不向用户的命令行环境注册命令。用户在设置的“ - Core(共享 Rust 规则层)解析启动参数、根据原生文件类型选择模式,并给出单文件模式允许的命令。桌面端只做路径观察、进程启动和界面呈现。单文件窗口借用既有文档与标签生命周期,不调用项目打开流程;父目录只是文件位置,不进行项目恢复、信任确认、递归搜索或插件上下文绑定。 - 单文件模式保留内置语法高亮、文本编辑、查找替换、保存、编码与换行选择和单个文档的外部变更观察。Markdown 文件与项目窗口一样显示渲染页(编辑 / 编辑与预览 / 预览切换和目录按钮,#2324):渲染页只读当前文档文本和它引用的图片,链接在本窗口打开文件,不需要项目服务。项目侧栏、Git、运行、调试、测试不呈现;SVG、HTML 文件只显示源码,图片、PDF 等查看器也不打开。终端默认关闭,由用户点击后才创建,使用当前文件所在目录,复用现有终端标签、分隔条及进程清理;隐藏面板保留会话,关闭标签或窗口释放 shell。单文件窗口不恢复或改写项目面板布局。双 Shift 和查找动作只列出允许的动作,不读取父目录。关闭全部文件后仍保持单文件空态。 +- 单文件 Markdown 没有工作区;调用 Core 的 `markdown.resolveReference` 时省略 `workspaceRoot`,让每个文档自己的目录决定图片读取范围以及 `/` 开头的图片、链接的基准。`p.root` 只是首个文件的父目录,不能用来解析随后打开的子目录或其他目录文档。原生图片、网页图片回调与点击链接使用同一上下文;项目窗口继续传项目根。 - 顶栏显示当前文件名与“单文件模式”,通过 ⓘ 解释边界。“以项目打开所在文件夹”使用当前选中文件的父目录,在另一窗口进入正常的项目信任和插件流程,保留原编辑窗口。 - CLI 卡片直接读取默认 shell 配置与启动器内容。macOS zsh 使用 `.zshrc`(尊重 `ZDOTDIR`),bash 使用实际登录配置,fish 使用自身配置;Windows 编辑当前用户注册表 PATH,保留其原生字符串类型,并有界广播环境变更。已有终端需重新打开。 - 打开页面只读,注册、修复和移除都来自明确按钮。Unix 来源使用带标记的配置块,Windows 只编辑匹配目录项;保留其他内容。写入前核对文件内容、来源目录和符号链接目标;读取后被外部改写则拒绝覆盖。Windows 注册表核对与单值写入间仍受外部进程竞争限制,不宣称跨进程事务。 @@ -25,6 +26,7 @@ Lithe 默认不向用户的命令行环境注册命令。用户在设置的“ - 安装或首次启动时自动写 PATH:违背默认不注册的要求,也无法让用户审阅修改目标。 - 在单文件窗口创建项目并禁用部分插件:项目管线仍会创建目录观察和恢复状态,无法保证任何插件都不激活。 +- 把首个单文件的父目录当成所有文档的工作区:打开子目录文档时会允许它读取父目录图片,并把 `/image.png` 指向首个文件的目录。按文档传正确上下文,复用 Core 的既有解析及授权规则;首目录外的文档原本已由 Core 回退到自身目录,继续保持该行为。 - 只注册全局系统目录中的符号链接:Windows 与 Unix 所需权限不同,还可能要求管理员权限。用户级启动器可以保持显式注册且不修改安装资源。 - 在当前窗口将单文件直接升级为项目:会混合两种服务生命周期,产生插件初始化与未保存文档处理的隐含转换。使用正常项目新窗口复用已有完整流程,代价是多一个窗口。 @@ -36,6 +38,7 @@ Lithe 默认不向用户的命令行环境注册命令。用户在设置的“ - `scripts/desktop/desktop-go.sh test ./cmd/lithe ./internal/cli ./internal/core ./internal/workbench -run 'TestCLI|TestStandalone|TestDesktopStartupMakesInstalledPluginSettingsReachable' -count=1 -timeout=90s`:链接同一 Core 检出,验证真实文件类型、共享 fixture、真实临时 shell 配置的注册/修复/移除/冲突保护和独立编辑保存。 - `scripts/desktop/desktop-go.sh test -tags 'vivahead lithevisual' ./internal/settingswindow ./internal/workbench -run 'TestVisualCLISettings|TestVisualStandalone' -count=1 -timeout=90s`:生成明暗主题、中英文的真实渲染截图。通过 `LITHE_VISUAL_OUTPUT_DIR` 指定本工作树的输出目录。 +- `TestStandaloneMarkdownImagesUseEachDocumentsFolder` 验证后续子目录及无关目录文档的本地图片、`/` 引用和越界图片;`TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle` 用真实 Core 验证网页图片回调与根路径链接,并检查单文件会话与关闭清理。网页视图替身只证明宿主接线,不替代 WKWebView/WebView2 原生验收。 - `scripts/desktop/verify/verify-runtime-bundle-immutability.sh`:检查两个桌面平台的发行目录只读边界。 - Windows 规则可以在 macOS 上以平台参数验证;注册表写入、环境广播与 cmd/PowerShell 调用仍需 Windows 原生环境验收,交叉编译不替代该验收。 @@ -45,5 +48,7 @@ Lithe 默认不向用户的命令行环境注册命令。用户在设置的“ - `desktop/internal/cli/` - `desktop/internal/core/cli.go` - `desktop/internal/workbench/standalone.go` +- `desktop/internal/workbench/preview_pages.go` +- `desktop/internal/workbench/standalone_markdown_references_test.go` - `desktop/internal/settingswindow/cli_view.go` - `shared/platform-feature-matrix/features/cli-single-file-mode.json` diff --git a/desktop/internal/workbench/markdown_images.go b/desktop/internal/workbench/markdown_images.go index f2e830dda6..5569c2a10d 100644 --- a/desktop/internal/workbench/markdown_images.go +++ b/desktop/internal/workbench/markdown_images.go @@ -61,7 +61,7 @@ func (a *App) markdownImageBitmap(p *project, d *document, s *markdownPreviewSta } image := &markdownImage{loading: true} s.images[destination] = image - path, root, remote := s.path, p.root, a.previewRemote() + path, root, remote := s.path, p.previewWorkspaceRoot(), a.previewRemote() a.background(p, markdownImageTimeout, func(ctx context.Context) func(*App) { bitmap, err := loadMarkdownImage(ctx, kernel, path, root, destination, remote) return func(a *App) { diff --git a/desktop/internal/workbench/markdown_preview.go b/desktop/internal/workbench/markdown_preview.go index 3960c2a794..5396c64b8b 100644 --- a/desktop/internal/workbench/markdown_preview.go +++ b/desktop/internal/workbench/markdown_preview.go @@ -600,7 +600,7 @@ func (a *App) followDocumentLink(p *project, path, destination string, anchor fu if !ok { return } - root := p.root + root := p.previewWorkspaceRoot() a.background(p, kernelCallTimeout, func(ctx context.Context) func(*App) { target, err := kernel.ResolveMarkdownReference(ctx, path, root, destination, core.MarkdownLink) if err != nil { diff --git a/desktop/internal/workbench/markdown_web.go b/desktop/internal/workbench/markdown_web.go index c5ac27f82d..735513d997 100644 --- a/desktop/internal/workbench/markdown_web.go +++ b/desktop/internal/workbench/markdown_web.go @@ -93,7 +93,7 @@ func (a *App) markdownWebPage(p *project, d *document, s *markdownPreviewState) } w := &markdownWeb{path: d.path, rendered: -1, rendering: -1, line: line} // The page asks for its images on goroutines of its own: what they need is copied here. - path, root := d.path, p.root + path, root := d.path, p.previewWorkspaceRoot() entry := a.openPreviewPage(webpreview.Config{ Page: webpreview.MarkdownPage, Resolve: func(ctx context.Context, reference string) (core.MarkdownReference, error) { diff --git a/desktop/internal/workbench/preview_pages.go b/desktop/internal/workbench/preview_pages.go index 3ab458b124..7ec6b8f331 100644 --- a/desktop/internal/workbench/preview_pages.go +++ b/desktop/internal/workbench/preview_pages.go @@ -88,6 +88,16 @@ type markdownKernel interface { ResolveMarkdownReference(ctx context.Context, documentPath, workspaceRoot, reference string, purpose core.MarkdownReferencePurpose) (core.MarkdownReference, error) } +// previewWorkspaceRoot is the workspace given to Core when resolving a document's references. +// A standalone session has no workspace: p.root is only the first file's folder. With no root, +// Core uses each document's own folder for its image scope and root-relative references. +func (p *project) previewWorkspaceRoot() string { + if p.standalone { + return "" + } + return p.root +} + // previewWeb reports whether pages can be opened: the web host is ready and no page failed to // open. func (a *App) previewWeb() bool { diff --git a/desktop/internal/workbench/standalone_markdown_references_test.go b/desktop/internal/workbench/standalone_markdown_references_test.go new file mode 100644 index 0000000000..ddae4a3715 --- /dev/null +++ b/desktop/internal/workbench/standalone_markdown_references_test.go @@ -0,0 +1,155 @@ +package workbench + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/1lck/Lithe-IDEA/desktop/internal/core" + "github.com/1lck/Lithe-IDEA/desktop/internal/store" + "github.com/1lck/Lithe-IDEA/desktop/internal/webpreview" +) + +func standaloneMarkdownReferenceHarness(t *testing.T, path string) *harness { + t.Helper() + prefs := store.At(t.TempDir()) + if err := prefs.Save(settingsFile, map[string]string{"theme": "dark"}); err != nil { + t.Fatal(err) + } + options := DesktopOptions(core.NewClient(), prefs, "test") + options.Languages, options.OpenFile = []string{"en-US"}, path + return newHarnessWithOptions(t, options) +} + +func TestStandaloneMarkdownImagesUseEachDocumentsFolder(t *testing.T) { + root := t.TempDir() + first := filepath.Join(root, "first.md") + if err := os.WriteFile(first, []byte("# First\n"), 0600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(root, "outside.png"), pngOf(t, 1, 1), 0600); err != nil { + t.Fatal(err) + } + nested := filepath.Join(root, "nested") + if err := os.Mkdir(nested, 0700); err != nil { + t.Fatal(err) + } + h := standaloneMarkdownReferenceHarness(t, first) + h.app.openStandaloneFile(first) + h.await("first standalone file", func() bool { return h.app.project != nil && h.app.project.docs.byPath(first) != nil }) + // The second file is inside the first file's parent; the third is elsewhere. + // Neither inherits the first document's directory as a workspace root. + for _, folder := range []string{nested, t.TempDir()} { + path := filepath.Join(folder, "README.md") + if err := os.WriteFile(filepath.Join(folder, "local.png"), pngOf(t, 4, 5), 0600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte("# Pictures\n\n![local](local.png)\n\n![root](/local.png)\n\n![escape](../outside.png)\n"), 0600); err != nil { + t.Fatal(err) + } + h.app.openFile(h.app.project, path) + h.await("another standalone Markdown", func() bool { return h.app.project.docs.byPath(path) != nil }) + h.settle() + d := h.app.project.docs.byPath(path) + if d.markdown == nil { + t.Fatal("standalone Markdown has no image preview") + } + s := d.markdown + h.await("standalone image results", func() bool { + if len(s.images) != 3 { + return false + } + for _, image := range s.images { + if image.loading { + return false + } + } + return true + }) + for _, ref := range []string{"local.png", "/local.png"} { + image := s.images[ref] + if image == nil || image.failed || image.bitmap.Width != 4 || image.bitmap.Height != 5 { + t.Errorf("image %q in %s: %+v", ref, folder, image) + } + } + if image := s.images["../outside.png"]; image == nil || !image.failed { + t.Errorf("standalone preview read an image outside %s: %+v", folder, image) + } + } +} + +func TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle(t *testing.T) { + root := t.TempDir() + first := filepath.Join(root, "first.md") + folder := filepath.Join(root, "nested") + if err := os.Mkdir(folder, 0700); err != nil { + t.Fatal(err) + } + path := filepath.Join(folder, "README.md") + for name, data := range map[string][]byte{ + first: []byte("# First\n"), + path: []byte("# Safe preview\n\n[Next](/next.md)\n"), + filepath.Join(folder, "next.md"): []byte("# Next\n"), + filepath.Join(root, "next.md"): []byte("# Wrong document folder\n"), + filepath.Join(folder, "local.png"): pngOf(t, 4, 5), + filepath.Join(root, "outside.png"): pngOf(t, 1, 1), + } { + if err := os.WriteFile(name, data, 0600); err != nil { + t.Fatal(err) + } + } + h := standaloneMarkdownReferenceHarness(t, first) + pages := fakePreviews(h) + h.app.openStandaloneFile(first) + h.await("first standalone file", func() bool { return h.app.project != nil && h.app.project.docs.byPath(first) != nil }) + p := h.app.project + h.app.openFile(p, path) + h.await("nested standalone file", func() bool { return p.docs.byPath(path) != nil }) + h.settle() + d := p.docs.byPath(path) + if d.markdown == nil || d.markdown.web == nil { + t.Fatal("standalone Markdown did not open its web preview") + } + s := d.markdown + page := s.web.entry.view.(*fakePreview) + if page.config.Page != webpreview.MarkdownPage { + t.Fatal("standalone Markdown opened a site instead of the sanitized page") + } + h.await("Core Markdown HTML", func() bool { return s.web.html != "" }) + ctx, cancel := context.WithTimeout(context.Background(), kernelCallTimeout) + defer cancel() + for _, ref := range []string{"local.png", "/local.png"} { + resolved, err := page.config.Resolve(ctx, ref) + if err != nil || resolved.Kind != "file" || filepath.Base(resolved.Path) != "local.png" { + t.Errorf("web image %q: %+v, %v", ref, resolved, err) + } + } + for _, ref := range []string{"../outside.png", "file:///outside.png", "javascript:alert(1)"} { + resolved, err := page.config.Resolve(ctx, ref) + if err != nil || resolved.Kind != "none" { + t.Errorf("web image %q escaped its scope: %+v, %v", ref, resolved, err) + } + } + page.load() + page.config.Follow("/next.md") + next := filepath.Join(folder, "next.md") + wrong := filepath.Join(root, "next.md") + h.await("linked standalone file", func() bool { return p.docs.byPath(next) != nil || p.docs.byPath(wrong) != nil }) + if p.docs.byPath(next) == nil { + t.Errorf("root-relative link opened %s instead of %s", wrong, next) + } + if h.app.project != p || !p.standalone || h.app.plugins != nil || p.search.watch != nil || p.git.isRepository() { + t.Fatal("following a Markdown link opened a project") + } + h.app.closeDocument(p, d.id) + h.await("standalone Markdown tab close", func() bool { return p.docs.find(d.id) == nil && page.closed }) + h.app.closeProject() + h.await("standalone session close", func() bool { return h.app.project == nil }) + h.settle() + for _, page := range *pages { + if !page.closed { + t.Fatal("closing the standalone session kept a web preview alive") + } + } +} diff --git a/shared/platform-feature-matrix/features/cli-single-file-mode.json b/shared/platform-feature-matrix/features/cli-single-file-mode.json index 39de5e0e3f..027a140521 100644 --- a/shared/platform-feature-matrix/features/cli-single-file-mode.json +++ b/shared/platform-feature-matrix/features/cli-single-file-mode.json @@ -4,8 +4,8 @@ "group": "CLI", "capability": "可选注册 lithe 命令,按文件夹/单文件分别打开项目与轻量编辑器", "owner": "Desktop", - "verification": "macOS:打开通用 > CLI 时不写来源;点击注册后新终端能使用 lithe,分别传入带空格/中文文件、文件夹和 -- 保护的路径;单文件只使用内置高亮,能编辑保存,默认不创建终端,无插件、项目树、Git、运行/调试/测试或父目录索引,查找动作不搜索工作区;打开 Markdown 文件时标签栏有编辑 / 编辑与预览 / 预览切换和目录按钮,渲染页跟随编辑并显示相对路径图片,SVG、HTML 仍只显示源码。点击终端后使用当前文件目录,真实命令输出可见,隐藏后重开复用会话,关闭窗口清理 shell。以项目打开所在文件夹在新窗口走正常信任和插件流程。修复应用移动后的启动器;移除注册保留无关 shell 内容。Windows:检查 HKCU Environment Path 类型与无关项保留、外部修改冲突、环境广播、新 cmd/PowerShell 中调用及移除,原生运行待验收。", - "notes": "已在 macOS 执行真实临时配置注册、修复、移除和冲突保护测试,并验证单文件真实编辑保存、真实 PTY 命令及中英/明暗无头截图;Markdown 渲染页与视图切换(#2324)有无头回归和截图,原生网页引擎下的渲染页待两端验收。尚未通过真实系统终端完成注册到 GUI 的端到端验收。Windows 无本地来宾,规则测试及 Windows 目标编译通过,不代表原生验收完成。", + "verification": "macOS:打开通用 > CLI 时不写来源;点击注册后新终端能使用 lithe,分别传入带空格/中文文件、文件夹和 -- 保护的路径;单文件只使用内置高亮,能编辑保存,默认不创建终端,无插件、项目树、Git、运行/调试/测试或父目录索引,查找动作不搜索工作区;打开 Markdown 文件时标签栏有编辑 / 编辑与预览 / 预览切换和目录按钮,渲染页跟随编辑并显示相对路径图片,SVG、HTML 仍只显示源码。点击终端后使用当前文件目录,真实命令输出可见,隐藏后重开复用会话,关闭窗口清理 shell。以项目打开所在文件夹在新窗口走正常信任和插件流程。修复应用移动后的启动器;移除注册保留无关 shell 内容。Windows:检查 HKCU Environment Path 类型与无关项保留、外部修改冲突、环境广播、新 cmd/PowerShell 中调用及移除,原生运行待验收。 单文件多文档引用:先打开根目录 Markdown,再在同一窗口打开其子目录及其他目录的 Markdown;各文档的相对图片和 /local.png 应按自己的目录加载,../outside.png 不得读取文档目录外的图片,/next.md 应打开当前文档目录里的文件,保持单文件会话。运行 TestStandaloneMarkdownImagesUseEachDocumentsFolder、TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle;两端原生网页引擎仍待验收。", + "notes": "已在 macOS 执行真实临时配置注册、修复、移除和冲突保护测试,并验证单文件真实编辑保存、真实 PTY 命令及中英/明暗无头截图;Markdown 渲染页与视图切换(#2324)有无头回归和截图,原生网页引擎下的渲染页待两端验收。尚未通过真实系统终端完成注册到 GUI 的端到端验收。Windows 无本地来宾,规则测试及 Windows 目标编译通过,不代表原生验收完成。 #2334 已恢复单文件 Markdown 的预览入口;#2324 后续补全将原生图片、网页图片与链接解析的 workspaceRoot 留空,由 Core 按每个文档的目录解析引用。单文件会话的 p.root 不是工作区,后续文档不能继承首个文件的图片读取范围。项目窗口继续传项目根;本次不改变 Core 规则或两端 pending 的验收状态。", "macos": { "implementationStatus": "implemented", "verificationStatus": "pending", @@ -22,7 +22,8 @@ "desktop/internal/settingswindow/cli_visual_test.go", ".agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md", "desktop/internal/cli/source_unix.go", - "desktop/internal/cli/registration_unix_test.go" + "desktop/internal/cli/registration_unix_test.go", + "desktop/internal/workbench/standalone_markdown_references_test.go" ] }, "windows": { @@ -41,7 +42,8 @@ "desktop/internal/settingswindow/cli_visual_test.go", ".agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md", "desktop/internal/cli/source_windows.go", - "desktop/cmd/lithe/detach_windows.go" + "desktop/cmd/lithe/detach_windows.go", + "desktop/internal/workbench/standalone_markdown_references_test.go" ] } } diff --git a/shared/platform-feature-matrix/features/markdown-rendering.json b/shared/platform-feature-matrix/features/markdown-rendering.json index 8682059005..0aebeafc3b 100644 --- a/shared/platform-feature-matrix/features/markdown-rendering.json +++ b/shared/platform-feature-matrix/features/markdown-rendering.json @@ -18,7 +18,8 @@ "desktop/internal/workbench/markdown_preview_test.go", "desktop/internal/workbench/markdown_web_test.go", "desktop/internal/webpreview/webpreview_test.go", - "desktop/internal/core/markdown_test.go" + "desktop/internal/core/markdown_test.go", + "desktop/internal/workbench/standalone_markdown_references_test.go" ] }, "windows": { @@ -36,12 +37,13 @@ "desktop/internal/workbench/markdown_preview_test.go", "desktop/internal/workbench/markdown_web_test.go", "desktop/internal/webpreview/webpreview_test.go", - "desktop/internal/core/markdown_test.go" + "desktop/internal/core/markdown_test.go", + "desktop/internal/workbench/standalone_markdown_references_test.go" ] }, "owner": "Editor", - "verification": "Windows 使用 markdown-render-v1.json 验证嵌套列表、任务列表、代码空白、中文、emoji、CRLF、front matter 隐藏及危险 HTML 过滤;修改、清空及关闭预览时不得被旧异步结果覆盖,失败显示提示且后续编辑可恢复。使用代码高亮、表格、Mermaid、链接和相对路径 fixture 对比渲染结果。打开含上千个代码块的大型 Markdown 预览,渲染不卡住,编辑时预览不会先闪成未高亮的代码;代码里的 `$&`、`$1` 按原文显示。macOS 在深浅主题运行 markdown-rich-content-v1.json:行内数学、块级化学反应、物理单位均生成可访问 MathML,没有 katex-error;Mermaid 生成真实 SVG,脚注链接及回链存在;H~2~O 与 x^2^ 正确,双波浪线仍是删除线,代码内语法保持原文。运行前后逐文件比较预览资源哈希,确认无网络脚本和远程资源请求、bundle 不变。macOS 使用 markdown-html-v1.json 及仓库 README 验证:宽预览中同一段内多个徽章同行居中,窄分栏按可用宽度自然折行;div/p/h1 的 align 生效,显式 br 和普通段落保留,正文行内图片不拆行,相对图片与点击放大仍可用。深浅主题均检查,脚本、事件处理器、危险 URL 和任意 style 仍被剥离。 macOS PlantUML:在深浅主题打开时序图和类图 fenced block,离线生成可放大的 SVG 图片,不能在文档 DOM 执行 SVG;多个代码块顺序执行,切换文档拒绝旧结果;本地 include 被拒绝且保留源码与本地化提示,超大输入立即失败,关闭和取消释放渲染器;运行前后比较预览资源 SHA-256。 GitHub 预览保留 Core 的硬换行与代码空白;Notebook 共享队列最多同时执行两个渲染,关闭和替换内容会取消未开始请求。 Viva 新桌面端:在深浅主题打开仓库 README 与 markdown-html-v1.json、markdown-rich-content-v1.json 的输入,确认内嵌 HTML(徽章同行、align、details、br)、内嵌 SVG、KaTeX 数学与 mhchem、Mermaid、markmap、代码高亮、脚注和上下标都显示在 web view 页里,脚本、事件处理器、危险 URL 和任意 style 仍被 Core 剥离;页面只能访问自身 lithe-preview 源,所有脚本与字体随应用内嵌,无网络请求;源码与页面按源码行双向同步滚动,切换模式保持位置;超过 1 MiB 显示过大提示。Linux 无 web 引擎时回落到 Viva 原生渲染。自动回归:scripts/desktop/desktop-go.sh test ./internal/workbench ./internal/webpreview -run 'Markdown|Preview|Serves|EveryPage' -count=1。", - "notes": "Viva:有系统 web 引擎(macOS WKWebView、Windows WebView2)时,Markdown 页显示在 web view 中:Core markdown.render 生成过滤后的 HTML(放行内嵌 SVG 白名单),页面用内嵌的 KaTeX 0.16.0 与 mhchem、Highlight.js、Mermaid 11、markmap 渲染富内容,只在文档用到时加载;链接交回宿主按 Core 的解析结果跳转,图片按 Core 解析结果提供。每次编辑后重新渲染,同一时间只有一次渲染。Linux 没有 web 引擎,仍用 Viva 的 Markdown 组件渲染(不支持 HTML 和富内容)。标签栏末尾三态切换“编辑 / 编辑与预览 / 预览”,默认编辑与预览,超过 1 MiB 不渲染。PlantUML 尚未接入。", + "verification": "Windows 使用 markdown-render-v1.json 验证嵌套列表、任务列表、代码空白、中文、emoji、CRLF、front matter 隐藏及危险 HTML 过滤;修改、清空及关闭预览时不得被旧异步结果覆盖,失败显示提示且后续编辑可恢复。使用代码高亮、表格、Mermaid、链接和相对路径 fixture 对比渲染结果。打开含上千个代码块的大型 Markdown 预览,渲染不卡住,编辑时预览不会先闪成未高亮的代码;代码里的 `$&`、`$1` 按原文显示。macOS 在深浅主题运行 markdown-rich-content-v1.json:行内数学、块级化学反应、物理单位均生成可访问 MathML,没有 katex-error;Mermaid 生成真实 SVG,脚注链接及回链存在;H~2~O 与 x^2^ 正确,双波浪线仍是删除线,代码内语法保持原文。运行前后逐文件比较预览资源哈希,确认无网络脚本和远程资源请求、bundle 不变。macOS 使用 markdown-html-v1.json 及仓库 README 验证:宽预览中同一段内多个徽章同行居中,窄分栏按可用宽度自然折行;div/p/h1 的 align 生效,显式 br 和普通段落保留,正文行内图片不拆行,相对图片与点击放大仍可用。深浅主题均检查,脚本、事件处理器、危险 URL 和任意 style 仍被剥离。 macOS PlantUML:在深浅主题打开时序图和类图 fenced block,离线生成可放大的 SVG 图片,不能在文档 DOM 执行 SVG;多个代码块顺序执行,切换文档拒绝旧结果;本地 include 被拒绝且保留源码与本地化提示,超大输入立即失败,关闭和取消释放渲染器;运行前后比较预览资源 SHA-256。 GitHub 预览保留 Core 的硬换行与代码空白;Notebook 共享队列最多同时执行两个渲染,关闭和替换内容会取消未开始请求。 Viva 新桌面端:在深浅主题打开仓库 README 与 markdown-html-v1.json、markdown-rich-content-v1.json 的输入,确认内嵌 HTML(徽章同行、align、details、br)、内嵌 SVG、KaTeX 数学与 mhchem、Mermaid、markmap、代码高亮、脚注和上下标都显示在 web view 页里,脚本、事件处理器、危险 URL 和任意 style 仍被 Core 剥离;页面只能访问自身 lithe-preview 源,所有脚本与字体随应用内嵌,无网络请求;源码与页面按源码行双向同步滚动,切换模式保持位置;超过 1 MiB 显示过大提示。Linux 无 web 引擎时回落到 Viva 原生渲染。自动回归:scripts/desktop/desktop-go.sh test ./internal/workbench ./internal/webpreview -run 'Markdown|Preview|Serves|EveryPage' -count=1。 单文件多文档引用:先打开根目录 Markdown,再在同一窗口打开其子目录及其他目录的 Markdown;各文档的相对图片和 /local.png 应按自己的目录加载,../outside.png 不得读取文档目录外的图片,/next.md 应打开当前文档目录里的文件,保持单文件会话。运行 TestStandaloneMarkdownImagesUseEachDocumentsFolder、TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle;两端原生网页引擎仍待验收。", + "notes": "Viva:有系统 web 引擎(macOS WKWebView、Windows WebView2)时,Markdown 页显示在 web view 中:Core markdown.render 生成过滤后的 HTML(放行内嵌 SVG 白名单),页面用内嵌的 KaTeX 0.16.0 与 mhchem、Highlight.js、Mermaid 11、markmap 渲染富内容,只在文档用到时加载;链接交回宿主按 Core 的解析结果跳转,图片按 Core 解析结果提供。每次编辑后重新渲染,同一时间只有一次渲染。Linux 没有 web 引擎,仍用 Viva 的 Markdown 组件渲染(不支持 HTML 和富内容)。标签栏末尾三态切换“编辑 / 编辑与预览 / 预览”,默认编辑与预览,超过 1 MiB 不渲染。PlantUML 尚未接入。 #2334 已恢复单文件 Markdown 的预览入口;#2324 后续补全将原生图片、网页图片与链接解析的 workspaceRoot 留空,由 Core 按每个文档的目录解析引用。单文件会话的 p.root 不是工作区,后续文档不能继承首个文件的图片读取范围。项目窗口继续传项目根;本次不改变 Core 规则或两端 pending 的验收状态。", "legacy": { "revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36", "macos": { From 0b4d3ce3763a9a02c6bef1d4da3b73ef6f15dc4c Mon Sep 17 00:00:00 2001 From: lick <2188718831@qq.com> Date: Mon, 23 Nov 2026 19:11:50 -0800 Subject: [PATCH 2/3] test(desktop): harden standalone Markdown reference audit and nil guard --- .../2026-11-20-cli-and-standalone-editor.md | 4 +- desktop/internal/workbench/preview_pages.go | 2 +- .../standalone_markdown_references_test.go | 236 ++++++++++++++++-- .../features/cli-single-file-mode.json | 4 +- .../features/markdown-rendering.json | 2 +- 5 files changed, 222 insertions(+), 26 deletions(-) diff --git a/.agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md b/.agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md index df320aa79d..4adeedd607 100644 --- a/.agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md +++ b/.agents/notes/implemented/feature/2026-11-20-cli-and-standalone-editor.md @@ -14,7 +14,7 @@ Lithe 默认不向用户的命令行环境注册命令。用户在设置的“ - Core(共享 Rust 规则层)解析启动参数、根据原生文件类型选择模式,并给出单文件模式允许的命令。桌面端只做路径观察、进程启动和界面呈现。单文件窗口借用既有文档与标签生命周期,不调用项目打开流程;父目录只是文件位置,不进行项目恢复、信任确认、递归搜索或插件上下文绑定。 - 单文件模式保留内置语法高亮、文本编辑、查找替换、保存、编码与换行选择和单个文档的外部变更观察。Markdown 文件与项目窗口一样显示渲染页(编辑 / 编辑与预览 / 预览切换和目录按钮,#2324):渲染页只读当前文档文本和它引用的图片,链接在本窗口打开文件,不需要项目服务。项目侧栏、Git、运行、调试、测试不呈现;SVG、HTML 文件只显示源码,图片、PDF 等查看器也不打开。终端默认关闭,由用户点击后才创建,使用当前文件所在目录,复用现有终端标签、分隔条及进程清理;隐藏面板保留会话,关闭标签或窗口释放 shell。单文件窗口不恢复或改写项目面板布局。双 Shift 和查找动作只列出允许的动作,不读取父目录。关闭全部文件后仍保持单文件空态。 -- 单文件 Markdown 没有工作区;调用 Core 的 `markdown.resolveReference` 时省略 `workspaceRoot`,让每个文档自己的目录决定图片读取范围以及 `/` 开头的图片、链接的基准。`p.root` 只是首个文件的父目录,不能用来解析随后打开的子目录或其他目录文档。原生图片、网页图片回调与点击链接使用同一上下文;项目窗口继续传项目根。 +- 单文件 Markdown 没有工作区;调用 Core 的引用解析接口(`markdown.resolveReference`)时省略工作区根目录参数(`workspaceRoot`),让每个文档自己的目录决定图片读取范围以及 `/` 开头的图片、链接的基准。单文件会话记录的首个文件父目录(`p.root`)只是窗口起点,不能当成随后打开的子目录或其他目录文档的工作区。原生图片、网页图片回调与点击链接使用同一上下文;项目窗口继续传项目根。 - 顶栏显示当前文件名与“单文件模式”,通过 ⓘ 解释边界。“以项目打开所在文件夹”使用当前选中文件的父目录,在另一窗口进入正常的项目信任和插件流程,保留原编辑窗口。 - CLI 卡片直接读取默认 shell 配置与启动器内容。macOS zsh 使用 `.zshrc`(尊重 `ZDOTDIR`),bash 使用实际登录配置,fish 使用自身配置;Windows 编辑当前用户注册表 PATH,保留其原生字符串类型,并有界广播环境变更。已有终端需重新打开。 - 打开页面只读,注册、修复和移除都来自明确按钮。Unix 来源使用带标记的配置块,Windows 只编辑匹配目录项;保留其他内容。写入前核对文件内容、来源目录和符号链接目标;读取后被外部改写则拒绝覆盖。Windows 注册表核对与单值写入间仍受外部进程竞争限制,不宣称跨进程事务。 @@ -38,7 +38,7 @@ Lithe 默认不向用户的命令行环境注册命令。用户在设置的“ - `scripts/desktop/desktop-go.sh test ./cmd/lithe ./internal/cli ./internal/core ./internal/workbench -run 'TestCLI|TestStandalone|TestDesktopStartupMakesInstalledPluginSettingsReachable' -count=1 -timeout=90s`:链接同一 Core 检出,验证真实文件类型、共享 fixture、真实临时 shell 配置的注册/修复/移除/冲突保护和独立编辑保存。 - `scripts/desktop/desktop-go.sh test -tags 'vivahead lithevisual' ./internal/settingswindow ./internal/workbench -run 'TestVisualCLISettings|TestVisualStandalone' -count=1 -timeout=90s`:生成明暗主题、中英文的真实渲染截图。通过 `LITHE_VISUAL_OUTPUT_DIR` 指定本工作树的输出目录。 -- `TestStandaloneMarkdownImagesUseEachDocumentsFolder` 验证后续子目录及无关目录文档的本地图片、`/` 引用和越界图片;`TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle` 用真实 Core 验证网页图片回调与根路径链接,并检查单文件会话与关闭清理。网页视图替身只证明宿主接线,不替代 WKWebView/WebView2 原生验收。 +- `TestStandaloneMarkdownImagesUseEachDocumentsFolder`、`TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle`、`TestProjectMarkdownKeepsWorkspaceRootForNestedDocuments` 与 `TestPreviewWorkspaceRootReturnsEmptyOnlyForNilOrStandalone` 验证后续子目录及无关目录文档的本地图片、`/` 引用、符号链接与父目录越界图片、链接跟随及项目模式对照;网页视图替身只证明宿主接线,不替代 WKWebView/WebView2 原生验收。 - `scripts/desktop/verify/verify-runtime-bundle-immutability.sh`:检查两个桌面平台的发行目录只读边界。 - Windows 规则可以在 macOS 上以平台参数验证;注册表写入、环境广播与 cmd/PowerShell 调用仍需 Windows 原生环境验收,交叉编译不替代该验收。 diff --git a/desktop/internal/workbench/preview_pages.go b/desktop/internal/workbench/preview_pages.go index 7ec6b8f331..07c64fdab3 100644 --- a/desktop/internal/workbench/preview_pages.go +++ b/desktop/internal/workbench/preview_pages.go @@ -92,7 +92,7 @@ type markdownKernel interface { // A standalone session has no workspace: p.root is only the first file's folder. With no root, // Core uses each document's own folder for its image scope and root-relative references. func (p *project) previewWorkspaceRoot() string { - if p.standalone { + if p == nil || p.standalone { return "" } return p.root diff --git a/desktop/internal/workbench/standalone_markdown_references_test.go b/desktop/internal/workbench/standalone_markdown_references_test.go index ddae4a3715..f6bf48d719 100644 --- a/desktop/internal/workbench/standalone_markdown_references_test.go +++ b/desktop/internal/workbench/standalone_markdown_references_test.go @@ -1,9 +1,11 @@ package workbench import ( + "bytes" "context" "os" "path/filepath" + "runtime" "testing" "github.com/1lck/Lithe-IDEA/desktop/internal/core" @@ -19,16 +21,46 @@ func standaloneMarkdownReferenceHarness(t *testing.T, path string) *harness { } options := DesktopOptions(core.NewClient(), prefs, "test") options.Languages, options.OpenFile = []string{"en-US"}, path + options.PluginRoot = filepath.Join(filepath.Dir(path), "unread-plugins") return newHarnessWithOptions(t, options) } +func canonicalTestPath(t *testing.T, path string) string { + t.Helper() + resolved, err := filepath.EvalSymlinks(path) + if err != nil { + t.Fatal(err) + } + return resolved +} + +func TestPreviewWorkspaceRootReturnsEmptyOnlyForNilOrStandalone(t *testing.T) { + var nilProject *project + if got := nilProject.previewWorkspaceRoot(); got != "" { + t.Fatalf("nil project workspace root = %q, want empty", got) + } + if got := (&project{standalone: true, root: "/tmp/first"}).previewWorkspaceRoot(); got != "" { + t.Fatalf("standalone workspace root = %q, want empty", got) + } + want := filepath.Join(t.TempDir(), "workspace") + if got := (&project{root: want}).previewWorkspaceRoot(); got != want { + t.Fatalf("project workspace root = %q, want %q", got, want) + } +} + func TestStandaloneMarkdownImagesUseEachDocumentsFolder(t *testing.T) { root := t.TempDir() first := filepath.Join(root, "first.md") - if err := os.WriteFile(first, []byte("# First\n"), 0600); err != nil { + if err := os.WriteFile(first, []byte("# First\n\n![local](local.png)\n\n![root](/local.png)\n"), 0600); err != nil { + t.Fatal(err) + } + // Put local.png in the first file's folder with a distinct size (1x1) so nested + // documents would load the wrong image if p.root leaked as workspaceRoot. + if err := os.WriteFile(filepath.Join(root, "local.png"), pngOf(t, 1, 1), 0600); err != nil { t.Fatal(err) } - if err := os.WriteFile(filepath.Join(root, "outside.png"), pngOf(t, 1, 1), 0600); err != nil { + outside := filepath.Join(root, "outside.png") + if err := os.WriteFile(outside, pngOf(t, 1, 1), 0600); err != nil { t.Fatal(err) } nested := filepath.Join(root, "nested") @@ -38,14 +70,55 @@ func TestStandaloneMarkdownImagesUseEachDocumentsFolder(t *testing.T) { h := standaloneMarkdownReferenceHarness(t, first) h.app.openStandaloneFile(first) h.await("first standalone file", func() bool { return h.app.project != nil && h.app.project.docs.byPath(first) != nil }) + h.settle() + firstDoc := h.app.project.docs.byPath(first) + if firstDoc == nil || firstDoc.markdown == nil { + t.Fatal("first standalone Markdown has no image preview") + } + h.await("first standalone images", func() bool { + if len(firstDoc.markdown.images) != 2 { + return false + } + for _, image := range firstDoc.markdown.images { + if image.loading { + return false + } + } + return true + }) + for _, ref := range []string{"local.png", "/local.png"} { + image := firstDoc.markdown.images[ref] + if image == nil || image.failed || image.bitmap.Width != 1 || image.bitmap.Height != 1 { + t.Errorf("first image %q: %+v", ref, image) + } + } + // The second file is inside the first file's parent; the third is elsewhere. // Neither inherits the first document's directory as a workspace root. for _, folder := range []string{nested, t.TempDir()} { - path := filepath.Join(folder, "README.md") + if err := os.MkdirAll(filepath.Join(folder, "img"), 0700); err != nil { + t.Fatal(err) + } if err := os.WriteFile(filepath.Join(folder, "local.png"), pngOf(t, 4, 5), 0600); err != nil { t.Fatal(err) } - if err := os.WriteFile(path, []byte("# Pictures\n\n![local](local.png)\n\n![root](/local.png)\n\n![escape](../outside.png)\n"), 0600); err != nil { + if err := os.WriteFile(filepath.Join(folder, "img", "sub.png"), pngOf(t, 2, 3), 0600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(folder, "next.md"), []byte("# Next\n"), 0600); err != nil { + t.Fatal(err) + } + source := "# Pictures\n\n![local](local.png)\n\n![root](/local.png)\n\n![sub](img/sub.png)\n\n![parent](../local.png)\n\n![escape](../outside.png)\n" + wantImages := 5 + if runtime.GOOS != "windows" { + if err := os.Symlink(outside, filepath.Join(folder, "sym-outside.png")); err != nil { + t.Fatal(err) + } + source += "\n![sym](sym-outside.png)\n" + wantImages++ + } + path := filepath.Join(folder, "README.md") + if err := os.WriteFile(path, []byte(source), 0600); err != nil { t.Fatal(err) } h.app.openFile(h.app.project, path) @@ -57,7 +130,7 @@ func TestStandaloneMarkdownImagesUseEachDocumentsFolder(t *testing.T) { } s := d.markdown h.await("standalone image results", func() bool { - if len(s.images) != 3 { + if len(s.images) != wantImages { return false } for _, image := range s.images { @@ -73,8 +146,26 @@ func TestStandaloneMarkdownImagesUseEachDocumentsFolder(t *testing.T) { t.Errorf("image %q in %s: %+v", ref, folder, image) } } - if image := s.images["../outside.png"]; image == nil || !image.failed { - t.Errorf("standalone preview read an image outside %s: %+v", folder, image) + if sub := s.images["img/sub.png"]; sub == nil || sub.failed || sub.bitmap.Width != 2 || sub.bitmap.Height != 3 { + t.Errorf("subfolder image in %s: %+v", folder, sub) + } + for _, denied := range []string{"../local.png", "../outside.png"} { + if image := s.images[denied]; image == nil || !image.failed { + t.Errorf("standalone preview read %q outside %s: %+v", denied, folder, image) + } + } + if runtime.GOOS != "windows" { + if image := s.images["sym-outside.png"]; image == nil || !image.failed { + t.Errorf("standalone preview followed symlink outside %s: %+v", folder, image) + } + } + // Native preview links also resolve `/...` from the active document's folder. + h.app.followMarkdownLink(h.app.project, d, s, "/next.md") + next := filepath.Join(folder, "next.md") + h.await("native root-relative link", func() bool { return h.app.project.docs.byPath(next) != nil }) + p := h.app.project + if !p.standalone || h.app.plugins != nil || p.search.watch != nil || p.git.isRepository() { + t.Fatal("standalone preview activated project services") } } } @@ -86,14 +177,23 @@ func TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle(t *testing.T) { if err := os.Mkdir(folder, 0700); err != nil { t.Fatal(err) } + otherDir := t.TempDir() path := filepath.Join(folder, "README.md") + other := filepath.Join(otherDir, "other.md") + nestedPNG := pngOf(t, 4, 5) + otherPNG := pngOf(t, 3, 3) for name, data := range map[string][]byte{ - first: []byte("# First\n"), - path: []byte("# Safe preview\n\n[Next](/next.md)\n"), - filepath.Join(folder, "next.md"): []byte("# Next\n"), - filepath.Join(root, "next.md"): []byte("# Wrong document folder\n"), - filepath.Join(folder, "local.png"): pngOf(t, 4, 5), - filepath.Join(root, "outside.png"): pngOf(t, 1, 1), + first: []byte("# First\n\n[Nested](nested/README.md)\n"), + filepath.Join(root, "local.png"): pngOf(t, 1, 1), + filepath.Join(root, "outside.png"): pngOf(t, 1, 1), + filepath.Join(root, "next.md"): []byte("# Wrong document folder\n"), + path: []byte("# Safe preview\n\n[Next](/next.md)\n"), + filepath.Join(folder, "next.md"): []byte("# Next\n"), + filepath.Join(folder, "sub.md"): []byte("# Sub\n"), + filepath.Join(folder, "local.png"): nestedPNG, + other: []byte("# Other\n\n[OtherNext](/other_next.md)\n"), + filepath.Join(otherDir, "other_next.md"): []byte("# Other Next\n"), + filepath.Join(otherDir, "local.png"): otherPNG, } { if err := os.WriteFile(name, data, 0600); err != nil { t.Fatal(err) @@ -103,8 +203,17 @@ func TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle(t *testing.T) { pages := fakePreviews(h) h.app.openStandaloneFile(first) h.await("first standalone file", func() bool { return h.app.project != nil && h.app.project.docs.byPath(first) != nil }) + h.settle() p := h.app.project - h.app.openFile(p, path) + firstDoc := p.docs.byPath(first) + if firstDoc.markdown == nil || firstDoc.markdown.web == nil { + t.Fatal("first standalone Markdown did not open its web preview") + } + firstPage := firstDoc.markdown.web.entry.view.(*fakePreview) + firstPage.load() + // Follow a link from the first document into a nested folder; it must stay standalone and + // resolve subsequent references against the nested document's own folder. + firstPage.config.OnMessage(webpreview.Message{Type: "link", Href: "nested/README.md"}) h.await("nested standalone file", func() bool { return p.docs.byPath(path) != nil }) h.settle() d := p.docs.byPath(path) @@ -119,28 +228,56 @@ func TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle(t *testing.T) { h.await("Core Markdown HTML", func() bool { return s.web.html != "" }) ctx, cancel := context.WithTimeout(context.Background(), kernelCallTimeout) defer cancel() + wantNestedPNG := canonicalTestPath(t, filepath.Join(folder, "local.png")) for _, ref := range []string{"local.png", "/local.png"} { resolved, err := page.config.Resolve(ctx, ref) - if err != nil || resolved.Kind != "file" || filepath.Base(resolved.Path) != "local.png" { - t.Errorf("web image %q: %+v, %v", ref, resolved, err) + if err != nil || resolved.Kind != "file" || resolved.Path != wantNestedPNG || resolved.MIME != "image/png" { + t.Errorf("web image %q: %+v, %v (want %s)", ref, resolved, err, wantNestedPNG) + } + loaded, mime, err := webpreview.LoadResource(ctx, resolved, nil) + if err != nil || mime != "image/png" || !bytes.Equal(loaded, nestedPNG) { + t.Errorf("load resource %q: len=%d mime=%q err=%v", ref, len(loaded), mime, err) } } - for _, ref := range []string{"../outside.png", "file:///outside.png", "javascript:alert(1)"} { + for _, ref := range []string{"../local.png", "../outside.png", "file:///outside.png", "javascript:alert(1)", "data:image/png;base64,AA==", "C:/local.png"} { resolved, err := page.config.Resolve(ctx, ref) if err != nil || resolved.Kind != "none" { t.Errorf("web image %q escaped its scope: %+v, %v", ref, resolved, err) } } page.load() - page.config.Follow("/next.md") + page.config.OnMessage(webpreview.Message{Type: "link", Href: "/next.md"}) next := filepath.Join(folder, "next.md") wrong := filepath.Join(root, "next.md") h.await("linked standalone file", func() bool { return p.docs.byPath(next) != nil || p.docs.byPath(wrong) != nil }) - if p.docs.byPath(next) == nil { - t.Errorf("root-relative link opened %s instead of %s", wrong, next) + if p.docs.byPath(next) == nil || p.docs.byPath(wrong) != nil { + t.Errorf("root-relative link opened wrong=%v next=%v", p.docs.byPath(wrong) != nil, p.docs.byPath(next) != nil) + } + page.config.Follow("sub.md") + sub := filepath.Join(folder, "sub.md") + h.await("relative follow link", func() bool { return p.docs.byPath(sub) != nil }) + + // Opening another Markdown outside the first folder keeps its own folder as scope too. + h.app.openFile(p, other) + h.await("unrelated standalone file", func() bool { return p.docs.byPath(other) != nil }) + h.settle() + otherDoc := p.docs.byPath(other) + if otherDoc.markdown == nil || otherDoc.markdown.web == nil { + t.Fatal("unrelated standalone Markdown did not open its web preview") + } + otherPage := otherDoc.markdown.web.entry.view.(*fakePreview) + resolvedOther, err := otherPage.config.Resolve(ctx, "/local.png") + wantOtherPNG := canonicalTestPath(t, filepath.Join(otherDir, "local.png")) + if err != nil || resolvedOther.Kind != "file" || resolvedOther.Path != wantOtherPNG { + t.Errorf("unrelated web image /local.png: %+v, %v (want %s)", resolvedOther, err, wantOtherPNG) } + otherPage.load() + otherPage.config.Follow("/other_next.md") + otherNext := filepath.Join(otherDir, "other_next.md") + h.await("unrelated linked standalone file", func() bool { return p.docs.byPath(otherNext) != nil }) + if h.app.project != p || !p.standalone || h.app.plugins != nil || p.search.watch != nil || p.git.isRepository() { - t.Fatal("following a Markdown link opened a project") + t.Fatal("following Markdown links opened a project") } h.app.closeDocument(p, d.id) h.await("standalone Markdown tab close", func() bool { return p.docs.find(d.id) == nil && page.closed }) @@ -153,3 +290,60 @@ func TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle(t *testing.T) { } } } + +func TestProjectMarkdownKeepsWorkspaceRootForNestedDocuments(t *testing.T) { + outside := t.TempDir() + root := filepath.Join(outside, "work") + for _, dir := range []string{filepath.Join(root, "assets"), filepath.Join(root, "nested")} { + if err := os.MkdirAll(dir, 0700); err != nil { + t.Fatal(err) + } + } + readme := filepath.Join(root, "README.md") + nested := filepath.Join(root, "nested", "guide.md") + logo := filepath.Join(root, "assets", "logo.png") + for name, data := range map[string][]byte{ + readme: []byte("# Root\n"), + nested: []byte("# Nested\n\n![root](/assets/logo.png)\n\n![parent](../assets/logo.png)\n\n![escape](../../secret.png)\n"), + logo: pngOf(t, 2, 2), + filepath.Join(outside, "secret.png"): pngOf(t, 1, 1), + } { + if err := os.WriteFile(name, data, 0600); err != nil { + t.Fatal(err) + } + } + + h := newHarness(t, &testKernel{Client: core.NewClient()}) + pages := fakePreviews(h) + h.open(root) + p := h.app.project + if got := p.previewWorkspaceRoot(); got != root { + t.Fatalf("project previewWorkspaceRoot = %q, want %q", got, root) + } + h.app.openFile(p, nested) + h.await("nested project Markdown", func() bool { return p.docs.byPath(nested) != nil }) + h.settle() + d := p.docs.byPath(nested) + if d.markdown == nil || d.markdown.web == nil || len(*pages) != 1 { + t.Fatalf("project nested Markdown opened %d web pages", len(*pages)) + } + page := (*pages)[0] + ctx, cancel := context.WithTimeout(context.Background(), kernelCallTimeout) + defer cancel() + wantLogo := canonicalTestPath(t, logo) + for _, ref := range []string{"/assets/logo.png", "../assets/logo.png"} { + resolved, err := page.config.Resolve(ctx, ref) + if err != nil || resolved.Kind != "file" || resolved.Path != wantLogo || resolved.MIME != "image/png" { + t.Errorf("project web image %q: %+v, %v (want %s)", ref, resolved, err, wantLogo) + } + } + if secret, err := page.config.Resolve(ctx, "../../secret.png"); err != nil || secret.Kind != "none" { + t.Errorf("project web image outside workspace: %+v, %v", secret, err) + } + page.load() + page.config.OnMessage(webpreview.Message{Type: "link", Href: "/README.md"}) + h.await("project root-relative link", func() bool { + shown := p.selectedDocument(p.docs.group) + return shown != nil && filepath.Clean(shown.path) == filepath.Clean(readme) + }) +} diff --git a/shared/platform-feature-matrix/features/cli-single-file-mode.json b/shared/platform-feature-matrix/features/cli-single-file-mode.json index 027a140521..e6a2bec327 100644 --- a/shared/platform-feature-matrix/features/cli-single-file-mode.json +++ b/shared/platform-feature-matrix/features/cli-single-file-mode.json @@ -4,7 +4,7 @@ "group": "CLI", "capability": "可选注册 lithe 命令,按文件夹/单文件分别打开项目与轻量编辑器", "owner": "Desktop", - "verification": "macOS:打开通用 > CLI 时不写来源;点击注册后新终端能使用 lithe,分别传入带空格/中文文件、文件夹和 -- 保护的路径;单文件只使用内置高亮,能编辑保存,默认不创建终端,无插件、项目树、Git、运行/调试/测试或父目录索引,查找动作不搜索工作区;打开 Markdown 文件时标签栏有编辑 / 编辑与预览 / 预览切换和目录按钮,渲染页跟随编辑并显示相对路径图片,SVG、HTML 仍只显示源码。点击终端后使用当前文件目录,真实命令输出可见,隐藏后重开复用会话,关闭窗口清理 shell。以项目打开所在文件夹在新窗口走正常信任和插件流程。修复应用移动后的启动器;移除注册保留无关 shell 内容。Windows:检查 HKCU Environment Path 类型与无关项保留、外部修改冲突、环境广播、新 cmd/PowerShell 中调用及移除,原生运行待验收。 单文件多文档引用:先打开根目录 Markdown,再在同一窗口打开其子目录及其他目录的 Markdown;各文档的相对图片和 /local.png 应按自己的目录加载,../outside.png 不得读取文档目录外的图片,/next.md 应打开当前文档目录里的文件,保持单文件会话。运行 TestStandaloneMarkdownImagesUseEachDocumentsFolder、TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle;两端原生网页引擎仍待验收。", + "verification": "macOS:打开通用 > CLI 时不写来源;点击注册后新终端能使用 lithe,分别传入带空格/中文文件、文件夹和 -- 保护的路径;单文件只使用内置高亮,能编辑保存,默认不创建终端,无插件、项目树、Git、运行/调试/测试或父目录索引,查找动作不搜索工作区;打开 Markdown 文件时标签栏有编辑 / 编辑与预览 / 预览切换和目录按钮,渲染页跟随编辑并显示相对路径图片,SVG、HTML 仍只显示源码。点击终端后使用当前文件目录,真实命令输出可见,隐藏后重开复用会话,关闭窗口清理 shell。以项目打开所在文件夹在新窗口走正常信任和插件流程。修复应用移动后的启动器;移除注册保留无关 shell 内容。Windows:检查 HKCU Environment Path 类型与无关项保留、外部修改冲突、环境广播、新 cmd/PowerShell 中调用及移除,原生运行待验收。 单文件多文档引用:先打开根目录 Markdown,再在同一窗口打开其子目录及其他目录的 Markdown;各文档的相对图片和 /local.png 应按自己的目录加载,../local.png、../outside.png 与指向目录外的符号链接不得读取文档目录外的图片,/next.md 应打开当前文档目录里的文件,保持单文件会话。运行 TestStandaloneMarkdownImagesUseEachDocumentsFolder、TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle、TestProjectMarkdownKeepsWorkspaceRootForNestedDocuments;两端原生网页引擎仍待验收。", "notes": "已在 macOS 执行真实临时配置注册、修复、移除和冲突保护测试,并验证单文件真实编辑保存、真实 PTY 命令及中英/明暗无头截图;Markdown 渲染页与视图切换(#2324)有无头回归和截图,原生网页引擎下的渲染页待两端验收。尚未通过真实系统终端完成注册到 GUI 的端到端验收。Windows 无本地来宾,规则测试及 Windows 目标编译通过,不代表原生验收完成。 #2334 已恢复单文件 Markdown 的预览入口;#2324 后续补全将原生图片、网页图片与链接解析的 workspaceRoot 留空,由 Core 按每个文档的目录解析引用。单文件会话的 p.root 不是工作区,后续文档不能继承首个文件的图片读取范围。项目窗口继续传项目根;本次不改变 Core 规则或两端 pending 的验收状态。", "macos": { "implementationStatus": "implemented", @@ -15,6 +15,7 @@ "desktop/internal/core/cli.go", "desktop/internal/workbench/standalone.go", "desktop/internal/workbench/editor_view.go", + "desktop/internal/workbench/preview_pages.go", "desktop/internal/settingswindow/cli_view.go", "desktop/internal/workbench/standalone_test.go", "desktop/internal/core/cli_test.go", @@ -35,6 +36,7 @@ "desktop/internal/core/cli.go", "desktop/internal/workbench/standalone.go", "desktop/internal/workbench/editor_view.go", + "desktop/internal/workbench/preview_pages.go", "desktop/internal/settingswindow/cli_view.go", "desktop/internal/workbench/standalone_test.go", "desktop/internal/core/cli_test.go", diff --git a/shared/platform-feature-matrix/features/markdown-rendering.json b/shared/platform-feature-matrix/features/markdown-rendering.json index 0aebeafc3b..3619e795bf 100644 --- a/shared/platform-feature-matrix/features/markdown-rendering.json +++ b/shared/platform-feature-matrix/features/markdown-rendering.json @@ -42,7 +42,7 @@ ] }, "owner": "Editor", - "verification": "Windows 使用 markdown-render-v1.json 验证嵌套列表、任务列表、代码空白、中文、emoji、CRLF、front matter 隐藏及危险 HTML 过滤;修改、清空及关闭预览时不得被旧异步结果覆盖,失败显示提示且后续编辑可恢复。使用代码高亮、表格、Mermaid、链接和相对路径 fixture 对比渲染结果。打开含上千个代码块的大型 Markdown 预览,渲染不卡住,编辑时预览不会先闪成未高亮的代码;代码里的 `$&`、`$1` 按原文显示。macOS 在深浅主题运行 markdown-rich-content-v1.json:行内数学、块级化学反应、物理单位均生成可访问 MathML,没有 katex-error;Mermaid 生成真实 SVG,脚注链接及回链存在;H~2~O 与 x^2^ 正确,双波浪线仍是删除线,代码内语法保持原文。运行前后逐文件比较预览资源哈希,确认无网络脚本和远程资源请求、bundle 不变。macOS 使用 markdown-html-v1.json 及仓库 README 验证:宽预览中同一段内多个徽章同行居中,窄分栏按可用宽度自然折行;div/p/h1 的 align 生效,显式 br 和普通段落保留,正文行内图片不拆行,相对图片与点击放大仍可用。深浅主题均检查,脚本、事件处理器、危险 URL 和任意 style 仍被剥离。 macOS PlantUML:在深浅主题打开时序图和类图 fenced block,离线生成可放大的 SVG 图片,不能在文档 DOM 执行 SVG;多个代码块顺序执行,切换文档拒绝旧结果;本地 include 被拒绝且保留源码与本地化提示,超大输入立即失败,关闭和取消释放渲染器;运行前后比较预览资源 SHA-256。 GitHub 预览保留 Core 的硬换行与代码空白;Notebook 共享队列最多同时执行两个渲染,关闭和替换内容会取消未开始请求。 Viva 新桌面端:在深浅主题打开仓库 README 与 markdown-html-v1.json、markdown-rich-content-v1.json 的输入,确认内嵌 HTML(徽章同行、align、details、br)、内嵌 SVG、KaTeX 数学与 mhchem、Mermaid、markmap、代码高亮、脚注和上下标都显示在 web view 页里,脚本、事件处理器、危险 URL 和任意 style 仍被 Core 剥离;页面只能访问自身 lithe-preview 源,所有脚本与字体随应用内嵌,无网络请求;源码与页面按源码行双向同步滚动,切换模式保持位置;超过 1 MiB 显示过大提示。Linux 无 web 引擎时回落到 Viva 原生渲染。自动回归:scripts/desktop/desktop-go.sh test ./internal/workbench ./internal/webpreview -run 'Markdown|Preview|Serves|EveryPage' -count=1。 单文件多文档引用:先打开根目录 Markdown,再在同一窗口打开其子目录及其他目录的 Markdown;各文档的相对图片和 /local.png 应按自己的目录加载,../outside.png 不得读取文档目录外的图片,/next.md 应打开当前文档目录里的文件,保持单文件会话。运行 TestStandaloneMarkdownImagesUseEachDocumentsFolder、TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle;两端原生网页引擎仍待验收。", + "verification": "Windows 使用 markdown-render-v1.json 验证嵌套列表、任务列表、代码空白、中文、emoji、CRLF、front matter 隐藏及危险 HTML 过滤;修改、清空及关闭预览时不得被旧异步结果覆盖,失败显示提示且后续编辑可恢复。使用代码高亮、表格、Mermaid、链接和相对路径 fixture 对比渲染结果。打开含上千个代码块的大型 Markdown 预览,渲染不卡住,编辑时预览不会先闪成未高亮的代码;代码里的 `$&`、`$1` 按原文显示。macOS 在深浅主题运行 markdown-rich-content-v1.json:行内数学、块级化学反应、物理单位均生成可访问 MathML,没有 katex-error;Mermaid 生成真实 SVG,脚注链接及回链存在;H~2~O 与 x^2^ 正确,双波浪线仍是删除线,代码内语法保持原文。运行前后逐文件比较预览资源哈希,确认无网络脚本和远程资源请求、bundle 不变。macOS 使用 markdown-html-v1.json 及仓库 README 验证:宽预览中同一段内多个徽章同行居中,窄分栏按可用宽度自然折行;div/p/h1 的 align 生效,显式 br 和普通段落保留,正文行内图片不拆行,相对图片与点击放大仍可用。深浅主题均检查,脚本、事件处理器、危险 URL 和任意 style 仍被剥离。 macOS PlantUML:在深浅主题打开时序图和类图 fenced block,离线生成可放大的 SVG 图片,不能在文档 DOM 执行 SVG;多个代码块顺序执行,切换文档拒绝旧结果;本地 include 被拒绝且保留源码与本地化提示,超大输入立即失败,关闭和取消释放渲染器;运行前后比较预览资源 SHA-256。 GitHub 预览保留 Core 的硬换行与代码空白;Notebook 共享队列最多同时执行两个渲染,关闭和替换内容会取消未开始请求。 Viva 新桌面端:在深浅主题打开仓库 README 与 markdown-html-v1.json、markdown-rich-content-v1.json 的输入,确认内嵌 HTML(徽章同行、align、details、br)、内嵌 SVG、KaTeX 数学与 mhchem、Mermaid、markmap、代码高亮、脚注和上下标都显示在 web view 页里,脚本、事件处理器、危险 URL 和任意 style 仍被 Core 剥离;页面只能访问自身 lithe-preview 源,所有脚本与字体随应用内嵌,无网络请求;源码与页面按源码行双向同步滚动,切换模式保持位置;超过 1 MiB 显示过大提示。Linux 无 web 引擎时回落到 Viva 原生渲染。自动回归:scripts/desktop/desktop-go.sh test ./internal/workbench ./internal/webpreview -run 'Markdown|Preview|Serves|EveryPage' -count=1。 单文件多文档引用:先打开根目录 Markdown,再在同一窗口打开其子目录及其他目录的 Markdown;各文档的相对图片和 /local.png 应按自己的目录加载,../local.png、../outside.png 与指向目录外的符号链接不得读取文档目录外的图片,/next.md 应打开当前文档目录里的文件,保持单文件会话。运行 TestStandaloneMarkdownImagesUseEachDocumentsFolder、TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle、TestProjectMarkdownKeepsWorkspaceRootForNestedDocuments;两端原生网页引擎仍待验收。", "notes": "Viva:有系统 web 引擎(macOS WKWebView、Windows WebView2)时,Markdown 页显示在 web view 中:Core markdown.render 生成过滤后的 HTML(放行内嵌 SVG 白名单),页面用内嵌的 KaTeX 0.16.0 与 mhchem、Highlight.js、Mermaid 11、markmap 渲染富内容,只在文档用到时加载;链接交回宿主按 Core 的解析结果跳转,图片按 Core 解析结果提供。每次编辑后重新渲染,同一时间只有一次渲染。Linux 没有 web 引擎,仍用 Viva 的 Markdown 组件渲染(不支持 HTML 和富内容)。标签栏末尾三态切换“编辑 / 编辑与预览 / 预览”,默认编辑与预览,超过 1 MiB 不渲染。PlantUML 尚未接入。 #2334 已恢复单文件 Markdown 的预览入口;#2324 后续补全将原生图片、网页图片与链接解析的 workspaceRoot 留空,由 Core 按每个文档的目录解析引用。单文件会话的 p.root 不是工作区,后续文档不能继承首个文件的图片读取范围。项目窗口继续传项目根;本次不改变 Core 规则或两端 pending 的验收状态。", "legacy": { "revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36", From 5656ae184049547e172978a6aedfdb0e4522f714 Mon Sep 17 00:00:00 2001 From: lick <2188718831@qq.com> Date: Mon, 23 Nov 2026 19:38:37 -0800 Subject: [PATCH 3/3] test(desktop): handle Windows canonical paths in standalone markdown reference tests --- .../standalone_markdown_references_test.go | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/desktop/internal/workbench/standalone_markdown_references_test.go b/desktop/internal/workbench/standalone_markdown_references_test.go index f6bf48d719..34c660aa1c 100644 --- a/desktop/internal/workbench/standalone_markdown_references_test.go +++ b/desktop/internal/workbench/standalone_markdown_references_test.go @@ -11,6 +11,7 @@ import ( "github.com/1lck/Lithe-IDEA/desktop/internal/core" "github.com/1lck/Lithe-IDEA/desktop/internal/store" "github.com/1lck/Lithe-IDEA/desktop/internal/webpreview" + "github.com/1lck/Lithe-IDEA/desktop/internal/winpath" ) func standaloneMarkdownReferenceHarness(t *testing.T, path string) *harness { @@ -25,13 +26,18 @@ func standaloneMarkdownReferenceHarness(t *testing.T, path string) *harness { return newHarnessWithOptions(t, options) } -func canonicalTestPath(t *testing.T, path string) string { - t.Helper() - resolved, err := filepath.EvalSymlinks(path) - if err != nil { - t.Fatal(err) +// Core's canonical path to a served resource may resolve macOS TempDir symlinks (/private/var) +// or use Windows' \\?\ extended-length spelling. +func sameResolvedFile(got, want string) bool { + if want == "" || !filepath.IsAbs(got) { + return false + } + if got == want || winpath.Equal(got, want) { + return true } - return resolved + actual, actualErr := os.Stat(got) + expected, expectedErr := os.Stat(want) + return actualErr == nil && expectedErr == nil && os.SameFile(actual, expected) } func TestPreviewWorkspaceRootReturnsEmptyOnlyForNilOrStandalone(t *testing.T) { @@ -228,10 +234,10 @@ func TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle(t *testing.T) { h.await("Core Markdown HTML", func() bool { return s.web.html != "" }) ctx, cancel := context.WithTimeout(context.Background(), kernelCallTimeout) defer cancel() - wantNestedPNG := canonicalTestPath(t, filepath.Join(folder, "local.png")) + wantNestedPNG := filepath.Join(folder, "local.png") for _, ref := range []string{"local.png", "/local.png"} { resolved, err := page.config.Resolve(ctx, ref) - if err != nil || resolved.Kind != "file" || resolved.Path != wantNestedPNG || resolved.MIME != "image/png" { + if err != nil || resolved.Kind != "file" || !sameResolvedFile(resolved.Path, wantNestedPNG) || resolved.MIME != "image/png" { t.Errorf("web image %q: %+v, %v (want %s)", ref, resolved, err, wantNestedPNG) } loaded, mime, err := webpreview.LoadResource(ctx, resolved, nil) @@ -267,8 +273,8 @@ func TestStandaloneMarkdownWebPreviewKeepsScopeAndLifecycle(t *testing.T) { } otherPage := otherDoc.markdown.web.entry.view.(*fakePreview) resolvedOther, err := otherPage.config.Resolve(ctx, "/local.png") - wantOtherPNG := canonicalTestPath(t, filepath.Join(otherDir, "local.png")) - if err != nil || resolvedOther.Kind != "file" || resolvedOther.Path != wantOtherPNG { + wantOtherPNG := filepath.Join(otherDir, "local.png") + if err != nil || resolvedOther.Kind != "file" || !sameResolvedFile(resolvedOther.Path, wantOtherPNG) { t.Errorf("unrelated web image /local.png: %+v, %v (want %s)", resolvedOther, err, wantOtherPNG) } otherPage.load() @@ -330,11 +336,10 @@ func TestProjectMarkdownKeepsWorkspaceRootForNestedDocuments(t *testing.T) { page := (*pages)[0] ctx, cancel := context.WithTimeout(context.Background(), kernelCallTimeout) defer cancel() - wantLogo := canonicalTestPath(t, logo) for _, ref := range []string{"/assets/logo.png", "../assets/logo.png"} { resolved, err := page.config.Resolve(ctx, ref) - if err != nil || resolved.Kind != "file" || resolved.Path != wantLogo || resolved.MIME != "image/png" { - t.Errorf("project web image %q: %+v, %v (want %s)", ref, resolved, err, wantLogo) + if err != nil || resolved.Kind != "file" || !sameResolvedFile(resolved.Path, logo) || resolved.MIME != "image/png" { + t.Errorf("project web image %q: %+v, %v (want %s)", ref, resolved, err, logo) } } if secret, err := page.config.Resolve(ctx, "../../secret.png"); err != nil || secret.Kind != "none" { @@ -344,6 +349,6 @@ func TestProjectMarkdownKeepsWorkspaceRootForNestedDocuments(t *testing.T) { page.config.OnMessage(webpreview.Message{Type: "link", Href: "/README.md"}) h.await("project root-relative link", func() bool { shown := p.selectedDocument(p.docs.group) - return shown != nil && filepath.Clean(shown.path) == filepath.Clean(readme) + return shown != nil && winpath.Equal(filepath.Clean(shown.path), filepath.Clean(readme)) }) }