|
| 1 | +export function formatMarkdown(content) { |
| 2 | + if (typeof content !== 'string' || content.length === 0) { |
| 3 | + return ''; |
| 4 | + } |
| 5 | + |
| 6 | + const normalized = content.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); |
| 7 | + const lines = normalized.split('\n'); |
| 8 | + const formatted = []; |
| 9 | + |
| 10 | + let blankLineCount = 0; |
| 11 | + let inCodeFence = false; |
| 12 | + |
| 13 | + for (let i = 0; i < lines.length; i++) { |
| 14 | + const originalLine = lines[i]; |
| 15 | + const trimmedEnd = originalLine.replace(/\s+$/u, ''); |
| 16 | + const fenceMatch = trimmedEnd.trimStart().match(/^(?:```+|~~~+)(.*)$/); |
| 17 | + |
| 18 | + if (inCodeFence) { |
| 19 | + formatted.push(originalLine); |
| 20 | + if (fenceMatch) { |
| 21 | + inCodeFence = false; |
| 22 | + blankLineCount = 0; |
| 23 | + } |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 27 | + if (fenceMatch) { |
| 28 | + formatted.push(trimmedEnd); |
| 29 | + inCodeFence = true; |
| 30 | + blankLineCount = 0; |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + if (trimmedEnd.trim() === '') { |
| 35 | + if (formatted.length === 0) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + blankLineCount += 1; |
| 39 | + if (blankLineCount > 1) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + formatted.push(''); |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + blankLineCount = 0; |
| 47 | + let processedLine = trimmedEnd; |
| 48 | + |
| 49 | + const headingMatch = processedLine.match(/^(#{1,6})(\s*)(.*)$/); |
| 50 | + if (headingMatch) { |
| 51 | + const [, hashes, , text] = headingMatch; |
| 52 | + const headingText = text.trim(); |
| 53 | + processedLine = headingText ? `${hashes} ${headingText}` : hashes; |
| 54 | + formatted.push(processedLine); |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + const blockquoteMatch = processedLine.match(/^(\s*>+)(\s*)(.*)$/); |
| 59 | + if (blockquoteMatch) { |
| 60 | + const [, markers, , text] = blockquoteMatch; |
| 61 | + const quoteText = text.replace(/^\s+/, ''); |
| 62 | + processedLine = quoteText ? `${markers} ${quoteText}` : markers; |
| 63 | + formatted.push(processedLine); |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + const listMatch = processedLine.match(/^(\s*)(?:([-*+])|(\d+\.))(\s*)(.*)$/); |
| 68 | + if (listMatch) { |
| 69 | + const indent = listMatch[1] || ''; |
| 70 | + const bullet = listMatch[2] || listMatch[3] || ''; |
| 71 | + const listText = (listMatch[5] || '').replace(/^\s+/, ''); |
| 72 | + const spacer = listText ? ' ' : ''; |
| 73 | + processedLine = `${indent}${bullet}${spacer}${listText}`; |
| 74 | + formatted.push(processedLine); |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + formatted.push(processedLine); |
| 79 | + } |
| 80 | + |
| 81 | + while (formatted.length > 0 && formatted[formatted.length - 1] === '') { |
| 82 | + formatted.pop(); |
| 83 | + } |
| 84 | + |
| 85 | + if (formatted.length === 0) { |
| 86 | + return ''; |
| 87 | + } |
| 88 | + |
| 89 | + let result = formatted.join('\n'); |
| 90 | + if (!result.endsWith('\n')) { |
| 91 | + result += '\n'; |
| 92 | + } |
| 93 | + return result; |
| 94 | +} |
0 commit comments