@atproto/api 0.6.17 → 0.6.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@ import { UnicodeString } from './unicode';
4
4
  export declare type Facet = AppBskyRichtextFacet.Main;
5
5
  export declare type FacetLink = AppBskyRichtextFacet.Link;
6
6
  export declare type FacetMention = AppBskyRichtextFacet.Mention;
7
+ export declare type FacetTag = AppBskyRichtextFacet.Tag;
7
8
  export declare type Entity = AppBskyFeedPost.Entity;
8
9
  export interface RichTextProps {
9
10
  text: string;
@@ -21,6 +22,8 @@ export declare class RichTextSegment {
21
22
  isLink(): boolean;
22
23
  get mention(): FacetMention | undefined;
23
24
  isMention(): boolean;
25
+ get tag(): FacetTag | undefined;
26
+ isTag(): boolean;
24
27
  }
25
28
  export declare class RichText {
26
29
  unicodeText: UnicodeString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/api",
3
- "version": "0.6.17",
3
+ "version": "0.6.18",
4
4
  "license": "MIT",
5
5
  "description": "Client library for atproto and Bluesky",
6
6
  "keywords": [
@@ -27,7 +27,7 @@
27
27
  "devDependencies": {
28
28
  "common-tags": "^1.8.2",
29
29
  "@atproto/lex-cli": "^0.2.1",
30
- "@atproto/pds": "^0.1.17"
30
+ "@atproto/pds": "^0.1.18"
31
31
  },
32
32
  "scripts": {
33
33
  "codegen": "pnpm docgen && node ./scripts/generate-code.mjs && lex gen-api ./src/client ../../lexicons/com/atproto/*/* ../../lexicons/app/bsky/*/*",
@@ -69,6 +69,33 @@ export function detectFacets(text: UnicodeString): Facet[] | undefined {
69
69
  })
70
70
  }
71
71
  }
72
+ {
73
+ const re = /(?:^|\s)(#[^\d\s]\S*)(?=\s)?/g
74
+ while ((match = re.exec(text.utf16))) {
75
+ let [tag] = match
76
+ const hasLeadingSpace = /^\s/.test(tag)
77
+
78
+ tag = tag.trim().replace(/\p{P}+$/gu, '') // strip ending punctuation
79
+
80
+ // inclusive of #, max of 64 chars
81
+ if (tag.length > 66) continue
82
+
83
+ const index = match.index + (hasLeadingSpace ? 1 : 0)
84
+
85
+ facets.push({
86
+ index: {
87
+ byteStart: text.utf16IndexToUtf8Index(index),
88
+ byteEnd: text.utf16IndexToUtf8Index(index + tag.length), // inclusive of last char
89
+ },
90
+ features: [
91
+ {
92
+ $type: 'app.bsky.richtext.facet#tag',
93
+ tag,
94
+ },
95
+ ],
96
+ })
97
+ }
98
+ }
72
99
  return facets.length > 0 ? facets : undefined
73
100
  }
74
101
 
@@ -100,6 +100,7 @@ import { detectFacets } from './detection'
100
100
  export type Facet = AppBskyRichtextFacet.Main
101
101
  export type FacetLink = AppBskyRichtextFacet.Link
102
102
  export type FacetMention = AppBskyRichtextFacet.Mention
103
+ export type FacetTag = AppBskyRichtextFacet.Tag
103
104
  export type Entity = AppBskyFeedPost.Entity
104
105
 
105
106
  export interface RichTextProps {
@@ -141,6 +142,18 @@ export class RichTextSegment {
141
142
  isMention() {
142
143
  return !!this.mention
143
144
  }
145
+
146
+ get tag(): FacetTag | undefined {
147
+ const tag = this.facet?.features.find(AppBskyRichtextFacet.isTag)
148
+ if (AppBskyRichtextFacet.isTag(tag)) {
149
+ return tag
150
+ }
151
+ return undefined
152
+ }
153
+
154
+ isTag() {
155
+ return !!this.tag
156
+ }
144
157
  }
145
158
 
146
159
  export class RichText {
@@ -1,4 +1,5 @@
1
1
  import { AtpAgent, RichText, RichTextSegment } from '../src'
2
+ import { isTag } from '../src/client/types/app/bsky/richtext/facet'
2
3
 
3
4
  describe('detectFacets', () => {
4
5
  const agent = new AtpAgent({ service: 'http://localhost' })
@@ -208,6 +209,109 @@ describe('detectFacets', () => {
208
209
  expect(Array.from(rt.segments(), segmentToOutput)).toEqual(outputs[i])
209
210
  }
210
211
  })
212
+
213
+ it('correctly detects tags inline', async () => {
214
+ const inputs: [
215
+ string,
216
+ string[],
217
+ { byteStart: number; byteEnd: number }[],
218
+ ][] = [
219
+ ['#a', ['#a'], [{ byteStart: 0, byteEnd: 2 }]],
220
+ [
221
+ '#a #b',
222
+ ['#a', '#b'],
223
+ [
224
+ { byteStart: 0, byteEnd: 2 },
225
+ { byteStart: 3, byteEnd: 5 },
226
+ ],
227
+ ],
228
+ ['#1', [], []],
229
+ ['#tag', ['#tag'], [{ byteStart: 0, byteEnd: 4 }]],
230
+ ['body #tag', ['#tag'], [{ byteStart: 5, byteEnd: 9 }]],
231
+ ['#tag body', ['#tag'], [{ byteStart: 0, byteEnd: 4 }]],
232
+ ['body #tag body', ['#tag'], [{ byteStart: 5, byteEnd: 9 }]],
233
+ ['body #1', [], []],
234
+ ['body #a1', ['#a1'], [{ byteStart: 5, byteEnd: 8 }]],
235
+ ['#', [], []],
236
+ ['text #', [], []],
237
+ ['text # text', [], []],
238
+ [
239
+ 'body #thisisa64characterstring_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
240
+ ['#thisisa64characterstring_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'],
241
+ [{ byteStart: 5, byteEnd: 71 }],
242
+ ],
243
+ [
244
+ 'body #thisisa65characterstring_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab',
245
+ [],
246
+ [],
247
+ ],
248
+ [
249
+ 'its a #double#rainbow',
250
+ ['#double#rainbow'],
251
+ [{ byteStart: 6, byteEnd: 21 }],
252
+ ],
253
+ ['##hashash', ['##hashash'], [{ byteStart: 0, byteEnd: 9 }]],
254
+ ['some #n0n3s@n5e!', ['#n0n3s@n5e'], [{ byteStart: 5, byteEnd: 15 }]],
255
+ [
256
+ 'works #with,punctuation',
257
+ ['#with,punctuation'],
258
+ [{ byteStart: 6, byteEnd: 23 }],
259
+ ],
260
+ [
261
+ 'strips trailing #punctuation, #like. #this!',
262
+ ['#punctuation', '#like', '#this'],
263
+ [
264
+ { byteStart: 16, byteEnd: 28 },
265
+ { byteStart: 30, byteEnd: 35 },
266
+ { byteStart: 37, byteEnd: 42 },
267
+ ],
268
+ ],
269
+ [
270
+ 'strips #multi_trailing___...',
271
+ ['#multi_trailing'],
272
+ [{ byteStart: 7, byteEnd: 22 }],
273
+ ],
274
+ [
275
+ 'works with #🦋 emoji, and #butter🦋fly',
276
+ ['#🦋', '#butter🦋fly'],
277
+ [
278
+ { byteStart: 11, byteEnd: 16 },
279
+ { byteStart: 28, byteEnd: 42 },
280
+ ],
281
+ ],
282
+ [
283
+ '#same #same #but #diff',
284
+ ['#same', '#same', '#but', '#diff'],
285
+ [
286
+ { byteStart: 0, byteEnd: 5 },
287
+ { byteStart: 6, byteEnd: 11 },
288
+ { byteStart: 12, byteEnd: 16 },
289
+ { byteStart: 17, byteEnd: 22 },
290
+ ],
291
+ ],
292
+ ]
293
+
294
+ for (const [input, tags, indices] of inputs) {
295
+ const rt = new RichText({ text: input })
296
+ await rt.detectFacets(agent)
297
+
298
+ let detectedTags: string[] = []
299
+ let detectedIndices: { byteStart: number; byteEnd: number }[] = []
300
+
301
+ for (const { facet } of rt.segments()) {
302
+ if (!facet) continue
303
+ for (const feature of facet.features) {
304
+ if (isTag(feature)) {
305
+ detectedTags.push(feature.tag)
306
+ }
307
+ }
308
+ detectedIndices.push(facet.index)
309
+ }
310
+
311
+ expect(detectedTags).toEqual(tags)
312
+ expect(detectedIndices).toEqual(indices)
313
+ }
314
+ })
211
315
  })
212
316
 
213
317
  function segmentToOutput(segment: RichTextSegment): string[] {