mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-07 11:28:35 -05:00
refac
This commit is contained in:
@@ -32,6 +32,18 @@ function escapeRegExp(string: string): string {
|
||||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
// Replace tokens outside code blocks only
|
||||
export const replaceOutsideCode = (content: string, replacer: (str: string) => string) => {
|
||||
return content
|
||||
.split(/(```[\s\S]*?```|`[\s\S]*?`)/)
|
||||
.map((segment) => {
|
||||
return segment.startsWith('```') || segment.startsWith('`')
|
||||
? segment
|
||||
: replacer(segment);
|
||||
})
|
||||
.join('');
|
||||
};
|
||||
|
||||
export const replaceTokens = (content, char, user) => {
|
||||
const tokens = [
|
||||
{ regex: /{{char}}/gi, replacement: char },
|
||||
@@ -47,20 +59,8 @@ export const replaceTokens = (content, char, user) => {
|
||||
}
|
||||
];
|
||||
|
||||
// Replace tokens outside code blocks only
|
||||
const processOutsideCodeBlocks = (text, replacementFn) => {
|
||||
return text
|
||||
.split(/(```[\s\S]*?```|`[\s\S]*?`)/)
|
||||
.map((segment) => {
|
||||
return segment.startsWith('```') || segment.startsWith('`')
|
||||
? segment
|
||||
: replacementFn(segment);
|
||||
})
|
||||
.join('');
|
||||
};
|
||||
|
||||
// Apply replacements
|
||||
content = processOutsideCodeBlocks(content, (segment) => {
|
||||
content = replaceOutsideCode(content, (segment) => {
|
||||
tokens.forEach(({ regex, replacement }) => {
|
||||
if (replacement !== undefined && replacement !== null) {
|
||||
segment = segment.replace(regex, replacement);
|
||||
@@ -841,19 +841,21 @@ export const cleanText = (content: string) => {
|
||||
};
|
||||
|
||||
export const removeDetails = (content, types) => {
|
||||
for (const type of types) {
|
||||
content = content.replace(
|
||||
new RegExp(`<details\\s+type="${type}"[^>]*>.*?<\\/details>`, 'gis'),
|
||||
''
|
||||
);
|
||||
}
|
||||
|
||||
return content;
|
||||
return replaceOutsideCode(content, (segment) => {
|
||||
for (const type of types) {
|
||||
segment = segment.replace(
|
||||
new RegExp(`<details\\s+type="${type}"[^>]*>.*?<\\/details>`, 'gis'),
|
||||
''
|
||||
);
|
||||
}
|
||||
return segment;
|
||||
});
|
||||
};
|
||||
|
||||
export const removeAllDetails = (content) => {
|
||||
content = content.replace(/<details[^>]*>.*?<\/details>/gis, '');
|
||||
return content;
|
||||
return replaceOutsideCode(content, (segment) => {
|
||||
return segment.replace(/<details[^>]*>.*?<\/details>/gis, '');
|
||||
});
|
||||
};
|
||||
|
||||
export const processDetails = (content) => {
|
||||
@@ -1655,6 +1657,10 @@ export const getCodeBlockContents = (content: string): object => {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Remove details tags from the content to check if there are any code blocks
|
||||
// hidden in the details tags (e.g. reasoning, etc.)
|
||||
content = removeAllDetails(content);
|
||||
|
||||
const inlineHtml = content.match(/<html>[\s\S]*?<\/html>/gi);
|
||||
const inlineCss = content.match(/<style>[\s\S]*?<\/style>/gi);
|
||||
const inlineJs = content.match(/<script>[\s\S]*?<\/script>/gi);
|
||||
|
||||
Reference in New Issue
Block a user