@valbuild/core 0.17.0 → 0.19.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.
Files changed (44) hide show
  1. package/dist/declarations/src/index.d.ts +4 -1
  2. package/dist/declarations/src/initSchema.d.ts +8 -2
  3. package/dist/declarations/src/patch/deref.d.ts +2 -1
  4. package/dist/declarations/src/patch/operation.d.ts +8 -0
  5. package/dist/declarations/src/schema/keyOf.d.ts +2 -2
  6. package/dist/declarations/src/schema/richtext.d.ts +7 -6
  7. package/dist/declarations/src/schema/string.d.ts +6 -3
  8. package/dist/declarations/src/selector/future/index.d.ts +3 -3
  9. package/dist/declarations/src/selector/index.d.ts +3 -3
  10. package/dist/declarations/src/source/future/remote.d.ts +2 -2
  11. package/dist/declarations/src/source/index.d.ts +3 -3
  12. package/dist/declarations/src/source/richtext.d.ts +70 -54
  13. package/dist/{index-3e3e839e.esm.js → index-5d1ab97c.esm.js} +1 -1
  14. package/dist/{index-369caccf.esm.js → index-bccf1907.esm.js} +1 -1
  15. package/dist/{ops-f3015423.cjs.dev.js → ops-2d7e1742.cjs.dev.js} +106 -33
  16. package/dist/{ops-23a5abb2.esm.js → ops-7ef32b0a.esm.js} +107 -35
  17. package/dist/{ops-0d09f8ee.cjs.prod.js → ops-ae089ab2.cjs.prod.js} +106 -33
  18. package/dist/valbuild-core.cjs.dev.js +311 -15
  19. package/dist/valbuild-core.cjs.prod.js +311 -15
  20. package/dist/valbuild-core.esm.js +294 -18
  21. package/expr/dist/valbuild-core-expr.esm.js +2 -2
  22. package/package.json +4 -1
  23. package/patch/dist/valbuild-core-patch.cjs.dev.js +6 -1
  24. package/patch/dist/valbuild-core-patch.cjs.prod.js +6 -1
  25. package/patch/dist/valbuild-core-patch.esm.js +8 -3
  26. package/src/getSha256.ts +8 -0
  27. package/src/index.ts +21 -5
  28. package/src/initSchema.ts +6 -0
  29. package/src/module.ts +33 -2
  30. package/src/patch/deref.ts +14 -1
  31. package/src/patch/operation.ts +10 -0
  32. package/src/patch/parse.ts +1 -0
  33. package/src/patch/patch.ts +3 -0
  34. package/src/schema/keyOf.ts +2 -2
  35. package/src/schema/richtext.ts +19 -73
  36. package/src/schema/string.ts +14 -4
  37. package/src/schema/validation.test.ts +2 -2
  38. package/src/selector/future/index.ts +8 -4
  39. package/src/selector/index.ts +4 -4
  40. package/src/source/future/remote.ts +2 -2
  41. package/src/source/index.ts +2 -2
  42. package/src/source/richtext.test.ts +178 -0
  43. package/src/source/richtext.ts +295 -89
  44. package/tsconfig.json +2 -1
@@ -3,10 +3,31 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var index = require('./index-8706c87e.cjs.prod.js');
6
- var ops = require('./ops-0d09f8ee.cjs.prod.js');
6
+ var ops = require('./ops-ae089ab2.cjs.prod.js');
7
+ var marked = require('marked');
7
8
  var expr_dist_valbuildCoreExpr = require('./index-601a7d73.cjs.prod.js');
8
9
  var result = require('./result-26f67b40.cjs.prod.js');
9
10
 
