Skip to content

Commit 9270477

Browse files
authored
fix: normalize relative img src (#514)
fix #450
1 parent 34d1542 commit 9270477

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/node/markdown/markdown.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { hoistPlugin } from './plugins/hoist'
1111
import { preWrapperPlugin } from './plugins/preWrapper'
1212
import { linkPlugin } from './plugins/link'
1313
import { headingPlugin } from './plugins/headings'
14+
import { imagePlugin } from './plugins/image'
1415
import { Header } from '../shared'
1516
import anchor from 'markdown-it-anchor'
1617
import attrs from 'markdown-it-attrs'
@@ -67,6 +68,7 @@ export const createMarkdownRenderer = (
6768
.use(hoistPlugin)
6869
.use(containerPlugin)
6970
.use(headingPlugin)
71+
.use(imagePlugin)
7072
.use(
7173
linkPlugin,
7274
{

src/node/markdown/plugins/image.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// markdown-it plugin for normalizing image source
2+
3+
import MarkdownIt from 'markdown-it'
4+
import { EXTERNAL_URL_RE } from '../../shared'
5+
6+
export const imagePlugin = (md: MarkdownIt) => {
7+
md.renderer.rules.image = (tokens, idx, options, env, self) => {
8+
const token = tokens[idx]
9+
const url = token.attrGet('src')
10+
if (url && !EXTERNAL_URL_RE.test(url) && !/^\.?\//.test(url)) {
11+
token.attrSet('src', './' + url)
12+
}
13+
return self.renderToken(tokens, idx, options)
14+
}
15+
}

0 commit comments

Comments
 (0)