Skip to content

Commit

Permalink
Strip leading # from from detected tag facets (bluesky-social#1674)
Browse files Browse the repository at this point in the history
ensure # is removed from facets
  • Loading branch information
estrattonbailey authored Sep 26, 2023
1 parent 64288a0 commit 509ba50
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-mayflies-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@atproto/api': patch
---

Strip leading `#` from from detected tag facets
2 changes: 1 addition & 1 deletion packages/api/src/rich-text/detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function detectFacets(text: UnicodeString): Facet[] | undefined {
features: [
{
$type: 'app.bsky.richtext.facet#tag',
tag,
tag: tag.replace(/^#/, ''),
},
],
})
Expand Down
34 changes: 17 additions & 17 deletions packages/api/tests/rich-text-detection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,28 +216,28 @@ describe('detectFacets', () => {
string[],
{ byteStart: number; byteEnd: number }[],
][] = [
['#a', ['#a'], [{ byteStart: 0, byteEnd: 2 }]],
['#a', ['a'], [{ byteStart: 0, byteEnd: 2 }]],
[
'#a #b',
['#a', '#b'],
['a', 'b'],
[
{ byteStart: 0, byteEnd: 2 },
{ byteStart: 3, byteEnd: 5 },
],
],
['#1', [], []],
['#tag', ['#tag'], [{ byteStart: 0, byteEnd: 4 }]],
['body #tag', ['#tag'], [{ byteStart: 5, byteEnd: 9 }]],
['#tag body', ['#tag'], [{ byteStart: 0, byteEnd: 4 }]],
['body #tag body', ['#tag'], [{ byteStart: 5, byteEnd: 9 }]],
['#tag', ['tag'], [{ byteStart: 0, byteEnd: 4 }]],
['body #tag', ['tag'], [{ byteStart: 5, byteEnd: 9 }]],
['#tag body', ['tag'], [{ byteStart: 0, byteEnd: 4 }]],
['body #tag body', ['tag'], [{ byteStart: 5, byteEnd: 9 }]],
['body #1', [], []],
['body #a1', ['#a1'], [{ byteStart: 5, byteEnd: 8 }]],
['body #a1', ['a1'], [{ byteStart: 5, byteEnd: 8 }]],
['#', [], []],
['text #', [], []],
['text # text', [], []],
[
'body #thisisa64characterstring_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
['#thisisa64characterstring_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'],
['thisisa64characterstring_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'],
[{ byteStart: 5, byteEnd: 71 }],
],
[
Expand All @@ -247,19 +247,19 @@ describe('detectFacets', () => {
],
[
'its a #double#rainbow',
['#double#rainbow'],
['double#rainbow'],
[{ byteStart: 6, byteEnd: 21 }],
],
['##hashash', ['##hashash'], [{ byteStart: 0, byteEnd: 9 }]],
['some #n0n3s@n5e!', ['#n0n3s@n5e'], [{ byteStart: 5, byteEnd: 15 }]],
['##hashash', ['#hashash'], [{ byteStart: 0, byteEnd: 9 }]],
['some #n0n3s@n5e!', ['n0n3s@n5e'], [{ byteStart: 5, byteEnd: 15 }]],
[
'works #with,punctuation',
['#with,punctuation'],
['with,punctuation'],
[{ byteStart: 6, byteEnd: 23 }],
],
[
'strips trailing #punctuation, #like. #this!',
['#punctuation', '#like', '#this'],
['punctuation', 'like', 'this'],
[
{ byteStart: 16, byteEnd: 28 },
{ byteStart: 30, byteEnd: 35 },
Expand All @@ -268,20 +268,20 @@ describe('detectFacets', () => {
],
[
'strips #multi_trailing___...',
['#multi_trailing'],
['multi_trailing'],
[{ byteStart: 7, byteEnd: 22 }],
],
[
'works with #🦋 emoji, and #butter🦋fly',
['#🦋', '#butter🦋fly'],
['🦋', 'butter🦋fly'],
[
{ byteStart: 11, byteEnd: 16 },
{ byteStart: 28, byteEnd: 42 },
],
],
[
'#same #same #but #diff',
['#same', '#same', '#but', '#diff'],
['same', 'same', 'but', 'diff'],
[
{ byteStart: 0, byteEnd: 5 },
{ byteStart: 6, byteEnd: 11 },
Expand All @@ -298,7 +298,7 @@ describe('detectFacets', () => {
let detectedTags: string[] = []
let detectedIndices: { byteStart: number; byteEnd: number }[] = []

for (const { facet } of rt.segments()) {
for (const { facet, ...rest } of rt.segments()) {
if (!facet) continue
for (const feature of facet.features) {
if (isTag(feature)) {
Expand Down

0 comments on commit 509ba50

Please sign in to comment.