* 删除带 `#to-be-updated` 的整个尾部块。 * 通常的写法是: * * --- * #to-be-updated 2026-04-22: <说明> * * 把这一段(含前面的 `---` 与空行)整体清掉。 * * 边界处理: * - 跳过围栏代码块(``` 之间):示例代码里讨论 `#to-be-updated` 这个 tag * 不应触发删除,否则会把整段示例代码连带前导分隔符吃掉 * - 回退前导空行/分隔符**最多 2 行**(一个 `---` + 一个空行):避免误删 * 正文段落之间的合法空行
(content: string)
| 153 | * 正文段落之间的合法空行 |
| 154 | */ |
| 155 | function removeToBeUpdatedBlock(content: string): string { |
| 156 | const lines = content.split("\n"); |
| 157 | const out: string[] = []; |
| 158 | let inCodeFence = false; |
| 159 | for (let i = 0; i < lines.length; i++) { |
| 160 | const line = lines[i]; |
| 161 | // 切换围栏状态(``` 或 ~~~ 起头) |
| 162 | if (/^[ \t]{0,3}(```|~~~)/.test(line)) { |
| 163 | inCodeFence = !inCodeFence; |
| 164 | out.push(line); |
| 165 | continue; |
| 166 | } |
| 167 | if (!inCodeFence && TO_BE_UPDATED_RE.test(line)) { |
| 168 | // 删除当前行;同时回退去掉前面的 "---" 分隔符与空行(最多 2 行) |
| 169 | let popped = 0; |
| 170 | while (out.length > 0 && popped < 2) { |
| 171 | const last = out[out.length - 1]; |
| 172 | if (last.trim() === "" || last.trim() === "---") { |
| 173 | out.pop(); |
| 174 | popped += 1; |
| 175 | continue; |
| 176 | } |
| 177 | break; |
| 178 | } |
| 179 | // 跳过当前行 |
| 180 | continue; |
| 181 | } |
| 182 | out.push(line); |
| 183 | } |
| 184 | // 收尾:保证文件以单个换行结尾 |
| 185 | let result = out.join("\n"); |
| 186 | if (!result.endsWith("\n")) result += "\n"; |
| 187 | return result; |
| 188 | } |