11
+ function _interopNamespace(e) {
12
+ if (e && e.__esModule) return e;
13
+ var n = Object.create(null);
14
+ if (e) {
15
+ Object.keys(e).forEach(function (k) {
16
+ if (k !== 'default') {
17
+ var d = Object.getOwnPropertyDescriptor(e, k);
18
+ Object.defineProperty(n, k, d.get ? d : {
19
+ enumerable: true,
20
+ get: function () { return e[k]; }
21
+ });
22
+ }
23
+ });
24
+ }
25
+ n["default"] = e;
26
+ return Object.freeze(n);
27
+ }
28
+
29
+ var marked__namespace = /*#__PURE__*/_interopNamespace(marked);
30
+
10
31
  var NumberSchema = /*#__PURE__*/function (_Schema) {
11
32
  index._inherits(NumberSchema, _Schema);
12
33
  var _super = index._createSuper(NumberSchema);
@@ -68,10 +89,12 @@ var StringSchema = /*#__PURE__*/function (_Schema) {
68
89
  function StringSchema(options) {
69
90
  var _this;
70
91
  var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
92
+ var isRaw = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
71
93
  index._classCallCheck(this, StringSchema);
72
94
  _this = _super.call(this);
73
95
  _this.options = options;
74
96
  _this.opt = opt;
97
+ _this.isRaw = isRaw;
75
98
  return _this;
76
99
  }
77
100
  index._createClass(StringSchema, [{
@@ -99,7 +122,12 @@ var StringSchema = /*#__PURE__*/function (_Schema) {
99
122
  }, {
100
123
  key: "optional",
101
124
  value: function optional() {
102
- return new StringSchema(this.options, true);
125
+ return new StringSchema(this.options, true, this.isRaw);
126
+ }
127
+ }, {
128
+ key: "raw",
129
+ value: function raw() {
130
+ return new StringSchema(this.options, this.opt, true);
103
131
  }
104
132
  }, {
105
133
  key: "serialize",
@@ -107,7 +135,8 @@ var StringSchema = /*#__PURE__*/function (_Schema) {
107
135
  return {
108
136
  type: "string",
109
137
  options: this.options,
110
- opt: this.opt
138
+ opt: this.opt,
139
+ raw: this.isRaw
111
140
  };
112
141
  }
113
142
  }]);
@@ -231,6 +260,115 @@ var literal = function literal(value) {
231
260
  return new LiteralSchema(value);
232
261
  };
233
262
 
263
+ var KeyOfSchema = /*#__PURE__*/function (_Schema) {
264
+ index._inherits(KeyOfSchema, _Schema);
265
+ var _super = index._createSuper(KeyOfSchema);
266
+ function KeyOfSchema(selector) {
267
+ var _this;
268
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
269
+ index._classCallCheck(this, KeyOfSchema);
270
+ _this = _super.call(this);
271
+ _this.selector = selector;
272
+ _this.opt = opt;
273
+ return _this;
274
+ }
275
+ index._createClass(KeyOfSchema, [{
276
+ key: "validate",
277
+ value: function validate(path, src) {
278
+ if (this.opt && (src === null || src === undefined)) {
279
+ return false;
280
+ }
281
+ var schema = this.selector[index.GetSchema];
282
+ if (!schema) {
283
+ return index._defineProperty({}, path, [{
284
+ message: "Schema not found for module. keyOf must be used with a Val Module"
285
+ }]);
286
+ }
287
+ var serializedSchema = schema.serialize();
288
+ if (!(serializedSchema.type === "array" || serializedSchema.type === "object" || serializedSchema.type === "record")) {
289
+ return index._defineProperty({}, path, [{
290
+ message: "Schema in keyOf must be an 'array', 'object' or 'record'. Found '".concat(serializedSchema.type, "'")
291
+ }]);
292
+ }
293
+ if (serializedSchema.opt && (src === null || src === undefined)) {
294
+ return false;
295
+ }
296
+ if (serializedSchema.type === "array" && typeof src !== "number") {
297
+ return index._defineProperty({}, path, [{
298
+ message: "Type of value in keyof (array) must be 'number'"
299
+ }]);
300
+ }
301
+ if (serializedSchema.type === "record" && typeof src !== "string") {
302
+ return index._defineProperty({}, path, [{
303
+ message: "Type of value in keyof (record) must be 'string'"
304
+ }]);
305
+ }
306
+ if (serializedSchema.type === "object") {
307
+ var keys = Object.keys(serializedSchema.items);
308
+ if (!keys.includes(src)) {
309
+ return index._defineProperty({}, path, [{
310
+ message: "Value of keyOf (object) must be: ".concat(keys.join(", "), ". Found: ").concat(src)
311
+ }]);
312
+ }
313
+ }
314
+ return false;
315
+ }
316
+ }, {
317
+ key: "assert",
318
+ value: function assert(src) {
319
+ if (this.opt && (src === null || src === undefined)) {
320
+ return true;
321
+ }
322
+ var schema = this.selector[index.GetSchema];
323
+ if (!schema) {
324
+ return false;
325
+ }
326
+ var serializedSchema = schema.serialize();
327
+ if (!(serializedSchema.type === "array" || serializedSchema.type === "object" || serializedSchema.type === "record")) {
328
+ return false;
329
+ }
330
+ if (serializedSchema.opt && (src === null || src === undefined)) {
331
+ return true;
332
+ }
333
+ if (serializedSchema.type === "array" && typeof src !== "number") {
334
+ return false;
335
+ }
336
+ if (serializedSchema.type === "record" && typeof src !== "string") {
337
+ return false;
338
+ }
339
+ if (serializedSchema.type === "object") {
340
+ var keys = Object.keys(serializedSchema.items);
341
+ if (!keys.includes(src)) {
342
+ return false;
343
+ }
344
+ }
345
+ return true;
346
+ }
347
+ }, {
348
+ key: "optional",
349
+ value: function optional() {
350
+ return new KeyOfSchema(this.selector, true);
351
+ }
352
+ }, {
353
+ key: "serialize",
354
+ value: function serialize() {
355
+ var path = index.getValPath(this.selector);
356
+ if (!path) {
357
+ throw new Error("Cannot serialize oneOf schema with empty selector. keyOf must be used with a Val Module.");
358
+ }
359
+ return {
360
+ type: "keyOf",
361
+ selector: path,
362
+ opt: this.opt
363
+ };
364
+ }
365
+ }]);
366
+ return KeyOfSchema;
367
+ }(index.Schema);
368
+ var keyOf = function keyOf(valModule) {
369
+ return new KeyOfSchema(valModule);
370
+ };
371
+
234
372
  // import type { F } from "ts-toolbelt";
235
373
  // import { i18n, I18n } from "./schema/future/i18n";
236
374
  // import { oneOf } from "./schema/future/oneOf";
@@ -249,23 +387,145 @@ function initSchema() {
249
387
  // oneOf,
250
388
  richtext: ops.richtext,
251
389
  image: index.image,
252
- literal: literal
390
+ literal: literal,
391
+ keyOf: keyOf,
392
+ record: ops.record
253
393
  // i18n: i18n(locales),
254
394
  };
255
395
  }
256
396
 
257
- function richtext(data) {
258
- if (typeof data === "string") {
259
- var _ref;
260
- return _ref = {}, index._defineProperty(_ref, index.VAL_EXTENSION, "richtext"), index._defineProperty(_ref, "type", "root"), index._defineProperty(_ref, "children", [{
261
- type: "paragraph",
262
- children: [{
263
- type: "text",
264
- text: data
265
- }]
266
- }]), _ref;
397
+ // export type FontFamily<O extends RichTextOptions> =
398
+ // O["fontFamily"] extends Record<string, unknown>
399
+ // ? `font-${keyof O["fontFamily"] & string}`
400
+ // : never;
401
+ // export type FontSize<O extends RichTextOptions> = O["fontSize"] extends Record<
402
+ // string,
403
+ // unknown
404
+ // >
405
+ // ? `text-${keyof O["fontSize"] & string}`
406
+ // : never;
407
+ // | FontFamily<O>
408
+ // | FontSize<O>;
409
+ // export type AnchorNode<O extends RichTextOptions> = never; // TODO:
410
+ // O["link"] extends true
411
+ // ? {
412
+ // tag: "a";
413
+ // href: string;
414
+ // children: [string];
415
+ // }
416
+ // : never;
417
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
418
+ // export type BlockQuoteNode<O extends RichTextOptions> =
419
+ // O["blockQuote"] extends true
420
+ // ? { tag: "blockquote"; children: [string] }
421
+ // : never;
422
+ // TODO: rename to RichTextSelector?
423
+ function parseTokens(tokens) {
424
+ return tokens.flatMap(function (token) {
425
+ if (token.type === "heading") {
426
+ return [{
427
+ tag: "h".concat(token.depth),
428
+ children: parseTokens(token.tokens ? token.tokens : [])
429
+ }];
430
+ }
431
+ if (token.type === "paragraph") {
432
+ return [{
433
+ tag: "p",
434
+ children: parseTokens(token.tokens ? token.tokens : [])
435
+ }];
436
+ }
437
+ if (token.type === "strong") {
438
+ return [{
439
+ tag: "span",
440
+ classes: ["bold"],
441
+ children: parseTokens(token.tokens ? token.tokens : [])
442
+ }];
443
+ }
444
+ if (token.type === "em") {
445
+ return [{
446
+ tag: "span",
447
+ classes: ["italic"],
448
+ children: parseTokens(token.tokens ? token.tokens : [])
449
+ }];
450
+ }
451
+ if (token.type === "del") {
452
+ return [{
453
+ tag: "span",
454
+ classes: ["line-through"],
455
+ children: parseTokens(token.tokens ? token.tokens : [])
456
+ }];
457
+ }
458
+ if (token.type === "text") {
459
+ return [token.text];
460
+ }
461
+ if (token.type === "list") {
462
+ return [{
463
+ tag: token.ordered ? "ol" : "ul",
464
+ children: parseTokens(token.items)
465
+ }];
466
+ }
467
+ if (token.type === "list_item") {
468
+ return [{
469
+ tag: "li",
470
+ children: parseTokens(token.tokens ? token.tokens : [])
471
+ }];
472
+ }
473
+ if (token.type === "space") {
474
+ return [];
475
+ }
476
+ if (token.type === "code") {
477
+ return [{
478
+ tag: "span",
479
+ classes: [],
480
+ children: [token.text]
481
+ }];
482
+ }
483
+ console.error("Could not parse markdown: unsupported token type: ".concat(token.type, ". Found: ").concat(token.raw));
484
+ return [token.raw];
485
+ });
486
+ }
487
+
488
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
489
+ function nodeToTag(node) {
490
+ if (node[index.VAL_EXTENSION] === "file") {
491
+ return node;
267
492
  }
268
- return index._objectSpread2(index._objectSpread2({}, data), {}, index._defineProperty({}, index.VAL_EXTENSION, "richtext"));
493
+ throw Error("Unexpected node: ".concat(JSON.stringify(node)));
494
+ }
495
+ function imgSrcToImgTag(imageSrc) {
496
+ var _imageSrc$metadata, _imageSrc$metadata2;
497
+ var converted = index.convertFileSource(imageSrc);
498
+ return {
499
+ tag: "img",
500
+ src: converted.url,
501
+ width: (_imageSrc$metadata = imageSrc.metadata) === null || _imageSrc$metadata === void 0 ? void 0 : _imageSrc$metadata.width,
502
+ height: (_imageSrc$metadata2 = imageSrc.metadata) === null || _imageSrc$metadata2 === void 0 ? void 0 : _imageSrc$metadata2.height
503
+ };
504
+ }
505
+ function convertRichTextSource(src) {
506
+ var _ref;
507
+ return _ref = {}, index._defineProperty(_ref, index.VAL_EXTENSION, "richtext"), index._defineProperty(_ref, "children", src.children.map(function (source) {
508
+ if (index.VAL_EXTENSION in source && source[index.VAL_EXTENSION] === "file") {
509
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
510
+ return imgSrcToImgTag(source);
511
+ }
512
+ return source;
513
+ })), _ref;
514
+ }
515
+ function richtext(templateStrings) {
516
+ var _ref2;
517
+ for (var _len = arguments.length, expr = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
518
+ expr[_key - 1] = arguments[_key];
519
+ }
520
+ return _ref2 = {}, index._defineProperty(_ref2, index.VAL_EXTENSION, "richtext"), index._defineProperty(_ref2, "children", templateStrings.flatMap(function (templateString, i) {
521
+ var lex = marked__namespace.lexer(templateString, {
522
+ gfm: true
523
+ });
524
+ if (expr[i]) {
525
+ return parseTokens(lex).concat(nodeToTag(expr[i]));
526
+ }
527
+ return parseTokens(lex);
528
+ })), _ref2;
269
529
  }
270
530
 
271
531
  // import { i18n, I18n } from "./source/future/i18n";
@@ -403,6 +663,14 @@ function derefPatch(patch, document, ops$1) {
403
663
  } else {
404
664
  dereferencedPatch.push(op);
405
665
  }
666
+ } else if (op.op === "file") {
667
+ if (op.path[0] !== "public") {
668
+ return result.err(new ops.PatchError("Path must start with public"));
669
+ }
670
+ if (typeof op.value !== "string") {
671
+ return result.err(new ops.PatchError("File operation must have a value that is typeof string. Found: ".concat(index._typeof(op.value))));
672
+ }
673
+ fileUpdates["/".concat(op.path.join("/"))] = op.value;
406
674
  } else {
407
675
  var _maybeDerefRes = derefPath(op.path);
408
676
  if (result.isErr(_maybeDerefRes)) {
@@ -896,6 +1164,32 @@ function _asyncToGenerator(fn) {
896
1164
  };
897
1165
  }
898
1166
 
1167
+ var getSHA256Hash = /*#__PURE__*/function () {
1168
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(bits) {
1169
+ var hashBuffer, hashArray, hash;
1170
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1171
+ while (1) switch (_context.prev = _context.next) {
1172
+ case 0:
1173
+ _context.next = 2;
1174
+ return crypto.subtle.digest("SHA-256", bits);
1175
+ case 2:
1176
+ hashBuffer = _context.sent;
1177
+ hashArray = Array.from(new Uint8Array(hashBuffer));
1178
+ hash = hashArray.map(function (item) {
1179
+ return item.toString(16).padStart(2, "0");
1180
+ }).join("");
1181
+ return _context.abrupt("return", hash);
1182
+ case 6:
1183
+ case "end":
1184
+ return _context.stop();
1185
+ }
1186
+ }, _callee);
1187
+ }));
1188
+ return function getSHA256Hash(_x) {
1189
+ return _ref.apply(this, arguments);
1190
+ };
1191
+ }();
1192
+
899
1193
  // TODO: move this to internal, only reason this is here is that react, ui and server all depend on it
900
1194
  var ValApi = /*#__PURE__*/function () {
901
1195
  function ValApi(host) {
@@ -1005,6 +1299,7 @@ function _parse() {
1005
1299
 
1006
1300
  var Internal = {
1007
1301
  convertFileSource: index.convertFileSource,
1302
+ convertRichTextSource: convertRichTextSource,
1008
1303
  getSchema: index.getSchema,
1009
1304
  getValPath: index.getValPath,
1010
1305
  getVal: getVal,
@@ -1013,6 +1308,7 @@ var Internal = {
1013
1308
  splitModuleIdAndModulePath: ops.splitModuleIdAndModulePath,
1014
1309
  isVal: index.isVal,
1015
1310
  createValPathOfItem: ops.createValPathOfItem,
1311
+ getSHA256Hash: getSHA256Hash,
1016
1312
  createPatchJSONPath: function createPatchJSONPath(modulePath) {
1017
1313
  return "/".concat(modulePath.split(".").map(function (segment) {
1018
1314
  return JSON.parse(segment);