diff --git a/scripts/documents.conflicts.spec.ts b/scripts/documents.conflicts.spec.ts
index 3e86fac1..b0e39b93 100644
--- a/scripts/documents.conflicts.spec.ts
+++ b/scripts/documents.conflicts.spec.ts
@@ -14,6 +14,7 @@ const { createDocumentSession } = await import('../src/lib/sessions/documentSess
const { createWindowExit } = await import('../src/lib/sessions/windowExit.js');
const { createExternalChange } = await import('../src/lib/sessions/externalChange.svelte.js');
const { HOME_TAB_PATH } = await import('../src/lib/utils/homeTab.js');
+const { stampImageGeneration } = await import('../src/lib/utils/markdown.js');
// An unanswered external-change conflict is a question only the user can
// answer. The viewer's saves that nobody asked for — leaving the editor or
@@ -276,6 +277,21 @@ describe('external change conflicts', () => {
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
new file mode 100644
index 00000000..f3954d6a
--- /dev/null
+++ b/scripts/preview.imageReload.test.ts
@@ -0,0 +1,49 @@
+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(''), '');
+ });
+
+ // 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/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..6f78dbfc 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, `;)