fix: strip local image refs with spaces/parens in filenames before markdown render

This commit is contained in:
Davide Scaini
2026-04-16 10:29:13 +02:00
parent cfdd8d2744
commit a78f6ee3bd
+10 -2
View File
@@ -70,15 +70,23 @@
$: rawDescription = localDescription || detail?.description || ''; $: rawDescription = localDescription || detail?.description || '';
$: descriptionHtml = (() => { $: descriptionHtml = (() => {
if (!rawDescription) return ''; if (!rawDescription) return '';
// Strip local image refs before marked sees them. marked only parses ![alt](url) as an
// image when the URL has no spaces — filenames like "WhatsApp Image 2026.jpg" are left
// as literal text instead. The lazy .*? anchored to the image extension handles filenames
// with spaces and nested parens (e.g. "file(2).jpg") correctly.
const stripped = rawDescription
.replace(/!\[[^\]]*\]\((?!https?:\/\/|\/|data:).*?\.(?:jpe?g|png|gif|webp|bmp|avif|heic)\)/gi, '')
.trim();
if (!stripped) return '';
const renderer = new marked.Renderer(); const renderer = new marked.Renderer();
// Local relative images are always shown in the gallery — suppress inline rendering // Any remaining remote images render inline; local ones (shouldn't exist after strip) are suppressed
renderer.image = ({ href, title, text }) => { renderer.image = ({ href, title, text }) => {
const isLocal = href && !href.startsWith('http') && !href.startsWith('/') && !href.startsWith('data:'); const isLocal = href && !href.startsWith('http') && !href.startsWith('/') && !href.startsWith('data:');
if (isLocal) return ''; if (isLocal) return '';
const titleAttr = title ? ` title="${title}"` : ''; const titleAttr = title ? ` title="${title}"` : '';
return `<img src="${href ?? ''}" alt="${text}"${titleAttr} class="rounded-lg max-w-full my-2">`; return `<img src="${href ?? ''}" alt="${text}"${titleAttr} class="rounded-lg max-w-full my-2">`;
}; };
return DOMPurify.sanitize(marked(rawDescription, { renderer }) as string); return DOMPurify.sanitize(marked(stripped, { renderer }) as string);
})(); })();
// Derive image dir from detail_url so multi-user paths resolve correctly. // Derive image dir from detail_url so multi-user paths resolve correctly.