@atcute/bluesky-richtext-builder 1.0.0 → 1.0.2

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/lib/index.ts +139 -0
  3. package/package.json +12 -8
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @atcute/bluesky-richtext-builder
2
2
 
3
- builder pattern for Bluesky's rich text format
3
+ builder pattern for Bluesky's rich text facets.
4
4
 
5
5
  ```ts
6
6
  import RichtextBuilder from '@atcute/bluesky-richtext-builder';
package/lib/index.ts ADDED
@@ -0,0 +1,139 @@
1
+ import '@atcute/bluesky/lexicons';
2
+ import type { AppBskyRichtextFacet, At } from '@atcute/client/lexicons';
3
+
4
+ type UnwrapArray<T> = T extends (infer V)[] ? V : never;
5
+
6
+ /** Facet interface, `app.bsky.richtext.facet#main` from the lexicon */
7
+ export type Facet = AppBskyRichtextFacet.Main;
8
+ /** Feature union type from Facet['features'] */
9
+ export type FacetFeature = UnwrapArray<Facet['features']>;
10
+
11
+ const encoder = new TextEncoder();
12
+
13
+ /** Resulting rich text */
14
+ export interface BakedRichtext {
15
+ text: string;
16
+ facets: Facet[];
17
+ }
18
+
19
+ /** Builder for constructing Bluesky rich texts */
20
+ class RichtextBuilder {
21
+ // Even-numbered are substrings, odd-numbered are facets
22
+ // This way we'll avoid taking the hit on calculating UTF-8 indices up until
23
+ // a facet is actually being inserted.
24
+ #segments: (string | Facet)[] = [''];
25
+
26
+ /** Resulting composed text */
27
+ get text(): string {
28
+ const segments = this.#segments;
29
+ let str = '';
30
+
31
+ for (let idx = 0, len = segments.length; idx < len; idx += 2) {
32
+ str += segments[idx] as string;
33
+ }
34
+
35
+ return str;
36
+ }
37
+
38
+ /** Resulting composed facets */
39
+ get facets(): Facet[] {
40
+ const segments = this.#segments;
41
+ const facets: Facet[] = [];
42
+
43
+ for (let idx = 1, len = segments.length; idx < len; idx += 2) {
44
+ facets.push(segments[idx] as Facet);
45
+ }
46
+
47
+ return facets;
48
+ }
49
+
50
+ /** Retrieve the composed rich text */
51
+ build(): BakedRichtext {
52
+ return {
53
+ text: this.text,
54
+ facets: this.facets,
55
+ };
56
+ }
57
+
58
+ /** Clone rich text builder instance */
59
+ clone(): RichtextBuilder {
60
+ const instance = new RichtextBuilder();
61
+ instance.#segments = this.#segments.slice(0);
62
+
63
+ return instance;
64
+ }
65
+
66
+ /**
67
+ * Add plain text to the rich text
68
+ * @param substr The plain text
69
+ * @returns The builder instance, for chaining
70
+ */
71
+ addText(substr: string): this {
72
+ const segments = this.#segments;
73
+ segments[segments.length - 1] += substr;
74
+
75
+ return this;
76
+ }
77
+
78
+ /**
79
+ * Add decorated text to the rich text
80
+ * @param substr The text itself
81
+ * @param feature Feature to imbue on the text
82
+ * @returns The builder instance, for chaining
83
+ */
84
+ addDecoratedText(substr: string, feature: FacetFeature): this {
85
+ const segments = this.#segments;
86
+ const last = segments.length - 1;
87
+
88
+ // Calculate the starting index
89
+ let start = 0;
90
+
91
+ start += encoder.encode(segments[last] as string).byteLength;
92
+ if (last !== 0) {
93
+ start += (segments[last - 1] as Facet).index.byteEnd;
94
+ }
95
+
96
+ const facet: Facet = {
97
+ index: {
98
+ byteStart: start,
99
+ byteEnd: start + encoder.encode(substr).byteLength,
100
+ },
101
+ features: [feature],
102
+ };
103
+
104
+ segments[last] += substr;
105
+ segments.push(facet, '');
106
+ return this;
107
+ }
108
+
109
+ /**
110
+ * Add link to the rich text
111
+ * @param substr Text of the link
112
+ * @param uri Valid URL, for example: https://example.com
113
+ * @returns The builder instance, for chaining
114
+ */
115
+ addLink(substr: string, uri: string): this {
116
+ return this.addDecoratedText(substr, { $type: 'app.bsky.richtext.facet#link', uri: uri });
117
+ }
118
+
119
+ /**
120
+ * Mentions a user in rich text
121
+ * @param substr Text of the mention, this is usually in the form of `@handle`
122
+ * @param did Valid DID, for example: did:plc:ia76kvnndjutgedggx2ibrem
123
+ * @returns The builder instance, for chaining
124
+ */
125
+ addMention(substr: string, did: At.DID): this {
126
+ return this.addDecoratedText(substr, { $type: 'app.bsky.richtext.facet#mention', did: did });
127
+ }
128
+
129
+ /**
130
+ * Add inline hashtag to the rich text
131
+ * @param tag The tag, without the pound prefix
132
+ * @returns THe builder instance, for chaining
133
+ */
134
+ addTag(tag: string): this {
135
+ return this.addDecoratedText('#' + tag, { $type: 'app.bsky.richtext.facet#tag', tag: tag });
136
+ }
137
+ }
138
+
139
+ export default RichtextBuilder;
package/package.json CHANGED
@@ -1,14 +1,18 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@atcute/bluesky-richtext-builder",
4
- "version": "1.0.0",
5
- "description": "builder pattern for Bluesky's rich text format",
4
+ "version": "1.0.2",
5
+ "description": "builder pattern for Bluesky's rich text facets",
6
6
  "license": "MIT",
7
7
  "repository": {
8
- "url": "https://codeberg.org/mary-ext/atcute"
8
+ "url": "https://github.com/mary-ext/atcute",
9
+ "directory": "packages/bluesky/richtext-builder"
9
10
  },
10
11
  "files": [
11
- "dist/"
12
+ "dist/",
13
+ "lib/",
14
+ "!lib/**/*.bench.ts",
15
+ "!lib/**/*.test.ts"
12
16
  ],
13
17
  "exports": {
14
18
  ".": "./dist/index.js"
@@ -16,12 +20,12 @@
16
20
  "sideEffects": false,
17
21
  "peerDependencies": {
18
22
  "@atcute/bluesky": "^1.0.0",
19
- "@atcute/client": "^1.0.0"
23
+ "@atcute/client": "^1.0.0 || ^2.0.0"
20
24
  },
21
25
  "devDependencies": {
22
- "@types/bun": "^1.1.6",
23
- "@atcute/bluesky": "^1.0.0",
24
- "@atcute/client": "^1.0.0"
26
+ "@types/bun": "^1.1.12",
27
+ "@atcute/bluesky": "^1.0.8",
28
+ "@atcute/client": "^2.0.4"
25
29
  },
26
30
  "scripts": {
27
31
  "build": "tsc --project tsconfig.build.json",