Skip to content

Commit

Permalink
fix: #927 修复 CSDN 和知乎公式解析不正常的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Dec 3, 2023
1 parent abc9095 commit aee7081
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/adaptors/web/csdn/csdnUtils.ts
Expand Up @@ -87,11 +87,11 @@ class CsdnUtils {

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

// 处理两个$符号包裹的公式
const doubleDollarRegex = /\$\$([^$]+)\$\$/g
$("*").each((index, element) => {
$("*:not(pre)").each((index, element) => {
const content = $(element).html()
const newContent = content.replace(doubleDollarRegex, (match, mathContent) => {
const mathHtml = KatexUtils.renderToString(mathContent)
Expand All @@ -102,7 +102,7 @@ class CsdnUtils {

// 处理一个$符号包裹的公式
const singleDollarRegex = /\$([^$]+)\$/g
$("*").each((index, element) => {
$("*:not(pre)").each((index, element) => {
const content = $(element).html()
const newContent = content.replace(singleDollarRegex, (match, mathContent) => {
const mathHtml = KatexUtils.renderToString(mathContent)
Expand Down
6 changes: 3 additions & 3 deletions src/adaptors/web/zhihu/zhihuUtils.ts
Expand Up @@ -62,7 +62,7 @@ class ZhihuUtils {

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

// // 选择所有带有类名"language-math"的<span>元素
// $("span.language-math").each((index, element) => {
Expand Down Expand Up @@ -92,7 +92,7 @@ class ZhihuUtils {

// 处理两个$符号包裹的公式
const doubleDollarRegex = /\$\$([^$]+)\$\$/g
$("*").each((index, element) => {
$("*: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)}"
Expand All @@ -103,7 +103,7 @@ class ZhihuUtils {

// 处理一个$符号包裹的公式
const singleDollarRegex = /\$([^$]+)\$/g
$("*").each((index, element) => {
$("*: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)}"
Expand Down
4 changes: 4 additions & 0 deletions src/utils/KatexUtils.ts
Expand Up @@ -24,6 +24,7 @@
*/

import katex from "katex"
import { createAppLogger } from "~/src/utils/appLogger.ts"

/**
* 公式渲染
Expand All @@ -32,12 +33,15 @@ import katex from "katex"
* @since 1.18.6
*/
class KatexUtils {
private static logger = createAppLogger("katex-utils")

/**
* 获得要渲染 KaTeX 表达式的 HTML
*
* @param mathExpression katex
*/
public static renderToString(mathExpression: string) {
this.logger.debug("准备处理 Katex =>", { mathExpression: mathExpression })
return katex.renderToString(mathExpression)
}
}
Expand Down

0 comments on commit aee7081

Please sign in to comment.