From 288a5da74e630fcb27cdf8e651aafe7c2a5918d9 Mon Sep 17 00:00:00 2001 From: PathGao <42336971+PathGao@users.noreply.github.com> Date: Sun, 8 Nov 2026 14:13:02 +0800 Subject: [PATCH 1/2] fix(preview): reload fetches images edited on disk again (#1066) Reload re-read the document but kept every image: a block whose markup did not change is not re-inserted, and an unchanged URL is served from the webview's decoded copy. Each explicit reload now stamps a generation onto , so only the blocks holding an image are replaced, and their local URLs carry it as a query the asset protocol ignores. --- scripts/preview.imageReload.test.ts | 41 +++++++++++++++++++++++ src/lib/MarkdownViewer.svelte | 3 +- src/lib/sessions/externalChange.svelte.ts | 3 ++ src/lib/utils/markdown.ts | 23 ++++++++++++- 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 scripts/preview.imageReload.test.ts diff --git a/scripts/preview.imageReload.test.ts b/scripts/preview.imageReload.test.ts new file mode 100644 index 00000000..0c8c3050 --- /dev/null +++ b/scripts/preview.imageReload.test.ts @@ -0,0 +1,41 @@ +import { describe, test } from 'node:test'; +import assert from 'node:assert/strict'; +import { processMarkdownHtml, refetchLocalImages, stampImageGeneration } from '../src/lib/utils/markdown.ts'; +import { useShimDom } from './preview.domShim.ts'; +import { parseHtml } from './renderProtocolDom.ts'; + +// #1066: Reload must fetch an image edited on disk again, not reuse the old copy. +describe('reload refetches local images', () => { + useShimDom({ window: { __TAURI_INTERNALS__: { convertFileSrc: (path: string) => `asset://localhost/${path}` } } }); + + const FILE_PATH = '/documents/notes.md'; + const RENDERED = '

text

\0

'; + const srcOf = (html: string) => parseHtml(processMarkdownHtml(html, FILE_PATH, new Set())).body.querySelector('img')?.getAttribute('src'); + + test('before any reload the markup and the URL are untouched', () => { + assert.equal(stampImageGeneration(RENDERED), RENDERED); + assert.equal(srcOf(RENDERED), 'asset://localhost//documents/a.png'); + }); + + test('each reload changes only the blocks holding an image, and their URL', () => { + refetchLocalImages(); + const first = stampImageGeneration(RENDERED).split('\0'); + assert.equal(first[0], RENDERED.split('\0')[0], 'a block without an image would be re-inserted for nothing'); + assert.notEqual(first[1], RENDERED.split('\0')[1], 'the image block would be kept, so its never reloads'); + assert.equal(srcOf(first.join('\0')), 'asset://localhost//documents/a.png?v=1'); + + refetchLocalImages(); + assert.equal(srcOf(stampImageGeneration(RENDERED)), 'asset://localhost//documents/a.png?v=2'); + }); + + test('the stamp does not survive into the preview, and only real img tags get it', () => { + const html = processMarkdownHtml(stampImageGeneration(RENDERED), FILE_PATH, new Set()); + assert.doesNotMatch(html, /data-image-generation/); + assert.match(stampImageGeneration(''), /data-image-generation/); + assert.equal(stampImageGeneration(''), ''); + }); + + test('a remote image keeps the URL its author wrote', () => { + assert.equal(srcOf(stampImageGeneration('

')), 'https://example.com/a.png'); + }); +}); diff --git a/src/lib/MarkdownViewer.svelte b/src/lib/MarkdownViewer.svelte index ad4c19b4..2c1e2c23 100644 --- a/src/lib/MarkdownViewer.svelte +++ b/src/lib/MarkdownViewer.svelte @@ -51,6 +51,7 @@ import { type FoldLayoutObservation } from './utils/foldLayout.js'; import { followFoldLayout, previewPaneShown } from './utils/foldLayoutEffect.svelte.js'; import { followPreviewPatch } from './utils/previewPatchEffect.svelte.js'; import { patchHost } from './utils/previewPatch.js'; +import { stampImageGeneration } from './utils/markdown.js'; import { refreshesPreviewCache } from './utils/previewRefresh.js'; import { revealFold, @@ -908,7 +909,7 @@ import { followActiveFile } from './sessions/fileWatch.svelte.js'; // moved the tab to the new file and cleared its folds by then. async function renderMarkdownPreview(raw: string) { const body = getMarkdownBodyWithoutFrontMatter(raw); - return await commands.renderMarkdown(body, true); + return stampImageGeneration(await commands.renderMarkdown(body, true)); } async function renderTabPreviewFromRaw(tab: Tab) { diff --git a/src/lib/sessions/externalChange.svelte.ts b/src/lib/sessions/externalChange.svelte.ts index af6a2b30..fa173cd3 100644 --- a/src/lib/sessions/externalChange.svelte.ts +++ b/src/lib/sessions/externalChange.svelte.ts @@ -1,5 +1,6 @@ import type { LanguageCode, TranslationKey } from '../utils/i18n.js'; import type { LoadMarkdownOptions } from './documentSession.svelte.js'; +import { refetchLocalImages } from '../utils/markdown.js'; type ConflictTab = { id: string; path: string; originalContent: string; rawContent: string; isDirty: boolean }; @@ -147,6 +148,7 @@ export function createExternalChange(options: ExternalChangeOptions) { if (!tab?.path || tab.id !== tabId) return; comparison = null; clearExternalChangeConflict(tab.id); + refetchLocalImages(); await loadMarkdown(tab.path, { preserveEditState: true, skipTabManagement: true, @@ -165,6 +167,7 @@ export function createExternalChange(options: ExternalChangeOptions) { if (!activeId || !tab?.path) return; if (!(await options.canCloseTab(activeId))) return; + refetchLocalImages(); await loadMarkdown(tab.path, { preserveEditState: true, skipTabManagement: true, diff --git a/src/lib/utils/markdown.ts b/src/lib/utils/markdown.ts index de3dae2e..7b7fb94c 100644 --- a/src/lib/utils/markdown.ts +++ b/src/lib/utils/markdown.ts @@ -698,6 +698,25 @@ export function processMarkdownHtml( /** `+` or `-`, on a foldable callout: what the document opens it as. */ const CALLOUT_FOLD_ATTR = "data-callout-fold"; +/** + * Reload re-reads the document, not its images (#1066). An image edited on + * disk keeps its URL, so the webview shows the copy it already decoded, and a + * block whose markup did not change is not even re-inserted. Each explicit + * reload starts a new generation: the preview stamps it onto every ``, + * which changes exactly the blocks holding one, and their local URLs carry it + * as a query the asset protocol ignores (it reads only the path). + */ +let imageGeneration = 0; +const GENERATION_ATTR = "data-image-generation"; + +export function refetchLocalImages(): void { + imageGeneration++; +} + +export function stampImageGeneration(html: string): string { + return imageGeneration ? html.replace(/])/gi, ` Date: Sun, 8 Nov 2026 14:24:29 +0800 Subject: [PATCH 2/2] fix(preview): stamp images without quotes, test both reload paths Raw HTML can hold the text { assert.deepEqual(viewer.toasts, ['toast.reloadedFromDisk']); }); + // #1066: both reverts refetch the document's images, not only its text. + test('both reload commands make the preview fetch local images again', async () => { + const image = ''; + const viewer = makeViewer(); + const tab = conflicted(viewer); + + const before = stampImageGeneration(image); + await viewer.conflicts.resolveExternalChangeByReloading(tab.id); + const afterConflictReload = stampImageGeneration(image); + assert.notEqual(afterConflictReload, before, 'Reload on the conflict bar kept the cached images'); + + await viewer.conflicts.reloadFromDisk(); + assert.notEqual(stampImageGeneration(image), afterConflictReload, 'Reload from disk kept the cached images'); + }); + // --- the watcher's `file-changed` event --- /** a.md in the background, b.md on screen, both saved and clean. */ diff --git a/scripts/preview.imageReload.test.ts b/scripts/preview.imageReload.test.ts index 0c8c3050..f3954d6a 100644 --- a/scripts/preview.imageReload.test.ts +++ b/scripts/preview.imageReload.test.ts @@ -35,6 +35,14 @@ describe('reload refetches local images', () => { assert.equal(stampImageGeneration(''), ''); }); + // The stamp is text, so it also lands on ` written inside an attribute value leaves the attribute whole', () => { + const body = parseHtml(stampImageGeneration('

x

')).body; + assert.equal(body.querySelector('span')?.textContent, 'x'); + assert.match(body.querySelector('span')?.getAttribute('title') ?? '', /^a b$/); + }); + test('a remote image keeps the URL its author wrote', () => { assert.equal(srcOf(stampImageGeneration('

')), 'https://example.com/a.png'); }); diff --git a/src/lib/utils/markdown.ts b/src/lib/utils/markdown.ts index 7b7fb94c..6f78dbfc 100644 --- a/src/lib/utils/markdown.ts +++ b/src/lib/utils/markdown.ts @@ -714,7 +714,7 @@ export function refetchLocalImages(): void { } export function stampImageGeneration(html: string): string { - return imageGeneration ? html.replace(/])/gi, `])/gi, `