@atcute/bluesky-richtext-builder 1.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/LICENSE +17 -0
- package/README.md +19 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +104 -0
- package/dist/index.js.map +1 -0
- package/package.json +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
2
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
3
|
+
in the Software without restriction, including without limitation the rights
|
|
4
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
5
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
6
|
+
furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all
|
|
9
|
+
copies or substantial portions of the Software.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
12
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
13
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
14
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
15
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
16
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
17
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @atcute/bluesky-richtext-builder
|
|
2
|
+
|
|
3
|
+
builder pattern for Bluesky's rich text format
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import RichtextBuilder from '@atcute/bluesky-richtext-builder';
|
|
7
|
+
|
|
8
|
+
const { text, facets } = new RichtextBuilder()
|
|
9
|
+
.addText(`hello, `)
|
|
10
|
+
.addMention(`@user`, 'did:plc:ia76kvnndjutgedggx2ibrem')
|
|
11
|
+
.addText(`! please visit my`)
|
|
12
|
+
.addLink(`website`, 'https://example.com');
|
|
13
|
+
|
|
14
|
+
text;
|
|
15
|
+
// ^? `hello, @user! please visit my website`
|
|
16
|
+
|
|
17
|
+
facets;
|
|
18
|
+
// ^? [{ index: { byteStart: 7, byteEnd: 12 }, ... }, { index: { byteStart: 30, byteEnd: 37 }, ... }];
|
|
19
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import '@atcute/bluesky/lexicons';
|
|
2
|
+
import type { AppBskyRichtextFacet, At } from '@atcute/client/lexicons';
|
|
3
|
+
type UnwrapArray<T> = T extends (infer V)[] ? V : never;
|
|
4
|
+
/** Facet interface, `app.bsky.richtext.facet#main` from the lexicon */
|
|
5
|
+
export type Facet = AppBskyRichtextFacet.Main;
|
|
6
|
+
/** Feature union type from Facet['features'] */
|
|
7
|
+
export type FacetFeature = UnwrapArray<Facet['features']>;
|
|
8
|
+
/** Resulting rich text */
|
|
9
|
+
export interface BakedRichtext {
|
|
10
|
+
text: string;
|
|
11
|
+
facets: Facet[];
|
|
12
|
+
}
|
|
13
|
+
/** Builder for constructing Bluesky rich texts */
|
|
14
|
+
declare class RichtextBuilder {
|
|
15
|
+
#private;
|
|
16
|
+
/** Resulting composed text */
|
|
17
|
+
get text(): string;
|
|
18
|
+
/** Resulting composed facets */
|
|
19
|
+
get facets(): Facet[];
|
|
20
|
+
/** Retrieve the composed rich text */
|
|
21
|
+
build(): BakedRichtext;
|
|
22
|
+
/** Clone rich text builder instance */
|
|
23
|
+
clone(): RichtextBuilder;
|
|
24
|
+
/**
|
|
25
|
+
* Add plain text to the rich text
|
|
26
|
+
* @param substr The plain text
|
|
27
|
+
* @returns The builder instance, for chaining
|
|
28
|
+
*/
|
|
29
|
+
addText(substr: string): this;
|
|
30
|
+
/**
|
|
31
|
+
* Add decorated text to the rich text
|
|
32
|
+
* @param substr The text itself
|
|
33
|
+
* @param feature Feature to imbue on the text
|
|
34
|
+
* @returns The builder instance, for chaining
|
|
35
|
+
*/
|
|
36
|
+
addDecoratedText(substr: string, feature: FacetFeature): this;
|
|
37
|
+
/**
|
|
38
|
+
* Add link to the rich text
|
|
39
|
+
* @param substr Text of the link
|
|
40
|
+
* @param uri Valid URL, for example: https://example.com
|
|
41
|
+
* @returns The builder instance, for chaining
|
|
42
|
+
*/
|
|
43
|
+
addLink(substr: string, uri: string): this;
|
|
44
|
+
/**
|
|
45
|
+
* Mentions a user in rich text
|
|
46
|
+
* @param substr Text of the mention, this is usually in the form of `@handle`
|
|
47
|
+
* @param did Valid DID, for example: did:plc:ia76kvnndjutgedggx2ibrem
|
|
48
|
+
* @returns The builder instance, for chaining
|
|
49
|
+
*/
|
|
50
|
+
addMention(substr: string, did: At.DID): this;
|
|
51
|
+
/**
|
|
52
|
+
* Add inline hashtag to the rich text
|
|
53
|
+
* @param tag The tag, without the pound prefix
|
|
54
|
+
* @returns THe builder instance, for chaining
|
|
55
|
+
*/
|
|
56
|
+
addTag(tag: string): this;
|
|
57
|
+
}
|
|
58
|
+
export default RichtextBuilder;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import '@atcute/bluesky/lexicons';
|
|
2
|
+
const encoder = new TextEncoder();
|
|
3
|
+
/** Builder for constructing Bluesky rich texts */
|
|
4
|
+
class RichtextBuilder {
|
|
5
|
+
// Even-numbered are substrings, odd-numbered are facets
|
|
6
|
+
// This way we'll avoid taking the hit on calculating UTF-8 indices up until
|
|
7
|
+
// a facet is actually being inserted.
|
|
8
|
+
#segments = [''];
|
|
9
|
+
/** Resulting composed text */
|
|
10
|
+
get text() {
|
|
11
|
+
const segments = this.#segments;
|
|
12
|
+
let str = '';
|
|
13
|
+
for (let idx = 0, len = segments.length; idx < len; idx += 2) {
|
|
14
|
+
str += segments[idx];
|
|
15
|
+
}
|
|
16
|
+
return str;
|
|
17
|
+
}
|
|
18
|
+
/** Resulting composed facets */
|
|
19
|
+
get facets() {
|
|
20
|
+
const segments = this.#segments;
|
|
21
|
+
const facets = [];
|
|
22
|
+
for (let idx = 1, len = segments.length; idx < len; idx += 2) {
|
|
23
|
+
facets.push(segments[idx]);
|
|
24
|
+
}
|
|
25
|
+
return facets;
|
|
26
|
+
}
|
|
27
|
+
/** Retrieve the composed rich text */
|
|
28
|
+
build() {
|
|
29
|
+
return {
|
|
30
|
+
text: this.text,
|
|
31
|
+
facets: this.facets,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Clone rich text builder instance */
|
|
35
|
+
clone() {
|
|
36
|
+
const instance = new RichtextBuilder();
|
|
37
|
+
instance.#segments = this.#segments.slice(0);
|
|
38
|
+
return instance;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Add plain text to the rich text
|
|
42
|
+
* @param substr The plain text
|
|
43
|
+
* @returns The builder instance, for chaining
|
|
44
|
+
*/
|
|
45
|
+
addText(substr) {
|
|
46
|
+
const segments = this.#segments;
|
|
47
|
+
segments[segments.length - 1] += substr;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Add decorated text to the rich text
|
|
52
|
+
* @param substr The text itself
|
|
53
|
+
* @param feature Feature to imbue on the text
|
|
54
|
+
* @returns The builder instance, for chaining
|
|
55
|
+
*/
|
|
56
|
+
addDecoratedText(substr, feature) {
|
|
57
|
+
const segments = this.#segments;
|
|
58
|
+
const last = segments.length - 1;
|
|
59
|
+
// Calculate the starting index
|
|
60
|
+
let start = 0;
|
|
61
|
+
start += encoder.encode(segments[last]).byteLength;
|
|
62
|
+
if (last !== 0) {
|
|
63
|
+
start += segments[last - 1].index.byteEnd;
|
|
64
|
+
}
|
|
65
|
+
const facet = {
|
|
66
|
+
index: {
|
|
67
|
+
byteStart: start,
|
|
68
|
+
byteEnd: start + encoder.encode(substr).byteLength,
|
|
69
|
+
},
|
|
70
|
+
features: [feature],
|
|
71
|
+
};
|
|
72
|
+
segments[last] += substr;
|
|
73
|
+
segments.push(facet, '');
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Add link to the rich text
|
|
78
|
+
* @param substr Text of the link
|
|
79
|
+
* @param uri Valid URL, for example: https://example.com
|
|
80
|
+
* @returns The builder instance, for chaining
|
|
81
|
+
*/
|
|
82
|
+
addLink(substr, uri) {
|
|
83
|
+
return this.addDecoratedText(substr, { $type: 'app.bsky.richtext.facet#link', uri: uri });
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Mentions a user in rich text
|
|
87
|
+
* @param substr Text of the mention, this is usually in the form of `@handle`
|
|
88
|
+
* @param did Valid DID, for example: did:plc:ia76kvnndjutgedggx2ibrem
|
|
89
|
+
* @returns The builder instance, for chaining
|
|
90
|
+
*/
|
|
91
|
+
addMention(substr, did) {
|
|
92
|
+
return this.addDecoratedText(substr, { $type: 'app.bsky.richtext.facet#mention', did: did });
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Add inline hashtag to the rich text
|
|
96
|
+
* @param tag The tag, without the pound prefix
|
|
97
|
+
* @returns THe builder instance, for chaining
|
|
98
|
+
*/
|
|
99
|
+
addTag(tag) {
|
|
100
|
+
return this.addDecoratedText('#' + tag, { $type: 'app.bsky.richtext.facet#tag', tag: tag });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export default RichtextBuilder;
|
|
104
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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;IACp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AAC;IAC9F,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,GAAW;QACjB,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,GAAG,GAAG,EAAE,EAAE,KAAK,EAAE,6BAA6B,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;IAC7F,CAAC;CACD;AAED,eAAe,eAAe,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "@atcute/bluesky-richtext-builder",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "builder pattern for Bluesky's rich text format",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"url": "https://codeberg.org/mary-ext/atcute"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@atcute/bluesky": "^1.0.0",
|
|
19
|
+
"@atcute/client": "^1.0.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/bun": "^1.1.6",
|
|
23
|
+
"@atcute/bluesky": "^1.0.0",
|
|
24
|
+
"@atcute/client": "^1.0.0"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc --project tsconfig.build.json",
|
|
28
|
+
"test": "bun test --coverage",
|
|
29
|
+
"prepublish": "rm -rf dist; pnpm run build"
|
|
30
|
+
}
|
|
31
|
+
}
|