diff --git a/src/node/markdown/markdown.ts b/src/node/markdown/markdown.ts index c9d68b9ec862..46cbff35bc20 100644 --- a/src/node/markdown/markdown.ts +++ b/src/node/markdown/markdown.ts @@ -11,6 +11,7 @@ import { hoistPlugin } from './plugins/hoist' import { preWrapperPlugin } from './plugins/preWrapper' import { linkPlugin } from './plugins/link' import { headingPlugin } from './plugins/headings' +import { imagePlugin } from './plugins/image' import { Header } from '../shared' import anchor from 'markdown-it-anchor' import attrs from 'markdown-it-attrs' @@ -67,6 +68,7 @@ export const createMarkdownRenderer = ( .use(hoistPlugin) .use(containerPlugin) .use(headingPlugin) + .use(imagePlugin) .use( linkPlugin, { diff --git a/src/node/markdown/plugins/image.ts b/src/node/markdown/plugins/image.ts new file mode 100644 index 000000000000..641a04c71c99 --- /dev/null +++ b/src/node/markdown/plugins/image.ts @@ -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) + } +}