Skip to content

Commit

Permalink
fix: post image replace not work
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 30, 2024
1 parent 31265d2 commit b552a2a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion libs/zhi-siyuan-picgo/src/lib/parser/ImageParser.ts
Expand Up @@ -217,7 +217,15 @@ export class ImageParser {

// 使用正则表达式和replace方法来实现replaceAll方法
// 将search转换为正则表达式,使用g标志表示全局匹配
const imgRegex = new RegExp(img, "g")
//
// 注意需要编码,否则无法替换
// 问题说明:由于原始图片链接中包含特殊字符,如斜杠和点,这些字符在正则表达式中具有特殊含义,可能导致匹配失败。需要对原始图片链接中的特殊字符进行转义处理,以确保正则表达式能够正确匹配。
// 修复方法:对原始图片链接中的特殊字符进行转义处理,然后再创建正则表达式进行匹配替换。
// MDN 的解释
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#flags_in_constructor
const escapedImg = img.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // 转义处理
const imgRegex = new RegExp(escapedImg, "g")
this.logger.debug("imgRegex =>", imgRegex)
newcontent = newcontent.replace(imgRegex, newImg)
}

Expand Down
1 change: 1 addition & 0 deletions libs/zhi-siyuan-picgo/src/lib/siyuanPicgoPostApi.ts
Expand Up @@ -157,6 +157,7 @@ class SiyuanPicgoPostApi {
ret.hasImages = true

const imageItemArray = await this.doConvertImagesToImagesItemArray(attrs, uniqueLocalImages)
this.logger.debug("pared image array =>", imageItemArray)

const replaceMap = {} as any
let hasLocalImages = false
Expand Down

0 comments on commit b552a2a

Please sign in to comment.