Skip to content

Commit

Permalink
fix: #964 修复转义文本导致的知乎依旧公式解析错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jan 8, 2024
1 parent 1bc1a03 commit 988b46d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/adaptors/web/csdn/csdnUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CsdnUtils {

public static processCsdnMath(html: string): string {
// 使用Cheerio加载HTML
const $ = cheerio.load(html, { xmlMode: true })
const $ = cheerio.load(html, { xmlMode: true, decodeEntities: false })

// 处理两个$符号包裹的公式
const doubleDollarRegex = /\$\$([^$]+)\$\$/g
Expand Down
106 changes: 63 additions & 43 deletions src/adaptors/web/zhihu/zhihuUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,58 +62,78 @@ class ZhihuUtils {

public static processZHMath(html: string): string {
// 使用Cheerio加载HTML
const $ = cheerio.load(html, { xmlMode: true })
const $ = cheerio.load(html, { xmlMode: true, decodeEntities: false })

// // 选择所有带有类名"language-math"的<span>元素
// $("span.language-math").each((index, element) => {
// // 获取元素的文本内容
// const mathContent = $(element).text()
//
// // 创建替代的<img>标签
// const imgTag = `<img eeimg="1" src="//www.zhihu.com/equation?tex=${encodeURIComponent(mathContent)}"
// alt="${mathContent}" />`
//
// // 用新的<img>标签替换原始元素
// $(element).replaceWith(imgTag)
// })
//
// // 选择所有带有类名"language-math"的<div>元素
// $("div.language-math").each((index, element) => {
// // 获取元素的文本内容
// const mathContent = $(element).text()
//
// // 创建替代的<img>标签
// const imgTag = `<p><img eeimg="1" src="//www.zhihu.com/equation?tex=${encodeURIComponent(mathContent)}"
// alt="${mathContent}"/></p>`
//
// // 用新的<img>标签替换原始元素
// $(element).replaceWith(imgTag)
// })
// 处理两个$符号和一个$符号包裹的公式
const mathRegex = /\$\$([^\$]+)\$\$|\$([^\$]+)\$/g

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

// 处理一个$符号包裹的公式
const singleDollarRegex = /\$([^$]+)\$/g
$("*:not(pre)").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}" />`
alt="${mathContent}" />`
})
$(element).html(newContent)
})

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

// // 使用Cheerio加载HTML
// const $ = cheerio.load(html, { xmlMode: true })
//
// // // 选择所有带有类名"language-math"的<span>元素
// // $("span.language-math").each((index, element) => {
// // // 获取元素的文本内容
// // const mathContent = $(element).text()
// //
// // // 创建替代的<img>标签
// // const imgTag = `<img eeimg="1" src="//www.zhihu.com/equation?tex=${encodeURIComponent(mathContent)}"
// // alt="${mathContent}" />`
// //
// // // 用新的<img>标签替换原始元素
// // $(element).replaceWith(imgTag)
// // })
// //
// // // 选择所有带有类名"language-math"的<div>元素
// // $("div.language-math").each((index, element) => {
// // // 获取元素的文本内容
// // const mathContent = $(element).text()
// //
// // // 创建替代的<img>标签
// // const imgTag = `<p><img eeimg="1" src="//www.zhihu.com/equation?tex=${encodeURIComponent(mathContent)}"
// // alt="${mathContent}"/></p>`
// //
// // // 用新的<img>标签替换原始元素
// // $(element).replaceWith(imgTag)
// // })
//
// // 处理两个$符号包裹的公式
// const doubleDollarRegex = /\$\$([^$]+)\$\$/g
// $("*:not(pre)").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
// $("*:not(pre)").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 988b46d

Please sign in to comment.