(text: string)
| 239 | * language line) but no closer. |
| 240 | */ |
| 241 | export function isBalanced(text: string): boolean { |
| 242 | if (!text) return true; |
| 243 | |
| 244 | // ── Fences ───────────────────────────────────────────────────── |
| 245 | const fences = (text.match(/```/g) || []).length; |
| 246 | if (fences % 2 !== 0) { |
| 247 | const lastFenceIdx = text.lastIndexOf("```"); |
| 248 | const tail = text.slice(lastFenceIdx + 3); |
| 249 | const nl = tail.indexOf("\n"); |
| 250 | const codeBody = nl >= 0 ? tail.slice(nl + 1) : ""; |
| 251 | if (/\S/.test(codeBody)) return false; // real content past the lang line |
| 252 | // else: just-opened fence; treat as balanced |
| 253 | } |
| 254 | |
| 255 | // ── Inline backticks (outside fences) ────────────────────────── |
| 256 | const noFence = text.replace(/```[\s\S]*?```/g, ""); |
| 257 | const inline = (noFence.match(/`/g) || []).length; |
| 258 | if (inline % 2 !== 0) { |
| 259 | const lastBt = noFence.lastIndexOf("`"); |
| 260 | const after = noFence.slice(lastBt + 1); |
| 261 | if (/\S/.test(after)) return false; // real content past the open backtick |
| 262 | } |
| 263 | return true; |
| 264 | } |
no outgoing calls
no test coverage detected