-
Notifications
You must be signed in to change notification settings - Fork 61
Render agent thread updates: markdown links and agent-authored messages #3380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
2ef43de
dd391e3
5ce84d0
911f0d9
90c7a61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| const URL_PATTERN = /https?:\/\/[^\s<>]+/gi; | ||
|
|
||
| // Named links written as markdown `[label](https://url)` — the shape auto-posted | ||
| // thread messages use. Label excludes brackets and the URL excludes parens so | ||
| // the token boundaries are unambiguous (same reasoning as MENTION_PATTERN). | ||
| const MARKDOWN_LINK_PATTERN = /\[([^\][\n]+)\]\((https?:\/\/[^\s()]+)\)/gi; | ||
|
k11kirky marked this conversation as resolved.
|
||
|
|
||
| export interface LinkTextSegment { | ||
| type: "text"; | ||
| text: string; | ||
|
|
@@ -39,8 +44,34 @@ function trimTrailingPunctuation(url: string): string { | |
| return url.slice(0, end); | ||
| } | ||
|
|
||
| /** Split plain text into text and http(s) link segments, in document order. */ | ||
| /** | ||
| * Split plain text into text and http(s) link segments, in document order. | ||
| * Markdown-style `[label](url)` tokens become links titled by their label; | ||
| * bare URLs in the remaining text link as themselves. | ||
| */ | ||
| export function splitLinkSegments(text: string): LinkSegment[] { | ||
| const segments: LinkSegment[] = []; | ||
| let lastIndex = 0; | ||
| MARKDOWN_LINK_PATTERN.lastIndex = 0; | ||
| for (const match of text.matchAll(MARKDOWN_LINK_PATTERN)) { | ||
| const index = match.index ?? 0; | ||
| if (index > lastIndex) { | ||
| segments.push(...splitBareUrlSegments(text.slice(lastIndex, index))); | ||
| } | ||
| segments.push({ | ||
| type: "link", | ||
| text: match[1] ?? "", | ||
| href: match[2] ?? "", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Medium: Deceptive external links A collaborator can post |
||
| }); | ||
| lastIndex = index + match[0].length; | ||
| } | ||
| if (lastIndex < text.length) { | ||
| segments.push(...splitBareUrlSegments(text.slice(lastIndex))); | ||
| } | ||
| return segments; | ||
| } | ||
|
|
||
| function splitBareUrlSegments(text: string): LinkSegment[] { | ||
| const segments: LinkSegment[] = []; | ||
| let lastIndex = 0; | ||
| URL_PATTERN.lastIndex = 0; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.