-
Notifications
You must be signed in to change notification settings - Fork 1
/
Facet.ts
126 lines (109 loc) · 4.1 KB
/
Facet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import type { AppBskyRichtextFacet } from "@atproto/api";
import { utf8IndexToUtf16Index } from "../../richtext/detectFacets.js";
/**
* A facet represents a span of text within a string with special meaning (e.g. mentions, links, tags).
* @see https://docs.bsky.app/docs/advanced-guides/post-richtext#rich-text-facets
*/
export class Facet {
/** The original text the facet is contained within. */
sourceText: string;
/** The range of bytes in the source text that this facet applies to, when the source text is encoded as UTF-8.
* Unless you know what you're doing, you should use the {@link index} property.
*/
byteIndex: {
/** The index of the first byte included in the facet. */
byteStart: number;
/** The index of the first byte excluded from the facet. */
byteEnd: number;
};
private _index?: { start: number; end: number };
/** The decorations applied to the text within the facet range. */
features: Array<FacetFeature>;
/** The span of text this facet applies to. */
get span() {
return this.sourceText.slice(this.index.start, this.index.end);
}
/**
* The range of indices within the source text that this facet applies to.
* @property start The index of the first character included in the facet.
* @property end The index of the first character excluded from the facet.
*/
get index() {
if (this._index) return this._index;
return this._index = {
start: utf8IndexToUtf16Index(this.sourceText, this.byteIndex.byteStart),
end: utf8IndexToUtf16Index(this.sourceText, this.byteIndex.byteEnd),
};
}
/**
* Creates a new facet.
* @param text The full source text.
* @param facet The facet data.
*/
constructor(text: string, facet: AppBskyRichtextFacet.Main) {
this.sourceText = text;
this.byteIndex = { ...facet.index };
this.features = facet.features.map((feature) => {
if (typeof feature.did === "string") return new MentionFeature(feature.did);
else if (typeof feature.uri === "string") return new LinkFeature(feature.uri);
else if (typeof feature.tag === "string") return new TagFeature(feature.tag);
else throw new Error("Unknown facet feature type " + feature.$type + ".");
});
}
/**
* Returns a record representation of the facet.
*/
toRecord(): AppBskyRichtextFacet.Main {
return {
index: { ...this.byteIndex },
features: this.features.map((feature) => {
if (feature.isMention()) {
return { $type: "app.bsky.richtext.facet#mention", did: feature.did };
} else if (feature.isLink()) {
return { $type: "app.bsky.richtext.facet#link", uri: feature.uri };
} else if (feature.isTag()) {
return { $type: "app.bsky.richtext.facet#tag", tag: feature.tag };
} else throw new Error("Unknown facet feature type.");
}),
};
}
}
/** Represents a decoration applied to a span of text. */
export class FacetFeature {
/** Whether this facet is a mention. */
isMention(): this is MentionFeature {
return this.$type === "app.bsky.richtext.facet#mention";
}
/** Whether this facet is a link. */
isLink(): this is LinkFeature {
return this.$type === "app.bsky.richtext.facet#link";
}
/** Whether this facet is an in-text hashtag. */
isTag(): this is TagFeature {
return this.$type === "app.bsky.richtext.facet#tag";
}
/** Represents a specific decoration applied to a span of text. */
/** @internal */
constructor(/** The facet type. */ public $type: `app.bsky.richtext.facet#${string}`) {}
}
/** Represents a user mention. */
export class MentionFeature extends FacetFeature {
declare $type: "app.bsky.richtext.facet#mention";
constructor(/** The mentioned user's DID. */ public did: string) {
super("app.bsky.richtext.facet#mention");
}
}
/** Represents a hyperlink. */
export class LinkFeature extends FacetFeature {
declare $type: "app.bsky.richtext.facet#link";
constructor(/** The referenced link. */ public uri: string) {
super("app.bsky.richtext.facet#link");
}
}
/** Represents an in-text hashtag. */
export class TagFeature extends FacetFeature {
declare $type: "app.bsky.richtext.facet#tag";
constructor(/** The hashtag, without the leading #. */ public tag: string) {
super("app.bsky.richtext.facet#tag");
}
}