@valbuild/shared 0.33.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.
@@ -0,0 +1,1166 @@
1
+ import { VAL_EXTENSION, FILE_REF_PROP, Internal } from '@valbuild/core';
2
+ import * as marked from 'marked';
3
+
4
+ function _regeneratorRuntime() {
5
+ _regeneratorRuntime = function () {
6
+ return e;
7
+ };
8
+ var t,
9
+ e = {},
10
+ r = Object.prototype,
11
+ n = r.hasOwnProperty,
12
+ o = Object.defineProperty || function (t, e, r) {
13
+ t[e] = r.value;
14
+ },
15
+ i = "function" == typeof Symbol ? Symbol : {},
16
+ a = i.iterator || "@@iterator",
17
+ c = i.asyncIterator || "@@asyncIterator",
18
+ u = i.toStringTag || "@@toStringTag";
19
+ function define(t, e, r) {
20
+ return Object.defineProperty(t, e, {
21
+ value: r,
22
+ enumerable: !0,
23
+ configurable: !0,
24
+ writable: !0
25
+ }), t[e];
26
+ }
27
+ try {
28
+ define({}, "");
29
+ } catch (t) {
30
+ define = function (t, e, r) {
31
+ return t[e] = r;
32
+ };
33
+ }
34
+ function wrap(t, e, r, n) {
35
+ var i = e && e.prototype instanceof Generator ? e : Generator,
36
+ a = Object.create(i.prototype),
37
+ c = new Context(n || []);
38
+ return o(a, "_invoke", {
39
+ value: makeInvokeMethod(t, r, c)
40
+ }), a;
41
+ }
42
+ function tryCatch(t, e, r) {
43
+ try {
44
+ return {
45
+ type: "normal",
46
+ arg: t.call(e, r)
47
+ };
48
+ } catch (t) {
49
+ return {
50
+ type: "throw",
51
+ arg: t
52
+ };
53
+ }
54
+ }
55
+ e.wrap = wrap;
56
+ var h = "suspendedStart",
57
+ l = "suspendedYield",
58
+ f = "executing",
59
+ s = "completed",
60
+ y = {};
61
+ function Generator() {}
62
+ function GeneratorFunction() {}
63
+ function GeneratorFunctionPrototype() {}
64
+ var p = {};
65
+ define(p, a, function () {
66
+ return this;
67
+ });
68
+ var d = Object.getPrototypeOf,
69
+ v = d && d(d(values([])));
70
+ v && v !== r && n.call(v, a) && (p = v);
71
+ var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
72
+ function defineIteratorMethods(t) {
73
+ ["next", "throw", "return"].forEach(function (e) {
74
+ define(t, e, function (t) {
75
+ return this._invoke(e, t);
76
+ });
77
+ });
78
+ }
79
+ function AsyncIterator(t, e) {
80
+ function invoke(r, o, i, a) {
81
+ var c = tryCatch(t[r], t, o);
82
+ if ("throw" !== c.type) {
83
+ var u = c.arg,
84
+ h = u.value;
85
+ return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
86
+ invoke("next", t, i, a);
87
+ }, function (t) {
88
+ invoke("throw", t, i, a);
89
+ }) : e.resolve(h).then(function (t) {
90
+ u.value = t, i(u);
91
+ }, function (t) {
92
+ return invoke("throw", t, i, a);
93
+ });
94
+ }
95
+ a(c.arg);
96
+ }
97
+ var r;
98
+ o(this, "_invoke", {
99
+ value: function (t, n) {
100
+ function callInvokeWithMethodAndArg() {
101
+ return new e(function (e, r) {
102
+ invoke(t, n, e, r);
103
+ });
104
+ }
105
+ return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
106
+ }
107
+ });
108
+ }
109
+ function makeInvokeMethod(e, r, n) {
110
+ var o = h;
111
+ return function (i, a) {
112
+ if (o === f) throw new Error("Generator is already running");
113
+ if (o === s) {
114
+ if ("throw" === i) throw a;
115
+ return {
116
+ value: t,
117
+ done: !0
118
+ };
119
+ }
120
+ for (n.method = i, n.arg = a;;) {
121
+ var c = n.delegate;
122
+ if (c) {
123
+ var u = maybeInvokeDelegate(c, n);
124
+ if (u) {
125
+ if (u === y) continue;
126
+ return u;
127
+ }
128
+ }
129
+ if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
130
+ if (o === h) throw o = s, n.arg;
131
+ n.dispatchException(n.arg);
132
+ } else "return" === n.method && n.abrupt("return", n.arg);
133
+ o = f;
134
+ var p = tryCatch(e, r, n);
135
+ if ("normal" === p.type) {
136
+ if (o = n.done ? s : l, p.arg === y) continue;
137
+ return {
138
+ value: p.arg,
139
+ done: n.done
140
+ };
141
+ }
142
+ "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
143
+ }
144
+ };
145
+ }
146
+ function maybeInvokeDelegate(e, r) {
147
+ var n = r.method,
148
+ o = e.iterator[n];
149
+ if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
150
+ var i = tryCatch(o, e.iterator, r.arg);
151
+ if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
152
+ var a = i.arg;
153
+ return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
154
+ }
155
+ function pushTryEntry(t) {
156
+ var e = {
157
+ tryLoc: t[0]
158
+ };
159
+ 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
160
+ }
161
+ function resetTryEntry(t) {
162
+ var e = t.completion || {};
163
+ e.type = "normal", delete e.arg, t.completion = e;
164
+ }
165
+ function Context(t) {
166
+ this.tryEntries = [{
167
+ tryLoc: "root"
168
+ }], t.forEach(pushTryEntry, this), this.reset(!0);
169
+ }
170
+ function values(e) {
171
+ if (e || "" === e) {
172
+ var r = e[a];
173
+ if (r) return r.call(e);
174
+ if ("function" == typeof e.next) return e;
175
+ if (!isNaN(e.length)) {
176
+ var o = -1,
177
+ i = function next() {
178
+ for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
179
+ return next.value = t, next.done = !0, next;
180
+ };
181
+ return i.next = i;
182
+ }
183
+ }
184
+ throw new TypeError(typeof e + " is not iterable");
185
+ }
186
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
187
+ value: GeneratorFunctionPrototype,
188
+ configurable: !0
189
+ }), o(GeneratorFunctionPrototype, "constructor", {
190
+ value: GeneratorFunction,
191
+ configurable: !0
192
+ }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
193
+ var e = "function" == typeof t && t.constructor;
194
+ return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
195
+ }, e.mark = function (t) {
196
+ return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
197
+ }, e.awrap = function (t) {
198
+ return {
199
+ __await: t
200
+ };
201
+ }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
202
+ return this;
203
+ }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
204
+ void 0 === i && (i = Promise);
205
+ var a = new AsyncIterator(wrap(t, r, n, o), i);
206
+ return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
207
+ return t.done ? t.value : a.next();
208
+ });
209
+ }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
210
+ return this;
211
+ }), define(g, "toString", function () {
212
+ return "[object Generator]";
213
+ }), e.keys = function (t) {
214
+ var e = Object(t),
215
+ r = [];
216
+ for (var n in e) r.push(n);
217
+ return r.reverse(), function next() {
218
+ for (; r.length;) {
219
+ var t = r.pop();
220
+ if (t in e) return next.value = t, next.done = !1, next;
221
+ }
222
+ return next.done = !0, next;
223
+ };
224
+ }, e.values = values, Context.prototype = {
225
+ constructor: Context,
226
+ reset: function (e) {
227
+ if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
228
+ },
229
+ stop: function () {
230
+ this.done = !0;
231
+ var t = this.tryEntries[0].completion;
232
+ if ("throw" === t.type) throw t.arg;
233
+ return this.rval;
234
+ },
235
+ dispatchException: function (e) {
236
+ if (this.done) throw e;
237
+ var r = this;
238
+ function handle(n, o) {
239
+ return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
240
+ }
241
+ for (var o = this.tryEntries.length - 1; o >= 0; --o) {
242
+ var i = this.tryEntries[o],
243
+ a = i.completion;
244
+ if ("root" === i.tryLoc) return handle("end");
245
+ if (i.tryLoc <= this.prev) {
246
+ var c = n.call(i, "catchLoc"),
247
+ u = n.call(i, "finallyLoc");
248
+ if (c && u) {
249
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
250
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
251
+ } else if (c) {
252
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
253
+ } else {
254
+ if (!u) throw new Error("try statement without catch or finally");
255
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
256
+ }
257
+ }
258
+ }
259
+ },
260
+ abrupt: function (t, e) {
261
+ for (var r = this.tryEntries.length - 1; r >= 0; --r) {
262
+ var o = this.tryEntries[r];
263
+ if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
264
+ var i = o;
265
+ break;
266
+ }
267
+ }
268
+ i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
269
+ var a = i ? i.completion : {};
270
+ return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
271
+ },
272
+ complete: function (t, e) {
273
+ if ("throw" === t.type) throw t.arg;
274
+ return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
275
+ },
276
+ finish: function (t) {
277
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
278
+ var r = this.tryEntries[e];
279
+ if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
280
+ }
281
+ },
282
+ catch: function (t) {
283
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
284
+ var r = this.tryEntries[e];
285
+ if (r.tryLoc === t) {
286
+ var n = r.completion;
287
+ if ("throw" === n.type) {
288
+ var o = n.arg;
289
+ resetTryEntry(r);
290
+ }
291
+ return o;
292
+ }
293
+ }
294
+ throw new Error("illegal catch attempt");
295
+ },
296
+ delegateYield: function (e, r, n) {
297
+ return this.delegate = {
298
+ iterator: values(e),
299
+ resultName: r,
300
+ nextLoc: n
301
+ }, "next" === this.method && (this.arg = t), y;
302
+ }
303
+ }, e;
304
+ }
305
+
306
+ function _arrayLikeToArray(arr, len) {
307
+ if (len == null || len > arr.length) len = arr.length;
308
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
309
+ return arr2;
310
+ }
311
+
312
+ function _unsupportedIterableToArray(o, minLen) {
313
+ if (!o) return;
314
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
315
+ var n = Object.prototype.toString.call(o).slice(8, -1);
316
+ if (n === "Object" && o.constructor) n = o.constructor.name;
317
+ if (n === "Map" || n === "Set") return Array.from(o);
318
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
319
+ }
320
+
321
+ function _createForOfIteratorHelper(o, allowArrayLike) {
322
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
323
+ if (!it) {
324
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
325
+ if (it) o = it;
326
+ var i = 0;
327
+ var F = function () {};
328
+ return {
329
+ s: F,
330
+ n: function () {
331
+ if (i >= o.length) return {
332
+ done: true
333
+ };
334
+ return {
335
+ done: false,
336
+ value: o[i++]
337
+ };
338
+ },
339
+ e: function (e) {
340
+ throw e;
341
+ },
342
+ f: F
343
+ };
344
+ }
345
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
346
+ }
347
+ var normalCompletion = true,
348
+ didErr = false,
349
+ err;
350
+ return {
351
+ s: function () {
352
+ it = it.call(o);
353
+ },
354
+ n: function () {
355
+ var step = it.next();
356
+ normalCompletion = step.done;
357
+ return step;
358
+ },
359
+ e: function (e) {
360
+ didErr = true;
361
+ err = e;
362
+ },
363
+ f: function () {
364
+ try {
365
+ if (!normalCompletion && it.return != null) it.return();
366
+ } finally {
367
+ if (didErr) throw err;
368
+ }
369
+ }
370
+ };
371
+ }
372
+
373
+ function _toPrimitive(input, hint) {
374
+ if (typeof input !== "object" || input === null) return input;
375
+ var prim = input[Symbol.toPrimitive];
376
+ if (prim !== undefined) {
377
+ var res = prim.call(input, hint || "default");
378
+ if (typeof res !== "object") return res;
379
+ throw new TypeError("@@toPrimitive must return a primitive value.");
380
+ }
381
+ return (hint === "string" ? String : Number)(input);
382
+ }
383
+
384
+ function _toPropertyKey(arg) {
385
+ var key = _toPrimitive(arg, "string");
386
+ return typeof key === "symbol" ? key : String(key);
387
+ }
388
+
389
+ function _defineProperty(obj, key, value) {
390
+ key = _toPropertyKey(key);
391
+ if (key in obj) {
392
+ Object.defineProperty(obj, key, {
393
+ value: value,
394
+ enumerable: true,
395
+ configurable: true,
396
+ writable: true
397
+ });
398
+ } else {
399
+ obj[key] = value;
400
+ }
401
+ return obj;
402
+ }
403
+
404
+ function _arrayWithHoles(arr) {
405
+ if (Array.isArray(arr)) return arr;
406
+ }
407
+
408
+ function _iterableToArrayLimit(r, l) {
409
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
410
+ if (null != t) {
411
+ var e,
412
+ n,
413
+ i,
414
+ u,
415
+ a = [],
416
+ f = !0,
417
+ o = !1;
418
+ try {
419
+ if (i = (t = t.call(r)).next, 0 === l) {
420
+ if (Object(t) !== t) return;
421
+ f = !1;
422
+ } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
423
+ } catch (r) {
424
+ o = !0, n = r;
425
+ } finally {
426
+ try {
427
+ if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
428
+ } finally {
429
+ if (o) throw n;
430
+ }
431
+ }
432
+ return a;
433
+ }
434
+ }
435
+
436
+ function _nonIterableRest() {
437
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
438
+ }
439
+
440
+ function _slicedToArray(arr, i) {
441
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
442
+ }
443
+
444
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
445
+ try {
446
+ var info = gen[key](arg);
447
+ var value = info.value;
448
+ } catch (error) {
449
+ reject(error);
450
+ return;
451
+ }
452
+ if (info.done) {
453
+ resolve(value);
454
+ } else {
455
+ Promise.resolve(value).then(_next, _throw);
456
+ }
457
+ }
458
+ function _asyncToGenerator(fn) {
459
+ return function () {
460
+ var self = this,
461
+ args = arguments;
462
+ return new Promise(function (resolve, reject) {
463
+ var gen = fn.apply(self, args);
464
+ function _next(value) {
465
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
466
+ }
467
+ function _throw(err) {
468
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
469
+ }
470
+ _next(undefined);
471
+ });
472
+ };
473
+ }
474
+
475
+ function _arrayWithoutHoles(arr) {
476
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
477
+ }
478
+
479
+ function _iterableToArray(iter) {
480
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
481
+ }
482
+
483
+ function _nonIterableSpread() {
484
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
485
+ }
486
+
487
+ function _toConsumableArray(arr) {
488
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
489
+ }
490
+
491
+ var MIME_TYPE_REGEX = /^data:(image\/(png|jpeg|jpg|gif|webp|bmp|tiff|ico|svg\+xml));base64,/;
492
+ function getMimeType(base64Url) {
493
+ var match = MIME_TYPE_REGEX.exec(base64Url);
494
+ if (match && match[1]) {
495
+ return match[1];
496
+ }
497
+ return;
498
+ }
499
+ function mimeTypeToFileExt(mimeType) {
500
+ if (mimeType === "image/svg+xml") {
501
+ return "svg";
502
+ }
503
+ if (mimeType === "image/vnd.microsoft.icon") {
504
+ return "ico";
505
+ }
506
+ if (mimeType.startsWith("image/")) {
507
+ return mimeType.slice("image/".length);
508
+ }
509
+ return mimeType;
510
+ }
511
+
512
+ var HeaderRegEx = /^h([\d+])$/;
513
+ function lexicalToRichTextSource(node) {
514
+ var markdownIRBlocks = node.children.map(createBlock);
515
+ return fromIRToRichTextSource(markdownIRBlocks);
516
+ }
517
+ function createBlock(node) {
518
+ if (node.type === "heading") {
519
+ var _node$tag$match;
520
+ var headingTag = "";
521
+ var depth = Number((_node$tag$match = node.tag.match(HeaderRegEx)) === null || _node$tag$match === void 0 ? void 0 : _node$tag$match[1]);
522
+ if (Number.isNaN(depth)) {
523
+ throw new Error("Invalid header depth");
524
+ }
525
+ for (var i = 0; i < Number(depth); i++) {
526
+ headingTag += "#";
527
+ }
528
+ var headingText = ["".concat(headingTag, " ")];
529
+ return {
530
+ type: "block",
531
+ children: headingText.concat.apply(headingText, _toConsumableArray(node.children.map(transformLeafNode)))
532
+ };
533
+ } else if (node.type === "paragraph") {
534
+ if (node.children.length === 0) {
535
+ return {
536
+ type: "block",
537
+ children: ["<br />"]
538
+ };
539
+ }
540
+ return {
541
+ type: "block",
542
+ children: node.children.map(function (child) {
543
+ return transformLeafNode(child);
544
+ })
545
+ };
546
+ } else if (node.type === "list") {
547
+ return {
548
+ type: "block",
549
+ children: node.children.flatMap(function (child, i) {
550
+ return formatListItemNode(getListPrefix(node), child, 0, i === 0);
551
+ })
552
+ };
553
+ } else {
554
+ var exhaustiveCheck = node;
555
+ throw new Error("Unhandled node type: ".concat("type" in exhaustiveCheck ? "exhaustiveCheck.type" : "unknown"));
556
+ }
557
+ }
558
+ function fromIRToRichTextSource(_x) {
559
+ return _fromIRToRichTextSource.apply(this, arguments);
560
+ }
561
+ function _fromIRToRichTextSource() {
562
+ _fromIRToRichTextSource = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(markdownIRBlocks) {
563
+ var _ref4;
564
+ var templateStrings, exprs, files, blockIdx, block, _iterator, _step, child, exhaustiveCheck;
565
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
566
+ while (1) switch (_context.prev = _context.next) {
567
+ case 0:
568
+ templateStrings = ["\n"];
569
+ exprs = [];
570
+ files = {};
571
+ blockIdx = 0;
572
+ case 4:
573
+ if (!(blockIdx < markdownIRBlocks.length)) {
574
+ _context.next = 44;
575
+ break;
576
+ }
577
+ block = markdownIRBlocks[blockIdx];
578
+ _iterator = _createForOfIteratorHelper(block.children);
579
+ _context.prev = 7;
580
+ _iterator.s();
581
+ case 9:
582
+ if ((_step = _iterator.n()).done) {
583
+ _context.next = 32;
584
+ break;
585
+ }
586
+ child = _step.value;
587
+ if (!(typeof child === "string")) {
588
+ _context.next = 15;
589
+ break;
590
+ }
591
+ templateStrings[templateStrings.length - 1] += child;
592
+ _context.next = 30;
593
+ break;
594
+ case 15:
595
+ if (!(child.type === "image")) {
596
+ _context.next = 23;
597
+ break;
598
+ }
599
+ _context.t0 = exprs;
600
+ _context.next = 19;
601
+ return fromLexicalImageNode(child, files);
602
+ case 19:
603
+ _context.t1 = _context.sent;
604
+ _context.t0.push.call(_context.t0, _context.t1);
605
+ _context.next = 29;
606
+ break;
607
+ case 23:
608
+ if (!(child.type === "link")) {
609
+ _context.next = 27;
610
+ break;
611
+ }
612
+ exprs.push(fromLexicalLinkNode(child));
613
+ _context.next = 29;
614
+ break;
615
+ case 27:
616
+ exhaustiveCheck = child;
617
+ throw new Error("Unexpected node type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
618
+ case 29:
619
+ templateStrings.push("");
620
+ case 30:
621
+ _context.next = 9;
622
+ break;
623
+ case 32:
624
+ _context.next = 37;
625
+ break;
626
+ case 34:
627
+ _context.prev = 34;
628
+ _context.t2 = _context["catch"](7);
629
+ _iterator.e(_context.t2);
630
+ case 37:
631
+ _context.prev = 37;
632
+ _iterator.f();
633
+ return _context.finish(37);
634
+ case 40:
635
+ if (blockIdx === markdownIRBlocks.length - 1) {
636
+ templateStrings[templateStrings.length - 1] += "\n";
637
+ } else {
638
+ templateStrings[templateStrings.length - 1] += "\n\n";
639
+ }
640
+ case 41:
641
+ blockIdx++;
642
+ _context.next = 4;
643
+ break;
644
+ case 44:
645
+ return _context.abrupt("return", (_ref4 = {}, _defineProperty(_ref4, VAL_EXTENSION, "richtext"), _defineProperty(_ref4, "templateStrings", templateStrings), _defineProperty(_ref4, "exprs", exprs), _defineProperty(_ref4, "files", files), _ref4));
646
+ case 45:
647
+ case "end":
648
+ return _context.stop();
649
+ }
650
+ }, _callee, null, [[7, 34, 37, 40]]);
651
+ }));
652
+ return _fromIRToRichTextSource.apply(this, arguments);
653
+ }
654
+ function formatText(node) {
655
+ var classes = typeof node.format === "number" ? fromLexicalFormat(node.format) : [];
656
+ var text = node.text.trimStart();
657
+ var prefixWS = node.text.length - text.length;
658
+ text = text.trimEnd();
659
+ var suffixWS = node.text.length - text.length - prefixWS;
660
+ if (classes.includes("bold") && classes.includes("italic")) {
661
+ text = "***".concat(text, "***");
662
+ } else if (classes.includes("bold")) {
663
+ text = "**".concat(text, "**");
664
+ } else if (classes.includes("italic")) {
665
+ text = "_".concat(text, "_");
666
+ }
667
+ if (classes.includes("line-through")) {
668
+ text = "~~".concat(text, "~~");
669
+ }
670
+ // TODO:
671
+ // text = splitIntoChunks(text);
672
+ return "".concat(" ".repeat(prefixWS)).concat(text).concat(" ".repeat(suffixWS));
673
+ }
674
+ function transformLeafNode(node) {
675
+ if (node.type === "text") {
676
+ return formatText(node);
677
+ } else if (node.type === "linebreak") {
678
+ return "\n";
679
+ } else {
680
+ return node;
681
+ }
682
+ }
683
+ function formatListItemNode(listPrefix, node, indent) {
684
+ var _node$children, _node$children$;
685
+ var isFirstTopLevelListItem = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
686
+ var newLine = isFirstTopLevelListItem ? "" : "\n";
687
+ var prefix = ["".concat(newLine).concat(" ".repeat(indent)).concat(listPrefix)];
688
+ if (((_node$children = node.children) === null || _node$children === void 0 ? void 0 : (_node$children$ = _node$children[0]) === null || _node$children$ === void 0 ? void 0 : _node$children$.type) !== "list") {
689
+ prefix.push(" ");
690
+ }
691
+ return prefix.concat(node.children.flatMap(function (child) {
692
+ if (child.type === "list") {
693
+ return child.children.flatMap(function (subChild) {
694
+ return formatListItemNode(getListPrefix(child), subChild, indent + 4);
695
+ });
696
+ } else {
697
+ return [transformLeafNode(child)];
698
+ }
699
+ }));
700
+ }
701
+ function getListPrefix(node) {
702
+ if (node.listType === "bullet") {
703
+ return "-";
704
+ } else if (node.listType === "number") {
705
+ return "1.";
706
+ } else {
707
+ throw new Error("Unhandled list type: ".concat(node.listType));
708
+ }
709
+ }
710
+ var FORMAT_MAPPING$1 = {
711
+ bold: 1,
712
+ // 0001
713
+ italic: 2,
714
+ // 0010
715
+ "line-through": 4 // 0100
716
+ // underline: 8, // 1000
717
+ };
718
+
719
+ function fromLexicalFormat(format) {
720
+ return Object.entries(FORMAT_MAPPING$1).flatMap(function (_ref) {
721
+ var _ref2 = _slicedToArray(_ref, 2),
722
+ key = _ref2[0],
723
+ value = _ref2[1];
724
+ if ((value & /* bitwise and */format) === value) {
725
+ return [key];
726
+ }
727
+ return [];
728
+ });
729
+ }
730
+ var textEncoder = new TextEncoder();
731
+ function fromLexicalImageNode(_x2, _x3) {
732
+ return _fromLexicalImageNode.apply(this, arguments);
733
+ }
734
+ function _fromLexicalImageNode() {
735
+ _fromLexicalImageNode = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(node, files) {
736
+ var _ref5, sha256, mimeType, fileExt, filePath, _ref6, _sha;
737
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
738
+ while (1) switch (_context2.prev = _context2.next) {
739
+ case 0:
740
+ if (!node.src.startsWith("data:")) {
741
+ _context2.next = 13;
742
+ break;
743
+ }
744
+ _context2.next = 3;
745
+ return Internal.getSHA256Hash(textEncoder.encode(node.src));
746
+ case 3:
747
+ sha256 = _context2.sent;
748
+ mimeType = getMimeType(node.src);
749
+ if (!(mimeType === undefined)) {
750
+ _context2.next = 7;
751
+ break;
752
+ }
753
+ throw new Error("Could not detect Mime Type for image: ".concat(node.src));
754
+ case 7:
755
+ fileExt = mimeTypeToFileExt(mimeType);
756
+ filePath = "/public/".concat(sha256, ".").concat(fileExt);
757
+ files[filePath] = node.src;
758
+ return _context2.abrupt("return", (_ref5 = {}, _defineProperty(_ref5, VAL_EXTENSION, "file"), _defineProperty(_ref5, FILE_REF_PROP, filePath), _defineProperty(_ref5, "metadata", {
759
+ width: node.width || 0,
760
+ height: node.height || 0,
761
+ sha256: sha256 || ""
762
+ }), _ref5));
763
+ case 13:
764
+ _sha = getParam("sha256", node.src);
765
+ return _context2.abrupt("return", (_ref6 = {}, _defineProperty(_ref6, VAL_EXTENSION, "file"), _defineProperty(_ref6, FILE_REF_PROP, "/public".concat(node.src.split("?")[0])), _defineProperty(_ref6, "metadata", {
766
+ width: node.width || 0,
767
+ height: node.height || 0,
768
+ sha256: _sha || ""
769
+ }), _ref6));
770
+ case 15:
771
+ case "end":
772
+ return _context2.stop();
773
+ }
774
+ }, _callee2);
775
+ }));
776
+ return _fromLexicalImageNode.apply(this, arguments);
777
+ }
778
+ function getParam(param, url) {
779
+ var urlParts = url.split("?");
780
+ if (urlParts.length < 2) {
781
+ return undefined;
782
+ }
783
+ var queryString = urlParts[1];
784
+ var params = new URLSearchParams(queryString);
785
+ if (params.has(param)) {
786
+ return params.get(param);
787
+ }
788
+ return undefined;
789
+ }
790
+ function fromLexicalLinkNode(node) {
791
+ var _ref3;
792
+ return _ref3 = {}, _defineProperty(_ref3, VAL_EXTENSION, "link"), _defineProperty(_ref3, "href", node.url), _defineProperty(_ref3, "children", node.children.map(formatText)), _ref3;
793
+ }
794
+
795
+ function ownKeys(e, r) {
796
+ var t = Object.keys(e);
797
+ if (Object.getOwnPropertySymbols) {
798
+ var o = Object.getOwnPropertySymbols(e);
799
+ r && (o = o.filter(function (r) {
800
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
801
+ })), t.push.apply(t, o);
802
+ }
803
+ return t;
804
+ }
805
+ function _objectSpread2(e) {
806
+ for (var r = 1; r < arguments.length; r++) {
807
+ var t = null != arguments[r] ? arguments[r] : {};
808
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
809
+ _defineProperty(e, r, t[r]);
810
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
811
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
812
+ });
813
+ }
814
+ return e;
815
+ }
816
+
817
+ /// Serialized Lexical Nodes:
818
+ // TODO: replace with Lexical libs types - not currently exported?
819
+ var COMMON_LEXICAL_PROPS = {
820
+ version: 1,
821
+ format: "",
822
+ indent: 0,
823
+ direction: null
824
+ };
825
+ function toLexicalNode(node) {
826
+ var useBreakNode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
827
+ if (typeof node === "string") {
828
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
829
+ type: "text",
830
+ format: "",
831
+ text: node
832
+ });
833
+ }
834
+ if ("tag" in node) {
835
+ switch (node.tag) {
836
+ case "h1":
837
+ return toLexicalHeadingNode(node);
838
+ case "h2":
839
+ return toLexicalHeadingNode(node);
840
+ case "h3":
841
+ return toLexicalHeadingNode(node);
842
+ case "h4":
843
+ return toLexicalHeadingNode(node);
844
+ case "h5":
845
+ return toLexicalHeadingNode(node);
846
+ case "h6":
847
+ return toLexicalHeadingNode(node);
848
+ case "li":
849
+ return toLexicalListItemNode(node);
850
+ case "p":
851
+ return toLexicalParagraphNode(node);
852
+ case "ul":
853
+ return toLexicalListNode(node);
854
+ case "ol":
855
+ return toLexicalListNode(node);
856
+ case "span":
857
+ return toLexicalTextNode(node);
858
+ case "a":
859
+ return toLexicalLinkNode(node);
860
+ case "img":
861
+ return toLexicalImageNode(node);
862
+ case "br":
863
+ if (useBreakNode) {
864
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
865
+ type: "linebreak"
866
+ });
867
+ }
868
+ return toLexicalPseudoLineBreakNode();
869
+ default:
870
+ throw Error("Unexpected node tag: " + JSON.stringify(node, null, 2));
871
+ }
872
+ } else {
873
+ throw Error("Unexpected node: " + JSON.stringify(node, null, 2));
874
+ }
875
+ }
876
+ function toLexicalImageNode(node) {
877
+ var url = node.src;
878
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
879
+ type: "image",
880
+ src: url,
881
+ width: node.width,
882
+ height: node.height
883
+ // TODO: altText
884
+ });
885
+ }
886
+
887
+ function toLexicalLinkNode(link) {
888
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
889
+ type: "link",
890
+ url: link.href,
891
+ children: link.children.map(function (child) {
892
+ return toLexicalNode(child);
893
+ })
894
+ });
895
+ }
896
+ function richTextSourceToLexical(richtext) {
897
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
898
+ format: "",
899
+ type: "root",
900
+ children: richtext.children.map(function (child) {
901
+ return toLexicalNode(child);
902
+ })
903
+ });
904
+ }
905
+ function toLexicalHeadingNode(heading) {
906
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
907
+ type: "heading",
908
+ tag: heading.tag,
909
+ children: heading.children.map(function (child) {
910
+ return toLexicalNode(child);
911
+ })
912
+ });
913
+ }
914
+ function toLexicalParagraphNode(paragraph) {
915
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
916
+ type: "paragraph",
917
+ children: paragraph.children.map(function (child) {
918
+ return toLexicalNode(child, true);
919
+ })
920
+ });
921
+ }
922
+
923
+ // Lexical does not support line breaks, so we convert them to empty paragraphs
924
+ function toLexicalPseudoLineBreakNode() {
925
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
926
+ type: "paragraph",
927
+ children: []
928
+ });
929
+ }
930
+ function toLexicalListItemNode(listItem) {
931
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
932
+ type: "listitem",
933
+ children: listItem.children.map(function (child) {
934
+ return toLexicalNode(child, true);
935
+ })
936
+ });
937
+ }
938
+ function toLexicalListNode(list) {
939
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
940
+ type: "list",
941
+ listType: list.tag === "ol" ? "number" : "bullet",
942
+ children: list.children.map(function (child) {
943
+ return toLexicalNode(child);
944
+ })
945
+ }, list.dir ? {
946
+ direction: list.dir
947
+ } : {
948
+ direction: null
949
+ });
950
+ }
951
+ var FORMAT_MAPPING = {
952
+ bold: 1,
953
+ // 0001
954
+ italic: 2,
955
+ // 0010
956
+ "line-through": 4 // 0100
957
+ // underline: 8, // 1000
958
+ };
959
+
960
+ function toLexicalFormat(classes) {
961
+ return classes.reduce(function (prev, curr) {
962
+ return prev | /* bitwise or */FORMAT_MAPPING[curr];
963
+ }, 0);
964
+ }
965
+ function toLexicalTextNode(spanNode) {
966
+ var child = spanNode.children[0];
967
+ if (typeof child === "string") {
968
+ return _objectSpread2(_objectSpread2({}, COMMON_LEXICAL_PROPS), {}, {
969
+ type: "text",
970
+ text: child,
971
+ format: toLexicalFormat(spanNode.classes)
972
+ });
973
+ } else {
974
+ // recurse the spans and merge their classes
975
+ return toLexicalTextNode(_objectSpread2(_objectSpread2({}, child), {}, {
976
+ classes: spanNode.classes.concat(child.classes)
977
+ }));
978
+ }
979
+ }
980
+
981
+ function _typeof(o) {
982
+ "@babel/helpers - typeof";
983
+
984
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
985
+ return typeof o;
986
+ } : function (o) {
987
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
988
+ }, _typeof(o);
989
+ }
990
+
991
+ var VAL_START_TAG_PREFIX = '<val value="';
992
+ var VAL_START_TAG_SUFFIX = '">';
993
+ var VAL_END_TAG = "</val>";
994
+ function parseTokens(tokens, sourceNodes, cursor) {
995
+ var insideList = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
996
+ var children = [];
997
+ var _loop = function _loop() {
998
+ var token = tokens[cursor];
999
+ if (token.type === "heading") {
1000
+ children.push({
1001
+ tag: "h".concat(token.depth),
1002
+ children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1003
+ });
1004
+ } else if (token.type === "paragraph") {
1005
+ children.push({
1006
+ tag: "p",
1007
+ children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1008
+ });
1009
+ } else if (token.type === "strong") {
1010
+ children.push({
1011
+ tag: "span",
1012
+ classes: ["bold"],
1013
+ children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1014
+ });
1015
+ } else if (token.type === "em") {
1016
+ children.push({
1017
+ tag: "span",
1018
+ classes: ["italic"],
1019
+ children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1020
+ });
1021
+ } else if (token.type === "del") {
1022
+ children.push({
1023
+ tag: "span",
1024
+ classes: ["line-through"],
1025
+ children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1026
+ });
1027
+ } else if (token.type === "text") {
1028
+ if ("tokens" in token && Array.isArray(token.tokens)) {
1029
+ children.push.apply(children, _toConsumableArray(parseTokens(token.tokens, sourceNodes, cursor, insideList).children));
1030
+ } else {
1031
+ if (insideList && typeof token.raw === "string") {
1032
+ var lines = token.raw.split("\n");
1033
+ var tags = lines.flatMap(function (line, i) {
1034
+ if (i === lines.length - 1) return [line];
1035
+ return [line, {
1036
+ tag: "br",
1037
+ children: []
1038
+ }];
1039
+ });
1040
+ children.push.apply(children, _toConsumableArray(tags));
1041
+ } else {
1042
+ children.push(token.raw);
1043
+ }
1044
+ }
1045
+ } else if (token.type === "list") {
1046
+ children.push({
1047
+ tag: token.ordered ? "ol" : "ul",
1048
+ children: parseTokens(token.items, sourceNodes, 0).children
1049
+ });
1050
+ } else if (token.type === "list_item") {
1051
+ children.push({
1052
+ tag: "li",
1053
+ children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0, true).children
1054
+ });
1055
+ } else if (token.type === "space") ; else if (token.type === "html") {
1056
+ if (token.text === VAL_END_TAG) {
1057
+ return {
1058
+ v: {
1059
+ children: children,
1060
+ cursor: cursor
1061
+ }
1062
+ };
1063
+ }
1064
+ var suffixIndex = token.text.indexOf(VAL_START_TAG_SUFFIX);
1065
+ if (token.text.startsWith(VAL_START_TAG_PREFIX) && suffixIndex > -1) {
1066
+ var number = Number(token.text.slice(VAL_START_TAG_PREFIX.length, suffixIndex));
1067
+ if (Number.isNaN(number)) {
1068
+ throw Error("Illegal val intermediate node: ".concat(JSON.stringify(token)));
1069
+ }
1070
+ var _parseTokens = parseTokens(tokens.map(function (token) {
1071
+ if (token.type === "link" || token.type === "list") {
1072
+ return {
1073
+ type: "text",
1074
+ raw: token.raw,
1075
+ text: token.raw
1076
+ };
1077
+ }
1078
+ return token;
1079
+ }), sourceNodes, cursor + 1),
1080
+ subChildren = _parseTokens.children,
1081
+ subCursor = _parseTokens.cursor;
1082
+ var sourceNode = sourceNodes[number];
1083
+ if (sourceNode._type === "link") {
1084
+ children.push({
1085
+ tag: "a",
1086
+ href: sourceNode.href,
1087
+ children: subChildren
1088
+ });
1089
+ } else if (sourceNode._type === "file") {
1090
+ var _sourceNode$metadata, _sourceNode$metadata2;
1091
+ children.push({
1092
+ tag: "img",
1093
+ src: Internal.convertFileSource(sourceNode).url,
1094
+ width: (_sourceNode$metadata = sourceNode.metadata) === null || _sourceNode$metadata === void 0 ? void 0 : _sourceNode$metadata.width,
1095
+ height: (_sourceNode$metadata2 = sourceNode.metadata) === null || _sourceNode$metadata2 === void 0 ? void 0 : _sourceNode$metadata2.height,
1096
+ children: []
1097
+ });
1098
+ }
1099
+ cursor = subCursor;
1100
+ }
1101
+ var br_html_regex = /<br\s*\/?>/gi; // matches <br>, <br/>, <br />; case insensitive
1102
+ if (token.text.trim().match(br_html_regex)) {
1103
+ children.push({
1104
+ tag: "br",
1105
+ children: []
1106
+ });
1107
+ }
1108
+ } else if (token.type === "link") {
1109
+ if (token.raw === token.href) {
1110
+ // avoid auto-linking (provided by github flavoured markdown, but we want strikethrough so keep it enabled)
1111
+ children.push(token.raw);
1112
+ } else {
1113
+ children.push({
1114
+ tag: "a",
1115
+ href: token.href,
1116
+ children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1117
+ });
1118
+ }
1119
+ } else if (token.type === "br") {
1120
+ children.push({
1121
+ tag: "br",
1122
+ children: []
1123
+ });
1124
+ } else {
1125
+ console.error("Could not parse markdown: unsupported token type: ".concat(token.type, ". Found: ").concat(token.raw));
1126
+ }
1127
+ cursor++;
1128
+ };
1129
+ while (cursor < tokens.length) {
1130
+ var _ret = _loop();
1131
+ if (_typeof(_ret) === "object") return _ret.v;
1132
+ }
1133
+ return {
1134
+ children: children,
1135
+ cursor: cursor
1136
+ };
1137
+ }
1138
+ function parseRichTextSource(_ref) {
1139
+ var _ref2;
1140
+ var templateStrings = _ref.templateStrings,
1141
+ nodes = _ref.exprs;
1142
+ // TODO: validate that templateStrings does not contain VAL_NODE_PREFIX
1143
+ var inputText = templateStrings.flatMap(function (templateString, i) {
1144
+ var node = nodes[i];
1145
+ if (node) {
1146
+ if (node[VAL_EXTENSION] === "link") {
1147
+ return templateString.concat("".concat(VAL_START_TAG_PREFIX).concat(i).concat(VAL_START_TAG_SUFFIX).concat(node.children[0]).concat(VAL_END_TAG));
1148
+ } else {
1149
+ return templateString.concat("".concat(VAL_START_TAG_PREFIX).concat(i).concat(VAL_START_TAG_SUFFIX).concat(VAL_END_TAG));
1150
+ }
1151
+ }
1152
+ return templateString;
1153
+ }).join("");
1154
+ var tokenList = marked.lexer(inputText, {
1155
+ gfm: true
1156
+ });
1157
+ var _parseTokens2 = parseTokens(tokenList, nodes, 0),
1158
+ children = _parseTokens2.children,
1159
+ cursor = _parseTokens2.cursor;
1160
+ if (cursor !== tokenList.length) {
1161
+ throw Error("Unexpectedly terminated markdown parsing. Possible reason: unclosed html tag?");
1162
+ }
1163
+ return _ref2 = {}, _defineProperty(_ref2, VAL_EXTENSION, "richtext"), _defineProperty(_ref2, "children", children), _ref2;
1164
+ }
1165
+
1166
+ export { COMMON_LEXICAL_PROPS, fromLexicalFormat, getMimeType, lexicalToRichTextSource, mimeTypeToFileExt, parseRichTextSource, richTextSourceToLexical, toLexicalFormat, toLexicalNode };