Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | 'use client'; import React from 'react'; import DOMPurify from 'dompurify'; interface SafeMarkdownProps { content: string; className?: string; } /** * SafeMarkdown - Renders markdown content with XSS protection * * Uses DOMPurify to sanitize all HTML output before rendering. * Supports: headers, paragraphs, lists, tables, code blocks, checkboxes, * inline formatting (bold, italic, code, links). */ export default function SafeMarkdown({ content, className = '' }: SafeMarkdownProps) { const elements = React.useMemo(() => renderMarkdown(content), [content]); return <div className={className}>{elements}</div>; } /** * Sanitize HTML string using DOMPurify */ function sanitizeHtml(html: string): string { return DOMPurify.sanitize(html, { ALLOWED_TAGS: ['strong', 'em', 'code', 'a', 'br', 'span'], ALLOWED_ATTR: ['href', 'class', 'target', 'rel'], ADD_ATTR: ['target', 'rel']}); } /** * Apply inline formatting and sanitize */ function formatInlineText(text: string): string { const formatted = text .replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>') .replace(/\*(.+?)\*/g, '<em>$1</em>') .replace(/`(.+?)`/g, '<code class="bg-gray-100 dark:bg-gray-700 px-1 rounded text-sm">$1</code>') .replace(/\[(.+?)\]\((.+?)\)/g, '<a href="$2" class="text-blue hover:underline" target="_blank" rel="noopener noreferrer">$1</a>'); return sanitizeHtml(formatted); } /** * Generate heading ID from text */ function generateHeadingId(text: string): string { return text .toLowerCase() .replace(/[^\w\s-]/g, '') .replace(/\s+/g, '-'); } /** * Parse and render markdown content */ function renderMarkdown(md: string): React.ReactNode[] { const lines = md.split('\n'); const elements: React.ReactNode[] = []; let currentBlock: string[] = []; let blockType: 'none' | 'code' | 'list' | 'table' = 'none'; let codeLanguage = ''; const flushBlock = () => { if (currentBlock.length === 0) return; if (blockType === 'code') { // Code blocks are plain text, no HTML injection possible elements.push( <pre key={elements.length} className="bg-gray-900 dark:bg-gray-950 text-gray-100 rounded-lg p-4 overflow-x-auto my-4" > <code className={`language-${codeLanguage}`}>{currentBlock.join('\n')}</code> </pre> ); } else if (blockType === 'list') { const isOrdered = /^\d+\./.test(currentBlock[0]); const ListTag = isOrdered ? 'ol' : 'ul'; elements.push( <ListTag key={elements.length} className={`my-4 ${isOrdered ? 'list-decimal' : 'list-disc'} list-inside`} > {currentBlock.map((item, i) => { const cleanItem = item.replace(/^[-*]\s*|\d+\.\s*/, ''); return ( <li key={i} className="text-gray-700 dark:text-gray-300" dangerouslySetInnerHTML={{ __html: formatInlineText(cleanItem) }} /> ); })} </ListTag> ); } else if (blockType === 'table') { const rows = currentBlock.map((row) => row .split('|') .filter((cell) => cell.trim()) .map((cell) => cell.trim()) ); if (rows.length > 0) { elements.push( <div key={elements.length} className="overflow-x-auto my-4"> <table className="min-w-full border border-gray-200 dark:border-gray-700"> <thead className="bg-gray-50 dark:bg-gray-800"> <tr> {rows[0].map((cell, i) => ( <th key={i} className="px-4 py-2 text-left text-sm font-medium text-gray-700 dark:text-gray-300 border-b dark:border-gray-700" dangerouslySetInnerHTML={{ __html: formatInlineText(cell) }} /> ))} </tr> </thead> <tbody> {rows.slice(2).map((row, i) => ( <tr key={i} className={i % 2 === 0 ? 'bg-white dark:bg-gray-900' : 'bg-gray-50 dark:bg-gray-800'} > {row.map((cell, j) => ( <td key={j} className="px-4 py-2 text-sm text-gray-600 dark:text-gray-400 border-b dark:border-gray-700" dangerouslySetInnerHTML={{ __html: formatInlineText(cell) }} /> ))} </tr> ))} </tbody> </table> </div> ); } } currentBlock = []; blockType = 'none'; }; for (let i = 0; i < lines.length; i++) { const line = lines[i]; // Code block if (line.startsWith('```')) { if (blockType === 'code') { flushBlock(); } else { flushBlock(); blockType = 'code'; codeLanguage = line.slice(3).trim() || 'text'; } continue; } if (blockType === 'code') { currentBlock.push(line); continue; } // Table if (line.startsWith('|')) { if (blockType !== 'table') { flushBlock(); blockType = 'table'; } currentBlock.push(line); continue; } else if (blockType === 'table') { flushBlock(); } // List if (/^[-*]\s/.test(line) || /^\d+\.\s/.test(line)) { if (blockType !== 'list') { flushBlock(); blockType = 'list'; } currentBlock.push(line); continue; } else if (blockType === 'list') { flushBlock(); } // Headers const h1Match = line.match(/^#\s+(.+)$/); if (h1Match) { flushBlock(); const id = generateHeadingId(h1Match[1]); elements.push( <h1 key={elements.length} id={id} className="text-2xl font-bold text-dark dark:text-gray-100 mt-8 mb-4 first:mt-0" dangerouslySetInnerHTML={{ __html: formatInlineText(h1Match[1]) }} /> ); continue; } const h2Match = line.match(/^##\s+(.+)$/); if (h2Match) { flushBlock(); const id = generateHeadingId(h2Match[1]); elements.push( <h2 key={elements.length} id={id} className="text-xl font-semibold text-dark dark:text-gray-100 mt-6 mb-3" dangerouslySetInnerHTML={{ __html: formatInlineText(h2Match[1]) }} /> ); continue; } const h3Match = line.match(/^###\s+(.+)$/); if (h3Match) { flushBlock(); const id = generateHeadingId(h3Match[1]); elements.push( <h3 key={elements.length} id={id} className="text-lg font-medium text-dark dark:text-gray-100 mt-4 mb-2" dangerouslySetInnerHTML={{ __html: formatInlineText(h3Match[1]) }} /> ); continue; } const h4Match = line.match(/^####\s+(.+)$/); if (h4Match) { flushBlock(); elements.push( <h4 key={elements.length} className="text-base font-medium text-dark dark:text-gray-100 mt-3 mb-2" dangerouslySetInnerHTML={{ __html: formatInlineText(h4Match[1]) }} /> ); continue; } // Horizontal rule if (line.match(/^---+$/)) { flushBlock(); elements.push(<hr key={elements.length} className="my-6 border-gray-200 dark:border-gray-700" />); continue; } // Checkbox if (line.match(/^-\s*\[[ x]\]/)) { flushBlock(); const checked = line.includes('[x]'); const text = line.replace(/^-\s*\[[ x]\]\s*/, ''); elements.push( <div key={elements.length} className="flex items-center gap-2 my-1"> <input type="checkbox" checked={checked} readOnly className="w-4 h-4 rounded border-gray-300 dark:border-gray-600" aria-label={text} /> <span className={checked ? 'text-gray-500 line-through' : 'text-gray-700 dark:text-gray-300'} dangerouslySetInnerHTML={{ __html: formatInlineText(text) }} /> </div> ); continue; } // Empty line if (!line.trim()) { flushBlock(); continue; } // Paragraph flushBlock(); elements.push( <p key={elements.length} className="text-gray-700 dark:text-gray-300 my-2" dangerouslySetInnerHTML={{ __html: formatInlineText(line) }} /> ); } flushBlock(); return elements; } |