mirror of
https://github.com/pionxzh/chatgpt-exporter.git
synced 2026-07-23 09:00:51 -05:00
fix: fix unexpected latex formula escape
This commit is contained in:
@@ -63,6 +63,8 @@ export async function exportAllToMarkdown(fileNameFormat: string, apiConversatio
|
||||
return true
|
||||
}
|
||||
|
||||
const LatexRegex = /(\s\$\$.+\$\$\s|\s\$.+\$\s|\\\[.+\\\]|\\\(.+\\\))|(^\$$[\S\s]+^\$$)|(^\$\$[\S\s]+^\$\$$)/gm
|
||||
|
||||
function conversationToMarkdown(conversation: ConversationResult, metaList?: ExportMeta[]) {
|
||||
const { id, title, model, modelSlug, createTime, updateTime, conversationNodes } = conversation
|
||||
const source = `${baseUrl}/c/${id}`
|
||||
@@ -125,25 +127,44 @@ function conversationToMarkdown(conversation: ConversationResult, metaList?: Exp
|
||||
|
||||
const author = transformAuthor(message.author)
|
||||
|
||||
let postSteps: Array<(input: string) => string> = []
|
||||
const postSteps: Array<(input: string) => string> = []
|
||||
if (message.author.role === 'assistant') {
|
||||
postSteps = [...postSteps, input => transformFootNotes(input, message.metadata)]
|
||||
postSteps.push(input => transformFootNotes(input, message.metadata))
|
||||
}
|
||||
// Only message from assistant will be reformatted
|
||||
if (message.author.role === 'assistant') {
|
||||
postSteps = [...postSteps, (input) => {
|
||||
postSteps.push((input) => {
|
||||
// Replace mathematical formula annotation
|
||||
input = input
|
||||
.replace(/^\\\[(.+)\\\]$/gm, '$$$$$1$$$$')
|
||||
.replace(/\\\[/g, '$')
|
||||
.replace(/\\\]/g, '$')
|
||||
.replace(/\\\(/g, '$')
|
||||
.replace(/\\\)/g, '$')
|
||||
|
||||
const matches = input.match(LatexRegex)
|
||||
|
||||
// Skip code block as the following steps can potentially break the code
|
||||
if (!(/```/.test(input))) {
|
||||
// Replace mathematical formula annotation
|
||||
input = input
|
||||
.replace(/^\\\[(.+)\\\]$/gm, '$$$$$1$$$$')
|
||||
.replace(/\\\[/g, '$')
|
||||
.replace(/\\\]/g, '$')
|
||||
.replace(/\\\(/g, '$')
|
||||
.replace(/\\\)/g, '$')
|
||||
const isCodeBlock = /```/.test(input)
|
||||
if (!isCodeBlock && matches) {
|
||||
let index = 0
|
||||
input = input.replace(LatexRegex, () => {
|
||||
// Replace it with `╬${index}╬` to avoid markdown processor ruin the formula
|
||||
return `╬${index++}╬`
|
||||
})
|
||||
}
|
||||
return toMarkdown(fromMarkdown(input))
|
||||
}]
|
||||
|
||||
let transformed = toMarkdown(fromMarkdown(input))
|
||||
|
||||
if (!isCodeBlock && matches) {
|
||||
// Replace `╬${index}╬` back to the original latex
|
||||
transformed = transformed.replace(/╬(\d+)╬/g, (_, index) => {
|
||||
return matches[+index]
|
||||
})
|
||||
}
|
||||
|
||||
return transformed
|
||||
})
|
||||
}
|
||||
const postProcess = (input: string) => postSteps.reduce((acc, fn) => fn(acc), input)
|
||||
const content = transformContent(message.content, message.metadata, postProcess)
|
||||
|
||||
@@ -29,6 +29,8 @@ export async function exportToText() {
|
||||
return true
|
||||
}
|
||||
|
||||
const LatexRegex = /(\s\$\$.+\$\$\s|\s\$.+\$\s|\\\[.+\\\]|\\\(.+\\\))|(^\$$[\S\s]+^\$$)|(^\$\$[\S\s]+^\$\$$)/gm
|
||||
|
||||
function transformMessage(message?: ConversationNodeMessage) {
|
||||
if (!message || !message.content) return null
|
||||
|
||||
@@ -53,6 +55,16 @@ function transformMessage(message?: ConversationNodeMessage) {
|
||||
|
||||
const author = transformAuthor(message.author)
|
||||
let content = transformContent(message.content, message.metadata)
|
||||
|
||||
const matches = content.match(LatexRegex)
|
||||
if (matches) {
|
||||
let index = 0
|
||||
content = content.replace(LatexRegex, () => {
|
||||
// Replace it with `╬${index}╬` to avoid markdown processor ruin the formula
|
||||
return `╬${index++}╬`
|
||||
})
|
||||
}
|
||||
|
||||
if (message.author.role === 'assistant') {
|
||||
content = transformFootNotes(content, message.metadata)
|
||||
}
|
||||
@@ -61,6 +73,14 @@ function transformMessage(message?: ConversationNodeMessage) {
|
||||
if (message.author.role === 'assistant' && content) {
|
||||
content = reformatContent(content)
|
||||
}
|
||||
|
||||
if (matches) {
|
||||
// Replace `╬${index}╬` back to the original latex
|
||||
content = content.replace(/╬(\d+)╬/g, (_, index) => {
|
||||
return matches[+index]
|
||||
})
|
||||
}
|
||||
|
||||
return `${author}:\n${content}`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user