Skip to content

Commit

Permalink
fix(markdown): images src with baseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Jan 20, 2023
1 parent 383f707 commit d480eba
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/runtime/components/ContentRendererMarkdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { h, resolveComponent, Text, defineComponent } from 'vue'
import destr from 'destr'
import { pascalCase } from 'scule'
import { withBase } from 'ufo'
import { find, html } from 'property-information'
// eslint-disable-next-line import/no-named-as-default
import htmlTags from 'html-tags'
Expand Down Expand Up @@ -116,7 +117,17 @@ function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedC
}
if (node.tag === 'script') {
return renderToText(node)
return renderToString(node)
}
// Resolve component props
const props = propsToData(node, documentMeta)
// Prepend base URL to absolute images
if (node.tag === 'img') {
if (props.src?.startsWith('/') && !props.src.startsWith('//')) {
props.src = withBase(props.src, useRuntimeConfig().app.baseURL)
}
}
const originalTag = node.tag!
Expand All @@ -132,16 +143,14 @@ function renderNode (node: MarkdownNode, h: CreateElement, documentMeta: ParsedC
component.tag = originalTag
}
const props = propsToData(node, documentMeta)
return h(
component as any,
props,
renderSlots(node, h, documentMeta, { ...parentScope, ...props })
)
}
function renderToText (node: MarkdownNode) {
function renderToString (node: MarkdownNode) {
if (node.type === 'text') {
return node.value
}
Expand All @@ -150,7 +159,7 @@ function renderToText (node: MarkdownNode) {
return `<${node.tag}>`
}
return `<${node.tag}>${node.children?.map(renderToText).join('') || ''}</${node.tag}>`
return `<${node.tag}>${node.children?.map(renderToString).join('') || ''}</${node.tag}>`
}
function renderBinding (node: MarkdownNode, h: CreateElement, documentMeta: ParsedContentMeta, parentScope: any = {}): VNode {
Expand Down

0 comments on commit d480eba

Please sign in to comment.