Skip to content

Commit

Permalink
Merge branch 'master' into ru
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh authored Sep 9, 2024
2 parents 12f8dfa + dd52ffc commit a45ee8f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{".":"2.24.5"}
{".":"2.24.6"}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [2.24.6](https://github.com/pionxzh/chatgpt-exporter/compare/userscript-v2.24.5...userscript-v2.24.6) (2024-09-08)


### Bug Fixes

* fix unexpected latex formula escape ([30f0b62](https://github.com/pionxzh/chatgpt-exporter/commit/30f0b623b306aff6ec5a2278207cf2481a609b85))

## [2.24.5](https://github.com/pionxzh/chatgpt-exporter/compare/userscript-v2.24.4...userscript-v2.24.5) (2024-09-07)


Expand Down
42 changes: 34 additions & 8 deletions dist/chatgpt.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// @name:zh-CN ChatGPT Exporter
// @name:zh-TW ChatGPT Exporter
// @namespace pionxzh
// @version 2.24.5
// @version 2.24.6
// @author pionxzh
// @description Easily export the whole ChatGPT conversation history for further analysis or sharing.
// @description:zh-CN 轻松导出 ChatGPT 聊天记录,以便进一步分析或分享。
Expand Down Expand Up @@ -20903,6 +20903,7 @@ ${content2.text}
downloadFile("chatgpt-export.zip", "application/zip", blob);
return true;
}
const LatexRegex$1 = /(\s\$\$.+\$\$\s|\s\$.+\$\s|\\\[.+\\\]|\\\(.+\\\))|(^\$$[\S\s]+^\$$)|(^\$\$[\S\s]+^\$\$$)/gm;
function conversationToMarkdown(conversation, metaList) {
const { id, title: title2, model, modelSlug, createTime, updateTime, conversationNodes } = conversation;
const source = `${baseUrl}/c/${id}`;
Expand Down Expand Up @@ -20942,17 +20943,29 @@ ${_metaList.join("\n")}
`;
}
const author = transformAuthor$1(message.author);
let postSteps = [];
const postSteps = [];
if (message.author.role === "assistant") {
postSteps = [...postSteps, (input) => transformFootNotes$1(input, message.metadata)];
postSteps.push((input) => transformFootNotes$1(input, message.metadata));
}
if (message.author.role === "assistant") {
postSteps = [...postSteps, (input) => {
if (!/```/.test(input)) {
input = input.replace(/^\\\[(.+)\\\]$/gm, "$$$$$1$$$$").replace(/\\\[/g, "$").replace(/\\\]/g, "$").replace(/\\\(/g, "$").replace(/\\\)/g, "$");
postSteps.push((input) => {
input = input.replace(/^\\\[(.+)\\\]$/gm, "$$$$$1$$$$").replace(/\\\[/g, "$").replace(/\\\]/g, "$").replace(/\\\(/g, "$").replace(/\\\)/g, "$");
const matches = input.match(LatexRegex$1);
const isCodeBlock = /```/.test(input);
if (!isCodeBlock && matches) {
let index2 = 0;
input = input.replace(LatexRegex$1, () => {
return `╬${index2++}╬`;
});
}
return toMarkdown(fromMarkdown(input));
}];
let transformed = toMarkdown(fromMarkdown(input));
if (!isCodeBlock && matches) {
transformed = transformed.replace(/╬(\d+)╬/g, (_24, index2) => {
return matches[+index2];
});
}
return transformed;
});
}
const postProcess = (input) => postSteps.reduce((acc, fn2) => fn2(acc), input);
const content22 = transformContent$1(message.content, message.metadata, postProcess);
Expand Down Expand Up @@ -21065,6 +21078,7 @@ ${content2.text}
copyToClipboard(standardizeLineBreaks(text2));
return true;
}
const LatexRegex = /(\s\$\$.+\$\$\s|\s\$.+\$\s|\\\[.+\\\]|\\\(.+\\\))|(^\$$[\S\s]+^\$$)|(^\$\$[\S\s]+^\$\$$)/gm;
function transformMessage(message) {
var _a, _b, _c;
if (!message || !message.content) return null;
Expand All @@ -21080,12 +21094,24 @@ ${content2.text}
}
const author = transformAuthor(message.author);
let content2 = transformContent(message.content, message.metadata);
const matches = content2.match(LatexRegex);
if (matches) {
let index2 = 0;
content2 = content2.replace(LatexRegex, () => {
return `╬${index2++}╬`;
});
}
if (message.author.role === "assistant") {
content2 = transformFootNotes(content2, message.metadata);
}
if (message.author.role === "assistant" && content2) {
content2 = reformatContent(content2);
}
if (matches) {
content2 = content2.replace(/╬(\d+)╬/g, (_24, index2) => {
return matches[+index2];
});
}
return `${author}:
${content2}`;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@pionxzh/chatgpt-exporter",
"type": "module",
"version": "2.24.5",
"version": "2.24.6",
"private": true,
"packageManager": "[email protected]",
"title": "ChatGPT Exporter",
Expand Down
47 changes: 34 additions & 13 deletions src/exporter/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions src/exporter/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
Expand All @@ -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}`
}

Expand Down

0 comments on commit a45ee8f

Please sign in to comment.