fix: Simplify regex

This commit is contained in:
Hwang In Tak 2024-09-24 22:00:01 +09:00
parent 377cc427b6
commit 0bfbace9aa
No known key found for this signature in database
2 changed files with 4 additions and 20 deletions

View File

@ -8,23 +8,6 @@ import { TTS_RESPONSE_SPLIT } from '$lib/types';
// Helper functions
//////////////////////////
const convertLatexToSingleLine = (content) => {
// Patterns to match multiline LaTeX blocks
const patterns = [
/(\$\$\s[\s\S]*?\s\$\$)/g, // Match $$ ... $$
/(\\\[[\s\S]*?\\\])/g, // Match \[ ... \]
/(\\begin\{[a-z]+\}[\s\S]*?\\end\{[a-z]+\})/g // Match \begin{...} ... \end{...}
];
patterns.forEach((pattern) => {
content = content.replace(pattern, (match) => {
return match.replace(/\s*\n\s*/g, ' ').trim();
});
});
return content;
};
export const replaceTokens = (content, char, user) => {
const charToken = /{{char}}/gi;
const userToken = /{{user}}/gi;
@ -68,7 +51,6 @@ export const sanitizeResponseContent = (content: string) => {
};
export const processResponseContent = (content: string) => {
content = convertLatexToSingleLine(content);
return content.trim();
};

View File

@ -35,11 +35,11 @@ function generateRegexRules(delimiters) {
if (!display) {
inlinePatterns.push(
`${escapedLeft}((?:\\\\.|[^\\\\\\n])*?(?:\\\\.|[^\\\\\\n${escapedRight}]))${escapedRight}`
`${escapedLeft}((?:\\\\[^]|[^\\\\])+?)${escapedRight}`
);
} else {
blockPatterns.push(
`${escapedLeft}((?:\\\\.|[^\\\\\\n])*?(?:\\\\.|[^\\\\\\n${escapedRight}]))${escapedRight}`
`${escapedLeft}((?:\\\\[^]|[^\\\\])+?)${escapedRight}`
);
}
});
@ -109,6 +109,8 @@ function katexTokenizer(src, tokens, displayMode: boolean) {
const match = src.match(ruleReg);
console.log("searching:", src);
if (match) {
const text = match
.slice(2)