Skip to content

fix(muya): render relative image paths anchored to the document dir (Phase G blocker) - #4428

Merged
Jocs merged 1 commit into
developfrom
fix/muya-relative-image-src
Jun 9, 2026
Merged

Jocs merged 1 commit into
developfrom
fix/muya-relative-image-src

Conversation

@Jocs

@Jocs Jocs commented Jun 9, 2026

Copy link
Copy Markdown
Member

The bug (Phase G — G1, BLOCKER)

Relative-path images stopped rendering in the live editor after the @muyajs/core migration.

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 absolute file:// URL. So ![](assets/foo.png) in 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 a urlMap entry that died on reload.

The desktop already maintains window.DIRNAME on every open / tab-switch / rename, but the engine never read it (the legacy string resolver's baseUrl/path.resolve relative-path resolution was dropped, not ported).

The fix (engine-only — packages/muya)

getImageSrc now mirrors legacy getImageInfo:

  • Resolves a relative local image path against window.DIRNAME → file://<resolved-absolute>.
  • Leaves URLs, data: URIs, absolute local paths, and already-file:// srcs untouched (no file://file:// double-prefix). imageExtension && file:// is now classed as a URL, matching legacy.
  • Uses a small self-contained POSIX-style resolver (collapses ./.., normalises Windows backslashes) so the engine does not depend on the desktop's window.path polyfill.
  • Falls back to the legacy bare file:// form when window.DIRNAME is absent (headless / no open file).
  • Window.DIRNAME declared on the engine's global types.

Also covers the reference-image render path (referenceImage.ts), which shares getImageSrc.

Tests

This path was previously untested. Adds packages/muya/src/utils/__tests__/image.spec.ts (12 cases) asserting:

  • relative path → anchored file:// absolute
  • ./ and ../ resolution; Windows-drive base dirs
  • absolute / URL / data: / already-file:// left unchanged
  • no double file:// prefix
  • no-DIRNAME fallback to bare file://

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.DIRNAME current, which the engine now consumes directly — no editor.vue / constructor wiring change is needed.

🤖 Generated with Claude Code

…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 `![](assets/foo.png)` 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]>
Copilot AI review requested due to automatic review settings June 9, 2026 23:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression in @muyajs/core where relative image paths (e.g. ![](assets/foo.png)) stopped rendering because getImageSrc didn’t anchor them to the current document directory (window.DIRNAME) after the migration.

Changes:

  • Update getImageSrc to resolve relative local image paths against window.DIRNAME, while avoiding double-file:// prefixing.
  • Add a self-contained relative-path resolver used by getImageSrc (no dependency on the desktop window.path polyfill).
  • Add Vitest coverage for relative/absolute/URL/data/file image-src cases and declare window.DIRNAME in 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.

Comment on lines +43 to +61
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('/')}`;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Jocs
Jocs merged commit 1e8f8d2 into develop Jun 9, 2026
8 checks passed
Jocs added a commit that referenced this pull request Jun 9, 2026
…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]>
@Jocs
Jocs deleted the fix/muya-relative-image-src branch June 10, 2026 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants