Skip to content

Commit d0ab5a1

Browse files
authored
fix: resolve relative image attachments (#200)
1 parent 34f50fb commit d0ab5a1

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/modules/markdown.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ md.use(mila, {
4646
}
4747
});
4848

49+
const defaultImageRenderer = md.renderer.rules.image;
50+
51+
md.renderer.rules.image = (tokens, index, options, env, self) => {
52+
const token = tokens[index];
53+
const source = token.attrGet("src");
54+
const documentPath = env?.documentPath;
55+
56+
if (source && documentPath && window.electronAPI?.resolveLocalAttachment) {
57+
token.attrSet(
58+
"src",
59+
window.electronAPI.resolveLocalAttachment(documentPath, source)
60+
);
61+
}
62+
63+
return defaultImageRenderer(tokens, index, options, env, self);
64+
};
65+
4966
["note", "warning", "tip"].forEach(type => {
5067
md.use(container, type, {
5168
render(tokens, idx) {
@@ -76,6 +93,6 @@ md.use(container, "info", {
7693

7794
// Exported Renderer
7895

79-
export function renderMarkdown(text) {
80-
return md.render(text || "");
81-
}
96+
export function renderMarkdown(text, env = {}) {
97+
return md.render(text || "", env);
98+
}

src/modules/ui/viewMode.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { renderMarkdown } from "../markdown.js";
2+
import { getActiveTab } from "../file/tabs.js";
23

34
const STORAGE_KEY = "snapdock:previewMode";
45

@@ -71,7 +72,9 @@ export function initViewModeToggle({ toggleBtn, editor, preview }) {
7172

7273
// --- Update preview content ---
7374
const applyPreviewContent = () => {
74-
const html = renderMarkdown(editor.value);
75+
const html = renderMarkdown(editor.value, {
76+
documentPath: getActiveTab()?.filePath,
77+
});
7578
const parsed = new DOMParser().parseFromString(html, "text/html");
7679
preview.replaceChildren(...parsed.body.childNodes);
7780
};

src/preload.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
const { contextBridge, ipcRenderer } = require("electron");
2+
const path = require("path");
3+
const { pathToFileURL } = require("url");
24
const MarkdownIt = require("markdown-it");
35
const md = new MarkdownIt();
46

7+
function resolveLocalAttachment(documentPath, attachmentPath) {
8+
if (!documentPath || !attachmentPath) return attachmentPath;
9+
10+
const isRelativePath =
11+
!path.isAbsolute(attachmentPath) &&
12+
!/^(?:[a-z][a-z\d+.-]*:|\/\/|[\\/])/i.test(attachmentPath);
13+
14+
if (!isRelativePath) return attachmentPath;
15+
16+
return pathToFileURL(
17+
path.resolve(path.dirname(documentPath), attachmentPath)
18+
).href;
19+
}
20+
521
// -----------------------------
622
// Markdown renderer
723
// -----------------------------
@@ -24,6 +40,7 @@ contextBridge.exposeInMainWorld("electronAPI", {
2440
ipcRenderer.invoke("confirm-tab-close", title),
2541
openFileByPath: (path) =>
2642
ipcRenderer.invoke("open-file-by-path", path),
43+
resolveLocalAttachment,
2744

2845
// Workspace watcher
2946
onWorkspaceUpdated: (callback) =>

0 commit comments

Comments
 (0)