@valbuild/core 0.18.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 (41) hide show
  1. package/dist/declarations/src/index.d.ts +4 -1
  2. package/dist/declarations/src/initSchema.d.ts +2 -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/richtext.d.ts +7 -6
  6. package/dist/declarations/src/schema/string.d.ts +6 -3
  7. package/dist/declarations/src/selector/future/index.d.ts +3 -3
  8. package/dist/declarations/src/selector/index.d.ts +3 -3
  9. package/dist/declarations/src/source/future/remote.d.ts +2 -2
  10. package/dist/declarations/src/source/index.d.ts +3 -3
  11. package/dist/declarations/src/source/richtext.d.ts +70 -54
  12. package/dist/{index-4abf3a1f.esm.js → index-5d1ab97c.esm.js} +1 -1
  13. package/dist/{index-a9235737.esm.js → index-bccf1907.esm.js} +1 -1
  14. package/dist/{ops-f3015423.cjs.dev.js → ops-2d7e1742.cjs.dev.js} +106 -33
  15. package/dist/{ops-a2a295f8.esm.js → ops-7ef32b0a.esm.js} +107 -35
  16. package/dist/{ops-0d09f8ee.cjs.prod.js → ops-ae089ab2.cjs.prod.js} +106 -33
  17. package/dist/valbuild-core.cjs.dev.js +200 -104
  18. package/dist/valbuild-core.cjs.prod.js +200 -104
  19. package/dist/valbuild-core.esm.js +182 -106
  20. package/expr/dist/valbuild-core-expr.esm.js +2 -2
  21. package/package.json +4 -1
  22. package/patch/dist/valbuild-core-patch.cjs.dev.js +6 -1
  23. package/patch/dist/valbuild-core-patch.cjs.prod.js +6 -1
  24. package/patch/dist/valbuild-core-patch.esm.js +8 -3
  25. package/src/getSha256.ts +8 -0
  26. package/src/index.ts +21 -5
  27. package/src/module.ts +33 -2
  28. package/src/patch/deref.ts +14 -1
  29. package/src/patch/operation.ts +10 -0
  30. package/src/patch/parse.ts +1 -0
  31. package/src/patch/patch.ts +3 -0
  32. package/src/schema/richtext.ts +19 -73
  33. package/src/schema/string.ts +14 -4
  34. package/src/schema/validation.test.ts +2 -2
  35. package/src/selector/future/index.ts +8 -4
  36. package/src/selector/index.ts +4 -4
  37. package/src/source/future/remote.ts +2 -2
  38. package/src/source/index.ts +2 -2
  39. package/src/source/richtext.test.ts +178 -0
  40. package/src/source/richtext.ts +295 -89
  41. 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-a6e642dd.cjs.dev.js');
6
- var ops = require('./ops-f3015423.cjs.dev.js');
6
+ var ops = require('./ops-2d7e1742.cjs.dev.js');
7
+ var marked = require('marked');
7
8
  var expr_dist_valbuildCoreExpr = require('./index-486c7fbf.cjs.dev.js');
8
9
  var result = require('./result-48320acd.cjs.dev.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
  }]);
@@ -340,95 +369,6 @@ var keyOf = function keyOf(valModule) {
340
369
  return new KeyOfSchema(valModule);
341
370
  };
342
371
 
