Repository navigation
fix(tui): add OSC 8 hyperlinks for wrapped URLs - #17814
Conversation
| } | ||
|
|
||
| // Find new URL starts in visible text | ||
| const urlRe = /https?:\/\/[^\s)\]>]*/g; |
There was a problem hiding this comment.
Regex quantifier mismatch: * should be +
The regex here uses * (zero or more) after ://, while extractUrls on line 30 uses + (one or more). With *, this regex can match a bare "https://" with no host or path, which would then be incorrectly resolved to whichever known URL happens to appear first in the array. This could produce spurious hyperlinks on text like "Use https:// for secure connections".
| const urlRe = /https?:\/\/[^\s)\]>]*/g; | |
| const urlRe = /https?:\/\/[^\s)\]>]+/g; |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/tui/osc8-hyperlinks.ts
Line: 91:91
Comment:
**Regex quantifier mismatch: `*` should be `+`**
The regex here uses `*` (zero or more) after `://`, while `extractUrls` on line 30 uses `+` (one or more). With `*`, this regex can match a bare `"https://"` with no host or path, which would then be incorrectly resolved to whichever known URL happens to appear first in the array. This could produce spurious hyperlinks on text like `"Use https:// for secure connections"`.
```suggestion
const urlRe = /https?:\/\/[^\s)\]>]+/g;
```
How can I resolve this? If you propose a fix, please make it concise.| if (!found) { | ||
| for (const known of knownUrls) { | ||
| if (fragment.startsWith(known)) { | ||
| resolvedUrl = known; | ||
| break; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Prefix resolution picks first match, not longest
When a fragment is a superstring of multiple known URLs (e.g., knownUrls contains both https://example.com and https://example.com/api/v2, and the fragment is https://example.com/api/v2/users), this loop resolves to whichever known URL appears first in the array rather than the most specific (longest) match. Consider selecting the longest matching prefix instead:
| if (!found) { | |
| for (const known of knownUrls) { | |
| if (fragment.startsWith(known)) { | |
| resolvedUrl = known; | |
| break; | |
| } | |
| } | |
| } | |
| if (!found) { | |
| let bestLen = 0; | |
| for (const known of knownUrls) { | |
| if (fragment.startsWith(known) && known.length > bestLen) { | |
| resolvedUrl = known; | |
| bestLen = known.length; | |
| } | |
| } | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/tui/osc8-hyperlinks.ts
Line: 119:126
Comment:
**Prefix resolution picks first match, not longest**
When a fragment is a superstring of multiple known URLs (e.g., `knownUrls` contains both `https://example.com` and `https://example.com/api/v2`, and the fragment is `https://example.com/api/v2/users`), this loop resolves to whichever known URL appears first in the array rather than the most specific (longest) match. Consider selecting the longest matching prefix instead:
```suggestion
if (!found) {
let bestLen = 0;
for (const known of knownUrls) {
if (fragment.startsWith(known) && known.length > bestLen) {
resolvedUrl = known;
bestLen = known.length;
}
}
}
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Pull request overview
Adds OSC 8 terminal hyperlink support to the TUI markdown renderer so URLs remain clickable even when pi-tui wraps them across lines.
Changes:
- Introduces URL extraction + OSC 8 post-processing to hyperlink wrapped URL fragments
- Wraps pi-tui
Markdownwith a newHyperlinkMarkdowncomponent and updates assistant message rendering - Adds Vitest unit coverage for the new hyperlink behavior
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| src/tui/osc8-hyperlinks.ts | Implements URL extraction and OSC 8 hyperlink injection over rendered/wrapped lines |
| src/tui/osc8-hyperlinks.test.ts | Adds unit tests for URL extraction and hyperlink insertion (including wrapped URLs) |
| src/tui/components/hyperlink-markdown.ts | Adds a wrapper component that applies OSC 8 post-processing during render |
| src/tui/components/assistant-message.ts | Switches assistant message body from Markdown to HyperlinkMarkdown |
| } | ||
|
|
||
| // Find new URL starts in visible text | ||
| const urlRe = /https?:\/\/[^\s)\]>]*/g; |
There was a problem hiding this comment.
This regex allows zero characters after https:// because of *, so it can match and hyperlink a bare scheme like https:// (no host/path). Consider requiring at least one character after the scheme (e.g., use +) or adding a minimal host check to avoid producing hyperlinks for invalid/partial URLs.
| const urlRe = /https?:\/\/[^\s)\]>]*/g; | |
| const urlRe = /https?:\/\/[^\s)\]>]+/g; |
| // Markdown link hrefs: [text](url) | ||
| const mdLinkRe = /\[(?:[^\]]*)\]\((https?:\/\/[^)]+)\)/g; | ||
| let m: RegExpExecArray | null; | ||
| while ((m = mdLinkRe.exec(markdown)) !== null) { | ||
| urls.add(m[1]); | ||
| } | ||
|
|
||
| // Bare URLs (remove markdown links first to avoid double-matching) | ||
| const stripped = markdown.replace(/\[(?:[^\]]*)\]\([^)]+\)/g, ""); |
There was a problem hiding this comment.
Markdown links can legally include a title after the URL (e.g., [text](https://ex.com \"title\")) and/or wrap the URL in angle brackets; with the current regex, mdLinkRe will capture the title as part of the URL, and stripped will remove patterns that don’t match real link boundaries (e.g., URLs containing )), causing incorrect URL resolution later. Consider parsing the markdown link destination more precisely (stop at whitespace before an optional title, support <...>, and avoid naive [^)]+), or leverage an existing markdown parser/tokenizer if available in this codebase.
| // Markdown link hrefs: [text](url) | |
| const mdLinkRe = /\[(?:[^\]]*)\]\((https?:\/\/[^)]+)\)/g; | |
| let m: RegExpExecArray | null; | |
| while ((m = mdLinkRe.exec(markdown)) !== null) { | |
| urls.add(m[1]); | |
| } | |
| // Bare URLs (remove markdown links first to avoid double-matching) | |
| const stripped = markdown.replace(/\[(?:[^\]]*)\]\([^)]+\)/g, ""); | |
| // Markdown link hrefs: [text](url) or [text](<url> "title") | |
| const mdLinkRe = | |
| /\[(?:[^\]]*)\]\(\s*(<https?:\/\/[^>\s]+>|https?:\/\/[^\s)]+)[^)]*\)/g; | |
| let m: RegExpExecArray | null; | |
| while ((m = mdLinkRe.exec(markdown)) !== null) { | |
| let dest = m[1]; | |
| if (dest.startsWith("<") && dest.endsWith(">")) { | |
| dest = dest.slice(1, -1); | |
| } | |
| urls.add(dest); | |
| } | |
| // Bare URLs (remove markdown links first to avoid double-matching) | |
| const stripped = markdown.replace( | |
| /\[(?:[^\]]*)\]\(\s*(?:<https?:\/\/[^>\s]+>|https?:\/\/[^\s)]+)[^)]*\)/g, | |
| "", | |
| ); |
| // Markdown link hrefs: [text](url) | ||
| const mdLinkRe = /\[(?:[^\]]*)\]\((https?:\/\/[^)]+)\)/g; | ||
| let m: RegExpExecArray | null; | ||
| while ((m = mdLinkRe.exec(markdown)) !== null) { | ||
| urls.add(m[1]); | ||
| } | ||
|
|
||
| // Bare URLs (remove markdown links first to avoid double-matching) | ||
| const stripped = markdown.replace(/\[(?:[^\]]*)\]\([^)]+\)/g, ""); |
There was a problem hiding this comment.
Markdown links can legally include a title after the URL (e.g., [text](https://ex.com \"title\")) and/or wrap the URL in angle brackets; with the current regex, mdLinkRe will capture the title as part of the URL, and stripped will remove patterns that don’t match real link boundaries (e.g., URLs containing )), causing incorrect URL resolution later. Consider parsing the markdown link destination more precisely (stop at whitespace before an optional title, support <...>, and avoid naive [^)]+), or leverage an existing markdown parser/tokenizer if available in this codebase.
| // Markdown link hrefs: [text](url) | |
| const mdLinkRe = /\[(?:[^\]]*)\]\((https?:\/\/[^)]+)\)/g; | |
| let m: RegExpExecArray | null; | |
| while ((m = mdLinkRe.exec(markdown)) !== null) { | |
| urls.add(m[1]); | |
| } | |
| // Bare URLs (remove markdown links first to avoid double-matching) | |
| const stripped = markdown.replace(/\[(?:[^\]]*)\]\([^)]+\)/g, ""); | |
| // Markdown link hrefs: [text](url), with optional title and optional <...> dest | |
| // Examples: | |
| // [text](https://ex.com) | |
| // [text](https://ex.com "title") | |
| // [text](<https://ex.com>) | |
| // [text](<https://ex.com> "title") | |
| const mdLinkRe = | |
| /\[(?:[^\]]*)\]\(\s*(?:<([^>\s]+)>|([^\s)]+))(?:\s+["'(][^)]*)?\s*\)/g; | |
| let m: RegExpExecArray | null; | |
| while ((m = mdLinkRe.exec(markdown)) !== null) { | |
| const url = m[1] ?? m[2]; | |
| if (url) { | |
| urls.add(url); | |
| } | |
| } | |
| // Bare URLs (remove markdown links first to avoid double-matching) | |
| const stripped = markdown.replace(mdLinkRe, ""); |
| } | ||
|
|
||
| // Find new URL starts in visible text | ||
| const urlRe = /https?:\/\/[^\s)\]>]*/g; |
There was a problem hiding this comment.
Because the URL matcher allows trailing punctuation (e.g., https://foo.com,), the fragment.startsWith(known) fallback will resolve the target to the clean known URL while still hyperlinking the punctuated fragment range. This makes the visible hyperlink text include punctuation (and can interfere with split detection/pending matching), even though the link target is different. Consider trimming common trailing punctuation from matched fragments before range creation (or using a stricter URL terminator set) so the hyperlinked visible span matches the resolved target more closely.
| // Resolve fragment to a known URL (exact > prefix > superstring) | ||
| let resolvedUrl = fragment; | ||
| let found = false; |
There was a problem hiding this comment.
Because the URL matcher allows trailing punctuation (e.g., https://foo.com,), the fragment.startsWith(known) fallback will resolve the target to the clean known URL while still hyperlinking the punctuated fragment range. This makes the visible hyperlink text include punctuation (and can interfere with split detection/pending matching), even though the link target is different. Consider trimming common trailing punctuation from matched fragments before range creation (or using a stricter URL terminator set) so the hyperlinked visible span matches the resolved target more closely.
| for (const known of knownUrls) { | ||
| if (fragment.startsWith(known)) { | ||
| resolvedUrl = known; | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
Because the URL matcher allows trailing punctuation (e.g., https://foo.com,), the fragment.startsWith(known) fallback will resolve the target to the clean known URL while still hyperlinking the punctuated fragment range. This makes the visible hyperlink text include punctuation (and can interfere with split detection/pending matching), even though the link target is different. Consider trimming common trailing punctuation from matched fragments before range creation (or using a stricter URL terminator set) so the hyperlinked visible span matches the resolved target more closely.
| // Existing OSC 8 sequence (pass through) | ||
| const osc = line.slice(i).match(OSC8_START_RE); | ||
| if (osc) { | ||
| result += osc[0]; | ||
| i += osc[0].length; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Visible character — toggle OSC 8 at range boundaries | ||
| const targetUrl = urlAt.get(visiblePos) ?? null; | ||
| if (targetUrl !== activeUrl) { | ||
| if (activeUrl !== null) { | ||
| result += "\x1b]8;;\x07"; | ||
| } | ||
| if (targetUrl !== null) { | ||
| result += `\x1b]8;;${targetUrl}\x07`; | ||
| } | ||
| activeUrl = targetUrl; | ||
| } |
There was a problem hiding this comment.
If the input line already contains OSC 8 hyperlinks, the code currently passes the OSC 8 escape sequences through but does not track whether it is inside an existing OSC 8 span. That can lead to nested/mismatched hyperlink sequences when applyOsc8Ranges toggles activeUrl over the same visible text. Consider detecting existing OSC 8 usage and either (a) skipping post-processing for that line, or (b) tracking OSC 8 open/close state while walking the raw string so you don’t insert new OSC 8 sequences inside an existing hyperlink.
| }); | ||
| }); | ||
|
|
||
| describe("extractUrls", () => { |
There was a problem hiding this comment.
Given the regex-based markdown link extraction, it would be valuable to add tests for common markdown link variants that are currently untested: link titles (e.g., [t](https://ex.com \"title\")), angle-bracket destinations ([t](<https://ex.com>)), and URLs containing )-like characters. These cases are where extractUrls() is most likely to mis-extract and cause addOsc8Hyperlinks() to fail to resolve fragments correctly.
| it("extracts markdown link hrefs", () => { | ||
| const urls = extractUrls("[Click here](https://example.com/path)"); | ||
| expect(urls).toEqual(["https://example.com/path"]); | ||
| }); |
There was a problem hiding this comment.
Given the regex-based markdown link extraction, it would be valuable to add tests for common markdown link variants that are currently untested: link titles (e.g., [t](https://ex.com \"title\")), angle-bracket destinations ([t](<https://ex.com>)), and URLs containing )-like characters. These cases are where extractUrls() is most likely to mis-extract and cause addOsc8Hyperlinks() to fail to resolve fragments correctly.
38c84ce to
ac29c75
Compare
|
This pull request has been automatically marked as stale due to inactivity. |
|
you have been detected be spamming with unwarranted prs and issues and your issues and prs have been automatically closed. please read contributing guide Contributing.md. |
|
Follow-up pushed to address review findings. What changed:
Thanks @Phineas1500 for the original implementation. |
b2b4023 to
03490de
Compare
|
Rebuilt this branch on current
The PR now reports mergeable ( |
Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support.
03490de to
882d46c
Compare
* feat(tui): add OSC 8 hyperlinks to make wrapped URLs clickable Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support. * tui: harden OSC8 URL extraction and prefix resolution * tui: add changelog entry for OSC 8 markdown hyperlinks --------- Co-authored-by: Vincent Koc <[email protected]>
* feat(tui): add OSC 8 hyperlinks to make wrapped URLs clickable Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support. * tui: harden OSC8 URL extraction and prefix resolution * tui: add changelog entry for OSC 8 markdown hyperlinks --------- Co-authored-by: Vincent Koc <[email protected]>
* feat(tui): add OSC 8 hyperlinks to make wrapped URLs clickable Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support. * tui: harden OSC8 URL extraction and prefix resolution * tui: add changelog entry for OSC 8 markdown hyperlinks --------- Co-authored-by: Vincent Koc <[email protected]>
* feat(tui): add OSC 8 hyperlinks to make wrapped URLs clickable Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support. * tui: harden OSC8 URL extraction and prefix resolution * tui: add changelog entry for OSC 8 markdown hyperlinks --------- Co-authored-by: Vincent Koc <[email protected]>
* feat(tui): add OSC 8 hyperlinks to make wrapped URLs clickable Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support. * tui: harden OSC8 URL extraction and prefix resolution * tui: add changelog entry for OSC 8 markdown hyperlinks --------- Co-authored-by: Vincent Koc <[email protected]>
* feat(tui): add OSC 8 hyperlinks to make wrapped URLs clickable Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support. * tui: harden OSC8 URL extraction and prefix resolution * tui: add changelog entry for OSC 8 markdown hyperlinks --------- Co-authored-by: Vincent Koc <[email protected]>
* feat(tui): add OSC 8 hyperlinks to make wrapped URLs clickable Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support. * tui: harden OSC8 URL extraction and prefix resolution * tui: add changelog entry for OSC 8 markdown hyperlinks --------- Co-authored-by: Vincent Koc <[email protected]>
* feat(tui): add OSC 8 hyperlinks to make wrapped URLs clickable Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support. * tui: harden OSC8 URL extraction and prefix resolution * tui: add changelog entry for OSC 8 markdown hyperlinks --------- Co-authored-by: Vincent Koc <[email protected]>
* feat(tui): add OSC 8 hyperlinks to make wrapped URLs clickable Long URLs that exceed terminal width get broken across lines by pi-tui's word wrapping, making them unclickable. Post-process rendered markdown output to add OSC 8 terminal hyperlink sequences around URL fragments, so each line fragment links to the full URL. Gracefully degrades on terminals without OSC 8 support. * tui: harden OSC8 URL extraction and prefix resolution * tui: add changelog entry for OSC 8 markdown hyperlinks --------- Co-authored-by: Vincent Koc <[email protected]>
Summary
Fixes #17772 — an alternative to #17777 that uses OSC 8 terminal hyperlinks instead of ZWNJ characters.
Why this over #17777 (ZWNJ approach)
breakLongWord()still breaks char-by-char regardless of ZWNJCM, notWJ; doesn't actually prevent breaksThe ZWNJ approach tries to prevent wrapping but fails because
breakLongWord()ignores ZWNJ. Our approach accepts wrapping and makes the broken fragments clickable using the industry-standard OSC 8 mechanism.Changes
src/tui/osc8-hyperlinks.ts—extractUrls()to find URLs in markdown source,addOsc8Hyperlinks()to post-process rendered lines with OSC 8 sequences (handles multi-line URL splits)src/tui/components/hyperlink-markdown.ts—HyperlinkMarkdownwrapper around pi-tui'sMarkdownthat applies OSC 8 post-processing inrender()src/tui/components/assistant-message.ts— UseHyperlinkMarkdowninstead ofMarkdownsrc/tui/osc8-hyperlinks.test.ts— 17 unit testsTest plan
pnpm vitest run src/tui/osc8-hyperlinks.test.ts— 17/17 passpnpm buildpassespnpm checkpasses (lint + format + types)Greptile Summary
Adds OSC 8 terminal hyperlinks to make long URLs clickable when wrapped across multiple lines in the TUI. Post-processes rendered markdown to wrap URL fragments with hyperlink sequences.
osc8-hyperlinks.tsmodule extracts URLs from markdown and adds OSC 8 sequences to rendered linesHyperlinkMarkdowncomponent wraps pi-tui'sMarkdownto apply hyperlink processingAssistantMessageComponentnow usesHyperlinkMarkdowninstead of plainMarkdownConfidence Score: 4/5
src/tui/osc8-hyperlinks.ts- ensure the two previously-identified issues are resolved before mergingLast reviewed commit: ac29c75