Brain Structural Validation / Validate Markdown Structure & Links (push) Successful in 7s
Replace the two independent fence implementations with a single
scan_code_blocks() function that both check_code_fences() and
check_mermaid() consume.
Previous state:
check_code_fences() counted lines starting with ``` globally,
including lines inside other fences, and produced a single
"found N delimiters" message with no way to distinguish
multiple unclosed fences.
check_mermaid() used re.finditer with DOTALL, which could match
a ```mermaid line inside a documentation fence as a real
diagram, producing false positives.
scan_code_blocks(content) walks the document line-by-line:
- Tracks the opening fence backtick width (3, 4, ...)
- Closing fence requires >= opener width and no info string
(4-backtick outer not closed by 3-backtick inner — CommonMark)
- Up to 3 leading spaces permitted on fence lines
Returns:
blocks — CodeBlock per matched pair (language, content,
start_line, end_line)
unclosed — start_line for every opener with no matching close
check_code_fences() now reports each unclosed fence separately
with the precise start_line.
check_mermaid() filters blocks by language == mermaid so
documentation examples inside outer fences are never validated.
Line numbering uses block.start_line + 1 + i from the scanner.
Integration tested: balanced fences, unclosed fence, 4-backtick
outer/3-backtick inner, real mermaid labels and structure, both
checks agree on the same document.