Skip to content

Commit

Permalink
feat: #774 #751 知乎平台支持公式和表格
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Oct 10, 2023
1 parent 720c3b4 commit 21f2513
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 7 deletions.
4 changes: 2 additions & 2 deletions esbuild.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const isWindows = os.platform() === "win32"

let baseDir
if (isWatch || isServe) {
baseDir = "/Users/terwer/Documents/mydocs/SiYuanWorkspace/test/data/plugins/siyuan-plugin-publisher"
// baseDir = "/Users/zhangyue/Documents/terwer/SiyuanWorkspace/test/data/plugins/siyuan-plugin-publisher"
// baseDir = "/Users/terwer/Documents/mydocs/SiYuanWorkspace/test/data/plugins/siyuan-plugin-publisher"
baseDir = "/Users/zhangyue/Documents/terwer/SiyuanWorkspace/test/data/plugins/siyuan-plugin-publisher"
// baseDir = "/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/plugins/siyuan-plugin-publisher"
if (isWindows) {
baseDir = "C:\\Users\\terwer\\Documents\\mydocs\\SiyuanWorkspace\\test\\data\\plugins\\siyuan-plugin-publisher"
Expand Down
4 changes: 2 additions & 2 deletions src/adaptors/web/wechat/wechatWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,9 @@ class WechatWebAdaptor extends BaseWebApi {

// 发布格式
if (cfg?.pageType == PageTypeEnum.Markdown) {
post.description = post.markdown
updatedPost.description = updatedPost.markdown
} else {
post.description = post.html
updatedPost.description = updatedPost.html
}

return updatedPost
Expand Down
98 changes: 98 additions & 0 deletions src/adaptors/web/zhihu/zhihuUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import * as cheerio from "cheerio"

/**
* 知乎工具类
*
* @author terwer
* @since 1.6.0
*/
class ZhihuUtils {
/**
* 处理HTML中的表格,将表格头部移动到表格体部分
*
* @param html - 包含表格的HTML字符串
* @returns 处理后的HTML字符串
*/
public static processZHTable(html: string): string {
// 使用Cheerio加载HTML
const $ = cheerio.load(html)

// 获取thead内容
const theadContent = $("table thead").html()
// 移除thead
$("table thead").remove()
// 将thead内容添加到tbody的第一个位置
$("table tbody tr:first-child").before(theadContent)

// 选择表格元素并修改属性
const table = $("table")
table
.attr("data-draft-node", "block")
.attr("data-draft-type", "table")
.attr("data-size", "normal")
.attr("data-row-style", "normal")

return $.html()
}

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

// 选择所有带有类名"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)
})

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

export default ZhihuUtils
36 changes: 35 additions & 1 deletion src/adaptors/web/zhihu/zhihuWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
*/

import { BaseWebApi } from "~/src/adaptors/web/base/baseWebApi.ts"
import { CategoryInfo, Post, UserBlog } from "zhi-blog-api"
import { BlogConfig, CategoryInfo, PageTypeEnum, Post, UserBlog } from "zhi-blog-api"
import * as cheerio from "cheerio"
import { JsonUtil, StrUtil } from "zhi-common"
import CryptoJS from "crypto-js"
import { arrayToBuffer } from "~/src/utils/polyfillUtils.ts"
import { getAliOssClient } from "~/src/vendors/alioss/s3oss.ts"
import _ from "lodash"
import ZhihuUtils from "~/src/adaptors/web/zhihu/zhihuUtils.ts"

/**
* 知乎网页授权适配器
Expand Down Expand Up @@ -98,6 +100,38 @@ class ZhihuWebAdaptor extends BaseWebApi {
return result
}

public override async preEditPost(post: Post, id?: string, publishCfg?: any): Promise<Post> {
// 公共的属性预处理
const doc = await super.preEditPost(post, id, publishCfg)

// 知乎自定义的处理
const cfg: BlogConfig = publishCfg?.cfg
const updatedPost = _.cloneDeep(doc) as Post
const html = updatedPost.html
this.logger.info("准备处理知乎正文")
this.logger.debug("html =>", { html: html })
let updatedHtml = html

// 处理表格
updatedHtml = ZhihuUtils.processZHTable(updatedHtml)
// 处理数学公式
updatedHtml = ZhihuUtils.processZHMath(updatedHtml)

// 处理完毕
updatedPost.html = updatedHtml
this.logger.info("知乎正文处理完毕")
this.logger.debug("updatedHtml =>", { updatedHtml: updatedHtml })

// 发布格式
if (cfg?.pageType == PageTypeEnum.Markdown) {
updatedPost.description = updatedPost.markdown
} else {
updatedPost.description = updatedPost.html
}

return updatedPost
}

public async addPost(post: Post) {
const params = JSON.stringify({
title: post.title,
Expand Down
10 changes: 10 additions & 0 deletions testdata/zhihu/zhihu.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PATCH https://zhuanlan.zhihu.com/api/articles/660599054/draft
cookie: {{cookie}}
Content-Type: application/json

{
"content": "<p><br></p><table data-draft-node=\"block\" data-draft-type=\"table\" data-size=\"normal\"><tbody><tr><th>f</th><th>dfd</th><th>df</th><th>df</th><th>df</th></tr><tr><td>df</td><td>df</td><td>df</td><td>d</td><td></td></tr><tr><td>df</td><td>d</td><td></td><td></td><td></td></tr><tr><td>df</td><td>d</td><td></td><td></td><td></td></tr></tbody></table><p><br></p>",
"table_of_contents": false,
"delta_time": 10
}

4 changes: 2 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ const isServe = process.env.IS_SERVE
const isWatch = args.watch || args.w || false
const isDev = isServe || isWatch || debugMode
const isWindows = process.platform === "win32"
let devDistDir = "/Users/terwer/Documents/mydocs/SiYuanWorkspace/test/data/plugins/siyuan-plugin-publisher"
// let devDistDir = "/Users/zhangyue/Documents/terwer/SiyuanWorkspace/test/data/plugins/siyuan-plugin-publisher"
// let devDistDir = "/Users/terwer/Documents/mydocs/SiYuanWorkspace/test/data/plugins/siyuan-plugin-publisher"
let devDistDir = "/Users/zhangyue/Documents/terwer/SiyuanWorkspace/test/data/plugins/siyuan-plugin-publisher"
// let devDistDir = "/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/plugins/siyuan-plugin-publisher"
if (isWindows) {
devDistDir = "C:\\Users\\terwer\\Documents\\mydocs\\SiyuanWorkspace\\test\\data\\plugins\\siyuan-plugin-publisher"
Expand Down

0 comments on commit 21f2513

Please sign in to comment.