fix: normalize relative img src (#514)

fix #450
pull/545/head
Divyansh Singh 3 years ago committed by GitHub
parent 34d1542f46
commit 9270477fa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,6 +11,7 @@ import { hoistPlugin } from './plugins/hoist'
import { preWrapperPlugin } from './plugins/preWrapper' import { preWrapperPlugin } from './plugins/preWrapper'
import { linkPlugin } from './plugins/link' import { linkPlugin } from './plugins/link'
import { headingPlugin } from './plugins/headings' import { headingPlugin } from './plugins/headings'
import { imagePlugin } from './plugins/image'
import { Header } from '../shared' import { Header } from '../shared'
import anchor from 'markdown-it-anchor' import anchor from 'markdown-it-anchor'
import attrs from 'markdown-it-attrs' import attrs from 'markdown-it-attrs'
@ -67,6 +68,7 @@ export const createMarkdownRenderer = (
.use(hoistPlugin) .use(hoistPlugin)
.use(containerPlugin) .use(containerPlugin)
.use(headingPlugin) .use(headingPlugin)
.use(imagePlugin)
.use( .use(
linkPlugin, linkPlugin,
{ {

@ -0,0 +1,15 @@
// markdown-it plugin for normalizing image source
import MarkdownIt from 'markdown-it'
import { EXTERNAL_URL_RE } from '../../shared'
export const imagePlugin = (md: MarkdownIt) => {
md.renderer.rules.image = (tokens, idx, options, env, self) => {
const token = tokens[idx]
const url = token.attrGet('src')
if (url && !EXTERNAL_URL_RE.test(url) && !/^\.?\//.test(url)) {
token.attrSet('src', './' + url)
}
return self.renderToken(tokens, idx, options)
}
}
Loading…
Cancel
Save