mirror of
https://github.com/open-webui/open-webui
synced 2025-06-14 18:33:15 +00:00
fix: Handle special cases of LLM output not following markdown syntax (20250525, formated) This commit introduces a utility function to address rendering issues in LLM output, particularly for Chinese characters and parentheses. The function ensures minimal modification of the original text while fixing markdown parsing problems. Changes include: - Added in for handling specific cases. - Updated in to incorporate the new utility. The fix ensures proper rendering of bold/italic text containing Chinese parentheses, improving readability for non-English content.
This commit is contained in:
parent
49fe137553
commit
8ef7938c96
@ -20,7 +20,7 @@ import markedExtension from '$lib/utils/marked/extension';
|
||||
import markedKatexExtension from '$lib/utils/marked/katex-extension';
|
||||
import hljs from 'highlight.js';
|
||||
|
||||
import { specialCases } from '$lib/utils/processResponseContent/special-cases'
|
||||
import { specialCases } from '$lib/utils/processResponseContent/special-cases';
|
||||
|
||||
//////////////////////////
|
||||
// Helper functions
|
||||
|
@ -12,10 +12,10 @@
|
||||
|
||||
export const specialCases = (src: string): string => {
|
||||
const lines = src.split('\n'); // Process from line to line.
|
||||
const processedLines = lines.map(line => {
|
||||
|
||||
const processedLines = lines.map((line) => {
|
||||
// 1. 中文 (Chinese, CN)
|
||||
if (/[\u4e00-\u9fa5]/.test(line)) { // Only execute if there are Chinese characters.
|
||||
if (/[\u4e00-\u9fa5]/.test(line)) {
|
||||
// Only execute if there are Chinese characters.
|
||||
|
||||
// 1.1. Problems caused by Chinese parentheses
|
||||
/* Discription:
|
||||
@ -31,19 +31,20 @@ export const specialCases = (src: string): string => {
|
||||
* Change the behavior in future if needed.
|
||||
*/
|
||||
|
||||
if (line.includes('*')) { // Only execute if `*` is found in line.
|
||||
if (line.includes('*')) {
|
||||
// Only execute if `*` is found in line.
|
||||
|
||||
// 1.1.1. Handle **bold** with Chinese parentheses
|
||||
line = processCN_01(line, '**', '(', ')');
|
||||
// 1.1.2. Handle *italic* with Chinese parentheses
|
||||
line = processCN_01(line, '*', '(', ')');
|
||||
}
|
||||
|
||||
}
|
||||
return line;
|
||||
});
|
||||
const result = processedLines.join('\n');
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
//////////////////////////
|
||||
// Helper functions
|
||||
@ -57,21 +58,27 @@ function escapeRegExp(string: string): string {
|
||||
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////
|
||||
// Main functions
|
||||
//////////////////////////
|
||||
|
||||
// Handle case `1.1.1` and `1.1.2`
|
||||
function processCN_01(line: string, symbol: string, leftSymbol: string, rightSymbol: string): string {
|
||||
function processCN_01(
|
||||
line: string,
|
||||
symbol: string,
|
||||
leftSymbol: string,
|
||||
rightSymbol: string
|
||||
): string {
|
||||
const escapedSymbol = escapeRegExp(symbol);
|
||||
const regex = new RegExp(`(.*?)(?<!${escapedSymbol})(${escapedSymbol})([^${escapedSymbol}]+)(${escapedSymbol})(?!${escapedSymbol})(.*?)`, 'g');
|
||||
const regex = new RegExp(
|
||||
`(.*?)(?<!${escapedSymbol})(${escapedSymbol})([^${escapedSymbol}]+)(${escapedSymbol})(?!${escapedSymbol})(.*?)`,
|
||||
'g'
|
||||
);
|
||||
return line.replace(regex, (match, l, left, content, right, r) => {
|
||||
const result = (
|
||||
const result =
|
||||
(content.startsWith(leftSymbol) || content.endsWith(rightSymbol)) &&
|
||||
(!l || (l && l.length > 0 && isChineseChar(l[l.length - 1]))) &&
|
||||
(!r || (r && r.length > 0 && isChineseChar(r[0])))
|
||||
)
|
||||
(!r || (r && r.length > 0 && isChineseChar(r[0])));
|
||||
if (result) {
|
||||
return ` ${left}${content}${right} `;
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user