diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 2ea221ab82..96f290a2b7 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -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>`, 'gis'), - '' - ); - } - - return content; + return replaceOutsideCode(content, (segment) => { + for (const type of types) { + segment = segment.replace( + new RegExp(`]*>.*?<\\/details>`, 'gis'), + '' + ); + } + return segment; + }); }; export const removeAllDetails = (content) => { - content = content.replace(/]*>.*?<\/details>/gis, ''); - return content; + return replaceOutsideCode(content, (segment) => { + return segment.replace(/]*>.*?<\/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(/[\s\S]*?<\/html>/gi); const inlineCss = content.match(/