Skip to content

Commit

Permalink
fix: normalize relative img src (#514)
Browse files Browse the repository at this point in the history
fix #450
  • Loading branch information
brc-dd committed Feb 22, 2022
1 parent 34d1542 commit 9270477
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/node/markdown/markdown.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -67,6 +68,7 @@ export const createMarkdownRenderer = (
.use(hoistPlugin)
.use(containerPlugin)
.use(headingPlugin)
.use(imagePlugin)
.use(
linkPlugin,
{
Expand Down
15 changes: 15 additions & 0 deletions 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)
}
}

0 comments on commit 9270477

Please sign in to comment.