343
- var RecordSchema = /*#__PURE__*/function (_Schema) {
344
- index._inherits(RecordSchema, _Schema);
345
- var _super = index._createSuper(RecordSchema);
346
- function RecordSchema(item) {
347
- var _this;
348
- var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
349
- index._classCallCheck(this, RecordSchema);
350
- _this = _super.call(this);
351
- _this.item = item;
352
- _this.opt = opt;
353
- return _this;
354
- }
355
- index._createClass(RecordSchema, [{
356
- key: "validate",
357
- value: function validate(path, src) {
358
- var _this2 = this;
359
- var error = false;
360
- if (this.opt && (src === null || src === undefined)) {
361
- return false;
362
- }
363
- if (index._typeof(src) !== "object") {
364
- return index._defineProperty({}, path, [{
365
- message: "Expected 'object', got '".concat(index._typeof(src), "'")
366
- }]);
367
- }
368
- if (Array.isArray(src)) {
369
- return index._defineProperty({}, path, [{
370
- message: "Expected 'object', got 'array'"
371
- }]);
372
- }
373
- Object.entries(src).forEach(function (_ref3) {
374
- var _ref4 = index._slicedToArray(_ref3, 2),
375
- key = _ref4[0],
376
- elem = _ref4[1];
377
- var subPath = ops.createValPathOfItem(path, key);
378
- if (!subPath) {
379
- error = _this2.appendValidationError(error, path, "Internal error: could not create path at ".concat(!path && typeof path === "string" ? "<empty string>" : path, " at key ").concat(elem),
380
- // Should! never happen
381
- src);
382
- } else {
383
- var subError = _this2.item.validate(subPath, elem);
384
- if (subError && error) {
385
- error = index._objectSpread2(index._objectSpread2({}, subError), error);
386
- } else if (subError) {
387
- error = subError;
388
- }
389
- }
390
- });
391
- return error;
392
- }
393
- }, {
394
- key: "assert",
395
- value: function assert(src) {
396
- if (this.opt && (src === null || src === undefined)) {
397
- return true;
398
- }
399
- if (!src) {
400
- return false;
401
- }
402
- for (var _i = 0, _Object$entries = Object.entries(src); _i < _Object$entries.length; _i++) {
403
- var _Object$entries$_i = index._slicedToArray(_Object$entries[_i], 2),
404
- _item = _Object$entries$_i[1];
405
- if (!this.item.assert(_item)) {
406
- return false;
407
- }
408
- }
409
- return index._typeof(src) === "object" && !Array.isArray(src);
410
- }
411
- }, {
412
- key: "optional",
413
- value: function optional() {
414
- return new RecordSchema(this.item, true);
415
- }
416
- }, {
417
- key: "serialize",
418
- value: function serialize() {
419
- return {
420
- type: "record",
421
- item: this.item.serialize(),
422
- opt: this.opt
423
- };
424
- }
425
- }]);
426
- return RecordSchema;
427
- }(index.Schema);
428
- var record = function record(schema) {
429
- return new RecordSchema(schema);
430
- };
431
-
432
372
  // import type { F } from "ts-toolbelt";
433
373
  // import { i18n, I18n } from "./schema/future/i18n";
434
374
  // import { oneOf } from "./schema/future/oneOf";
@@ -449,23 +389,143 @@ function initSchema() {
449
389
  image: index.image,
450
390
  literal: literal,
451
391
  keyOf: keyOf,
452
- record: record
392
+ record: ops.record
453
393
  // i18n: i18n(locales),
454
394
  };
455
395
  }
456
396
 
457
- function richtext(data) {
458
- if (typeof data === "string") {
459
- var _ref;
460
- return _ref = {}, index._defineProperty(_ref, index.VAL_EXTENSION, "richtext"), index._defineProperty(_ref, "type", "root"), index._defineProperty(_ref, "children", [{
461
- type: "paragraph",
462
- children: [{
463
- type: "text",
464
- text: data
465
- }]
466
- }]), _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;
492
+ }
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];
467
519
  }
468
- return index._objectSpread2(index._objectSpread2({}, data), {}, index._defineProperty({}, index.VAL_EXTENSION, "richtext"));
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;
469
529
  }
470
530
 
471
531
  // import { i18n, I18n } from "./source/future/i18n";
@@ -603,6 +663,14 @@ function derefPatch(patch, document, ops$1) {
603
663
  } else {
604
664
  dereferencedPatch.push(op);
605
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;
606
674
  } else {
607
675
  var _maybeDerefRes = derefPath(op.path);
608
676
  if (result.isErr(_maybeDerefRes)) {
@@ -1096,6 +1164,32 @@ function _asyncToGenerator(fn) {
1096
1164
  };
1097
1165
  }
1098
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
+
1099
1193
  // TODO: move this to internal, only reason this is here is that react, ui and server all depend on it
1100
1194
  var ValApi = /*#__PURE__*/function () {
1101
1195
  function ValApi(host) {
@@ -1205,6 +1299,7 @@ function _parse() {
1205
1299
 
1206
1300
  var Internal = {
1207
1301
  convertFileSource: index.convertFileSource,
1302
+ convertRichTextSource: convertRichTextSource,
1208
1303
  getSchema: index.getSchema,
1209
1304
  getValPath: index.getValPath,
1210
1305
  getVal: getVal,
@@ -1213,6 +1308,7 @@ var Internal = {
1213
1308
  splitModuleIdAndModulePath: ops.splitModuleIdAndModulePath,
1214
1309
  isVal: index.isVal,
1215
1310
  createValPathOfItem: ops.createValPathOfItem,
1311
+ getSHA256Hash: getSHA256Hash,
1216
1312
  createPatchJSONPath: function createPatchJSONPath(modulePath) {
1217
1313
  return "/".concat(modulePath.split(".").map(function (segment) {
1218
1314
  return JSON.parse(segment);