Skip to content

Commit

Permalink
Merge pull request #1036 from terwer/feature/hotfix
Browse files Browse the repository at this point in the history
fix: #1035 代码块里面的文本不应该被错误转义
  • Loading branch information
terwer committed Feb 18, 2024
2 parents 990dc54 + 8fc148f commit 341ba53
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
99 changes: 96 additions & 3 deletions src/adaptors/base/baseExtendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,15 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {

// 处理标记
// #691 闪卡标记渲染成Markdown之后去除==
md = md.replace(/==([^=]+)==/g, '<span style="font-weight: bold;" class="mark">$1</span>')
// md = md.replace(/==([^=]+)==/g, '<span style="font-weight: bold;" class="mark">$1</span>')
md = this.replaceMarks(md)

// 处理加粗
// #821 html发布的时候会出现有些格式没有转化
// **这里是加粗**
// <p id="2<span data-type="strong">这里是加粗</span>
// <span data-type="strong">这里是加粗</span>
// md = md.replace(/\*\*(.*?)\*\*/g, '<span style="font-weight: bold;" data-type="strong">$1</span>')
md = md.replace(/\*\*(.*?)\*\*/g, '<span style="font-weight: bold;" data-type="strong">$1</span>')
md = this.replaceBold(md)

// 处理外链
const { getReadOnlyPublishPreferenceSetting } = usePreferenceSettingStore()
Expand All @@ -317,6 +318,98 @@ class BaseExtendApi extends WebApi implements IBlogApi, IWebApi {
return post
}

/**
* 替换标记
*
* @param md
* @protected
*/
private replaceMarks(md: string) {
// 匹配代码块
let codeBlockRegex = /```[\s\S]*?```/g

// 将代码块替换为占位符,避免在后续处理中受到影响
let placeholders = []
md = md.replace(codeBlockRegex, function (match) {
let placeholder = `CODE_BLOCK_${placeholders.length}`
placeholders.push(match)
return placeholder
})

// 匹配行内代码块
let inlineCodeRegex = /`[^`]*`/g
let inlineCodePlaceholders = []
md = md.replace(inlineCodeRegex, function (match) {
let placeholder = `INLINE_CODE_${inlineCodePlaceholders.length}`
inlineCodePlaceholders.push(match)
return placeholder
})

// 正则表达式,匹配严格符合 == 开始和结束的部分,但不在代码块和行内代码块内
let regex = /(?<!`|```)==([^=]+)==(?!.*(?:`|```))(?!.*INLINE_CODE_\d+)/g

// 替换非代码块和行内代码块内的 == 部分
md = md.replace(regex, '<span style="font-weight: bold;" class="mark">$1</span>')

// 将代码块恢复回去
for (let i = 0; i < placeholders.length; i++) {
md = md.replace(`CODE_BLOCK_${i}`, placeholders[i])
}

// 将行内代码块恢复回去
for (let i = 0; i < inlineCodePlaceholders.length; i++) {
md = md.replace(`INLINE_CODE_${i}`, inlineCodePlaceholders[i])
}

return md
}

/**
* 处理加粗
*
* @param md
* @private
*/
private replaceBold(md: string) {
// 匹配代码块
let codeBlockRegex = /```[\s\S]*?```/g

// 将代码块替换为占位符,避免在后续处理中受到影响
let placeholders = []
md = md.replace(codeBlockRegex, function (match) {
let placeholder = `CODE_BLOCK_${placeholders.length}`
placeholders.push(match)
return placeholder
})

// 匹配行内代码块
let inlineCodeRegex = /`[^`]*`/g
let inlineCodePlaceholders = []
md = md.replace(inlineCodeRegex, function (match) {
let placeholder = `INLINE_CODE_${inlineCodePlaceholders.length}`
inlineCodePlaceholders.push(match)
return placeholder
})

// 正则表达式,匹配严格符合 ** 开始和结束的部分,但不在代码块和行内代码块内
let regex = /(?<!`|```)\*\*([^*]+)\*\*(?!.*(?:`|```))(?!.*INLINE_CODE_\d+)/g

// 替换非代码块和行内代码块内的 ** 部分
md = md.replace(regex, '<span style="font-weight: bold;" class="bold">$1</span>')

// 将代码块恢复回去
for (let i = 0; i < placeholders.length; i++) {
md = md.replace(`CODE_BLOCK_${i}`, placeholders[i])
}

// 将行内代码块恢复回去
for (let i = 0; i < inlineCodePlaceholders.length; i++) {
md = md.replace(`INLINE_CODE_${i}`, inlineCodePlaceholders[i])
}

return md
}

/**
* 处理其他属性
*
Expand Down
2 changes: 1 addition & 1 deletion src/ai/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const shortDescPrompt = <AiPrompt>{
key: "desc",
content:
"请为这篇文章生成简明扼要的摘要,只处理文本,尽量返回中文摘要。" +
"标题长度不超过255个中文字符或512个英文字符。" +
"摘要长度不超过255个中文字符或512个英文字符。" +
"输出为 JSON 格式,键名为 desc,结果需放在 {} 内。" +
"完整结果必须是合法JSON,不得包含非法 JSON 字符。",
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/polyfillUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function readFileToBase64(url: string): Promise<string> {
const reader = new FileReader()
reader.readAsDataURL(body)
reader.onloadend = function () {
var base64data = reader.result as string
const base64data = reader.result as string
resolve(base64data)
}
reader.onerror = function (e) {
Expand Down

0 comments on commit 341ba53

Please sign in to comment.