MCPcopy Create free account
hub / github.com/IfcOpenShell/IfcOpenShell / renderMarkdown

Function renderMarkdown

src/ifcchat/app.js:253–363  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

251}
252
253function renderMarkdown(text) {
254 const lines = String(text).replace(/\r\n?/g, "\n").split("\n");
255 const html = [];
256 let paragraphLines = [];
257 let quoteLines = [];
258 let listType = null;
259 let listItems = [];
260
261 const flushParagraph = () => {
262 if (!paragraphLines.length) return;
263 html.push(`<p>${renderInlineMarkdown(paragraphLines.join(" "))}</p>`);
264 paragraphLines = [];
265 };
266
267 const flushQuote = () => {
268 if (!quoteLines.length) return;
269 const quoteBody = quoteLines.map((line) => renderInlineMarkdown(line)).join("<br />");
270 html.push(`<blockquote><p>${quoteBody}</p></blockquote>`);
271 quoteLines = [];
272 };
273
274 const flushList = () => {
275 if (!listItems.length || !listType) return;
276 const items = listItems.map((item) => `<li>${renderInlineMarkdown(item)}</li>`).join("");
277 html.push(`<${listType}>${items}</${listType}>`);
278 listType = null;
279 listItems = [];
280 };
281
282 const flushAll = () => {
283 flushParagraph();
284 flushQuote();
285 flushList();
286 };
287
288 for (let index = 0; index < lines.length; index++) {
289 const line = lines[index];
290 const trimmed = line.trim();
291
292 if (trimmed.startsWith("```")) {
293 flushAll();
294 const language = trimmed.slice(3).trim();
295 const codeLines = [];
296 index += 1;
297 while (index < lines.length && !lines[index].trim().startsWith("```")) {
298 codeLines.push(lines[index]);
299 index += 1;
300 }
301 const languageClass = language ? ` class="language-${escapeHtml(language)}"` : "";
302 html.push(`<pre><code${languageClass}>${escapeHtml(codeLines.join("\n"))}</code></pre>`);
303 continue;
304 }
305
306 if (!trimmed) {
307 flushAll();
308 continue;
309 }
310

Callers 1

addMessageFunction · 0.85

Calls 10

flushAllFunction · 0.85
escapeHtmlFunction · 0.85
renderInlineMarkdownFunction · 0.85
flushParagraphFunction · 0.85
flushListFunction · 0.85
flushQuoteFunction · 0.85
splitMethod · 0.80
pushMethod · 0.45
joinMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected