Skip to content

Commit 9fc3f47

Browse files
XiNGRZartisnanbingxyz
authored andcommitted
✨ (useMarkdown.ts): add custom image renderer to prepend 'file://' to local image paths
closes #212
1 parent 206203b commit 9fc3f47

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/hooks/useMarkdown.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ export default function useMarkdown() {
104104
// Pass the token to the default renderer.
105105
return defaultRender(tokens, idx, options, env, self);
106106
};
107+
108+
const defaultImageRender = md.renderer.rules.image || function(tokens: any, idx: any, options: any, env: any, self: any) {
109+
return self.renderToken(tokens, idx, options);
110+
};
111+
112+
md.renderer.rules.image = function(tokens: any, idx: any, options: any, env: any, self: any) {
113+
const token = tokens[idx];
114+
const srcIndex = token.attrIndex('src');
115+
if (srcIndex >= 0) {
116+
const src = token.attrs[srcIndex][1];
117+
if (!src.startsWith('http') && !src.startsWith('file://')) {
118+
token.attrs[srcIndex][1] = `file://${src}`;
119+
}
120+
}
121+
return defaultImageRender(tokens, idx, options, env, self);
122+
};
123+
124+
107125
return {
108126
render: (str: string): string => md.render(str),
109127
};

0 commit comments

Comments
 (0)