|
| 1 | +/** |
| 2 | + * Markdown 结构规范化(轻量版): |
| 3 | + * - 覆盖一批低风险、可自动修复的结构规则; |
| 4 | + * - 跳过受保护区域(围栏代码块、HTML 注释块、表格分隔线)。 |
| 5 | + */ |
| 6 | + |
| 7 | +import { lineGuard } from "./line-guard"; |
| 8 | +import { initGuard } from "./shared"; |
| 9 | + |
| 10 | +type MarkdownNormalizeSwitches = { |
| 11 | + trimTrailingSpaces: boolean; |
| 12 | + headingSpaceAfterHash: boolean; |
| 13 | + headingSingleSpaceAfterHash: boolean; |
| 14 | + blankLineAroundHeadings: boolean; |
| 15 | + listMarkerSpace: boolean; |
| 16 | + blankLineAroundFences: boolean; |
| 17 | + blankLineAroundLists: boolean; |
| 18 | + ensureSingleTrailingNewline: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +function applyMarkdownNormalizeRules(text: string, opt: MarkdownNormalizeSwitches): string { |
| 22 | + const source = text.replace(/\r\n/g, "\n"); |
| 23 | + let lines = source.split("\n"); |
| 24 | + const protectedLines = markProtected(lines); |
| 25 | + |
| 26 | + for (let i = 0; i < lines.length; i += 1) { |
| 27 | + if (protectedLines[i]) { |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + let curr = lines[i]; |
| 32 | + |
| 33 | + if (opt.headingSpaceAfterHash || opt.headingSingleSpaceAfterHash) { |
| 34 | + const noSpaceHeading = curr.match(/^(\s{0,3}#{1,6})([^#\s].*)$/); |
| 35 | + if (noSpaceHeading && opt.headingSpaceAfterHash) { |
| 36 | + curr = `${noSpaceHeading[1]} ${noSpaceHeading[2]}`; |
| 37 | + } else { |
| 38 | + const multiSpaceHeading = curr.match(/^(\s{0,3}#{1,6})[ \t]{2,}(\S.*)$/); |
| 39 | + if (multiSpaceHeading && opt.headingSingleSpaceAfterHash) { |
| 40 | + curr = `${multiSpaceHeading[1]} ${multiSpaceHeading[2]}`; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (opt.listMarkerSpace) { |
| 46 | + const noSpaceList = curr.match(/^([ \t]{0,3})([-+*]|\d+[.)])(\S.*)$/); |
| 47 | + if (noSpaceList) { |
| 48 | + const indent = noSpaceList[1]; |
| 49 | + const marker = noSpaceList[2]; |
| 50 | + const content = noSpaceList[3]; |
| 51 | + if (shouldNormalizeNoSpaceList(marker, content)) { |
| 52 | + curr = `${indent}${marker} ${content}`; |
| 53 | + } |
| 54 | + } else { |
| 55 | + const multiSpaceList = curr.match(/^([ \t]{0,3}(?:[-+*]|\d+[.)]))[ \t]{2,}(\S.*)$/); |
| 56 | + if (multiSpaceList) { |
| 57 | + curr = `${multiSpaceList[1]} ${multiSpaceList[2]}`; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (opt.trimTrailingSpaces) { |
| 63 | + curr = curr.replace(/[ \t]+$/g, ""); |
| 64 | + } |
| 65 | + |
| 66 | + lines[i] = curr; |
| 67 | + } |
| 68 | + |
| 69 | + lines = fixStructuralBlankLines(lines, protectedLines, opt); |
| 70 | + |
| 71 | + let out = lines.join("\n"); |
| 72 | + if (opt.ensureSingleTrailingNewline) { |
| 73 | + out = out.replace(/\n*$/g, "\n"); |
| 74 | + } |
| 75 | + return out; |
| 76 | +} |
| 77 | + |
| 78 | +function fixStructuralBlankLines( |
| 79 | + lines: string[], |
| 80 | + protectedLines: boolean[], |
| 81 | + opt: MarkdownNormalizeSwitches |
| 82 | +): string[] { |
| 83 | + const out: string[] = []; |
| 84 | + |
| 85 | + for (let i = 0; i < lines.length; i += 1) { |
| 86 | + const curr = lines[i]; |
| 87 | + const prevOut = out.length > 0 ? out[out.length - 1] : null; |
| 88 | + const next = i + 1 < lines.length ? lines[i + 1] : null; |
| 89 | + |
| 90 | + const currFence = isFenceLine(curr); |
| 91 | + const currHeading = !protectedLines[i] && isAtxHeadingLine(curr); |
| 92 | + const currList = !protectedLines[i] && isListLine(curr); |
| 93 | + |
| 94 | + if (prevOut !== null && !isBlankLine(prevOut)) { |
| 95 | + if (currHeading && opt.blankLineAroundHeadings) { |
| 96 | + out.push(""); |
| 97 | + } else if (currFence && opt.blankLineAroundFences) { |
| 98 | + out.push(""); |
| 99 | + } else if ( |
| 100 | + currList && |
| 101 | + opt.blankLineAroundLists && |
| 102 | + !isListLine(prevOut) && |
| 103 | + !isListContinuationLine(prevOut) |
| 104 | + ) { |
| 105 | + out.push(""); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + out.push(curr); |
| 110 | + |
| 111 | + if (next !== null && !isBlankLine(next)) { |
| 112 | + if (currHeading && opt.blankLineAroundHeadings) { |
| 113 | + out.push(""); |
| 114 | + } else if (currFence && opt.blankLineAroundFences) { |
| 115 | + out.push(""); |
| 116 | + } else if ( |
| 117 | + currList && |
| 118 | + opt.blankLineAroundLists && |
| 119 | + !isListLine(next) && |
| 120 | + !isListContinuationLine(next) |
| 121 | + ) { |
| 122 | + out.push(""); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return out; |
| 128 | +} |
| 129 | + |
| 130 | +function markProtected(lines: string[]): boolean[] { |
| 131 | + const state = initGuard(); |
| 132 | + const out = new Array<boolean>(lines.length); |
| 133 | + |
| 134 | + for (let i = 0; i < lines.length; i += 1) { |
| 135 | + const reason = lineGuard(lines[i], state); |
| 136 | + out[i] = reason !== null; |
| 137 | + } |
| 138 | + |
| 139 | + return out; |
| 140 | +} |
| 141 | + |
| 142 | +function isBlankLine(line: string): boolean { |
| 143 | + return line.trim().length === 0; |
| 144 | +} |
| 145 | + |
| 146 | +function isAtxHeadingLine(line: string): boolean { |
| 147 | + return /^[ \t]{0,3}#{1,6}(?:\s|$)/.test(line); |
| 148 | +} |
| 149 | + |
| 150 | +function isListLine(line: string): boolean { |
| 151 | + return /^[ \t]{0,3}(?:[-+*]|\d+[.)])\s+/.test(line); |
| 152 | +} |
| 153 | + |
| 154 | +function isListContinuationLine(line: string): boolean { |
| 155 | + return /^\s{2,}\S/.test(line); |
| 156 | +} |
| 157 | + |
| 158 | +function isFenceLine(line: string): boolean { |
| 159 | + return /^\s*([`~])\1{2,}/.test(line); |
| 160 | +} |
| 161 | + |
| 162 | +function shouldNormalizeNoSpaceList(marker: string, content: string): boolean { |
| 163 | + if (marker !== "*") { |
| 164 | + return true; |
| 165 | + } |
| 166 | + |
| 167 | + const firstToken = content.match(/^\S+/)?.[0] ?? ""; |
| 168 | + return !firstToken.includes("*"); |
| 169 | +} |
| 170 | + |
| 171 | +export { applyMarkdownNormalizeRules }; |
| 172 | +export type { MarkdownNormalizeSwitches }; |
0 commit comments