fix(marketplace): enhance image tag handling in Markdown content

This commit is contained in:
charlie
2026-05-04 08:45:12 +08:00
parent a7672eb69c
commit 8b510ea034

View File

@@ -96,11 +96,11 @@
marked.use({
renderer: {
link (href, title, text) {
link({ href, title, text }) {
return `<a href="${href}" target="_blank" title="${title}">${text}</a>`
},
image (href, title, text) {
image({ href, title, text }) {
let link = isRelative(href)
if (link) {
@@ -113,8 +113,20 @@
},
})
// force fix Markdown content
function fixAllImageTagHref (content) {
const isRelativeImageSrc = (src) => src && !src.match(/^(?:[a-z][a-z\d+.-]*:|\/\/|#)/i) && isRelative(src)
return content.replace(/<img\b[^>]*>/gi, (img) => {
return img.replace(/\bsrc\s*=\s*(['"]?)([^'"\s>]+)\1/i, (attr, quote, src) => {
const link = isRelativeImageSrc(src)
return link ? attr.replace(src, fixLink(link)) : attr
})
})
}
// load exist points content
async function loadPage (points) {
async function loadPage(points) {
setMsg('Loading ...')
for (let url of points) {
@@ -131,7 +143,7 @@
}
}
content = marked.parse(content).replace('src="./', `src="${fixLink('')}`)
content = marked.parse(fixAllImageTagHref(content))
setContent(DOMPurify.sanitize(content))
}