|
| 1 | +/** |
| 2 | + * Inline Markdown Parser |
| 3 | + * |
| 4 | + * Parses inline Markdown syntax to HTML. |
| 5 | + * Supports: **bold**, *italic*, [links](url), ~~strikethrough~~ |
| 6 | + * |
| 7 | + * Security: |
| 8 | + * - HTML is escaped before parsing (XSS prevention) |
| 9 | + * - Only http/https/relative URLs allowed in links |
| 10 | + * - Generated tags: <strong>, <em>, <del>, <a> |
| 11 | + * |
| 12 | + * @license Unlicense <http://unlicense.org/> |
| 13 | + * @since 3.0.0 |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * Escape HTML special characters. |
| 18 | + * |
| 19 | + * @param str - Input string |
| 20 | + * @returns String with HTML entities escaped |
| 21 | + */ |
| 22 | +function escapeHtml(str: string): string { |
| 23 | + return str |
| 24 | + .replace(/&/g, '&') |
| 25 | + .replace(/</g, '<') |
| 26 | + .replace(/>/g, '>') |
| 27 | + .replace(/"/g, '"') |
| 28 | + .replace(/'/g, '''); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Sanitize URL for safe use in href attribute. |
| 33 | + * |
| 34 | + * Only allows http, https, and relative URLs. |
| 35 | + * Blocks javascript:, data:, and other potentially dangerous protocols. |
| 36 | + * |
| 37 | + * @param url - URL to sanitize |
| 38 | + * @returns Safe URL or '#' if blocked |
| 39 | + */ |
| 40 | +function sanitizeUrl(url: string): string { |
| 41 | + const trimmed = url.trim(); |
| 42 | + |
| 43 | + // Allow relative URLs |
| 44 | + if ( |
| 45 | + trimmed.startsWith('/') || |
| 46 | + trimmed.startsWith('./') || |
| 47 | + trimmed.startsWith('../') |
| 48 | + ) { |
| 49 | + return trimmed; |
| 50 | + } |
| 51 | + |
| 52 | + // Allow http/https |
| 53 | + if (/^https?:\/\//i.test(trimmed)) { |
| 54 | + return trimmed; |
| 55 | + } |
| 56 | + |
| 57 | + // Block everything else (javascript:, data:, etc.) |
| 58 | + return '#'; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Parse inline Markdown to HTML. |
| 63 | + * |
| 64 | + * Processing order matters: |
| 65 | + * 1. Escape HTML first (security) |
| 66 | + * 2. Links [text](url) - most specific pattern |
| 67 | + * 3. Bold **text** |
| 68 | + * 4. Italic *text* (after bold to avoid conflicts) |
| 69 | + * 5. Strikethrough ~~text~~ |
| 70 | + * |
| 71 | + * @param text - Input text with Markdown |
| 72 | + * @returns HTML string |
| 73 | + */ |
| 74 | +export function parseInlineMarkdown(text: string): string { |
| 75 | + if (!text) return ''; |
| 76 | + |
| 77 | + // Step 1: Escape HTML characters first |
| 78 | + let result = escapeHtml(text); |
| 79 | + |
| 80 | + // Step 2: Links [text](url) |
| 81 | + result = result.replace( |
| 82 | + /\[([^\]]+)\]\(([^)]+)\)/g, |
| 83 | + (_, linkText: string, url: string) => { |
| 84 | + const safeUrl = sanitizeUrl(url); |
| 85 | + return `<a href="${safeUrl}" target="_blank" rel="noopener noreferrer">${linkText}</a>`; |
| 86 | + } |
| 87 | + ); |
| 88 | + |
| 89 | + // Step 3: Bold **text** |
| 90 | + result = result.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>'); |
| 91 | + |
| 92 | + // Step 4: Italic *text* (not preceded/followed by asterisk) |
| 93 | + result = result.replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, '<em>$1</em>'); |
| 94 | + |
| 95 | + // Step 5: Strikethrough ~~text~~ |
| 96 | + result = result.replace(/~~([^~]+)~~/g, '<del>$1</del>'); |
| 97 | + |
| 98 | + return result; |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Check if text contains any inline Markdown syntax. |
| 103 | + * |
| 104 | + * Useful for optimization - skip parsing if no Markdown present. |
| 105 | + * |
| 106 | + * @param text - Input text |
| 107 | + * @returns True if Markdown syntax detected |
| 108 | + */ |
| 109 | +export function containsMarkdown(text: string): boolean { |
| 110 | + if (!text) return false; |
| 111 | + // Check for: **bold**, *italic*, [link](url), ~~strike~~ |
| 112 | + return /\*\*|(?<!\*)\*(?!\*)|\[.+\]\(.+\)|~~/.test(text); |
| 113 | +} |
0 commit comments