Skip to content

Commit ec38902

Browse files
committed
fix: 首页图片不显示
1 parent 67b12c5 commit ec38902

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

.vitepress/theme/composables/posts.data.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,30 @@ export default createContentLoader(POST_FILES, {
3535
categories: frontmatter?.categories ?? [],
3636
tags: frontmatter.tags ?? [],
3737
url: url,
38-
excerpt,
38+
excerpt: updateImageSrc(excerpt, url),
3939
date: formatDate(frontmatter.date),
4040
}
4141
})
4242
.sort((a, b) => b.date.time - a.date.time)
4343
},
4444
})
4545

46+
/**
47+
* 更新 excerpt 中 <img> 标签的 src 属性
48+
* @param {string} excerpt - 原始的 excerpt 字符串
49+
* @param {string} urlPrefix - 要拼接的 URL 前缀
50+
* @returns {string} 更新后的 excerpt 字符串
51+
*/
52+
function updateImageSrc(excerpt: string | undefined, urlPrefix: string) {
53+
if (excerpt == undefined) return
54+
return excerpt.replace(
55+
/<img\s+([^>]*?)src=["']\.\/([^"']*?)["']([^>]*?)>/gi,
56+
(match, p1, p2, p3) => {
57+
return `<img ${p1}src="${urlPrefix}${p2}"${p3}>`
58+
}
59+
)
60+
}
61+
4662
function generateTitle(
4763
frontmatter: Record<string, any>,
4864
src: string | undefined

0 commit comments

Comments
 (0)