Skip to content

Commit

Permalink
fix: avoid issues with square brackets in text with markdown and math…
Browse files Browse the repository at this point in the history
… parsing
  • Loading branch information
nzambello committed Nov 26, 2024
1 parent 5a5d30b commit a87569f
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/components/ChatBubble/ChatBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,29 @@ const ChatBubble: React.FC<Props> = ({

const text = message.translatedText || message.text;

// remove redundant mathjax delimiters: old code
// .replaceAll(/(?<!\\)\[/g, '\\[')
// .replaceAll(/(?<!\\)\]/g, '\\]')
// since old Safari < 16.4 doesn't support negative lookbehind, we need to use a workaround
const parseSquaredBrackets = (text: string) => {
let result = '';
let isEscaped = false;
for (let i = 0; i < text.length; i++) {
if (text[i] === '[' && !isEscaped) {
result += '\\[';
} else if (text[i] === ']' && !isEscaped) {
result += '\\]';
const rows = text.split('\n');

return rows.reduce((acc, row) => {
if (row.includes('=')) {
let result = '';
let isEscaped = false;
for (let i = 0; i < row.length; i++) {
if (row[i] === '[' && !isEscaped) {
result += '\\[';
} else if (row[i] === ']' && !isEscaped) {
result += '\\]';
} else {
result += row[i];
}
isEscaped = row[i] === '\\' && !isEscaped;
}

return acc?.length ? `${acc}\n${result}` : result;
} else {
result += text[i];
return acc?.length ? `${acc}\n${row}` : row;
}
isEscaped = text[i] === '\\' && !isEscaped;
}
return result;
}, '');
};

const renderMsg = (text: string) => {
Expand Down

0 comments on commit a87569f

Please sign in to comment.