Skip to content

MD051: Replace regex parsing with native tree-sitter link analysis #135

Description

@ekropotin

Problem

The current MD051 implementation uses regex to parse links within tree-sitter nodes, which is redundant and architecturally suboptimal.

Current flawed approach:

  1. Tree-sitter parses the document into structured nodes
  2. We extract text from inline nodes
  3. We re-parse the text with regex: r"\[([^\]]*)\]\(([^)]*#[^)]*)\)"
  4. 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:

  1. Find link patterns in tree-sitter nodes: sequence of [ → text → ]( → URL → )
  2. Extract fragments from URL tokens that contain #
  3. Use tree-sitter positions directly (no byte_to_point conversion needed)
  4. 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: triageNeeds review and prioritization

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions