-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Update Bluesky component versions and add text encoding functionality #18443
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
Merged
vunguyenhung
merged 1 commit into
master
from
15446-action-bluesky-hash-tags-rich-text-facets
Sep 24, 2025
+1,577
−42
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import TLDs from "./tlds.mjs"; | ||
|
|
||
| function isValidDomain(str) { | ||
| return !!TLDs.find((tld) => { | ||
| const i = str.lastIndexOf(tld); | ||
| if (i === -1) { | ||
| return false; | ||
| } | ||
| return str.charAt(i - 1) === "." && i === str.length - tld.length; | ||
| }); | ||
| } | ||
|
|
||
| function utf16IndexToUtf8Index(i, utf16) { | ||
| const encoder = new TextEncoder(); | ||
| return encoder.encode(utf16.slice(0, i)).byteLength; | ||
| } | ||
|
|
||
| async function detectFacets(text, app) { | ||
| let match; | ||
| const facets = []; | ||
|
|
||
| // mentions | ||
| const re1 = /(^|\s|\()(@)([a-zA-Z0-9.-]+)(\b)/g; | ||
| while ((match = re1.exec(text))) { | ||
| if (!isValidDomain(match[3]) && !match[3].endsWith(".test")) { | ||
| continue; // probably not a handle | ||
| } | ||
|
|
||
| const start = text.indexOf(match[3], match.index) - 1; | ||
| const handle = await app.resolveHandle({ | ||
| params: { | ||
| handle: match[3], | ||
| }, | ||
| }); | ||
| facets.push({ | ||
| $type: "app.bsky.richtext.facet", | ||
| index: { | ||
| byteStart: utf16IndexToUtf8Index(start, text), | ||
| byteEnd: utf16IndexToUtf8Index(start + match[3].length + 1, text), | ||
| }, | ||
| features: [ | ||
| { | ||
| $type: "app.bsky.richtext.facet#mention", | ||
| did: handle.did, // must be resolved afterwards | ||
| }, | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| // links | ||
| const re2 = | ||
| /(^|\s|\()((https?:\/\/[\S]+)|((?<domain>[a-z][a-z0-9]*(\.[a-z0-9]+)+)[\S]*))/gim; | ||
| while ((match = re2.exec(text))) { | ||
| let uri = match[2]; | ||
| if (!uri.startsWith("http")) { | ||
| const domain = match.groups?.domain; | ||
| if (!domain || !isValidDomain(domain)) { | ||
| continue; | ||
| } | ||
| uri = `https://${uri}`; | ||
| } | ||
| const start = text.indexOf(match[2], match.index); | ||
| const index = { | ||
| start, | ||
| end: start + match[2].length, | ||
| }; | ||
| // strip ending puncuation | ||
| if (/[.,;!?]$/.test(uri)) { | ||
| uri = uri.slice(0, -1); | ||
| index.end--; | ||
| } | ||
| if (/[)]$/.test(uri) && !uri.includes("(")) { | ||
| uri = uri.slice(0, -1); | ||
| index.end--; | ||
| } | ||
| facets.push({ | ||
| index: { | ||
| byteStart: utf16IndexToUtf8Index(index.start, text), | ||
| byteEnd: utf16IndexToUtf8Index(index.end, text), | ||
| }, | ||
| features: [ | ||
| { | ||
| $type: "app.bsky.richtext.facet#link", | ||
| uri, | ||
| }, | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| const re3 = /(?:^|\s)(#[^\d\s]\S*)(?=\s)?/g; | ||
| while ((match = re3.exec(text))) { | ||
| let [ | ||
| tag, | ||
| ] = match; | ||
| const hasLeadingSpace = /^\s/.test(tag); | ||
|
|
||
| tag = tag.trim().replace(/\p{P}+$/gu, ""); // strip ending punctuation | ||
|
|
||
| // inclusive of #, max of 64 chars | ||
| if (tag.length > 66) continue; | ||
|
|
||
| const index = match.index + (hasLeadingSpace | ||
| ? 1 | ||
| : 0); | ||
|
|
||
| facets.push({ | ||
| index: { | ||
| byteStart: utf16IndexToUtf8Index(index, text), | ||
| byteEnd: utf16IndexToUtf8Index(index + tag.length, text), // inclusive of last char | ||
| }, | ||
| features: [ | ||
| { | ||
| $type: "app.bsky.richtext.facet#tag", | ||
| tag: tag.replace(/^#/, ""), | ||
| }, | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| return facets.length > 0 | ||
| ? facets | ||
| : undefined; | ||
| } | ||
|
|
||
| export default { | ||
| detectFacets, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.