Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize image source #514

Merged
merged 2 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
}
}