Repository navigation
fix(muya): render relative image paths anchored to the document dir (Phase G blocker) - #4428
Conversation
…Phase G blocker) After the @muyajs/core migration, `getImageSrc` lost the base-dir resolution that legacy muyajs `getImageInfo(src, baseUrl = window.DIRNAME)` performed. For a relative local image path it returned a non-anchored `file://assets/foo.png`, so `` failed to load and rendered the fail icon in the live editor (insert/paste/pre-existing relative images). Restore the legacy behaviour inside the engine: - `getImageSrc` now resolves a relative local image path against `window.DIRNAME` (the host renderer's current-document directory, mirroring legacy muyajs), yielding a loadable `file://<absolute>` URL. - URLs, `data:` URIs, absolute local paths, and already-`file://` srcs are left untouched (no `file://file://` double-prefix). `imageExtension && file://` is now classed as a URL, matching legacy `getImageInfo`. - A small self-contained POSIX-style resolver (collapsing `.`/`..`, normalising Windows backslashes) keeps the engine free of the desktop's `window.path` polyfill; falls back to the legacy bare `file://` form when `window.DIRNAME` is absent (headless / no open file). - Declare `Window.DIRNAME` on the engine's global types. Adds a `utils/__tests__/image.spec.ts` regression suite (this path was untested) covering: relative -> anchored absolute `file://`; `./` and `../` resolution; Windows-drive base dirs; absolute/URL/data:/`file://` left unchanged; no double-prefix; and the no-DIRNAME fallback. This is an engine-only fix — no desktop change is required, since the desktop already maintains `window.DIRNAME` on every open/tab-switch/rename. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
Fixes a regression in @muyajs/core where relative image paths (e.g. ) stopped rendering because getImageSrc didn’t anchor them to the current document directory (window.DIRNAME) after the migration.
Changes:
- Update
getImageSrcto resolve relative local image paths againstwindow.DIRNAME, while avoiding double-file://prefixing. - Add a self-contained relative-path resolver used by
getImageSrc(no dependency on the desktopwindow.pathpolyfill). - Add Vitest coverage for relative/absolute/URL/data/file image-src cases and declare
window.DIRNAMEin engine global types.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/muya/src/utils/image.ts | Anchors relative image paths to window.DIRNAME via a custom resolver; avoids double file:// prefixing. |
| packages/muya/src/utils/tests/image.spec.ts | Adds regression/unit tests covering relative path anchoring and non-relative passthrough behavior. |
| packages/muya/src/types/global.d.ts | Declares window.DIRNAME for the engine TypeScript types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function resolveRelativePath(base: string, relative: string): string { | ||
| const normalizedBase = base.replace(/\\/g, '/').replace(/\/+$/, ''); | ||
| const combined = `${normalizedBase}/${relative.replace(/\\/g, '/')}`; | ||
| const isWinDrive = /^[a-z]:/i.test(combined); | ||
| const segments = combined.split('/'); | ||
| const resolved: string[] = []; | ||
| for (const segment of segments) { | ||
| if (segment === '' || segment === '.') | ||
| continue; | ||
|
|
||
| if (segment === '..') | ||
| resolved.pop(); | ||
| else | ||
| resolved.push(segment); | ||
| } | ||
| // POSIX absolute paths keep their leading slash; Windows drive paths | ||
| // (`C:/...`) do not get one. | ||
| return isWinDrive ? resolved.join('/') : `/${resolved.join('/')}`; | ||
| } |
There was a problem hiding this comment.
Good catch — valid, and it landed after I'd merged this PR, so fixed forward in a follow-up: the resolver now isolates the root (UNC share / Windows drive / POSIX) and never collapses .. past it (so C:/a/../../b -> C:/b keeps the drive, and UNC //server/share roots are preserved), with 6 added regression cases.
…hs (#4430) The Phase G #4428 relative-image resolver collapsed `..` past the root, dropping the Windows drive segment (`C:/a/../../b` -> `b`, losing `C:`) and mangling UNC `//server/share` bases. Isolate the root (UNC share / drive / POSIX) before resolving and never collapse past it, mirroring path.resolve. Adds 6 drive/UNC regression cases. Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
The bug (Phase G — G1, BLOCKER)
Relative-path images stopped rendering in the live editor after the
@muyajs/coremigration.Legacy muyajs rendered relative image paths anchored to the document directory: the inline renderer called
getImageInfo(src, baseUrl = window.DIRNAME)which did'file://' + path.resolve(baseUrl, src), yielding a loadable absolutefile://URL. Soin a document opened from disk rendered correctly.The migrated engine's
getImageSrc(packages/muya/src/utils/image.ts) had no baseUrl resolution — for a relative path it returned{ src: 'file://' + src }(e.g.file://assets/foo.png), a non-anchored, broken URL. The<img>then failed to load and rendered the fail icon. This affected every pre-existing relative image in opened documents, plus paste and programmatic-insert flows; drag-drop was only transiently masked by aurlMapentry that died on reload.The desktop already maintains
window.DIRNAMEon every open / tab-switch / rename, but the engine never read it (the legacy string resolver'sbaseUrl/path.resolverelative-path resolution was dropped, not ported).The fix (engine-only —
packages/muya)getImageSrcnow mirrors legacygetImageInfo:window.DIRNAME→file://<resolved-absolute>.data:URIs, absolute local paths, and already-file://srcs untouched (nofile://file://double-prefix).imageExtension && file://is now classed as a URL, matching legacy../.., normalises Windows backslashes) so the engine does not depend on the desktop'swindow.pathpolyfill.file://form whenwindow.DIRNAMEis absent (headless / no open file).Window.DIRNAMEdeclared on the engine's global types.Also covers the reference-image render path (
referenceImage.ts), which sharesgetImageSrc.Tests
This path was previously untested. Adds
packages/muya/src/utils/__tests__/image.spec.ts(12 cases) asserting:file://absolute./and../resolution; Windows-drive base dirsdata:/ already-file://left unchangedfile://prefixDIRNAMEfallback to barefile://Local CI gates all green:
lint(0 errors),lint:types,lint:css(0 errors),check-circular,test(593),test:spec(1347).Desktop change required
None. This is an engine-only fix. The desktop already keeps
window.DIRNAMEcurrent, which the engine now consumes directly — noeditor.vue/ constructor wiring change is needed.🤖 Generated with Claude Code