-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fix/agent-message' into develop
- Loading branch information
Showing
4 changed files
with
64 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 59 additions & 50 deletions
109
apps/builder/src/page/AI/components/MarkdownMessage/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,77 @@ | ||
class FormatTable { | ||
private hasStartTable: boolean = false | ||
private markCodeBlocks(markdownText: string) { | ||
const codeBlockRegex = /```[\s\S]*?```/g | ||
const placeholders: string[] = [] | ||
const placeholderPrefix = "__CODEBLOCK__" | ||
// handler --- | ||
let tempText = markdownText.replaceAll(/(^---\r?\n?)/gm, "\n---\n") | ||
tempText = tempText.replace(codeBlockRegex, (match) => { | ||
const placeholder = `${placeholderPrefix}${placeholders.length}` | ||
placeholders.push(match) | ||
return placeholder | ||
}) | ||
return { textWithoutCodeBlocks: tempText, placeholders } | ||
} | ||
const markCodeBlocks = (markdownText: string) => { | ||
const codeBlockRegex = /```[\s\S]*?```/g | ||
const placeholders: string[] = [] | ||
const placeholderPrefix = "__CODEBLOCK__" | ||
// handler "---" | ||
let tempText = markdownText.replaceAll(/(^---\r?\n?)/gm, "\n---\n") | ||
tempText = tempText.replace(codeBlockRegex, (match) => { | ||
const placeholder = `${placeholderPrefix}${placeholders.length}` | ||
placeholders.push(match) | ||
return placeholder | ||
}) | ||
return { textWithoutCodeBlocks: tempText, placeholders } | ||
} | ||
|
||
private convertTablesWithoutCodeBlocks(tempText: string) { | ||
let res = tempText | ||
let startRex = /(^\n?\|)|((\n\n)(.*)\|)/ | ||
let endRex = /(\|\n?$)|(\|\n\n)/ | ||
const convertTablesWithoutCodeBlocks = ( | ||
tempText: string, | ||
isOwnMessage?: boolean, | ||
) => { | ||
let res = tempText | ||
|
||
if (startRex.test(tempText)) { | ||
this.hasStartTable = true | ||
if (isOwnMessage) { | ||
const startRex = /(^\n?\|)|((\n\n)(.*)\|)/g | ||
const endRex = /(\|\n?$)|(\|\n\n)/g | ||
if (startRex.test(tempText) && endRex.test(tempText)) { | ||
res = tempText.replace(startRex, (match) => { | ||
return `\n\`\`\`markdown\n${match.trim()}` | ||
}) | ||
res = res.replace(endRex, (match) => { | ||
return `${match.trim()}\n\`\`\`\n` | ||
}) | ||
} | ||
|
||
if (this.hasStartTable && endRex.test(tempText)) { | ||
} else { | ||
const startRex = /(^\n?\|)|((\n\n)(.*)\|)/ | ||
const endRex = /(\|\n?$)|(\|\n\n)/ | ||
if (startRex.test(tempText) && endRex.test(tempText)) { | ||
res = tempText.replace(startRex, (match) => { | ||
return `\n\`\`\`markdown\n${match.trim()}` | ||
}) | ||
res = res.replace(endRex, (match) => { | ||
return `${match.trim()}\n\`\`\`\n` | ||
}) | ||
this.hasStartTable = false | ||
} else if (this.hasStartTable) { | ||
} else if (startRex.test(tempText)) { | ||
res = tempText.replace(startRex, (match) => { | ||
return `\n\`\`\`markdown\n${match.trim()}` | ||
}) | ||
} | ||
return res | ||
} | ||
|
||
private restoreCodeBlocks( | ||
textWithPlaceholders: string, | ||
placeholders: string[], | ||
) { | ||
placeholders.forEach((placeholderContent, index) => { | ||
const placeholder = `__CODEBLOCK__${index}` | ||
textWithPlaceholders = textWithPlaceholders.replace( | ||
placeholder, | ||
() => placeholderContent, | ||
) | ||
}) | ||
|
||
return textWithPlaceholders | ||
} | ||
return res | ||
} | ||
|
||
convertMarkdownTables(markdownText: string) { | ||
const { textWithoutCodeBlocks, placeholders } = | ||
this.markCodeBlocks(markdownText) | ||
let convertedText = this.convertTablesWithoutCodeBlocks( | ||
textWithoutCodeBlocks, | ||
const restoreCodeBlocks = ( | ||
textWithPlaceholders: string, | ||
placeholders: string[], | ||
) => { | ||
placeholders.forEach((placeholderContent, index) => { | ||
const placeholder = `__CODEBLOCK__${index}` | ||
textWithPlaceholders = textWithPlaceholders.replace( | ||
placeholder, | ||
() => placeholderContent, | ||
) | ||
convertedText = this.restoreCodeBlocks(convertedText, placeholders) | ||
return convertedText | ||
} | ||
}) | ||
|
||
return textWithPlaceholders | ||
} | ||
|
||
export const formatTable = new FormatTable() | ||
export const convertMarkdownTables = ( | ||
markdownText: string, | ||
isOwnMessage?: boolean, | ||
) => { | ||
const { textWithoutCodeBlocks, placeholders } = markCodeBlocks(markdownText) | ||
let convertedText = convertTablesWithoutCodeBlocks( | ||
textWithoutCodeBlocks, | ||
isOwnMessage, | ||
) | ||
convertedText = restoreCodeBlocks(convertedText, placeholders) | ||
return convertedText | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters