Skip to content

Commit

Permalink
fix: 兼容知乎公式
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Nov 8, 2023
1 parent fbad5ee commit 78688cd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/adaptors/web/zhihu/zhihuUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class ZhihuUtils {
const $ = cheerio.load(html)

// 选择所有带有类名"language-math"的<span>元素
debugger
$("span.language-math").each((index, element) => {
// 获取元素的文本内容
const mathContent = $(element).text()
Expand All @@ -90,6 +91,28 @@ class ZhihuUtils {
$(element).replaceWith(imgTag)
})

// 处理两个$符号包裹的公式
const doubleDollarRegex = /\$\$([^$]+)\$\$/g
$("*").each((index, element) => {
const content = $(element).html()
const newContent = content.replace(doubleDollarRegex, (match, mathContent) => {
return `<img eeimg="1" src="//www.zhihu.com/equation?tex=${encodeURIComponent(mathContent)}"
alt="${mathContent}" />`
})
$(element).html(newContent)
})

// 处理一个$符号包裹的公式
const singleDollarRegex = /\$([^$]+)\$/g
$("*").each((index, element) => {
const content = $(element).html()
const newContent = content.replace(singleDollarRegex, (match, mathContent) => {
return `<img eeimg="1" src="//www.zhihu.com/equation?tex=${encodeURIComponent(mathContent)}"
alt="${mathContent}" />`
})
$(element).html(newContent)
})

// 输出修改后的HTML
return $.html()
}
Expand Down

0 comments on commit 78688cd

Please sign in to comment.