@valbuild/shared 0.62.6 → 0.63.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.
@@ -3,30 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var core = require('@valbuild/core');
6
- var marked = require('marked');
7
6
  var zod = require('zod');
8
7
  var fp = require('@valbuild/core/fp');
9
8
 
10
- function _interopNamespace(e) {
11
- if (e && e.__esModule) return e;
12
- var n = Object.create(null);
13
- if (e) {
14
- Object.keys(e).forEach(function (k) {
15
- if (k !== 'default') {
16
- var d = Object.getOwnPropertyDescriptor(e, k);
17
- Object.defineProperty(n, k, d.get ? d : {
18
- enumerable: true,
19
- get: function () { return e[k]; }
20
- });
21
- }
22
- });
23
- }
24
- n["default"] = e;
25
- return Object.freeze(n);
26
- }
27
-
28
- var marked__namespace = /*#__PURE__*/_interopNamespace(marked);
29
-
30
9
  function toPrimitive(t, r) {
31
10
  if ("object" != typeof t || !t) return t;
32
11
  var e = t[Symbol.toPrimitive];
@@ -58,6 +37,28 @@ function _defineProperty(obj, key, value) {
58
37
  return obj;
59
38
  }
60
39
 
40
+ function ownKeys(e, r) {
41
+ var t = Object.keys(e);
42
+ if (Object.getOwnPropertySymbols) {
43
+ var o = Object.getOwnPropertySymbols(e);
44
+ r && (o = o.filter(function (r) {
45
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
46
+ })), t.push.apply(t, o);
47
+ }
48
+ return t;
49
+ }
50
+ function _objectSpread2(e) {
51
+ for (var r = 1; r < arguments.length; r++) {
52
+ var t = null != arguments[r] ? arguments[r] : {};
53
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
54
+ _defineProperty(e, r, t[r]);
55
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
56
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
57
+ });
58
+ }
59
+ return e;
60
+ }
61
+
61
62
  function _arrayLikeToArray(arr, len) {
62
63
  if (len == null || len > arr.length) len = arr.length;
63
64
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
@@ -125,22 +126,6 @@ function _createForOfIteratorHelper(o, allowArrayLike) {
125
126
  };
126
127
  }
127
128
 
128
- function _arrayWithoutHoles(arr) {
129
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
130
- }
131
-
132
- function _iterableToArray(iter) {
133
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
134
- }
135
-
136
- function _nonIterableSpread() {
137
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
138
- }
139
-
140
- function _toConsumableArray(arr) {
141
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
142
- }
143
-
144
129
  function _arrayWithHoles(arr) {
145
130
  if (Array.isArray(arr)) return arr;
146
131
  }
@@ -905,184 +890,183 @@ function filenameToMimeType(filename) {
905
890
 
906
891
  function remirrorToRichTextSource(node) {
907
892
  var files = {};
908
- var markdownIRBlocks = node.content.map(function (child) {
909
- return createBlock(child, files);
910
- });
911
- return fromIRToRichTextSource(markdownIRBlocks, files);
893
+ var blocks = [];
894
+ var i = 0;
895
+ var _iterator = _createForOfIteratorHelper(node.content),
896
+ _step;
897
+ try {
898
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
899
+ var child = _step.value;
900
+ var block = convertBlock([(i++).toString()], child, files);
901
+ blocks.push(block);
902
+ }
903
+ } catch (err) {
904
+ _iterator.e(err);
905
+ } finally {
906
+ _iterator.f();
907
+ }
908
+ return {
909
+ blocks: blocks,
910
+ files: files
911
+ };
912
912
  }
