Skip to content

Commit

Permalink
fix: #1245 发布csdn的时候, 代码块儿语言不显示
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jun 5, 2024
1 parent 5db7c1a commit b2abe6c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/adaptors/web/csdn/csdnUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import CryptoJS from "crypto-js"
import Base64 from "crypto-js/enc-base64"
import * as cheerio from "cheerio"
import KatexUtils from "~/src/utils/katexUtils.ts"
import { LEGENCY_SHARED_API } from "~/src/utils/constants.ts"
import { CsdnWebAdaptor } from "~/src/adaptors/web/csdn/csdnWebAdaptor.ts"

/**
* CSDN工具类,用于生成UUID和签名
Expand Down Expand Up @@ -114,6 +116,27 @@ class CsdnUtils {
// 输出修改后的HTML
return $.html()
}

/**
* 处理代码高亮
* @param adaptorInstance
* @param html
*/
public static async processPrismjs(adaptorInstance: CsdnWebAdaptor, html: string): Promise<string> {
const apiUrl = `${LEGENCY_SHARED_API}/prismjs`
// const apiUrl = `http://localhost:3000/api/prismjs`
const contentType = "application/json"
const headers = {
"Content-Type": contentType,
}
const params = JSON.stringify({
html: html,
})
const res = await adaptorInstance.webFetch(apiUrl, [headers], params, "POST", contentType, false)
console.log("processPrismjs res=>", res)

return res.html
}
}

export default CsdnUtils
15 changes: 11 additions & 4 deletions src/adaptors/web/csdn/csdnWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

import { BaseWebApi } from "~/src/adaptors/web/base/baseWebApi.ts"
import CsdnUtils from "~/src/adaptors/web/csdn/csdnUtils.ts"
import {BlogConfig, CategoryInfo, MediaObject, PageTypeEnum, Post, UserBlog} from "zhi-blog-api"
import { JsonUtil } from "zhi-common"
import { BlogConfig, CategoryInfo, MediaObject, PageTypeEnum, Post, UserBlog } from "zhi-blog-api"
import WebUtils from "~/src/adaptors/web/base/webUtils.ts"
import _ from "lodash-es"
import FormDataUtils from "~/src/utils/FormDataUtils.ts"
Expand Down Expand Up @@ -172,11 +171,15 @@ class CsdnWebAdaptor extends BaseWebApi {
}

public async addPost(post: Post) {
let csdnHtml = post.html
// 如果开启高亮,就使用高亮
csdnHtml = await CsdnUtils.processPrismjs(this, csdnHtml)

// 仅支持MD
const params = JSON.stringify({
title: post.title,
markdowncontent: post.markdown,
content: post.html,
content: csdnHtml,
readType: "public",
level: 0,
tags: post.mt_keywords,
Expand Down Expand Up @@ -217,12 +220,16 @@ class CsdnWebAdaptor extends BaseWebApi {
}

public async editPost(postid: string, post: Post, publish?: boolean): Promise<boolean> {
let csdnHtml = post.html
// 如果开启高亮,就使用高亮
csdnHtml = await CsdnUtils.processPrismjs(this, csdnHtml)

// 仅支持MD
const params = JSON.stringify({
id: postid,
title: post.title,
markdowncontent: post.markdown,
content: post.html,
content: csdnHtml,
readType: "public",
level: "1",
tags: post.mt_keywords,
Expand Down
8 changes: 8 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export const CATE_AUTO_NAME = "[auto]"
*/
export const MAX_TITLE_LENGTH = 10

/**
* 旧的通用接口
*
* @since 1.21.6
* @version 1.21.6
*/
export const LEGENCY_SHARED_API = "https://api.terwer.space/api"

/**
* 旧的通用 HTTP 代理
*
Expand Down

0 comments on commit b2abe6c

Please sign in to comment.