[PR #737] [CLOSED] feat(tree-sitter): detect URL scopes and set link property on text chunks #1528

Closed
opened 2026-03-14 09:41:40 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/anomalyco/opentui/pull/737
Author: @mugnimaestra
Created: 2/24/2026
Status: Closed

Base: mainHead: feat/tree-sitter-link-detection


📝 Commits (1)

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

📊 Changes

2 files changed (+150 additions, -0 deletions)

View changed files

📝 packages/core/src/lib/tree-sitter-styled-text.test.ts (+120 -0)
📝 packages/core/src/lib/tree-sitter-styled-text.ts (+30 -0)

📄 Description

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).

  • Fixes the default rendering path issue described in anomalyco/opentui#735
  • Complements #736 (which fixes OSC 8 id parameter for the experimental markdown path)
  • Related opencode issue: sst/opencode#(pending)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/anomalyco/opentui/pull/737 **Author:** [@mugnimaestra](https://github.com/mugnimaestra) **Created:** 2/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/tree-sitter-link-detection` --- ### 📝 Commits (1) - [`cc986ae`](https://github.com/anomalyco/opentui/commit/cc986aeb519da8dfbdb6a63c1a34727243373635) feat(tree-sitter): detect URL scopes and set link property on text chunks ### 📊 Changes **2 files changed** (+150 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/core/src/lib/tree-sitter-styled-text.test.ts` (+120 -0) 📝 `packages/core/src/lib/tree-sitter-styled-text.ts` (+30 -0) </details> ### 📄 Description ## 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 - Fixes the default rendering path issue described in anomalyco/opentui#735 - Complements #736 (which fixes OSC 8 id parameter for the experimental markdown path) - Related opencode issue: sst/opencode#(pending) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-14 09:41:40 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/opentui#1528
No description provided.