913
- function createBlock(node, files) {
913
+ function convertBlock(path, node, files) {
914
914
  if (node.type === "heading") {
915
915
  var _node$attrs, _node$content;
916
- var headingTag = "";
917
916
  var depth = ((_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.level) || 1;
918
917
  if (Number.isNaN(depth)) {
919
918
  throw new Error("Invalid header depth");
920
919
  }
921
- for (var i = 0; i < Number(depth); i++) {
922
- headingTag += "#";
923
- }
924
- var headingText = ["".concat(headingTag, " ")];
925
920
  return {
926
- type: "block",
927
- children: headingText.concat.apply(headingText, _toConsumableArray(((_node$content = node.content) === null || _node$content === void 0 ? void 0 : _node$content.map(function (child) {
928
- return transformLeafNode(child, files);
929
- })) || []))
930
- };
931
- } else if (node.type === "paragraph") {
932
- var _node$content2;
933
- if (!node.content || ((_node$content2 = node.content) === null || _node$content2 === void 0 ? void 0 : _node$content2.length) === 0) {
934
- return {
935
- type: "block",
936
- children: ["<br />"]
937
- };
938
- }
939
- return {
940
- type: "block",
941
- children: node.content.map(function (child) {
942
- return transformLeafNode(child, files);
943
- })
944
- };
945
- } else if (node.type === "bulletList" || node.type === "orderedList") {
946
- var _node$content3;
947
- return {
948
- type: "block",
949
- children: ((_node$content3 = node.content) === null || _node$content3 === void 0 ? void 0 : _node$content3.flatMap(function (child, i) {
950
- return formatListItemNode(getListPrefix(node), child, 0, files, i === 0);
921
+ tag: "h".concat(depth),
922
+ children: ((_node$content = node.content) === null || _node$content === void 0 ? void 0 : _node$content.map(function (child, i) {
923
+ return convertHeadingChild(path.concat("children", i.toString()), child, files);
951
924
  })) || []
952
925
  };
926
+ } else if (node.type === "paragraph") {
927
+ return convertParagraph(path, node, files);
928
+ } else if (node.type === "bulletList") {
929
+ return convertBulletList(path, node, files);
930
+ } else if (node.type === "orderedList") {
931
+ return convertOrderedList(path, node, files);
953
932
  } else {
954
933
  var exhaustiveCheck = node;
955
934
  throw new Error("Unhandled node type: ".concat("type" in exhaustiveCheck ? "exhaustiveCheck.type" : "unknown"));
956
935
  }
957
936
  }
958
- function fromIRToRichTextSource(markdownIRBlocks, files) {
959
- var templateStrings = ["\n"];
960
- var exprs = [];
961
- for (var blockIdx = 0; blockIdx < markdownIRBlocks.length; blockIdx++) {
962
- var block = markdownIRBlocks[blockIdx];
963
- var _iterator = _createForOfIteratorHelper(block.children),
964
- _step;
965
- try {
966
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
967
- var child = _step.value;
968
- if (typeof child === "string") {
969
- templateStrings[templateStrings.length - 1] += child;
970
- } else {
971
- if (child._type === "file") {
972
- exprs.push(child);
973
- } else if (child._type === "link") {
974
- exprs.push(child);
975
- } else {
976
- var exhaustiveCheck = child;
977
- throw new Error("Unexpected node type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
978
- }
979
- templateStrings.push("");
980
- }
981
- }
982
- } catch (err) {
983
- _iterator.e(err);
984
- } finally {
985
- _iterator.f();
986
- }
987
- if (blockIdx === markdownIRBlocks.length - 1) {
988
- templateStrings[templateStrings.length - 1] += "\n";
989
- } else {
990
- templateStrings[templateStrings.length - 1] += "\n\n";
991
- }
937
+ function convertHeadingChild(path, node, files) {
938
+ if (node.type === "text") {
939
+ return convertTextNode(node);
940
+ } else if (node.type === "hardBreak") {
941
+ return {
942
+ tag: "br"
943
+ };
944
+ } else if (node.type === "image") {
945
+ return convertImageNode(path, node, files);
946
+ } else {
947
+ var exhaustiveCheck = node;
948
+ throw new Error("Unexpected heading child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
992
949
  }
993
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "richtext"), "templateStrings", templateStrings), "exprs", exprs), "files", files);
994
950
  }
995
- function formatText(node) {
996
- var _node$marks;
997
- var classes = ((_node$marks = node.marks) === null || _node$marks === void 0 ? void 0 : _node$marks.flatMap(function (mark) {
998
- if (mark.type === "bold") {
999
- return ["bold"];
1000
- } else if (mark.type === "italic") {
1001
- return ["italic"];
1002
- } else if (mark.type === "strike") {
1003
- return ["line-through"];
1004
- }
1005
- return [];
1006
- })) || [];
1007
- var text = node.text.trimStart();
1008
- var prefixWS = node.text.length - text.length;
1009
- text = text.trimEnd();
1010
- var suffixWS = node.text.length - text.length - prefixWS;
1011
- if (classes.includes("bold") && classes.includes("italic")) {
1012
- text = "***".concat(text, "***");
1013
- } else if (classes.includes("bold")) {
1014
- text = "**".concat(text, "**");
1015
- } else if (classes.includes("italic")) {
1016
- text = "_".concat(text, "_");
1017
- }
1018
- if (classes.includes("line-through")) {
1019
- text = "~~".concat(text, "~~");
1020
- }
1021
- // TODO:
1022
- // text = splitIntoChunks(text);
1023
- return "".concat(" ".repeat(prefixWS)).concat(text).concat(" ".repeat(suffixWS));
951
+ function convertParagraph(path, child, files) {
952
+ var _child$content;
953
+ return {
954
+ tag: "p",
955
+ children: ((_child$content = child.content) === null || _child$content === void 0 ? void 0 : _child$content.map(function (child, i) {
956
+ if (child.type === "text") {
957
+ return convertTextNode(child);
958
+ } else if (child.type === "hardBreak") {
959
+ return {
960
+ tag: "br"
961
+ };
962
+ } else if (child.type === "image") {
963
+ return convertImageNode(path.concat("children", i.toString()), child, files);
964
+ }
965
+ var exhaustiveCheck = child;
966
+ throw new Error("Unexpected paragraph child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
967
+ })) || []
968
+ };
1024
969
  }
1025
- function transformLeafNode(node, files) {
970
+ function convertTextNode(node) {
1026
971
  if (node.type === "text") {
1027
- var _node$marks2;
1028
- var linkMark = (_node$marks2 = node.marks) === null || _node$marks2 === void 0 ? void 0 : _node$marks2.find(function (mark) {
972
+ var _node$marks, _node$marks2;
973
+ var styles = ((_node$marks = node.marks) === null || _node$marks === void 0 ? void 0 : _node$marks.flatMap(function (mark) {
974
+ if (mark.type !== "link") {
975
+ if (mark.type === "strike") {
976
+ return ["line-through"];
977
+ }
978
+ return [mark.type];
979
+ } else {
980
+ return [];
981
+ }
982
+ })) || [];
983
+ if ((_node$marks2 = node.marks) !== null && _node$marks2 !== void 0 && _node$marks2.some(function (mark) {
1029
984
  return mark.type === "link";
1030
- });
1031
- if ((linkMark === null || linkMark === void 0 ? void 0 : linkMark.type) === "link") {
985
+ })) {
986
+ var _node$marks$find2;
987
+ if (styles.length > 0) {
988
+ var _node$marks$find;
989
+ return {
990
+ tag: "a",
991
+ href: ((_node$marks$find = node.marks.find(function (mark) {
992
+ return mark.type === "link";
993
+ })) === null || _node$marks$find === void 0 ? void 0 : _node$marks$find.attrs.href) || "",
994
+ children: [{
995
+ tag: "span",
996
+ styles: styles,
997
+ children: [node.text]
998
+ }]
999
+ };
1000
+ }
1032
1001
  return {
1033
- _type: "link",
1034
- href: linkMark.attrs.href,
1035
- children: [formatText(node)]
1002
+ tag: "a",
1003
+ href: ((_node$marks$find2 = node.marks.find(function (mark) {
1004
+ return mark.type === "link";
1005
+ })) === null || _node$marks$find2 === void 0 ? void 0 : _node$marks$find2.attrs.href) || "",
1006
+ children: [node.text]
1036
1007
  };
1037
1008
  }
1038
- return formatText(node);
1039
- } else if (node.type === "hardBreak") {
1040
- return "<br />";
1041
- } else if (node.type === "image") {
1042
- return fromRemirrorImageNode(node, files);
1009
+ if (styles.length > 0) {
1010
+ return {
1011
+ tag: "span",
1012
+ styles: styles,
1013
+ children: [node.text]
1014
+ };
1015
+ }
1016
+ return node.text;
1043
1017
  } else {
1044
- var exhaustiveCheck = node;
1045
- throw new Error("Unexpected node type: ".concat(JSON.stringify(exhaustiveCheck)));
1018
+ var exhaustiveCheck = node.type;
1019
+ throw new Error("Unexpected node type: ".concat(exhaustiveCheck));
1046
1020
  }
1047
1021
  }
1048
- function formatListItemNode(listPrefix, node, indent, files) {
1049
- var _node$content4, _node$content5;
1050
- var isFirstTopLevelListItem = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
1051
- var newLine = isFirstTopLevelListItem ? "" : "\n";
1052
- var prefix = ["".concat(newLine).concat(" ".repeat(indent)).concat(listPrefix)];
1053
- if (!(((_node$content4 = node.content) === null || _node$content4 === void 0 || (_node$content4 = _node$content4[0]) === null || _node$content4 === void 0 ? void 0 : _node$content4.type) === "bulletList" || ((_node$content5 = node.content) === null || _node$content5 === void 0 || (_node$content5 = _node$content5[0]) === null || _node$content5 === void 0 ? void 0 : _node$content5.type) === "orderedList")) {
1054
- prefix.push(" ");
1055
- }
1056
- return prefix.concat((node.content || []).flatMap(function (child, i) {
1057
- if (child.type === "bulletList" || child.type === "orderedList") {
1058
- return (child.content || []).flatMap(function (subChild) {
1059
- return formatListItemNode(getListPrefix(child), subChild, indent + 4, files);
1060
- });
1061
- } else {
1062
- return (child.content || []).flatMap(function (subChild) {
1063
- var _child$content, _child$content2;
1064
- var res = [];
1065
- if ((_child$content = child.content) !== null && _child$content !== void 0 && _child$content.length && ((_child$content2 = child.content) === null || _child$content2 === void 0 ? void 0 : _child$content2.length) > 0 && i > 0) {
1066
- res.push("<br />\n");
1067
- }
1068
- res.push(transformLeafNode(subChild, files));
1069
- return res;
1070
- });
1071
- }
1072
- }));
1022
+ function convertListItem(path, child, files) {
1023
+ var _child$content2;
1024
+ return {
1025
+ tag: "li",
1026
+ children: ((_child$content2 = child.content) === null || _child$content2 === void 0 ? void 0 : _child$content2.map(function (child, i) {
1027
+ if (child.type === "paragraph") {
1028
+ return convertParagraph(path.concat("children", i.toString()), child, files);
1029
+ } else if (child.type === "bulletList") {
1030
+ return convertBulletList(path.concat("children", i.toString()), child, files);
1031
+ } else if (child.type === "orderedList") {
1032
+ return convertOrderedList(path.concat("children", i.toString()), child, files);
1033
+ } else {
1034
+ var exhaustiveCheck = child;
1035
+ throw new Error("Unexpected list child type: ".concat(exhaustiveCheck));
1036
+ }
1037
+ })) || []
1038
+ };
1073
1039
  }
1074
- function getListPrefix(node) {
1075
- if (node.type === "bulletList") {
1076
- return "-";
1077
- } else if (node.type === "orderedList") {
1078
- return "1.";
1079
- } else {
1080
- var exhaustiveCheck = node;
1081
- throw new Error("Unhandled list node: ".concat(JSON.stringify(exhaustiveCheck)));
1082
- }
1040
+ function convertBulletList(path, node, files) {
1041
+ var _node$content2;
1042
+ return {
1043
+ tag: "ul",
1044
+ children: ((_node$content2 = node.content) === null || _node$content2 === void 0 ? void 0 : _node$content2.map(function (child, i) {
1045
+ if (child.type === "listItem") {
1046
+ return convertListItem(path.concat("children", i.toString()), child, files);
1047
+ } else {
1048
+ var exhaustiveCheck = child.type;
1049
+ throw new Error("Unexpected bullet list child type: ".concat(exhaustiveCheck));
1050
+ }
1051
+ })) || []
1052
+ };
1053
+ }
1054
+ function convertOrderedList(path, node, files) {
1055
+ var _node$content3;
1056
+ return {
1057
+ tag: "ol",
1058
+ children: ((_node$content3 = node.content) === null || _node$content3 === void 0 ? void 0 : _node$content3.map(function (child, i) {
1059
+ if (child.type === "listItem") {
1060
+ return convertListItem(path.concat("children", i.toString()), child, files);
1061
+ } else {
1062
+ var exhaustiveCheck = child.type;
1063
+ throw new Error("Unexpected ordered list child type: ".concat(exhaustiveCheck));
1064
+ }
1065
+ })) || []
1066
+ };
1083
1067
  }
1084
1068
  var textEncoder = new TextEncoder();
1085
- function fromRemirrorImageNode(node, files) {
1069
+ function convertImageNode(path, node, files) {
1086
1070
  if (node.attrs && node.attrs.src.startsWith("data:")) {
1087
1071
  var sha256 = core.Internal.getSHA256Hash(textEncoder.encode(node.attrs.src));
1088
1072
  var mimeType = getMimeType(node.attrs.src);
@@ -1092,22 +1076,43 @@ function fromRemirrorImageNode(node, files) {
1092
1076
  var fileExt = mimeTypeToFileExt(mimeType);
1093
1077
  var fileName = node.attrs.fileName || "".concat(sha256, ".").concat(fileExt);
1094
1078
  var filePath = "/public/".concat(fileName);
1095
- files[filePath] = node.attrs.src;
1096
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_PROP, filePath), core.FILE_REF_SUBTYPE_TAG, core.RT_IMAGE_TAG), "metadata", {
1097
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1098
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1099
- sha256: sha256 || "",
1100
- mimeType: mimeType
1101
- });
1079
+ var existingFilesEntry = files[filePath];
1080
+ var thisPath = path
1081
+ // file is added as src (see below):
1082
+ .concat("src");
1083
+ if (existingFilesEntry) {
1084
+ existingFilesEntry.patchPaths.push(thisPath);
1085
+ } else {
1086
+ files[filePath] = {
1087
+ value: node.attrs.src,
1088
+ patchPaths: [thisPath]
1089
+ };
1090
+ }
1091
+ return {
1092
+ tag: "img",
1093
+ src: _defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_PROP, filePath), "metadata", {
1094
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1095
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1096
+ sha256: sha256 || "",
1097
+ mimeType: mimeType
1098
+ })
1099
+ };
1102
1100
  } else if (node.attrs) {
1103
1101
  var _sha = getParam("sha256", node.attrs.src);
1102
+ var patchId = getParam("patch_id", node.attrs.src);
1104
1103
  var noParamsSrc = node.attrs.src.split("?")[0];
1105
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_PROP, "/public".concat(node.attrs.src.split("?")[0])), core.FILE_REF_SUBTYPE_TAG, core.RT_IMAGE_TAG), "metadata", {
1106
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1107
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1108
- sha256: _sha || "",
1109
- mimeType: noParamsSrc && filenameToMimeType(noParamsSrc) || ""
1110
- });
1104
+ var tag = {
1105
+ tag: "img",
1106
+ src: _objectSpread2(_defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_PROP, "/public".concat(node.attrs.src.split("?")[0])), "metadata", {
1107
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1108
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1109
+ sha256: _sha || "",
1110
+ mimeType: noParamsSrc && filenameToMimeType(noParamsSrc) || ""
1111
+ }), patchId ? {
1112
+ patch_id: patchId
1113
+ } : {})
1114
+ };
1115
+ return tag;
1111
1116
  } else {
1112
1117
  throw new Error("Invalid image node (no attrs): " + JSON.stringify(node));
1113
1118
  }
@@ -1125,188 +1130,6 @@ function getParam(param, url) {
1125
1130
  return undefined;
1126
1131
  }
1127
1132
 
1128
- var VAL_START_TAG_PREFIX = '<val value="';
1129
- var VAL_START_TAG_SUFFIX = '">';
1130
- var VAL_END_TAG = "</val>";
1131
- function parseTokens(tokens, sourceNodes, cursor) {
1132
- var insideList = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
1133
- var children = [];
1134
- function merge(token, clazz) {
1135
- var parsedTokens = parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0);
1136
- children.push({
1137
- tag: "span",
1138
- classes: [clazz].concat(parsedTokens.children.flatMap(function (child) {
1139
- return typeof child === "string" ? [] : child.classes;
1140
- })),
1141
- children: parsedTokens.children.flatMap(function (child) {
1142
- return typeof child === "string" ? child : child.children;
1143
- })
1144
- });
1145
- }
1146
- var _loop = function _loop() {
1147
- var token = tokens[cursor];
1148
- if (token.type === "heading") {
1149
- children.push({
1150
- tag: "h".concat(token.depth),
1151
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1152
- });
1153
- } else if (token.type === "paragraph") {
1154
- children.push({
1155
- tag: "p",
1156
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1157
- });
1158
- } else if (token.type === "strong") {
1159
- merge(token, "bold");
1160
- } else if (token.type === "em") {
1161
- merge(token, "italic");
1162
- } else if (token.type === "del") {
1163
- merge(token, "line-through");
1164
- } else if (token.type === "text") {
1165
- if ("tokens" in token && Array.isArray(token.tokens)) {
1166
- children.push.apply(children, _toConsumableArray(parseTokens(token.tokens, sourceNodes, cursor, insideList).children));
1167
- } else {
1168
- if (insideList && typeof token.raw === "string") {
1169
- var lines = token.raw.split("\n");
1170
- var tags = lines.flatMap(function (line, i) {
1171
- if (i === lines.length - 1) return [line];
1172
- if (i === lines.length - 1 && line === "") return [];
1173
- if (line === "") return {
1174
- tag: "br"
1175
- };
1176
- return [line, {
1177
- tag: "br"
1178
- }];
1179
- });
1180
- children.push.apply(children, _toConsumableArray(tags));
1181
- } else {
1182
- children.push(token.raw);
1183
- }
1184
- }
1185
- } else if (token.type === "list") {
1186
- children.push({
1187
- tag: token.ordered ? "ol" : "ul",
1188
- children: parseTokens(token.items, sourceNodes, 0).children
1189
- });
1190
- } else if (token.type === "list_item") {
1191
- children.push({
1192
- tag: "li",
1193
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0, true).children
1194
- });
1195
- } else if (token.type === "space") ; else if (token.type === "html") {
1196
- if (token.text === VAL_END_TAG) {
1197
- return {
1198
- v: {
1199
- children: children,
1200
- cursor: cursor
1201
- }
1202
- };
1203
- }
1204
- var suffixIndex = token.text.indexOf(VAL_START_TAG_SUFFIX);
1205
- if (token.text.startsWith(VAL_START_TAG_PREFIX) && suffixIndex > -1) {
1206
- var number = Number(token.text.slice(VAL_START_TAG_PREFIX.length, suffixIndex));
1207
- if (Number.isNaN(number)) {
1208
- throw Error("Illegal val intermediate node: ".concat(JSON.stringify(token)));
1209
- }
1210
- var _parseTokens = parseTokens(tokens.map(function (token) {
1211
- if (token.type === "link" || token.type === "list") {
1212
- return {
1213
- type: "text",
1214
- raw: token.raw,
1215
- text: token.raw
1216
- };
1217
- }
1218
- return token;
1219
- }), sourceNodes, cursor + 1),
1220
- subChildren = _parseTokens.children,
1221
- subCursor = _parseTokens.cursor;
1222
- var sourceNode = sourceNodes[number];
1223
- if (sourceNode._type === "link") {
1224
- children.push({
1225
- tag: "a",
1226
- href: sourceNode.href,
1227
- children: subChildren
1228
- });
1229
- } else if (sourceNode._type === "file") {
1230
- var _sourceNode$metadata, _sourceNode$metadata2, _sourceNode$metadata3;
1231
- children.push({
1232
- tag: "img",
1233
- src: core.Internal.convertFileSource(sourceNode).url,
1234
- width: (_sourceNode$metadata = sourceNode.metadata) === null || _sourceNode$metadata === void 0 ? void 0 : _sourceNode$metadata.width,
1235
- height: (_sourceNode$metadata2 = sourceNode.metadata) === null || _sourceNode$metadata2 === void 0 ? void 0 : _sourceNode$metadata2.height,
1236
- mimeType: (_sourceNode$metadata3 = sourceNode.metadata) === null || _sourceNode$metadata3 === void 0 ? void 0 : _sourceNode$metadata3.mimeType
1237
- });
1238
- }
1239
- cursor = subCursor;
1240
- }
1241
- var br_html_regex = /<br\s*\/?>/gi; // matches <br>, <br/>, <br />; case insensitive
1242
- if (token.text.trim().match(br_html_regex)) {
1243
- var _tokens;
1244
- children.push({
1245
- tag: "br"
1246
- });
1247
- if (((_tokens = tokens[cursor + 1]) === null || _tokens === void 0 ? void 0 : _tokens.raw.trim()) === "") {
1248
- // if next token is a new line or white-spaces, skip it
1249
- // this typically means we have a <br> AND a new line, which, semantically, is just a <br>
1250
- cursor++;
1251
- }
1252
- }
1253
- } else if (token.type === "link") {
1254
- if (token.raw === token.href) {
1255
- // avoid auto-linking (provided by github flavoured markdown, but we want strikethrough so keep it enabled)
1256
- children.push(token.raw);
1257
- } else {
1258
- children.push({
1259
- tag: "a",
1260
- href: token.href,
1261
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1262
- });
1263
- }
1264
- } else if (token.type === "br") {
1265
- children.push({
1266
- tag: "br"
1267
- });
1268
- } else {
1269
- console.error("Could not parse markdown: unsupported token type: ".concat(token.type, ". Found: ").concat(token.raw));
1270
- }
1271
- cursor++;
1272
- },
1273
- _ret;
1274
- while (cursor < tokens.length) {
1275
- _ret = _loop();
1276
- if (_ret) return _ret.v;
1277
- }
1278
- return {
1279
- children: children,
1280
- cursor: cursor
1281
- };
1282
- }
1283
- function parseRichTextSource(_ref) {
1284
- var templateStrings = _ref.templateStrings,
1285
- nodes = _ref.exprs;
1286
- // TODO: validate that templateStrings does not contain VAL_NODE_PREFIX
1287
- var inputText = templateStrings.flatMap(function (templateString, i) {
1288
- var node = nodes[i];
1289
- if (node) {
1290
- if (node[core.VAL_EXTENSION] === "link") {
1291
- return templateString.concat("".concat(VAL_START_TAG_PREFIX).concat(i).concat(VAL_START_TAG_SUFFIX).concat(node.children[0]).concat(VAL_END_TAG));
1292
- } else {
1293
- return templateString.concat("".concat(VAL_START_TAG_PREFIX).concat(i).concat(VAL_START_TAG_SUFFIX).concat(VAL_END_TAG));
1294
- }
1295
- }
1296
- return templateString;
1297
- }).join("");
1298
- var tokenList = marked__namespace.lexer(inputText, {
1299
- gfm: true
1300
- });
1301
- var _parseTokens2 = parseTokens(tokenList, nodes, 0),
1302
- children = _parseTokens2.children,
1303
- cursor = _parseTokens2.cursor;
1304
- if (cursor !== tokenList.length) {
1305
- throw Error("Unexpectedly terminated markdown parsing. Possible reason: unclosed html tag?");
1306
- }
1307
- return _defineProperty(_defineProperty({}, core.VAL_EXTENSION, "richtext"), "children", children);
1308
- }
1309
-
1310
1133
  var RemirrorTextMark = zod.z.object({
1311
1134
  type: zod.z.union([zod.z.literal("bold"), zod.z.literal("strike"), zod.z.literal("italic")])
1312
1135
  });
@@ -1350,7 +1173,7 @@ var RemirrorHeading = zod.z.intersection(zod.z.object({
1350
1173
  attrs: zod.z.object({
1351
1174
  level: zod.z.number()
1352
1175
  }),
1353
- content: zod.z.array(zod.z.union([RemirrorText, RemirrorBr]))
1176
+ content: zod.z.array(zod.z.union([RemirrorText, RemirrorImage, RemirrorBr]))
1354
1177
  }).partial());
1355
1178
  var RemirrorBulletList = zod.z.lazy(function () {
1356
1179
  return zod.z.intersection(zod.z.object({
@@ -1370,45 +1193,45 @@ var RemirrorListItem = zod.z.lazy(function () {
1370
1193
  return zod.z.intersection(zod.z.object({
1371
1194
  type: zod.z.literal("listItem")
1372
1195
  }), zod.z.object({
1196
+ attrs: zod.z.object({
1197
+ closed: zod.z["boolean"](),
1198
+ nested: zod.z["boolean"]()
1199
+ }).partial().optional(),
1373
1200
  content: zod.z.array(zod.z.union([RemirrorParagraph, RemirrorBulletList, RemirrorOrderedList]))
1374
1201
  }).partial());
1375
1202
  });
1376
- var RemirrorParagraph = zod.z.intersection(zod.z.object({
1377
- type: zod.z.literal("paragraph")
1378
- }), zod.z.object({
1379
- content: zod.z.array(zod.z.union([RemirrorText, RemirrorImage, RemirrorBr]))
1380
- }).partial());
1203
+ var RemirrorParagraph = zod.z.lazy(function () {
1204
+ return zod.z.intersection(zod.z.object({
1205
+ type: zod.z.literal("paragraph")
1206
+ }), zod.z.object({
1207
+ content: zod.z.array(zod.z.union([RemirrorText, RemirrorImage, RemirrorBr]))
1208
+ }).partial());
1209
+ });
1381
1210
  var RemirrorJSON = zod.z.object({
1382
1211
  type: zod.z.literal("doc"),
1383
1212
  content: zod.z.array(zod.z.union([RemirrorParagraph, RemirrorHeading, RemirrorBulletList, RemirrorOrderedList]))
1384
1213
  });
1385
1214
 
1386
- function ownKeys(e, r) {
1387
- var t = Object.keys(e);
1388
- if (Object.getOwnPropertySymbols) {
1389
- var o = Object.getOwnPropertySymbols(e);
1390
- r && (o = o.filter(function (r) {
1391
- return Object.getOwnPropertyDescriptor(e, r).enumerable;
1392
- })), t.push.apply(t, o);
1393
- }
1394
- return t;
1215
+ function _arrayWithoutHoles(arr) {
1216
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
1395
1217
  }
1396
- function _objectSpread2(e) {
1397
- for (var r = 1; r < arguments.length; r++) {
1398
- var t = null != arguments[r] ? arguments[r] : {};
1399
- r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
1400
- _defineProperty(e, r, t[r]);
1401
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
1402
- Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
1403
- });
1404
- }
1405
- return e;
1218
+
1219
+ function _iterableToArray(iter) {
1220
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
1221
+ }
1222
+
1223
+ function _nonIterableSpread() {
1224
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1225
+ }
1226
+
1227
+ function _toConsumableArray(arr) {
1228
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
1406
1229
  }
1407
1230
 
1408
1231
  function richTextToRemirror(richtext) {
1409
1232
  return {
1410
1233
  type: "doc",
1411
- content: richtext.children.map(function (child) {
1234
+ content: richtext.map(function (child) {
1412
1235
  return toRemirrorNode(child);
1413
1236
  })
1414
1237
  };
@@ -1433,8 +1256,6 @@ function toRemirrorNode(child) {
1433
1256
  return convertUlToRemirror(child);
1434
1257
  case "ol":
1435
1258
  return convertOlToRemirror(child);
1436
- case "br":
1437
- return convertParagraphToRemirror([child]);
1438
1259
  default:
1439
1260
  {
1440
1261
  var _exhaustiveCheck = child;
@@ -1448,7 +1269,7 @@ function convertHeadingToRemirror(level, children) {
1448
1269
  attrs: {
1449
1270
  level: level
1450
1271
  },
1451
- content: children.flatMap(convertStringOrSpanNodeToRemirror)
1272
+ content: children.flatMap(convertInlineToRemirror)
1452
1273
  };
1453
1274
  }
1454
1275
  function convertStringToRemirror(child) {
@@ -1457,14 +1278,23 @@ function convertStringToRemirror(child) {
1457
1278
  text: child
1458
1279
  };
1459
1280
  }
1460
- function convertStringOrSpanNodeToRemirror(child) {
1281
+ function convertInlineToRemirror(child) {
1461
1282
  if (typeof child === "string") {
1462
1283
  return [convertStringToRemirror(child)];
1463
1284
  }
1285
+ if (child.tag === "img") {
1286
+ return [convertImageNodeToRemirror(child)];
1287
+ }
1288
+ if (child.tag === "br") {
1289
+ return [createRemirrorBr()];
1290
+ }
1291
+ if (child.tag === "a") {
1292
+ return convertLinkNodeToRemirror(child);
1293
+ }
1464
1294
  return convertSpanNodeToRemirror(child);
1465
1295
  }
1466
1296
  function convertSpanNodeToRemirror(spanNode) {
1467
- if (spanNode.classes.length === 0 && spanNode.children.length === 1) {
1297
+ if (spanNode.styles.length === 0 && spanNode.children.length === 1) {
1468
1298
  if (typeof spanNode.children[0] === "string") {
1469
1299
  return [convertStringToRemirror(spanNode.children[0])];
1470
1300
  } else {
@@ -1472,12 +1302,13 @@ function convertSpanNodeToRemirror(spanNode) {
1472
1302
  throw Error("Unexpected amount of children in span node: " + JSON.stringify(spanNode, null, 2));
1473
1303
  }
1474
1304
  }
1305
+ var uniqueStyles = Array.from(new Set(spanNode.styles));
1475
1306
  return spanNode.children.map(function (child) {
1476
1307
  return {
1477
1308
  type: "text",
1478
1309
  text: child,
1479
- marks: spanNode.classes.map(function (className) {
1480
- switch (className) {
1310
+ marks: uniqueStyles.map(function (style) {
1311
+ switch (style) {
1481
1312
  case "bold":
1482
1313
  return {
1483
1314
  type: "bold"
@@ -1492,7 +1323,7 @@ function convertSpanNodeToRemirror(spanNode) {
1492
1323
  };
1493
1324
  default:
1494
1325
  {
1495
- var _exhaustiveCheck = className;
1326
+ var _exhaustiveCheck = style;
1496
1327
  throw Error("Unexpected span class: " + _exhaustiveCheck);
1497
1328
  }
1498
1329
  }
@@ -1532,9 +1363,12 @@ function createRemirrorBr() {
1532
1363
  };
1533
1364
  }
1534
1365
  function convertLinkNodeToRemirror(linkNode) {
1535
- return linkNode.children.flatMap(convertStringOrSpanNodeToRemirror).map(function (remirrorText) {
1536
- return _objectSpread2(_objectSpread2({}, remirrorText), {}, {
1537
- marks: (remirrorText.marks || []).concat({
1366
+ return linkNode.children.flatMap(convertInlineToRemirror).map(function (remirrorNode) {
1367
+ if (remirrorNode.type !== "text") {
1368
+ return remirrorNode;
1369
+ }
1370
+ return _objectSpread2(_objectSpread2({}, remirrorNode), {}, {
1371
+ marks: (remirrorNode.marks || []).concat({
1538
1372
  type: "link",
1539
1373
  attrs: {
1540
1374
  href: linkNode.href,
@@ -1546,12 +1380,18 @@ function convertLinkNodeToRemirror(linkNode) {
1546
1380
  });
1547
1381
  }
1548
1382
  function convertImageNodeToRemirror(imageNode) {
1383
+ var _fileSource$metadata, _fileSource$metadata2;
1384
+ var fileSource = imageNode.src;
1385
+ if (!(core.VAL_EXTENSION in fileSource) || fileSource[core.VAL_EXTENSION] !== "file") {
1386
+ throw Error("Expected file source in image node");
1387
+ }
1388
+ var fileVal = core.Internal.convertFileSource(fileSource);
1549
1389
  return {
1550
1390
  type: "image",
1551
1391
  attrs: {
1552
- height: imageNode.height,
1553
- width: imageNode.width,
1554
- src: imageNode.src // at time of writing we are not sure if src as href or data url works, also: how to keep mimeType etc?
1392
+ height: (_fileSource$metadata = fileSource.metadata) === null || _fileSource$metadata === void 0 ? void 0 : _fileSource$metadata.height,
1393
+ width: (_fileSource$metadata2 = fileSource.metadata) === null || _fileSource$metadata2 === void 0 ? void 0 : _fileSource$metadata2.width,
1394
+ src: fileVal.url
1555
1395
  }
1556
1396
  };
1557
1397
  }
@@ -1593,24 +1433,6 @@ function convertListItemToRemirrorParagraph(rtChildren) {
1593
1433
  lastChildContent.push(convertStringToRemirror(child));
1594
1434
  } else {
1595
1435
  switch (child.tag) {
1596
- case "a":
1597
- {
1598
- lastChildContent.push.apply(lastChildContent, _toConsumableArray(convertLinkNodeToRemirror(child)));
1599
- break;
1600
- }
1601
- case "br":
1602
- {
1603
- children.push({
1604
- type: "paragraph",
1605
- content: [] // create new paragraph
1606
- });
1607
- break;
1608
- }
1609
- case "span":
1610
- {
1611
- lastChildContent.push.apply(lastChildContent, _toConsumableArray(convertSpanNodeToRemirror(child)));
1612
- break;
1613
- }
1614
1436
  case "ol":
1615
1437
  {
1616
1438
  children.push(convertOlToRemirror(child));
@@ -1621,6 +1443,20 @@ function convertListItemToRemirrorParagraph(rtChildren) {
1621
1443
  children.push(convertUlToRemirror(child));
1622
1444
  break;
1623
1445
  }
1446
+ case "p":
1447
+ {
1448
+ var newChild = convertParagraphToRemirror(child.children);
1449
+ if (lastChild.type === "paragraph" && newChild.type === "paragraph") {
1450
+ if (newChild.content) {
1451
+ lastChildContent.push.apply(lastChildContent, _toConsumableArray(newChild.content));
1452
+ } else {
1453
+ // no content - skip
1454
+ }
1455
+ } else {
1456
+ children.push(newChild);
1457
+ }
1458
+ break;
1459
+ }
1624
1460
  default:
1625
1461
  {
1626
1462
  var _exhaustiveCheck = child;
@@ -2371,6 +2207,5 @@ exports.ValStore = ValStore;
2371
2207
  exports.filenameToMimeType = filenameToMimeType;
2372
2208
  exports.getMimeType = getMimeType;
2373
2209
  exports.mimeTypeToFileExt = mimeTypeToFileExt;
2374
- exports.parseRichTextSource = parseRichTextSource;
2375
2210
  exports.remirrorToRichTextSource = remirrorToRichTextSource;
2376
2211
  exports.richTextToRemirror = richTextToRemirror;