@valbuild/server 0.20.0 → 0.20.1
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.
@@ -248,22 +248,41 @@ function analyzeValModule(sourceFile) {
|
|
248
248
|
|
249
249
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
250
250
|
|
251
|
-
const HeaderRegEx =
|
251
|
+
const HeaderRegEx = /^h([\d+])$/;
|
252
252
|
function richTextToTaggedStringTemplate(source) {
|
253
253
|
const texts = [""];
|
254
254
|
const nodes = [];
|
255
255
|
let didAppendNewLines = false;
|
256
|
+
let listContext = null;
|
256
257
|
function rec(node) {
|
257
258
|
if (typeof node === "string") {
|
258
259
|
texts[texts.length - 1] += node;
|
259
|
-
} else if (node.tag) {
|
260
|
-
var _node$
|
261
|
-
if (
|
262
|
-
|
260
|
+
} else if ("tag" in node && node.tag) {
|
261
|
+
var _node$children, _node$tag;
|
262
|
+
if (node.tag === "h1" || node.tag === "h2" || node.tag === "h3" || node.tag === "h4" || node.tag === "h5" || node.tag === "h6") {
|
263
|
+
var _node$tag$match;
|
264
|
+
const depth = Number((_node$tag$match = node.tag.match(HeaderRegEx)) === null || _node$tag$match === void 0 ? void 0 : _node$tag$match[1]);
|
265
|
+
if (Number.isNaN(depth)) {
|
266
|
+
throw new Error("Invalid header depth");
|
267
|
+
}
|
263
268
|
for (let i = 0; i < Number(depth); i++) {
|
264
269
|
texts[texts.length - 1] += "#";
|
265
270
|
}
|
266
271
|
texts[texts.length - 1] += " ";
|
272
|
+
} else if (node.tag === "p") ; else if (node.tag === "ul") {
|
273
|
+
listContext = "ul";
|
274
|
+
texts[texts.length - 1] += "\n";
|
275
|
+
} else if (node.tag === "ol") {
|
276
|
+
listContext = "ol";
|
277
|
+
texts[texts.length - 1] += "\n";
|
278
|
+
} else if (node.tag === "li") {
|
279
|
+
if (listContext === "ul") {
|
280
|
+
texts[texts.length - 1] += "\n- ";
|
281
|
+
} else if (listContext === "ol") {
|
282
|
+
texts[texts.length - 1] += "\n1. ";
|
283
|
+
} else {
|
284
|
+
throw new Error("Unexpected list context");
|
285
|
+
}
|
267
286
|
} else if (node.tag === "span") {
|
268
287
|
if (node.classes.includes("bold") && !node.classes.includes("italic")) {
|
269
288
|
texts[texts.length - 1] += "**";
|
@@ -277,10 +296,14 @@ function richTextToTaggedStringTemplate(source) {
|
|
277
296
|
if (node.classes.includes("line-through")) {
|
278
297
|
texts[texts.length - 1] += "~~";
|
279
298
|
}
|
299
|
+
} else {
|
300
|
+
//exhaustive match
|
301
|
+
const exhaustiveCheck = node.tag;
|
302
|
+
throw new Error("Unexpected node tag: " + JSON.stringify(node, exhaustiveCheck, 2));
|
280
303
|
}
|
281
304
|
(_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.forEach(rec);
|
305
|
+
didAppendNewLines = false;
|
282
306
|
if (node.tag === "span") {
|
283
|
-
didAppendNewLines = false;
|
284
307
|
if (node.classes.includes("line-through")) {
|
285
308
|
texts[texts.length - 1] += "~~";
|
286
309
|
}
|
@@ -296,12 +319,15 @@ function richTextToTaggedStringTemplate(source) {
|
|
296
319
|
} else if (node.tag === "p") {
|
297
320
|
didAppendNewLines = true;
|
298
321
|
texts[texts.length - 1] += "\n\n";
|
299
|
-
} else if (
|
322
|
+
} else if (node.tag === "ul" || node.tag === "ol") {
|
323
|
+
listContext = null;
|
324
|
+
texts[texts.length - 1] += "\n";
|
325
|
+
} else if ((_node$tag = node.tag) !== null && _node$tag !== void 0 && _node$tag.startsWith("h")) {
|
300
326
|
didAppendNewLines = true;
|
301
327
|
texts[texts.length - 1] += "\n\n";
|
302
328
|
}
|
303
329
|
} else {
|
304
|
-
nodes.push(node);
|
330
|
+
nodes.push(node); // ImageSource
|
305
331
|
texts.push("\n");
|
306
332
|
}
|
307
333
|
}
|
@@ -691,6 +717,7 @@ globalThis.valModule = {
|
|
691
717
|
const fatalErrors = [];
|
692
718
|
if (result.error) {
|
693
719
|
const error = result.error.consume(context.dump);
|
720
|
+
console.error(`Fatal error reading val file: ${error.message}\n`, error.stack);
|
694
721
|
return {
|
695
722
|
errors: {
|
696
723
|
invalidModuleId: id,
|
@@ -248,22 +248,41 @@ function analyzeValModule(sourceFile) {
|
|
248
248
|
|
249
249
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
250
250
|
|
251
|
-
const HeaderRegEx =
|
251
|
+
const HeaderRegEx = /^h([\d+])$/;
|
252
252
|
function richTextToTaggedStringTemplate(source) {
|
253
253
|
const texts = [""];
|
254
254
|
const nodes = [];
|
255
255
|
let didAppendNewLines = false;
|
256
|
+
let listContext = null;
|
256
257
|
function rec(node) {
|
257
258
|
if (typeof node === "string") {
|
258
259
|
texts[texts.length - 1] += node;
|
259
|
-
} else if (node.tag) {
|
260
|
-
var _node$
|
261
|
-
if (
|
262
|
-
|
260
|
+
} else if ("tag" in node && node.tag) {
|
261
|
+
var _node$children, _node$tag;
|
262
|
+
if (node.tag === "h1" || node.tag === "h2" || node.tag === "h3" || node.tag === "h4" || node.tag === "h5" || node.tag === "h6") {
|
263
|
+
var _node$tag$match;
|
264
|
+
const depth = Number((_node$tag$match = node.tag.match(HeaderRegEx)) === null || _node$tag$match === void 0 ? void 0 : _node$tag$match[1]);
|
265
|
+
if (Number.isNaN(depth)) {
|
266
|
+
throw new Error("Invalid header depth");
|
267
|
+
}
|
263
268
|
for (let i = 0; i < Number(depth); i++) {
|
264
269
|
texts[texts.length - 1] += "#";
|
265
270
|
}
|
266
271
|
texts[texts.length - 1] += " ";
|
272
|
+
} else if (node.tag === "p") ; else if (node.tag === "ul") {
|
273
|
+
listContext = "ul";
|
274
|
+
texts[texts.length - 1] += "\n";
|
275
|
+
} else if (node.tag === "ol") {
|
276
|
+
listContext = "ol";
|
277
|
+
texts[texts.length - 1] += "\n";
|
278
|
+
} else if (node.tag === "li") {
|
279
|
+
if (listContext === "ul") {
|
280
|
+
texts[texts.length - 1] += "\n- ";
|
281
|
+
} else if (listContext === "ol") {
|
282
|
+
texts[texts.length - 1] += "\n1. ";
|
283
|
+
} else {
|
284
|
+
throw new Error("Unexpected list context");
|
285
|
+
}
|
267
286
|
} else if (node.tag === "span") {
|
268
287
|
if (node.classes.includes("bold") && !node.classes.includes("italic")) {
|
269
288
|
texts[texts.length - 1] += "**";
|
@@ -277,10 +296,14 @@ function richTextToTaggedStringTemplate(source) {
|
|
277
296
|
if (node.classes.includes("line-through")) {
|
278
297
|
texts[texts.length - 1] += "~~";
|
279
298
|
}
|
299
|
+
} else {
|
300
|
+
//exhaustive match
|
301
|
+
const exhaustiveCheck = node.tag;
|
302
|
+
throw new Error("Unexpected node tag: " + JSON.stringify(node, exhaustiveCheck, 2));
|
280
303
|
}
|
281
304
|
(_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.forEach(rec);
|
305
|
+
didAppendNewLines = false;
|
282
306
|
if (node.tag === "span") {
|
283
|
-
didAppendNewLines = false;
|
284
307
|
if (node.classes.includes("line-through")) {
|
285
308
|
texts[texts.length - 1] += "~~";
|
286
309
|
}
|
@@ -296,12 +319,15 @@ function richTextToTaggedStringTemplate(source) {
|
|
296
319
|
} else if (node.tag === "p") {
|
297
320
|
didAppendNewLines = true;
|
298
321
|
texts[texts.length - 1] += "\n\n";
|
299
|
-
} else if (
|
322
|
+
} else if (node.tag === "ul" || node.tag === "ol") {
|
323
|
+
listContext = null;
|
324
|
+
texts[texts.length - 1] += "\n";
|
325
|
+
} else if ((_node$tag = node.tag) !== null && _node$tag !== void 0 && _node$tag.startsWith("h")) {
|
300
326
|
didAppendNewLines = true;
|
301
327
|
texts[texts.length - 1] += "\n\n";
|
302
328
|
}
|
303
329
|
} else {
|
304
|
-
nodes.push(node);
|
330
|
+
nodes.push(node); // ImageSource
|
305
331
|
texts.push("\n");
|
306
332
|
}
|
307
333
|
}
|
@@ -691,6 +717,7 @@ globalThis.valModule = {
|
|
691
717
|
const fatalErrors = [];
|
692
718
|
if (result.error) {
|
693
719
|
const error = result.error.consume(context.dump);
|
720
|
+
console.error(`Fatal error reading val file: ${error.message}\n`, error.stack);
|
694
721
|
return {
|
695
722
|
errors: {
|
696
723
|
invalidModuleId: id,
|
@@ -217,22 +217,41 @@ function analyzeValModule(sourceFile) {
|
|
217
217
|
|
218
218
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
219
219
|
|
220
|
-
const HeaderRegEx =
|
220
|
+
const HeaderRegEx = /^h([\d+])$/;
|
221
221
|
function richTextToTaggedStringTemplate(source) {
|
222
222
|
const texts = [""];
|
223
223
|
const nodes = [];
|
224
224
|
let didAppendNewLines = false;
|
225
|
+
let listContext = null;
|
225
226
|
function rec(node) {
|
226
227
|
if (typeof node === "string") {
|
227
228
|
texts[texts.length - 1] += node;
|
228
|
-
} else if (node.tag) {
|
229
|
-
var _node$
|
230
|
-
if (
|
231
|
-
|
229
|
+
} else if ("tag" in node && node.tag) {
|
230
|
+
var _node$children, _node$tag;
|
231
|
+
if (node.tag === "h1" || node.tag === "h2" || node.tag === "h3" || node.tag === "h4" || node.tag === "h5" || node.tag === "h6") {
|
232
|
+
var _node$tag$match;
|
233
|
+
const depth = Number((_node$tag$match = node.tag.match(HeaderRegEx)) === null || _node$tag$match === void 0 ? void 0 : _node$tag$match[1]);
|
234
|
+
if (Number.isNaN(depth)) {
|
235
|
+
throw new Error("Invalid header depth");
|
236
|
+
}
|
232
237
|
for (let i = 0; i < Number(depth); i++) {
|
233
238
|
texts[texts.length - 1] += "#";
|
234
239
|
}
|
235
240
|
texts[texts.length - 1] += " ";
|
241
|
+
} else if (node.tag === "p") ; else if (node.tag === "ul") {
|
242
|
+
listContext = "ul";
|
243
|
+
texts[texts.length - 1] += "\n";
|
244
|
+
} else if (node.tag === "ol") {
|
245
|
+
listContext = "ol";
|
246
|
+
texts[texts.length - 1] += "\n";
|
247
|
+
} else if (node.tag === "li") {
|
248
|
+
if (listContext === "ul") {
|
249
|
+
texts[texts.length - 1] += "\n- ";
|
250
|
+
} else if (listContext === "ol") {
|
251
|
+
texts[texts.length - 1] += "\n1. ";
|
252
|
+
} else {
|
253
|
+
throw new Error("Unexpected list context");
|
254
|
+
}
|
236
255
|
} else if (node.tag === "span") {
|
237
256
|
if (node.classes.includes("bold") && !node.classes.includes("italic")) {
|
238
257
|
texts[texts.length - 1] += "**";
|
@@ -246,10 +265,14 @@ function richTextToTaggedStringTemplate(source) {
|
|
246
265
|
if (node.classes.includes("line-through")) {
|
247
266
|
texts[texts.length - 1] += "~~";
|
248
267
|
}
|
268
|
+
} else {
|
269
|
+
//exhaustive match
|
270
|
+
const exhaustiveCheck = node.tag;
|
271
|
+
throw new Error("Unexpected node tag: " + JSON.stringify(node, exhaustiveCheck, 2));
|
249
272
|
}
|
250
273
|
(_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.forEach(rec);
|
274
|
+
didAppendNewLines = false;
|
251
275
|
if (node.tag === "span") {
|
252
|
-
didAppendNewLines = false;
|
253
276
|
if (node.classes.includes("line-through")) {
|
254
277
|
texts[texts.length - 1] += "~~";
|
255
278
|
}
|
@@ -265,12 +288,15 @@ function richTextToTaggedStringTemplate(source) {
|
|
265
288
|
} else if (node.tag === "p") {
|
266
289
|
didAppendNewLines = true;
|
267
290
|
texts[texts.length - 1] += "\n\n";
|
268
|
-
} else if (
|
291
|
+
} else if (node.tag === "ul" || node.tag === "ol") {
|
292
|
+
listContext = null;
|
293
|
+
texts[texts.length - 1] += "\n";
|
294
|
+
} else if ((_node$tag = node.tag) !== null && _node$tag !== void 0 && _node$tag.startsWith("h")) {
|
269
295
|
didAppendNewLines = true;
|
270
296
|
texts[texts.length - 1] += "\n\n";
|
271
297
|
}
|
272
298
|
} else {
|
273
|
-
nodes.push(node);
|
299
|
+
nodes.push(node); // ImageSource
|
274
300
|
texts.push("\n");
|
275
301
|
}
|
276
302
|
}
|
@@ -660,6 +686,7 @@ globalThis.valModule = {
|
|
660
686
|
const fatalErrors = [];
|
661
687
|
if (result.error) {
|
662
688
|
const error = result.error.consume(context.dump);
|
689
|
+
console.error(`Fatal error reading val file: ${error.message}\n`, error.stack);
|
663
690
|
return {
|
664
691
|
errors: {
|
665
692
|
invalidModuleId: id,
|
package/package.json
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
"./package.json": "./package.json"
|
13
13
|
},
|
14
14
|
"types": "dist/valbuild-server.cjs.d.ts",
|
15
|
-
"version": "0.20.
|
15
|
+
"version": "0.20.1",
|
16
16
|
"scripts": {
|
17
17
|
"typecheck": "tsc --noEmit",
|
18
18
|
"test": "jest",
|
@@ -25,7 +25,7 @@
|
|
25
25
|
"concurrently": "^7.6.0"
|
26
26
|
},
|
27
27
|
"dependencies": {
|
28
|
-
"@valbuild/core": "~0.20.
|
28
|
+
"@valbuild/core": "~0.20.1",
|
29
29
|
"@valbuild/ui": "~0.20.0",
|
30
30
|
"express": "^4.18.2",
|
31
31
|
"image-size": "^1.0.2",
|
@@ -59,6 +59,32 @@ describe("patch richtext", () => {
|
|
59
59
|
},
|
60
60
|
{ _type: "file", _ref: "/public/image.png" },
|
61
61
|
{ tag: "p", children: ["Paragraph 2"] },
|
62
|
+
{
|
63
|
+
tag: "ol",
|
64
|
+
children: [
|
65
|
+
{ tag: "li", children: ["OL:Item 1"] },
|
66
|
+
{ tag: "li", children: ["OL:Item 2"] },
|
67
|
+
],
|
68
|
+
},
|
69
|
+
{
|
70
|
+
tag: "ul",
|
71
|
+
children: [
|
72
|
+
{ tag: "li", children: ["UL:Item 2"] },
|
73
|
+
{ tag: "li", children: ["UL:Item 3"] },
|
74
|
+
],
|
75
|
+
},
|
76
|
+
{ tag: "h3", children: ["Title 3"] },
|
77
|
+
{
|
78
|
+
tag: "ul",
|
79
|
+
children: [
|
80
|
+
{
|
81
|
+
tag: "li",
|
82
|
+
children: [
|
83
|
+
{ tag: "span", classes: ["bold"], children: ["UL:Item 4"] },
|
84
|
+
],
|
85
|
+
},
|
86
|
+
],
|
87
|
+
},
|
62
88
|
],
|
63
89
|
};
|
64
90
|
const r = richTextToTaggedStringTemplate(input);
|
package/src/patch/ts/richtext.ts
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
AnyRichTextOptions,
|
4
|
+
RichTextSource,
|
5
|
+
RichTextSourceNode,
|
6
|
+
} from "@valbuild/core";
|
3
7
|
|
4
|
-
const HeaderRegEx =
|
8
|
+
const HeaderRegEx = /^h([\d+])$/;
|
5
9
|
|
6
10
|
export function richTextToTaggedStringTemplate(
|
7
11
|
source: RichTextSource<AnyRichTextOptions>
|
@@ -9,17 +13,44 @@ export function richTextToTaggedStringTemplate(
|
|
9
13
|
const texts: string[] = [""];
|
10
14
|
const nodes: any[] = [];
|
11
15
|
let didAppendNewLines = false;
|
16
|
+
let listContext: "ul" | "ol" | null = null;
|
12
17
|
|
13
|
-
function rec(node:
|
18
|
+
function rec(node: RichTextSourceNode<AnyRichTextOptions>) {
|
14
19
|
if (typeof node === "string") {
|
15
20
|
texts[texts.length - 1] += node;
|
16
|
-
} else if (node.tag) {
|
17
|
-
if (
|
18
|
-
|
21
|
+
} else if ("tag" in node && node.tag) {
|
22
|
+
if (
|
23
|
+
node.tag === "h1" ||
|
24
|
+
node.tag === "h2" ||
|
25
|
+
node.tag === "h3" ||
|
26
|
+
node.tag === "h4" ||
|
27
|
+
node.tag === "h5" ||
|
28
|
+
node.tag === "h6"
|
29
|
+
) {
|
30
|
+
const depth = Number(node.tag.match(HeaderRegEx)?.[1]);
|
31
|
+
if (Number.isNaN(depth)) {
|
32
|
+
throw new Error("Invalid header depth");
|
33
|
+
}
|
19
34
|
for (let i = 0; i < Number(depth); i++) {
|
20
35
|
texts[texts.length - 1] += "#";
|
21
36
|
}
|
22
37
|
texts[texts.length - 1] += " ";
|
38
|
+
} else if (node.tag === "p") {
|
39
|
+
// Ignore
|
40
|
+
} else if (node.tag === "ul") {
|
41
|
+
listContext = "ul";
|
42
|
+
texts[texts.length - 1] += "\n";
|
43
|
+
} else if (node.tag === "ol") {
|
44
|
+
listContext = "ol";
|
45
|
+
texts[texts.length - 1] += "\n";
|
46
|
+
} else if (node.tag === "li") {
|
47
|
+
if (listContext === "ul") {
|
48
|
+
texts[texts.length - 1] += "\n- ";
|
49
|
+
} else if (listContext === "ol") {
|
50
|
+
texts[texts.length - 1] += "\n1. ";
|
51
|
+
} else {
|
52
|
+
throw new Error("Unexpected list context");
|
53
|
+
}
|
23
54
|
} else if (node.tag === "span") {
|
24
55
|
if (node.classes.includes("bold") && !node.classes.includes("italic")) {
|
25
56
|
texts[texts.length - 1] += "**";
|
@@ -33,12 +64,18 @@ export function richTextToTaggedStringTemplate(
|
|
33
64
|
if (node.classes.includes("line-through")) {
|
34
65
|
texts[texts.length - 1] += "~~";
|
35
66
|
}
|
67
|
+
} else {
|
68
|
+
//exhaustive match
|
69
|
+
const exhaustiveCheck: never = node.tag;
|
70
|
+
throw new Error(
|
71
|
+
"Unexpected node tag: " + JSON.stringify(node, exhaustiveCheck, 2)
|
72
|
+
);
|
36
73
|
}
|
37
74
|
|
38
75
|
node.children?.forEach(rec);
|
39
76
|
|
77
|
+
didAppendNewLines = false;
|
40
78
|
if (node.tag === "span") {
|
41
|
-
didAppendNewLines = false;
|
42
79
|
if (node.classes.includes("line-through")) {
|
43
80
|
texts[texts.length - 1] += "~~";
|
44
81
|
}
|
@@ -54,12 +91,15 @@ export function richTextToTaggedStringTemplate(
|
|
54
91
|
} else if (node.tag === "p") {
|
55
92
|
didAppendNewLines = true;
|
56
93
|
texts[texts.length - 1] += "\n\n";
|
94
|
+
} else if (node.tag === "ul" || node.tag === "ol") {
|
95
|
+
listContext = null;
|
96
|
+
texts[texts.length - 1] += "\n";
|
57
97
|
} else if (node.tag?.startsWith("h")) {
|
58
98
|
didAppendNewLines = true;
|
59
99
|
texts[texts.length - 1] += "\n\n";
|
60
100
|
}
|
61
101
|
} else {
|
62
|
-
nodes.push(node);
|
102
|
+
nodes.push(node); // ImageSource
|
63
103
|
texts.push("\n");
|
64
104
|
}
|
65
105
|
}
|
package/src/readValFile.ts
CHANGED
@@ -31,6 +31,10 @@ globalThis.valModule = {
|
|
31
31
|
const fatalErrors: string[] = [];
|
32
32
|
if (result.error) {
|
33
33
|
const error = result.error.consume(context.dump);
|
34
|
+
console.error(
|
35
|
+
`Fatal error reading val file: ${error.message}\n`,
|
36
|
+
error.stack
|
37
|
+
);
|
34
38
|
return {
|
35
39
|
errors: {
|
36
40
|
invalidModuleId: id as ModuleId,
|