Skip to content

feat(tree-sitter): detect URL scopes and set link property on text chunks#737

Closed
mugnimaestra wants to merge 1 commit intoanomalyco:mainfrom
mugnimaestra:feat/tree-sitter-link-detection
Closed

feat(tree-sitter): detect URL scopes and set link property on text chunks#737
mugnimaestra wants to merge 1 commit intoanomalyco:mainfrom
mugnimaestra:feat/tree-sitter-link-detection

Conversation

@mugnimaestra
Copy link

Summary

When tree-sitter highlights markdown content via the <code filetype="markdown"> rendering path, URL-bearing text chunks never receive a link property, so OSC 8 hyperlink sequences are never emitted. This causes terminals to fall back on visual URL auto-detection, which breaks when URLs wrap across lines.

Changes

packages/core/src/lib/tree-sitter-styled-text.ts

Added URL detection to treeSitterToTextChunks():

  1. Pre-scan phase (lines 51-68): Builds a Map<number, string> mapping highlight indices to URLs. Scans for markup.link.url and string.special.url scopes, extracts the URL text, and also maps associated markup.link.label highlights (found by walking backwards past markup.link bracket highlights) to the same URL.

  2. Chunk creation (lines 204-227): After merging styles, checks if any active highlight has a URL mapping. If so, sets link: { url } on the chunk.

This enables:

  • URL text (https://example.com) → clickable OSC 8 hyperlink
  • Link labels (Click here in [Click here](url)) → also clickable, linking to the same URL
  • URLs in code comments tagged with string.special.url → also clickable

packages/core/src/lib/tree-sitter-styled-text.test.ts

Added 5 new tests in a "Link detection" describe block:

  • markup.link.url chunks get link property ✅
  • markup.link.label chunks get associated URL ✅
  • string.special.url chunks get link property ✅
  • Non-URL chunks don't get link
  • Integration test with real tree-sitter parser ✅

Test Results

All 49 tests pass (44 existing + 5 new).

Related

…unks

When tree-sitter highlights contain URL-bearing scopes (markup.link.url,
string.special.url), extract the URL text and set the link property on
the corresponding TextChunk. Also propagates the URL to associated
markup.link.label chunks so link text is clickable too.

This enables OSC 8 hyperlink emission for the tree-sitter code rendering
path, fixing URL click behavior when long URLs wrap across terminal lines.
@mugnimaestra
Copy link
Author

Combining this with #736 — both fixes are needed together to solve the same issue (URLs breaking at line wrap).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant