refac: enhanced response content sanitisation
'<' and '>' can be correctly displayed now
This commit is contained in:
+17
-34
@@ -23,39 +23,6 @@ const convertLatexToSingleLine = (content) => {
|
||||
return content;
|
||||
};
|
||||
|
||||
export const sanitizeResponseContent = (content: string) => {
|
||||
// replace single backslash with double backslash
|
||||
content = content.replace(/\\\\/g, '\\\\\\\\');
|
||||
|
||||
content = convertLatexToSingleLine(content);
|
||||
|
||||
// First, temporarily replace valid <video> tags with a placeholder
|
||||
const videoTagRegex = /<video\s+src="([^"]+)"\s+controls><\/video>/gi;
|
||||
const placeholders: string[] = [];
|
||||
content = content.replace(videoTagRegex, (_, src) => {
|
||||
const placeholder = `{{VIDEO_${placeholders.length}}}`;
|
||||
placeholders.push(`<video src="${src}" controls></video>`);
|
||||
return placeholder;
|
||||
});
|
||||
|
||||
// Now apply the sanitization to the rest of the content
|
||||
content = content
|
||||
.replace(/<\|[a-z]*$/, '')
|
||||
.replace(/<\|[a-z]+\|$/, '')
|
||||
.replace(/<$/, '')
|
||||
.replaceAll(/<\|[a-z]+\|>/g, ' ')
|
||||
.replaceAll('<', '<')
|
||||
.replaceAll('>', '>')
|
||||
.trim();
|
||||
|
||||
// Replace placeholders with original <video> tags
|
||||
placeholders.forEach((placeholder, index) => {
|
||||
content = content.replace(`{{VIDEO_${index}}}`, placeholder);
|
||||
});
|
||||
|
||||
return content.trim();
|
||||
};
|
||||
|
||||
export const replaceTokens = (content, char, user) => {
|
||||
const charToken = /{{char}}/gi;
|
||||
const userToken = /{{user}}/gi;
|
||||
@@ -87,8 +54,24 @@ export const replaceTokens = (content, char, user) => {
|
||||
return content;
|
||||
};
|
||||
|
||||
export const sanitizeResponseContent = (content: string) => {
|
||||
return content
|
||||
.replace(/<\|[a-z]*$/, '')
|
||||
.replace(/<\|[a-z]+\|$/, '')
|
||||
.replace(/<$/, '')
|
||||
.replaceAll(/<\|[a-z]+\|>/g, ' ')
|
||||
.replaceAll('<', '<')
|
||||
.replaceAll('>', '>')
|
||||
.trim();
|
||||
};
|
||||
|
||||
export const processResponseContent = (content: string) => {
|
||||
content = convertLatexToSingleLine(content);
|
||||
return content.trim();
|
||||
};
|
||||
|
||||
export const revertSanitizedResponseContent = (content: string) => {
|
||||
return content.replaceAll('<', '<').replaceAll('>', '>').replaceAll('\\\\', '\\');
|
||||
return content.replaceAll('<', '<').replaceAll('>', '>');
|
||||
};
|
||||
|
||||
export function unescapeHtml(html: string) {
|
||||
|
||||
Reference in New Issue
Block a user