Skip to content

Commit

Permalink
fix(core): parse links & comment images (close #31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Mar 4, 2024
1 parent 7aca8c2 commit 80e3963
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/core/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function parseText(text?: string) {
)
.replace(/<img[^>]+alt="([^"]*)"[^>]*>/gm, (_, alt) => alt) // 表情图片
.replace(/<img[^>]*>/gm, '') // 图标
.replace(/\/\/weibo.cn\/sinaurl\?u=(.+)/, (_, href) => decodeURIComponent(href)) // 去掉微博的链接跳转
.replace(/(https:)?\/\/weibo.cn\/sinaurl\?u=(.+)/, (_, __, href) => decodeURIComponent(href)) // 去掉微博的链接跳转

const retweetImg = /<a[^>]*href="([^"]*)"[^>]*>查看图片<\/a>/gm.exec(parsed)
let textImg = null
Expand Down Expand Up @@ -109,8 +109,12 @@ export function filterComments(

let img = ''
if (comment.url_struct) {
const { pic_ids, pic_infos } = comment.url_struct[0]
img = parseImg(imgSize, pic_ids, pic_infos)[0]
comment.url_struct.forEach((item: any) => {
if (item.pic_ids) {
const { pic_ids, pic_infos } = item
img = parseImg(imgSize, pic_ids, pic_infos)[0]
}
})
}

const res: Comment = {
Expand All @@ -133,7 +137,8 @@ export function filterComments(
console.log(`数据解析失败, id: ${comment.id}, ${e}`)
return null
}
}).filter((e): e is Comment => !!e)
})
.filter(Boolean) as Comment[]
}

export async function postFilter(
Expand Down Expand Up @@ -208,7 +213,7 @@ export function imgsParser(posts: Post[]): Set<string> {
].flat()
})
.flat()
.filter((e): e is string => !!e)
.filter(Boolean) as string[]

return new Set(imgs)
}
Expand Down

0 comments on commit 80e3963

Please sign in to comment.