From 35b616cd82232879937afc88d3f77d20c6395276 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Tue, 26 Sep 2023 11:53:12 -0500 Subject: [PATCH] Strip leading `#` from from detected tag facets (#1674) ensure # is removed from facets --- .changeset/wet-mayflies-turn.md | 5 +++ packages/api/src/rich-text/detection.ts | 2 +- .../api/tests/rich-text-detection.test.ts | 34 +++++++++---------- 3 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 .changeset/wet-mayflies-turn.md diff --git a/.changeset/wet-mayflies-turn.md b/.changeset/wet-mayflies-turn.md new file mode 100644 index 00000000000..26d00d014d1 --- /dev/null +++ b/.changeset/wet-mayflies-turn.md @@ -0,0 +1,5 @@ +--- +'@atproto/api': patch +--- + +Strip leading `#` from from detected tag facets diff --git a/packages/api/src/rich-text/detection.ts b/packages/api/src/rich-text/detection.ts index 503866d7df8..58a08e861e9 100644 --- a/packages/api/src/rich-text/detection.ts +++ b/packages/api/src/rich-text/detection.ts @@ -90,7 +90,7 @@ export function detectFacets(text: UnicodeString): Facet[] | undefined { features: [ { $type: 'app.bsky.richtext.facet#tag', - tag, + tag: tag.replace(/^#/, ''), }, ], }) diff --git a/packages/api/tests/rich-text-detection.test.ts b/packages/api/tests/rich-text-detection.test.ts index df2aed84889..52b10acb7d6 100644 --- a/packages/api/tests/rich-text-detection.test.ts +++ b/packages/api/tests/rich-text-detection.test.ts @@ -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 }], ], [ @@ -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 }, @@ -268,12 +268,12 @@ 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 }, @@ -281,7 +281,7 @@ describe('detectFacets', () => { ], [ '#same #same #but #diff', - ['#same', '#same', '#but', '#diff'], + ['same', 'same', 'but', 'diff'], [ { byteStart: 0, byteEnd: 5 }, { byteStart: 6, byteEnd: 11 }, @@ -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)) {