MCPcopy Create free account
hub / github.com/pollinations/pollinations / formatMessage

Function formatMessage

apps/chat/src/utils/markdown.js:170–210  ·  view source on GitHub ↗
(content)

Source from the content-addressed store, hash-verified

168};
169
170export 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, "&apos;")}'></div>`;
203 }
204
205 return html;
206 } catch (error) {
207 console.error("Markdown rendering error:", error);
208 return escapeHtml(String(content));
209 }
210};
211
212export const formatStreamingMessage = (content) => {
213 if (!content) return "";

Calls 9

sanitizeRenderedHtmlFunction · 0.85
renderMathFunction · 0.85
pushMethod · 0.80
parseMethod · 0.80
errorMethod · 0.80
stringifyMethod · 0.80
escapeHtmlFunction · 0.70
replaceMethod · 0.65
renderMethod · 0.45

Tested by

no test coverage detected