@atcute/bluesky-richtext-builder 1.0.1 → 2.0.0
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.
- package/README.md +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js.map +1 -1
- package/lib/index.ts +139 -0
- package/package.json +13 -9
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -40,14 +40,14 @@ declare class RichtextBuilder {
|
|
|
40
40
|
* @param uri Valid URL, for example: https://example.com
|
|
41
41
|
* @returns The builder instance, for chaining
|
|
42
42
|
*/
|
|
43
|
-
addLink(substr: string, uri:
|
|
43
|
+
addLink(substr: string, uri: At.GenericUri): this;
|
|
44
44
|
/**
|
|
45
45
|
* Mentions a user in rich text
|
|
46
46
|
* @param substr Text of the mention, this is usually in the form of `@handle`
|
|
47
47
|
* @param did Valid DID, for example: did:plc:ia76kvnndjutgedggx2ibrem
|
|
48
48
|
* @returns The builder instance, for chaining
|
|
49
49
|
*/
|
|
50
|
-
addMention(substr: string, did: At.
|
|
50
|
+
addMention(substr: string, did: At.Did): this;
|
|
51
51
|
/**
|
|
52
52
|
* Add inline hashtag to the rich text
|
|
53
53
|
* @param tag The tag, without the pound prefix
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,CAAC;AAUlC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAQlC,kDAAkD;AAClD,MAAM,eAAe;IACpB,wDAAwD;IACxD,4EAA4E;IAC5E,sCAAsC;IACtC,SAAS,GAAuB,CAAC,EAAE,CAAC,CAAC;IAErC,8BAA8B;IAC9B,IAAI,IAAI;QACP,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC;QAChC,IAAI,GAAG,GAAG,EAAE,CAAC;QAEb,KAAK,IAAI,GAAG,GAAG,CAAC,EAA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|
|
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: At.GenericUri): 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,27 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@atcute/bluesky-richtext-builder",
|
|
4
|
-
"version": "
|
|
5
|
-
"description": "builder pattern for Bluesky's rich text
|
|
4
|
+
"version": "2.0.0",
|
|
5
|
+
"description": "builder pattern for Bluesky's rich text facets",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
|
-
"url": "https://
|
|
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"
|
|
15
19
|
},
|
|
16
20
|
"sideEffects": false,
|
|
17
21
|
"peerDependencies": {
|
|
18
|
-
"@atcute/bluesky": "^
|
|
19
|
-
"@atcute/client": "^
|
|
22
|
+
"@atcute/bluesky": "^2.0.0",
|
|
23
|
+
"@atcute/client": "^3.0.0"
|
|
20
24
|
},
|
|
21
25
|
"devDependencies": {
|
|
22
|
-
"@types/bun": "^1.1
|
|
23
|
-
"@atcute/bluesky": "^
|
|
24
|
-
"@atcute/client": "^
|
|
26
|
+
"@types/bun": "^1.2.1",
|
|
27
|
+
"@atcute/bluesky": "^2.0.0",
|
|
28
|
+
"@atcute/client": "^3.0.0"
|
|
25
29
|
},
|
|
26
30
|
"scripts": {
|
|
27
31
|
"build": "tsc --project tsconfig.build.json",
|