+2
-2
lib/utils.ts
+2
-2
lib/utils.ts
···
246
246
}
247
247
}
248
248
249
-
function parseTextBlock(block: PubLeafletBlocksText.Main) {
249
+
export function parseTextBlock(block: PubLeafletBlocksText.Main) {
250
250
let html = "";
251
251
const rt = new RichText({
252
252
text: block.plaintext,
···
297
297
return html.trim();
298
298
}
299
299
300
-
function parseBlocks({
300
+
export function parseBlocks({
301
301
block,
302
302
did,
303
303
}: {
+133
tests/parse-text-blocks.test.ts
+133
tests/parse-text-blocks.test.ts
···
1
+
import { describe, expect, test } from "vitest";
2
+
import { parseTextBlock } from "../lib/utils";
3
+
4
+
describe("text block parsing", () => {
5
+
test("should correctly parse a text block without facets", () => {
6
+
const html = parseTextBlock({
7
+
$type: "pub.leaflet.blocks.text",
8
+
facets: [],
9
+
plaintext: "just plaintext no facets",
10
+
});
11
+
12
+
expect(html).toMatchInlineSnapshot(`"<p>just plaintext no facets</p>"`);
13
+
});
14
+
15
+
test("should correctly parse a text block with bolded text", () => {
16
+
const html = parseTextBlock({
17
+
$type: "pub.leaflet.blocks.text",
18
+
facets: [
19
+
{
20
+
index: {
21
+
byteEnd: 11,
22
+
byteStart: 0,
23
+
},
24
+
features: [
25
+
{
26
+
$type: "pub.leaflet.richtext.facet#bold",
27
+
},
28
+
],
29
+
},
30
+
],
31
+
plaintext: "bolded text with some plaintext",
32
+
});
33
+
34
+
expect(html).toMatchInlineSnapshot(
35
+
`"<p><b>bolded text</b> with some plaintext</p>"`,
36
+
);
37
+
});
38
+
39
+
test("should correctly parse a text block with an inline link", () => {
40
+
const html = parseTextBlock({
41
+
$type: "pub.leaflet.blocks.text",
42
+
facets: [
43
+
{
44
+
index: {
45
+
byteEnd: 27,
46
+
byteStart: 0,
47
+
},
48
+
features: [
49
+
{
50
+
uri: "https://blacksky.community/",
51
+
$type: "pub.leaflet.richtext.facet#link",
52
+
},
53
+
],
54
+
},
55
+
],
56
+
plaintext: "https://blacksky.community/",
57
+
});
58
+
59
+
expect(html).toMatchInlineSnapshot(
60
+
`"<p><a href="https://blacksky.community/" target="_blank" rel="noopener noreferrer">https://blacksky.community/</a></p>"`,
61
+
);
62
+
});
63
+
64
+
test("should correctly parse a text block with strikethrough text", () => {
65
+
const html = parseTextBlock({
66
+
$type: "pub.leaflet.blocks.text",
67
+
facets: [
68
+
{
69
+
index: {
70
+
byteEnd: 13,
71
+
byteStart: 0,
72
+
},
73
+
features: [
74
+
{
75
+
$type: "pub.leaflet.richtext.facet#strikethrough",
76
+
},
77
+
],
78
+
},
79
+
],
80
+
plaintext: "strikethrough text with some plaintext",
81
+
});
82
+
83
+
expect(html).toMatchInlineSnapshot(
84
+
`"<p><s>strikethrough</s> text with some plaintext</p>"`,
85
+
);
86
+
});
87
+
88
+
test("should correctly parse a text block with underlined text", () => {
89
+
const html = parseTextBlock({
90
+
$type: "pub.leaflet.blocks.text",
91
+
facets: [
92
+
{
93
+
index: {
94
+
byteEnd: 10,
95
+
byteStart: 0,
96
+
},
97
+
features: [
98
+
{
99
+
$type: "pub.leaflet.richtext.facet#underline",
100
+
},
101
+
],
102
+
},
103
+
],
104
+
plaintext: "underlined text with some plaintext",
105
+
});
106
+
107
+
expect(html).toMatchInlineSnapshot(
108
+
`"<p><span style="text-decoration:underline;">underlined</span> text with some plaintext</p>"`,
109
+
);
110
+
});
111
+
112
+
test("should correctly parse a text block with italicized text", () => {
113
+
const html = parseTextBlock({
114
+
$type: "pub.leaflet.blocks.text",
115
+
facets: [
116
+
{
117
+
index: {
118
+
byteEnd: 10,
119
+
byteStart: 0,
120
+
},
121
+
features: [
122
+
{
123
+
$type: "pub.leaflet.richtext.facet#italic",
124
+
},
125
+
],
126
+
},
127
+
],
128
+
plaintext: "italicized text with some plaintext",
129
+
});
130
+
131
+
expect(html).toMatchInlineSnapshot(`"<p><i>italicized</i> text with some plaintext</p>"`);
132
+
});
133
+
});