@@ -63,6 +63,8 @@ export async function exportAllToMarkdown(fileNameFormat: string, apiConversatio
63
63
return true
64
64
}
65
65
66
+ const LatexRegex = / ( \s \$ \$ .+ \$ \$ \s | \s \$ .+ \$ \s | \\ \[ .+ \\ \] | \\ \( .+ \\ \) ) | ( ^ \$ $ [ \S \s ] + ^ \$ $ ) | ( ^ \$ \$ [ \S \s ] + ^ \$ \$ $ ) / gm
67
+
66
68
function conversationToMarkdown ( conversation : ConversationResult , metaList ?: ExportMeta [ ] ) {
67
69
const { id, title, model, modelSlug, createTime, updateTime, conversationNodes } = conversation
68
70
const source = `${ baseUrl } /c/${ id } `
@@ -125,25 +127,44 @@ function conversationToMarkdown(conversation: ConversationResult, metaList?: Exp
125
127
126
128
const author = transformAuthor ( message . author )
127
129
128
- let postSteps : Array < ( input : string ) => string > = [ ]
130
+ const postSteps : Array < ( input : string ) => string > = [ ]
129
131
if ( message . author . role === 'assistant' ) {
130
- postSteps = [ ... postSteps , input => transformFootNotes ( input , message . metadata ) ]
132
+ postSteps . push ( input => transformFootNotes ( input , message . metadata ) )
131
133
}
132
134
// Only message from assistant will be reformatted
133
135
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
+
135
147
// 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
+ } )
144
155
}
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
+ } )
147
168
}
148
169
const postProcess = ( input : string ) => postSteps . reduce ( ( acc , fn ) => fn ( acc ) , input )
149
170
const content = transformContent ( message . content , message . metadata , postProcess )
0 commit comments