Skip to content

Commit 30f0b62

Browse files
committed
fix: fix unexpected latex formula escape
1 parent 67af2bf commit 30f0b62

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

src/exporter/markdown.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export async function exportAllToMarkdown(fileNameFormat: string, apiConversatio
6363
return true
6464
}
6565

66+
const LatexRegex = /(\s\$\$.+\$\$\s|\s\$.+\$\s|\\\[.+\\\]|\\\(.+\\\))|(^\$$[\S\s]+^\$$)|(^\$\$[\S\s]+^\$\$$)/gm
67+
6668
function conversationToMarkdown(conversation: ConversationResult, metaList?: ExportMeta[]) {
6769
const { id, title, model, modelSlug, createTime, updateTime, conversationNodes } = conversation
6870
const source = `${baseUrl}/c/${id}`
@@ -125,25 +127,44 @@ function conversationToMarkdown(conversation: ConversationResult, metaList?: Exp
125127

126128
const author = transformAuthor(message.author)
127129

128-
let postSteps: Array<(input: string) => string> = []
130+
const postSteps: Array<(input: string) => string> = []
129131
if (message.author.role === 'assistant') {
130-
postSteps = [...postSteps, input => transformFootNotes(input, message.metadata)]
132+
postSteps.push(input => transformFootNotes(input, message.metadata))
131133
}
132134
// Only message from assistant will be reformatted
133135
if (message.author.role === 'assistant') {
134-
postSteps = [...postSteps, (input) => {
136+
postSteps.push((input) => {
137+
// Replace mathematical formula annotation
138+
input = input
139+
.replace(/^\\\[(.+)\\\]$/gm, '$$$$$1$$$$')
140+
.replace(/\\\[/g, '$')
141+
.replace(/\\\]/g, '$')
142+
.replace(/\\\(/g, '$')
143+
.replace(/\\\)/g, '$')
144+
145+
const matches = input.match(LatexRegex)
146+
135147
// Skip code block as the following steps can potentially break the code
136-
if (!(/```/.test(input))) {
137-
// Replace mathematical formula annotation
138-
input = input
139-
.replace(/^\\\[(.+)\\\]$/gm, '$$$$$1$$$$')
140-
.replace(/\\\[/g, '$')
141-
.replace(/\\\]/g, '$')
142-
.replace(/\\\(/g, '$')
143-
.replace(/\\\)/g, '$')
148+
const isCodeBlock = /```/.test(input)
149+
if (!isCodeBlock && matches) {
150+
let index = 0
151+
input = input.replace(LatexRegex, () => {
152+
// Replace it with `╬${index}╬` to avoid markdown processor ruin the formula
153+
return `╬${index++}╬`
154+
})
144155
}
145-
return toMarkdown(fromMarkdown(input))
146-
}]
156+
157+
let transformed = toMarkdown(fromMarkdown(input))
158+
159+
if (!isCodeBlock && matches) {
160+
// Replace `╬${index}╬` back to the original latex
161+
transformed = transformed.replace(/(\d+)/g, (_, index) => {
162+
return matches[+index]
163+
})
164+
}
165+
166+
return transformed
167+
})
147168
}
148169
const postProcess = (input: string) => postSteps.reduce((acc, fn) => fn(acc), input)
149170
const content = transformContent(message.content, message.metadata, postProcess)

src/exporter/text.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export async function exportToText() {
2929
return true
3030
}
3131

32+
const LatexRegex = /(\s\$\$.+\$\$\s|\s\$.+\$\s|\\\[.+\\\]|\\\(.+\\\))|(^\$$[\S\s]+^\$$)|(^\$\$[\S\s]+^\$\$$)/gm
33+
3234
function transformMessage(message?: ConversationNodeMessage) {
3335
if (!message || !message.content) return null
3436

@@ -53,6 +55,16 @@ function transformMessage(message?: ConversationNodeMessage) {
5355

5456
const author = transformAuthor(message.author)
5557
let content = transformContent(message.content, message.metadata)
58+
59+
const matches = content.match(LatexRegex)
60+
if (matches) {
61+
let index = 0
62+
content = content.replace(LatexRegex, () => {
63+
// Replace it with `╬${index}╬` to avoid markdown processor ruin the formula
64+
return `╬${index++}╬`
65+
})
66+
}
67+
5668
if (message.author.role === 'assistant') {
5769
content = transformFootNotes(content, message.metadata)
5870
}
@@ -61,6 +73,14 @@ function transformMessage(message?: ConversationNodeMessage) {
6173
if (message.author.role === 'assistant' && content) {
6274
content = reformatContent(content)
6375
}
76+
77+
if (matches) {
78+
// Replace `╬${index}╬` back to the original latex
79+
content = content.replace(/(\d+)/g, (_, index) => {
80+
return matches[+index]
81+
})
82+
}
83+
6484
return `${author}:\n${content}`
6585
}
6686

0 commit comments

Comments
 (0)