Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : KA Maps Engine8 * File : server/km-maps/lib/markdown.ts9 * Module : server10 * Repository : https://git.spboucher.ai/ka-maps-engine11 * Created : 2026-12-0112 * Last Modified : 2026-12-0113 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Markdown → HTML, sous-ensemble suffisant pour docs/DATA_LICENSING.md : titres, paragraphes,43// listes, tableaux, code, gras, italique, liens http(s). Tout le reste est échappé.44export const esc = (s: string) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');4546function inline(s: string): string {47 let out = esc(s);48 out = out.replace(/`([^`]+)`/g, '<code>$1</code>');49 out = out.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');50 out = out.replace(/(^|[\s(])\*([^*\s][^*]*)\*/g, '$1<em>$2</em>');51 out = out.replace(/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g, '<a href="$2" rel="noopener">$1</a>');52 out = out.replace(/(^|[\s(])(https?:\/\/[^\s<)]+[^\s<).,;])/g, '$1<a href="$2" rel="noopener">$2</a>');53 return out;54}5556export function markdownToHtml(md: string): string {57 const lines = md.replace(/\r/g, '').split('\n');58 const out: string[] = [];59 let i = 0;60 const cells = (l: string) => l.trim().replace(/^\||\|$/g, '').split('|').map((c) => c.trim());61 while (i < lines.length) {62 const l = lines[i];63 if (!l.trim()) {64 i++;65 } else if (l.startsWith('```')) {66 const buf: string[] = [];67 i++;68 while (i < lines.length && !lines[i].startsWith('```')) buf.push(lines[i++]);69 i++;70 out.push(`<pre><code>${esc(buf.join('\n'))}</code></pre>`);71 } else if (/^#{1,6}\s/.test(l)) {72 const n = Math.min(l.match(/^#+/)![0].length + 1, 6);73 out.push(`<h${n}>${inline(l.replace(/^#+\s*/, ''))}</h${n}>`);74 i++;75 } else if (l.trim().startsWith('|') && /^\s*\|?[\s:|-]+\|?\s*$/.test(lines[i + 1] ?? '') && (lines[i + 1] ?? '').includes('-')) {76 const head = cells(l);77 i += 2;78 const rows: string[][] = [];79 while (i < lines.length && lines[i].trim().startsWith('|')) rows.push(cells(lines[i++]));80 out.push(`<div class="table"><table><thead><tr>${head.map((h) => `<th>${inline(h)}</th>`).join('')}</tr></thead><tbody>${rows.map((r) => `<tr>${r.map((c) => `<td>${inline(c)}</td>`).join('')}</tr>`).join('')}</tbody></table></div>`);81 } else if (/^\s*([-*]|\d+\.)\s+/.test(l)) {82 const ordered = /^\s*\d+\./.test(l);83 const items: string[] = [];84 while (i < lines.length && (/^\s*([-*]|\d+\.)\s+/.test(lines[i]) || (/^\s{2,}\S/.test(lines[i]) && items.length))) {85 if (/^\s*([-*]|\d+\.)\s+/.test(lines[i])) items.push(lines[i].replace(/^\s*([-*]|\d+\.)\s+/, ''));86 else items[items.length - 1] += ' ' + lines[i].trim();87 i++;88 }89 out.push(`<${ordered ? 'ol' : 'ul'}>${items.map((x) => `<li>${inline(x)}</li>`).join('')}</${ordered ? 'ol' : 'ul'}>`);90 } else if (l.startsWith('>')) {91 const buf: string[] = [];92 while (i < lines.length && lines[i].startsWith('>')) buf.push(lines[i++].replace(/^>\s?/, ''));93 out.push(`<blockquote><p>${inline(buf.join(' '))}</p></blockquote>`);94 } else {95 const buf: string[] = [];96 while (i < lines.length && lines[i].trim() && !/^(#{1,6}\s|```|\s*([-*]|\d+\.)\s+|>|\|)/.test(lines[i])) buf.push(lines[i++].trim());97 if (buf.length === 0) buf.push(lines[i++].trim());98 out.push(`<p>${inline(buf.join(' '))}</p>`);99 }100 }101 return out.join('\n');102}103