Problem
The current MD051 implementation uses regex to parse links within tree-sitter nodes, which is redundant and architecturally suboptimal.
Current flawed approach:
- Tree-sitter parses the document into structured nodes
- We extract text from
inline nodes
- We re-parse the text with regex:
r"\[([^\]]*)\]\(([^)]*#[^)]*)\)"
- We manually calculate positions using custom
byte_to_point conversion
Tree-Sitter Already Provides Link Structure
Tree-sitter markdown parser already gives us precise link structure:
paragraph: "[test link](#fragment)"
inline: "[test link](#fragment)" (0:0-0:22)
[: "[" (0:0-0:1) // Link start
]: "]" (0:10-0:11) // Link text end
(: "(" (0:11-0:12) // URL start
#: "#" (0:12-0:13) // Fragment marker
): ")" (0:21-0:22) // Link end
Proposed Solution
Replace regex parsing with native tree-sitter traversal:
- Find link patterns in tree-sitter nodes: sequence of
[ → text → ] → ( → URL → )
- Extract fragments from URL tokens that contain
#
- Use tree-sitter positions directly (no
byte_to_point conversion needed)
- Filter external links by checking if URL starts with protocol/path before
#
Benefits
- Eliminate regex dependency for link parsing
- Remove custom position calculation (use tree-sitter's precise positions)
- Better edge case handling (tree-sitter is more robust than regex)
- Performance improvement (no redundant parsing)
- Cleaner architecture (single source of truth for document structure)
Implementation Notes
The tree-sitter approach would be more aligned with how other rules work in the codebase and eliminate the architectural inconsistency of mixing tree-sitter and regex parsing.
Current regex-based approach works correctly but is architecturally suboptimal.
Problem
The current MD051 implementation uses regex to parse links within tree-sitter nodes, which is redundant and architecturally suboptimal.
Current flawed approach:
inlinenodesr"\[([^\]]*)\]\(([^)]*#[^)]*)\)"byte_to_pointconversionTree-Sitter Already Provides Link Structure
Tree-sitter markdown parser already gives us precise link structure:
Proposed Solution
Replace regex parsing with native tree-sitter traversal:
[→ text →]→(→ URL →)#byte_to_pointconversion needed)#Benefits
Implementation Notes
The tree-sitter approach would be more aligned with how other rules work in the codebase and eliminate the architectural inconsistency of mixing tree-sitter and regex parsing.
Current regex-based approach works correctly but is architecturally suboptimal.