(content)
| 168 | }; |
| 169 | |
| 170 | export const formatMessage = (content) => { |
| 171 | if (!content) return ""; |
| 172 | |
| 173 | try { |
| 174 | let textContent = String(content); |
| 175 | |
| 176 | textContent = textContent.trim(); |
| 177 | |
| 178 | textContent = textContent.replace(/\n{3,}/g, "\n\n"); |
| 179 | |
| 180 | textContent = textContent.replace( |
| 181 | /([^`\n])([ ]{2,})([^`\n])/g, |
| 182 | "$1 $3", |
| 183 | ); |
| 184 | |
| 185 | const chartRegex = /__CHART__(.*?)__CHART__/g; |
| 186 | const charts = []; |
| 187 | for (const match of textContent.matchAll(chartRegex)) { |
| 188 | try { |
| 189 | charts.push(JSON.parse(match[1])); |
| 190 | } catch (e) { |
| 191 | console.error("Failed to parse chart data:", e); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | textContent = textContent.replace(chartRegex, ""); |
| 196 | |
| 197 | let html = md.render(textContent); |
| 198 | |
| 199 | html = sanitizeRenderedHtml(renderMath(html)); |
| 200 | |
| 201 | if (charts.length > 0) { |
| 202 | html += `<div data-charts='${JSON.stringify(charts).replace(/'/g, "'")}'></div>`; |
| 203 | } |
| 204 | |
| 205 | return html; |
| 206 | } catch (error) { |
| 207 | console.error("Markdown rendering error:", error); |
| 208 | return escapeHtml(String(content)); |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | export const formatStreamingMessage = (content) => { |
| 213 | if (!content) return ""; |
no test coverage detected