@memori.ai/memori-react 7.17.0 → 7.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,8 +35,16 @@ marked.use({
35
35
  gfm: true,
36
36
  pedantic: false,
37
37
  renderer: {
38
- link: ({ href, title, text }) => {
39
- const cleanHref = cleanUrl(href);
38
+ link: ({
39
+ href,
40
+ title,
41
+ text,
42
+ }: {
43
+ href: string | null;
44
+ title?: string | null;
45
+ text: string;
46
+ }) => {
47
+ const cleanHref = href ? cleanUrl(href) : null;
40
48
 
41
49
  if (cleanHref === null) {
42
50
  return text;
@@ -54,71 +62,69 @@ marked.use({
54
62
  marked.use(markedLinkifyIt());
55
63
  marked.use(markedExtendedTables());
56
64
 
57
- const parseSquaredBrackets = (text: string) => {
58
- const rows = text.split('\n');
59
-
60
- return rows.reduce((acc, row) => {
61
- if (row.includes('=')) {
62
- let result = '';
63
- let isEscaped = false;
64
- for (let i = 0; i < row.length; i++) {
65
- if (row[i] === '[' && !isEscaped) {
66
- result += '\\[';
67
- } else if (row[i] === ']' && !isEscaped) {
68
- result += '\\]';
69
- } else {
70
- result += row[i];
71
- }
72
- isEscaped = row[i] === '\\' && !isEscaped;
73
- }
74
-
75
- return acc?.length ? `${acc}\n${result}` : result;
76
- } else {
77
- return acc?.length ? `${acc}\n${row}` : row;
78
- }
79
- }, '');
80
- };
81
-
82
- const renderMsg = (text: string, useMathFormatting = false) => {
65
+ // Update the renderMsg function to properly handle math formatting
66
+ const renderMsg = (text: string, useMathFormatting = false): string => {
83
67
  try {
84
- let parsedText = (
85
- marked.parse(
86
- text
87
- // remove leading and trailing whitespaces
88
- .trim()
89
- // remove markdown links
90
- .replaceAll(
91
- /\[([^\]]+)\]\(([^\)]+)\)/g,
92
- '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>'
93
- )
94
- // remove markdown multiline code blocks but keep the content
95
- .replaceAll(/```markdown([^```]+)```/g, '$1')
96
- .replaceAll('($', '( $')
97
- .replaceAll(':$', ': $')
98
- .replaceAll('\frac', '\\frac')
99
- .replaceAll('\beta', '\\beta')
100
- .replaceAll('cdot', '\\cdot')
101
- ) as string
102
- ).trim();
103
-
68
+ // Preprocessing del testo per gestire i delimitatori LaTeX
69
+ let preprocessedText = text
70
+ .trim()
71
+ .replaceAll(
72
+ /\[([^\]]+)\]\(([^\)]+)\)/g,
73
+ '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>'
74
+ )
75
+ .replaceAll(/```markdown([^```]+)```/g, '$1')
76
+ .replaceAll('($', '( $')
77
+ .replaceAll(':$', ': $')
78
+ .replaceAll('\frac', '\\frac')
79
+ .replaceAll('\beta', '\\beta')
80
+ .replaceAll('cdot', '\\cdot');
81
+
82
+ // Correzione dei delimitatori LaTeX inconsistenti
104
83
  if (useMathFormatting) {
105
- parsedText = parseSquaredBrackets(parsedText.replace(/\n/g, '<br>'));
84
+ // Normalizza tutti i delimitatori LaTeX per equazioni su linea separata
85
+ // Da \\[ ... \\] o \\[ ... ] a $$ ... $$
86
+ preprocessedText = preprocessedText.replace(
87
+ /\\+\[(.*?)\\*\]/gs,
88
+ (_, content) => {
89
+ return `$$${content}$$`;
90
+ }
91
+ );
92
+
93
+ // Gestione dei delimitatori [ ... ] che dovrebbero essere equazioni
94
+ preprocessedText = preprocessedText.replace(
95
+ /\[([^[\]]+?)\]/g,
96
+ (match, content) => {
97
+ // Verifica se sembra una formula matematica
98
+ if (/[\\+a-z0-9_{}^=\-\+\*\/]+/i.test(content) &&
99
+ !match.startsWith('[http') &&
100
+ !match.includes('](')) {
101
+ return `$$${content}$$`;
102
+ }
103
+ return match; // Mantieni invariati i link e altre strutture
104
+ }
105
+ );
106
106
  }
107
107
 
108
+ // Ora procedi con il parsing markdown
109
+ let parsedText = marked
110
+ .parse(preprocessedText)
111
+ .toString()
112
+ .trim();
113
+
114
+ // Sanitizza l'HTML
108
115
  parsedText = DOMPurify.sanitize(parsedText, {
109
116
  ADD_ATTR: ['target'],
110
117
  });
111
118
 
112
- return (
113
- parsedText
114
- // replace consecutive <br> with a single <br>
115
- .replace(/(<br>)+/g, '<br>')
116
- // remove empty paragraphs
117
- .replace(/<p><\/p>/g, '<br>')
118
- .replace(/<p><br><\/p>/g, '<br>')
119
- );
119
+ // Pulizia del testo finale
120
+ const finalText = parsedText
121
+ .replace(/(<br>)+/g, '<br>')
122
+ .replace(/<p><\/p>/g, '<br>')
123
+ .replace(/<p><br><\/p>/g, '<br>');
124
+
125
+ return finalText;
120
126
  } catch (e) {
121
- console.error(e);
127
+ console.error('Error rendering message:', e);
122
128
  return text;
123
129
  }
124
130
  };
@@ -189,16 +195,14 @@ const ChatBubble: React.FC<Props> = ({
189
195
  !message.fromUser &&
190
196
  useMathFormatting
191
197
  ) {
192
- // @ts-ignore
193
- // eslint-disable-next-line no-undef
194
- if ('MathJax' in window && window.MathJax.typesetPromise)
195
- // @ts-ignore
196
- // eslint-disable-next-line no-undef
197
- window.MathJax.typesetPromise(['.memori-chat--bubble-content']);
198
+ // Add type declaration for MathJax
199
+ const mathJax = (window as any).MathJax;
200
+ if (mathJax?.typesetPromise) {
201
+ mathJax.typesetPromise(['.memori-chat--bubble-content']);
202
+ }
198
203
  }
199
204
  }, [message.text, message.fromUser, useMathFormatting]);
200
205
 
201
-
202
206
  return (
203
207
  <>
204
208
  {(message.initial || isFirst) && (