Thread viewer for Bluesky
1/*
2 Extracted from https://github.com/bluesky-social/atproto/
3
4 Copyright (c) 2022-2024 Bluesky PBC, and Contributors
5 MIT License
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24*/
25
26// packages/api/src/rich-text/rich-text.ts
27
28class RichTextSegment {
29 /** @param {string} text, @param {json} [facet] */
30 constructor(text, facet) {
31 this.text = text;
32 this.facet = facet;
33 }
34
35 /** @returns {object | undefined} */
36 get link() {
37 return this.facet?.features?.find(v => v.$type === 'app.bsky.richtext.facet#link');
38 }
39
40 /** @returns {object | undefined} */
41 get mention() {
42 return this.facet?.features?.find(v => v.$type === 'app.bsky.richtext.facet#mention');
43 }
44
45 /** @returns {object | undefined} */
46 get tag() {
47 return this.facet?.features?.find(v => v.$type === 'app.bsky.richtext.facet#tag');
48 }
49}
50
51class RichText {
52 /** @params {json} props */
53 constructor(props) {
54 this.unicodeText = new UnicodeString(props.text);
55 this.facets = props.facets;
56
57 if (this.facets) {
58 this.facets.sort((a, b) => a.index.byteStart - b.index.byteStart);
59 }
60 }
61
62 /** @returns {string} */
63 get text() {
64 return this.unicodeText.toString();
65 }
66
67 /** @returns {number} */
68 get length() {
69 return this.unicodeText.length;
70 }
71
72 /** @returns {number} */
73 get graphemeLength() {
74 return this.unicodeText.graphemeLength;
75 }
76
77 *segments() {
78 const facets = this.facets || [];
79
80 if (facets.length == 0) {
81 yield new RichTextSegment(this.unicodeText.utf16);
82 return;
83 }
84
85 let textCursor = 0;
86 let facetCursor = 0;
87
88 do {
89 const currFacet = facets[facetCursor];
90
91 if (textCursor < currFacet.index.byteStart) {
92 yield new RichTextSegment(this.unicodeText.slice(textCursor, currFacet.index.byteStart));
93 } else if (textCursor > currFacet.index.byteStart) {
94 facetCursor++;
95 continue;
96 }
97
98 if (currFacet.index.byteStart < currFacet.index.byteEnd) {
99 const subtext = this.unicodeText.slice(currFacet.index.byteStart, currFacet.index.byteEnd);
100
101 if (subtext.trim().length == 0) {
102 yield new RichTextSegment(subtext);
103 } else {
104 yield new RichTextSegment(subtext, currFacet);
105 }
106 }
107
108 textCursor = currFacet.index.byteEnd;
109 facetCursor++;
110
111 } while (facetCursor < facets.length);
112
113 if (textCursor < this.unicodeText.length) {
114 yield new RichTextSegment(this.unicodeText.slice(textCursor, this.unicodeText.length));
115 }
116 }
117}
118
119
120// packages/api/src/rich-text/unicode.ts
121
122/**
123 * Javascript uses utf16-encoded strings while most environments and specs
124 * have standardized around utf8 (including JSON).
125 *
126 * After some lengthy debated we decided that richtext facets need to use
127 * utf8 indices. This means we need tools to convert indices between utf8
128 * and utf16, and that's precisely what this library handles.
129 */
130
131class UnicodeString {
132 static encoder = new TextEncoder();
133 static decoder = new TextDecoder();
134 static segmenter = window.Intl && Intl.Segmenter && new Intl.Segmenter();
135
136 /** @param {string} utf16 */
137 constructor(utf16) {
138 this.utf16 = utf16;
139 this.utf8 = UnicodeString.encoder.encode(utf16);
140 }
141
142 /** @returns number */
143 get length() {
144 return this.utf8.byteLength;
145 }
146
147 /** @returns number */
148 get graphemeLength() {
149 return Array.from(UnicodeString.segmenter.segment(this.utf16)).length;
150 }
151
152 /** @param {number} start, @param {number} end, @returns string */
153 slice(start, end) {
154 return UnicodeString.decoder.decode(this.utf8.slice(start, end));
155 }
156
157 /** @returns string */
158 toString() {
159 return this.utf16;
160 }
161}