@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.
@@ -1,5 +1,4 @@
1
- import { VAL_EXTENSION, Internal, FILE_REF_PROP, FILE_REF_SUBTYPE_TAG, RT_IMAGE_TAG } from '@valbuild/core';
2
- import * as marked from 'marked';
1
+ import { Internal, VAL_EXTENSION, FILE_REF_PROP } from '@valbuild/core';
3
2
  import { z } from 'zod';
4
3
  import { result } from '@valbuild/core/fp';
5
4
 
@@ -34,6 +33,28 @@ function _defineProperty(obj, key, value) {
34
33
  return obj;
35
34
  }
36
35
 
36
+ function ownKeys(e, r) {
37
+ var t = Object.keys(e);
38
+ if (Object.getOwnPropertySymbols) {
39
+ var o = Object.getOwnPropertySymbols(e);
40
+ r && (o = o.filter(function (r) {
41
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
42
+ })), t.push.apply(t, o);
43
+ }
44
+ return t;
45
+ }
46
+ function _objectSpread2(e) {
47
+ for (var r = 1; r < arguments.length; r++) {
48
+ var t = null != arguments[r] ? arguments[r] : {};
49
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
50
+ _defineProperty(e, r, t[r]);
51
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
52
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
53
+ });
54
+ }
55
+ return e;
56
+ }
57
+
37
58
  function _arrayLikeToArray(arr, len) {
38
59
  if (len == null || len > arr.length) len = arr.length;
39
60
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
@@ -101,22 +122,6 @@ function _createForOfIteratorHelper(o, allowArrayLike) {
101
122
  };
102
123
  }
103
124
 
104
- function _arrayWithoutHoles(arr) {
105
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
106
- }
107
-
108
- function _iterableToArray(iter) {
109
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
110
- }
111
-
112
- function _nonIterableSpread() {
113
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
114
- }
115
-
116
- function _toConsumableArray(arr) {
117
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
118
- }
119
-
120
125
  function _arrayWithHoles(arr) {
121
126
  if (Array.isArray(arr)) return arr;
122
127
  }
@@ -881,184 +886,183 @@ function filenameToMimeType(filename) {
881
886
 
882
887
  function remirrorToRichTextSource(node) {
883
888
  var files = {};
884
- var markdownIRBlocks = node.content.map(function (child) {
885
- return createBlock(child, files);
886
- });
887
- return fromIRToRichTextSource(markdownIRBlocks, files);
889
+ var blocks = [];
890
+ var i = 0;
891
+ var _iterator = _createForOfIteratorHelper(node.content),
892
+ _step;
893
+ try {
894
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
895
+ var child = _step.value;
896
+ var block = convertBlock([(i++).toString()], child, files);
897
+ blocks.push(block);
898
+ }
899
+ } catch (err) {
900
+ _iterator.e(err);
901
+ } finally {
902
+ _iterator.f();
903
+ }
904
+ return {
905
+ blocks: blocks,
906
+ files: files
907
+ };
888
908
  }
889
- function createBlock(node, files) {
909
+ function convertBlock(path, node, files) {
890
910
  if (node.type === "heading") {
891
911
  var _node$attrs, _node$content;
892
- var headingTag = "";
893
912
  var depth = ((_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.level) || 1;
894
913
  if (Number.isNaN(depth)) {
895
914
  throw new Error("Invalid header depth");
896
915
  }
897
- for (var i = 0; i < Number(depth); i++) {
898
- headingTag += "#";
899
- }
900
- var headingText = ["".concat(headingTag, " ")];
901
916
  return {
902
- type: "block",
903
- children: headingText.concat.apply(headingText, _toConsumableArray(((_node$content = node.content) === null || _node$content === void 0 ? void 0 : _node$content.map(function (child) {
904
- return transformLeafNode(child, files);
905
- })) || []))
906
- };
907
- } else if (node.type === "paragraph") {
908
- var _node$content2;
909
- if (!node.content || ((_node$content2 = node.content) === null || _node$content2 === void 0 ? void 0 : _node$content2.length) === 0) {
910
- return {
911
- type: "block",
912
- children: ["<br />"]
913
- };
914
- }
915
- return {
916
- type: "block",
917
- children: node.content.map(function (child) {
918
- return transformLeafNode(child, files);
919
- })
920
- };
921
- } else if (node.type === "bulletList" || node.type === "orderedList") {
922
- var _node$content3;
923
- return {
924
- type: "block",
925
- children: ((_node$content3 = node.content) === null || _node$content3 === void 0 ? void 0 : _node$content3.flatMap(function (child, i) {
926
- return formatListItemNode(getListPrefix(node), child, 0, files, i === 0);
917
+ tag: "h".concat(depth),
918
+ children: ((_node$content = node.content) === null || _node$content === void 0 ? void 0 : _node$content.map(function (child, i) {
919
+ return convertHeadingChild(path.concat("children", i.toString()), child, files);
927
920
  })) || []
928
921
  };
922
+ } else if (node.type === "paragraph") {
923
+ return convertParagraph(path, node, files);
924
+ } else if (node.type === "bulletList") {
925
+ return convertBulletList(path, node, files);
926
+ } else if (node.type === "orderedList") {
927
+ return convertOrderedList(path, node, files);
929
928
  } else {
930
929
  var exhaustiveCheck = node;
931
930
  throw new Error("Unhandled node type: ".concat("type" in exhaustiveCheck ? "exhaustiveCheck.type" : "unknown"));
932
931
  }
933
932
  }
934
- function fromIRToRichTextSource(markdownIRBlocks, files) {
935
- var templateStrings = ["\n"];
936
- var exprs = [];
937
- for (var blockIdx = 0; blockIdx < markdownIRBlocks.length; blockIdx++) {
938
- var block = markdownIRBlocks[blockIdx];
939
- var _iterator = _createForOfIteratorHelper(block.children),
940
- _step;
941
- try {
942
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
943
- var child = _step.value;
944
- if (typeof child === "string") {
945
- templateStrings[templateStrings.length - 1] += child;
946
- } else {
947
- if (child._type === "file") {
948
- exprs.push(child);
949
- } else if (child._type === "link") {
950
- exprs.push(child);
951
- } else {
952
- var exhaustiveCheck = child;
953
- throw new Error("Unexpected node type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
954
- }
955
- templateStrings.push("");
956
- }
957
- }
958
- } catch (err) {
959
- _iterator.e(err);
960
- } finally {
961
- _iterator.f();
962
- }
963
- if (blockIdx === markdownIRBlocks.length - 1) {
964
- templateStrings[templateStrings.length - 1] += "\n";
965
- } else {
966
- templateStrings[templateStrings.length - 1] += "\n\n";
967
- }
933
+ function convertHeadingChild(path, node, files) {
934
+ if (node.type === "text") {
935
+ return convertTextNode(node);
936
+ } else if (node.type === "hardBreak") {
937
+ return {
938
+ tag: "br"
939
+ };
940
+ } else if (node.type === "image") {
941
+ return convertImageNode(path, node, files);
942
+ } else {
943
+ var exhaustiveCheck = node;
944
+ throw new Error("Unexpected heading child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
968
945
  }
969
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "richtext"), "templateStrings", templateStrings), "exprs", exprs), "files", files);
970
946
  }
971
- function formatText(node) {
972
- var _node$marks;
973
- var classes = ((_node$marks = node.marks) === null || _node$marks === void 0 ? void 0 : _node$marks.flatMap(function (mark) {
974
- if (mark.type === "bold") {
975
- return ["bold"];
976
- } else if (mark.type === "italic") {
977
- return ["italic"];
978
- } else if (mark.type === "strike") {
979
- return ["line-through"];
980
- }
981
- return [];
982
- })) || [];
983
- var text = node.text.trimStart();
984
- var prefixWS = node.text.length - text.length;
985
- text = text.trimEnd();
986
- var suffixWS = node.text.length - text.length - prefixWS;
987
- if (classes.includes("bold") && classes.includes("italic")) {
988
- text = "***".concat(text, "***");
989
- } else if (classes.includes("bold")) {
990
- text = "**".concat(text, "**");
991
- } else if (classes.includes("italic")) {
992
- text = "_".concat(text, "_");
993
- }
994
- if (classes.includes("line-through")) {
995
- text = "~~".concat(text, "~~");
996
- }
997
- // TODO:
998
- // text = splitIntoChunks(text);
999
- return "".concat(" ".repeat(prefixWS)).concat(text).concat(" ".repeat(suffixWS));
947
+ function convertParagraph(path, child, files) {
948
+ var _child$content;
949
+ return {
950
+ tag: "p",
951
+ children: ((_child$content = child.content) === null || _child$content === void 0 ? void 0 : _child$content.map(function (child, i) {
952
+ if (child.type === "text") {
953
+ return convertTextNode(child);
954
+ } else if (child.type === "hardBreak") {
955
+ return {
956
+ tag: "br"
957
+ };
958
+ } else if (child.type === "image") {
959
+ return convertImageNode(path.concat("children", i.toString()), child, files);
960
+ }
961
+ var exhaustiveCheck = child;
962
+ throw new Error("Unexpected paragraph child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
963
+ })) || []
964
+ };
1000
965
  }
1001
- function transformLeafNode(node, files) {
966
+ function convertTextNode(node) {
1002
967
  if (node.type === "text") {
1003
- var _node$marks2;
1004
- var linkMark = (_node$marks2 = node.marks) === null || _node$marks2 === void 0 ? void 0 : _node$marks2.find(function (mark) {
968
+ var _node$marks, _node$marks2;
969
+ var styles = ((_node$marks = node.marks) === null || _node$marks === void 0 ? void 0 : _node$marks.flatMap(function (mark) {
970
+ if (mark.type !== "link") {
971
+ if (mark.type === "strike") {
972
+ return ["line-through"];
973
+ }
974
+ return [mark.type];
975
+ } else {
976
+ return [];
977
+ }
978
+ })) || [];
979
+ if ((_node$marks2 = node.marks) !== null && _node$marks2 !== void 0 && _node$marks2.some(function (mark) {
1005
980
  return mark.type === "link";
1006
- });
1007
- if ((linkMark === null || linkMark === void 0 ? void 0 : linkMark.type) === "link") {
981
+ })) {
982
+ var _node$marks$find2;
983
+ if (styles.length > 0) {
984
+ var _node$marks$find;
985
+ return {
986
+ tag: "a",
987
+ href: ((_node$marks$find = node.marks.find(function (mark) {
988
+ return mark.type === "link";
989
+ })) === null || _node$marks$find === void 0 ? void 0 : _node$marks$find.attrs.href) || "",
990
+ children: [{
991
+ tag: "span",
992
+ styles: styles,
993
+ children: [node.text]
994
+ }]
995
+ };
996
+ }
1008
997
  return {
1009
- _type: "link",
1010
- href: linkMark.attrs.href,
1011
- children: [formatText(node)]
998
+ tag: "a",
999
+ href: ((_node$marks$find2 = node.marks.find(function (mark) {
1000
+ return mark.type === "link";
1001
+ })) === null || _node$marks$find2 === void 0 ? void 0 : _node$marks$find2.attrs.href) || "",
1002
+ children: [node.text]
1012
1003
  };
1013
1004
  }
1014
- return formatText(node);
1015
- } else if (node.type === "hardBreak") {
1016
- return "<br />";
1017
- } else if (node.type === "image") {
1018
- return fromRemirrorImageNode(node, files);
1005
+ if (styles.length > 0) {
1006
+ return {
1007
+ tag: "span",
1008
+ styles: styles,
1009
+ children: [node.text]
1010
+ };
1011
+ }
1012
+ return node.text;
1019
1013
  } else {
1020
- var exhaustiveCheck = node;
1021
- throw new Error("Unexpected node type: ".concat(JSON.stringify(exhaustiveCheck)));
1014
+ var exhaustiveCheck = node.type;
1015
+ throw new Error("Unexpected node type: ".concat(exhaustiveCheck));
1022
1016
  }
1023
1017
  }
1024
- function formatListItemNode(listPrefix, node, indent, files) {
1025
- var _node$content4, _node$content5;
1026
- var isFirstTopLevelListItem = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
1027
- var newLine = isFirstTopLevelListItem ? "" : "\n";
1028
- var prefix = ["".concat(newLine).concat(" ".repeat(indent)).concat(listPrefix)];
1029
- 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")) {
1030
- prefix.push(" ");
1031
- }
1032
- return prefix.concat((node.content || []).flatMap(function (child, i) {
1033
- if (child.type === "bulletList" || child.type === "orderedList") {
1034
- return (child.content || []).flatMap(function (subChild) {
1035
- return formatListItemNode(getListPrefix(child), subChild, indent + 4, files);
1036
- });
1037
- } else {
1038
- return (child.content || []).flatMap(function (subChild) {
1039
- var _child$content, _child$content2;
1040
- var res = [];
1041
- 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) {
1042
- res.push("<br />\n");
1043
- }
1044
- res.push(transformLeafNode(subChild, files));
1045
- return res;
1046
- });
1047
- }
1048
- }));
1018
+ function convertListItem(path, child, files) {
1019
+ var _child$content2;
1020
+ return {
1021
+ tag: "li",
1022
+ children: ((_child$content2 = child.content) === null || _child$content2 === void 0 ? void 0 : _child$content2.map(function (child, i) {
1023
+ if (child.type === "paragraph") {
1024
+ return convertParagraph(path.concat("children", i.toString()), child, files);
1025
+ } else if (child.type === "bulletList") {
1026
+ return convertBulletList(path.concat("children", i.toString()), child, files);
1027
+ } else if (child.type === "orderedList") {
1028
+ return convertOrderedList(path.concat("children", i.toString()), child, files);
1029
+ } else {
1030
+ var exhaustiveCheck = child;
1031
+ throw new Error("Unexpected list child type: ".concat(exhaustiveCheck));
1032
+ }
1033
+ })) || []
1034
+ };
1049
1035
  }
1050
- function getListPrefix(node) {
1051
- if (node.type === "bulletList") {
1052
- return "-";
1053
- } else if (node.type === "orderedList") {
1054
- return "1.";
1055
- } else {
1056
- var exhaustiveCheck = node;
1057
- throw new Error("Unhandled list node: ".concat(JSON.stringify(exhaustiveCheck)));
1058
- }
1036
+ function convertBulletList(path, node, files) {
1037
+ var _node$content2;
1038
+ return {
1039
+ tag: "ul",
1040
+ children: ((_node$content2 = node.content) === null || _node$content2 === void 0 ? void 0 : _node$content2.map(function (child, i) {
1041
+ if (child.type === "listItem") {
1042
+ return convertListItem(path.concat("children", i.toString()), child, files);
1043
+ } else {
1044
+ var exhaustiveCheck = child.type;
1045
+ throw new Error("Unexpected bullet list child type: ".concat(exhaustiveCheck));
1046
+ }
1047
+ })) || []
1048
+ };
1049
+ }
1050
+ function convertOrderedList(path, node, files) {
1051
+ var _node$content3;
1052
+ return {
1053
+ tag: "ol",
1054
+ children: ((_node$content3 = node.content) === null || _node$content3 === void 0 ? void 0 : _node$content3.map(function (child, i) {
1055
+ if (child.type === "listItem") {
1056
+ return convertListItem(path.concat("children", i.toString()), child, files);
1057
+ } else {
1058
+ var exhaustiveCheck = child.type;
1059
+ throw new Error("Unexpected ordered list child type: ".concat(exhaustiveCheck));
1060
+ }
1061
+ })) || []
1062
+ };
1059
1063
  }
1060
1064
  var textEncoder = new TextEncoder();
1061
- function fromRemirrorImageNode(node, files) {
1065
+ function convertImageNode(path, node, files) {
1062
1066
  if (node.attrs && node.attrs.src.startsWith("data:")) {
1063
1067
  var sha256 = Internal.getSHA256Hash(textEncoder.encode(node.attrs.src));
1064
1068
  var mimeType = getMimeType(node.attrs.src);
@@ -1068,22 +1072,43 @@ function fromRemirrorImageNode(node, files) {
1068
1072
  var fileExt = mimeTypeToFileExt(mimeType);
1069
1073
  var fileName = node.attrs.fileName || "".concat(sha256, ".").concat(fileExt);
1070
1074
  var filePath = "/public/".concat(fileName);
1071
- files[filePath] = node.attrs.src;
1072
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, filePath), FILE_REF_SUBTYPE_TAG, RT_IMAGE_TAG), "metadata", {
1073
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1074
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1075
- sha256: sha256 || "",
1076
- mimeType: mimeType
1077
- });
1075
+ var existingFilesEntry = files[filePath];
1076
+ var thisPath = path
1077
+ // file is added as src (see below):
1078
+ .concat("src");
1079
+ if (existingFilesEntry) {
1080
+ existingFilesEntry.patchPaths.push(thisPath);
1081
+ } else {
1082
+ files[filePath] = {
1083
+ value: node.attrs.src,
1084
+ patchPaths: [thisPath]
1085
+ };
1086
+ }
1087
+ return {
1088
+ tag: "img",
1089
+ src: _defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, filePath), "metadata", {
1090
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1091
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1092
+ sha256: sha256 || "",
1093
+ mimeType: mimeType
1094
+ })
1095
+ };
1078
1096
  } else if (node.attrs) {
1079
1097
  var _sha = getParam("sha256", node.attrs.src);
1098
+ var patchId = getParam("patch_id", node.attrs.src);
1080
1099
  var noParamsSrc = node.attrs.src.split("?")[0];
1081
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, "/public".concat(node.attrs.src.split("?")[0])), FILE_REF_SUBTYPE_TAG, RT_IMAGE_TAG), "metadata", {
1082
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1083
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1084
- sha256: _sha || "",
1085
- mimeType: noParamsSrc && filenameToMimeType(noParamsSrc) || ""
1086
- });
1100
+ var tag = {
1101
+ tag: "img",
1102
+ src: _objectSpread2(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, "/public".concat(node.attrs.src.split("?")[0])), "metadata", {
1103
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1104
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1105
+ sha256: _sha || "",
1106
+ mimeType: noParamsSrc && filenameToMimeType(noParamsSrc) || ""
1107
+ }), patchId ? {
1108
+ patch_id: patchId
1109
+ } : {})
1110
+ };
1111
+ return tag;
1087
1112
  } else {
1088
1113
  throw new Error("Invalid image node (no attrs): " + JSON.stringify(node));
1089
1114
  }
@@ -1101,188 +1126,6 @@ function getParam(param, url) {
1101
1126
  return undefined;
1102
1127
  }
1103
1128
 
1104
- var VAL_START_TAG_PREFIX = '<val value="';
1105
- var VAL_START_TAG_SUFFIX = '">';
1106
- var VAL_END_TAG = "</val>";
1107
- function parseTokens(tokens, sourceNodes, cursor) {
1108
- var insideList = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
1109
- var children = [];
1110
- function merge(token, clazz) {
1111
- var parsedTokens = parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0);
1112
- children.push({
1113
- tag: "span",
1114
- classes: [clazz].concat(parsedTokens.children.flatMap(function (child) {
1115
- return typeof child === "string" ? [] : child.classes;
1116
- })),
1117
- children: parsedTokens.children.flatMap(function (child) {
1118
- return typeof child === "string" ? child : child.children;
1119
- })
1120
- });
1121
- }
1122
- var _loop = function _loop() {
1123
- var token = tokens[cursor];
1124
- if (token.type === "heading") {
1125
- children.push({
1126
- tag: "h".concat(token.depth),
1127
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1128
- });
1129
- } else if (token.type === "paragraph") {
1130
- children.push({
1131
- tag: "p",
1132
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1133
- });
1134
- } else if (token.type === "strong") {
1135
- merge(token, "bold");
1136
- } else if (token.type === "em") {
1137
- merge(token, "italic");
1138
- } else if (token.type === "del") {
1139
- merge(token, "line-through");
1140
- } else if (token.type === "text") {
1141
- if ("tokens" in token && Array.isArray(token.tokens)) {
1142
- children.push.apply(children, _toConsumableArray(parseTokens(token.tokens, sourceNodes, cursor, insideList).children));
1143
- } else {
1144
- if (insideList && typeof token.raw === "string") {
1145
- var lines = token.raw.split("\n");
1146
- var tags = lines.flatMap(function (line, i) {
1147
- if (i === lines.length - 1) return [line];
1148
- if (i === lines.length - 1 && line === "") return [];
1149
- if (line === "") return {
1150
- tag: "br"
1151
- };
1152
- return [line, {
1153
- tag: "br"
1154
- }];
1155
- });
1156
- children.push.apply(children, _toConsumableArray(tags));
1157
- } else {
1158
- children.push(token.raw);
1159
- }
1160
- }
1161
- } else if (token.type === "list") {
1162
- children.push({
1163
- tag: token.ordered ? "ol" : "ul",
1164
- children: parseTokens(token.items, sourceNodes, 0).children
1165
- });
1166
- } else if (token.type === "list_item") {
1167
- children.push({
1168
- tag: "li",
1169
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0, true).children
1170
- });
1171
- } else if (token.type === "space") ; else if (token.type === "html") {
1172
- if (token.text === VAL_END_TAG) {
1173
- return {
1174
- v: {
1175
- children: children,
1176
- cursor: cursor
1177
- }
1178
- };
1179
- }
1180
- var suffixIndex = token.text.indexOf(VAL_START_TAG_SUFFIX);
1181
- if (token.text.startsWith(VAL_START_TAG_PREFIX) && suffixIndex > -1) {
1182
- var number = Number(token.text.slice(VAL_START_TAG_PREFIX.length, suffixIndex));
1183
- if (Number.isNaN(number)) {
1184
- throw Error("Illegal val intermediate node: ".concat(JSON.stringify(token)));
1185
- }
1186
- var _parseTokens = parseTokens(tokens.map(function (token) {
1187
- if (token.type === "link" || token.type === "list") {
1188
- return {
1189
- type: "text",
1190
- raw: token.raw,
1191
- text: token.raw
1192
- };
1193
- }
1194
- return token;
1195
- }), sourceNodes, cursor + 1),
1196
- subChildren = _parseTokens.children,
1197
- subCursor = _parseTokens.cursor;
1198
- var sourceNode = sourceNodes[number];
1199
- if (sourceNode._type === "link") {
1200
- children.push({
1201
- tag: "a",
1202
- href: sourceNode.href,
1203
- children: subChildren
1204
- });
1205
- } else if (sourceNode._type === "file") {
1206
- var _sourceNode$metadata, _sourceNode$metadata2, _sourceNode$metadata3;
1207
- children.push({
1208
- tag: "img",
1209
- src: Internal.convertFileSource(sourceNode).url,
1210
- width: (_sourceNode$metadata = sourceNode.metadata) === null || _sourceNode$metadata === void 0 ? void 0 : _sourceNode$metadata.width,
1211
- height: (_sourceNode$metadata2 = sourceNode.metadata) === null || _sourceNode$metadata2 === void 0 ? void 0 : _sourceNode$metadata2.height,
1212
- mimeType: (_sourceNode$metadata3 = sourceNode.metadata) === null || _sourceNode$metadata3 === void 0 ? void 0 : _sourceNode$metadata3.mimeType
1213
- });
1214
- }
1215
- cursor = subCursor;
1216
- }
1217
- var br_html_regex = /<br\s*\/?>/gi; // matches <br>, <br/>, <br />; case insensitive
1218
- if (token.text.trim().match(br_html_regex)) {
1219
- var _tokens;
1220
- children.push({
1221
- tag: "br"
1222
- });
1223
- if (((_tokens = tokens[cursor + 1]) === null || _tokens === void 0 ? void 0 : _tokens.raw.trim()) === "") {
1224
- // if next token is a new line or white-spaces, skip it
1225
- // this typically means we have a <br> AND a new line, which, semantically, is just a <br>
1226
- cursor++;
1227
- }
1228
- }
1229
- } else if (token.type === "link") {
1230
- if (token.raw === token.href) {
1231
- // avoid auto-linking (provided by github flavoured markdown, but we want strikethrough so keep it enabled)
1232
- children.push(token.raw);
1233
- } else {
1234
- children.push({
1235
- tag: "a",
1236
- href: token.href,
1237
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1238
- });
1239
- }
1240
- } else if (token.type === "br") {
1241
- children.push({
1242
- tag: "br"
1243
- });
1244
- } else {
1245
- console.error("Could not parse markdown: unsupported token type: ".concat(token.type, ". Found: ").concat(token.raw));
1246
- }
1247
- cursor++;
1248
- },
1249
- _ret;
1250
- while (cursor < tokens.length) {
1251
- _ret = _loop();
1252
- if (_ret) return _ret.v;
1253
- }
1254
- return {
1255
- children: children,
1256
- cursor: cursor
1257
- };
1258
- }
1259
- function parseRichTextSource(_ref) {
1260
- var templateStrings = _ref.templateStrings,
1261
- nodes = _ref.exprs;
1262
- // TODO: validate that templateStrings does not contain VAL_NODE_PREFIX
1263
- var inputText = templateStrings.flatMap(function (templateString, i) {
1264
- var node = nodes[i];
1265
- if (node) {
1266
- if (node[VAL_EXTENSION] === "link") {
1267
- return templateString.concat("".concat(VAL_START_TAG_PREFIX).concat(i).concat(VAL_START_TAG_SUFFIX).concat(node.children[0]).concat(VAL_END_TAG));
1268
- } else {
1269
- return templateString.concat("".concat(VAL_START_TAG_PREFIX).concat(i).concat(VAL_START_TAG_SUFFIX).concat(VAL_END_TAG));
1270
- }
1271
- }
1272
- return templateString;
1273
- }).join("");
1274
- var tokenList = marked.lexer(inputText, {
1275
- gfm: true
1276
- });
1277
- var _parseTokens2 = parseTokens(tokenList, nodes, 0),
1278
- children = _parseTokens2.children,
1279
- cursor = _parseTokens2.cursor;
1280
- if (cursor !== tokenList.length) {
1281
- throw Error("Unexpectedly terminated markdown parsing. Possible reason: unclosed html tag?");
1282
- }
1283
- return _defineProperty(_defineProperty({}, VAL_EXTENSION, "richtext"), "children", children);
1284
- }
1285
-
1286
1129
  var RemirrorTextMark = z.object({
1287
1130
  type: z.union([z.literal("bold"), z.literal("strike"), z.literal("italic")])
1288
1131
  });
@@ -1326,7 +1169,7 @@ var RemirrorHeading = z.intersection(z.object({
1326
1169
  attrs: z.object({
1327
1170
  level: z.number()
1328
1171
  }),
1329
- content: z.array(z.union([RemirrorText, RemirrorBr]))
1172
+ content: z.array(z.union([RemirrorText, RemirrorImage, RemirrorBr]))
1330
1173
  }).partial());
1331
1174
  var RemirrorBulletList = z.lazy(function () {
1332
1175
  return z.intersection(z.object({
@@ -1346,45 +1189,45 @@ var RemirrorListItem = z.lazy(function () {
1346
1189
  return z.intersection(z.object({
1347
1190
  type: z.literal("listItem")
1348
1191
  }), z.object({
1192
+ attrs: z.object({
1193
+ closed: z["boolean"](),
1194
+ nested: z["boolean"]()
1195
+ }).partial().optional(),
1349
1196
  content: z.array(z.union([RemirrorParagraph, RemirrorBulletList, RemirrorOrderedList]))
1350
1197
  }).partial());
1351
1198
  });
1352
- var RemirrorParagraph = z.intersection(z.object({
1353
- type: z.literal("paragraph")
1354
- }), z.object({
1355
- content: z.array(z.union([RemirrorText, RemirrorImage, RemirrorBr]))
1356
- }).partial());
1199
+ var RemirrorParagraph = z.lazy(function () {
1200
+ return z.intersection(z.object({
1201
+ type: z.literal("paragraph")
1202
+ }), z.object({
1203
+ content: z.array(z.union([RemirrorText, RemirrorImage, RemirrorBr]))
1204
+ }).partial());
1205
+ });
1357
1206
  var RemirrorJSON = z.object({
1358
1207
  type: z.literal("doc"),
1359
1208
  content: z.array(z.union([RemirrorParagraph, RemirrorHeading, RemirrorBulletList, RemirrorOrderedList]))
1360
1209
  });
1361
1210
 
1362
- function ownKeys(e, r) {
1363
- var t = Object.keys(e);
1364
- if (Object.getOwnPropertySymbols) {
1365
- var o = Object.getOwnPropertySymbols(e);
1366
- r && (o = o.filter(function (r) {
1367
- return Object.getOwnPropertyDescriptor(e, r).enumerable;
1368
- })), t.push.apply(t, o);
1369
- }
1370
- return t;
1211
+ function _arrayWithoutHoles(arr) {
1212
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
1371
1213
  }
1372
- function _objectSpread2(e) {
1373
- for (var r = 1; r < arguments.length; r++) {
1374
- var t = null != arguments[r] ? arguments[r] : {};
1375
- r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
1376
- _defineProperty(e, r, t[r]);
1377
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
1378
- Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
1379
- });
1380
- }
1381
- return e;
1214
+
1215
+ function _iterableToArray(iter) {
1216
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
1217
+ }
1218
+
1219
+ function _nonIterableSpread() {
1220
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1221
+ }
1222
+
1223
+ function _toConsumableArray(arr) {
1224
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
1382
1225
  }
1383
1226
 
1384
1227
  function richTextToRemirror(richtext) {
1385
1228
  return {
1386
1229
  type: "doc",
1387
- content: richtext.children.map(function (child) {
1230
+ content: richtext.map(function (child) {
1388
1231
  return toRemirrorNode(child);
1389
1232
  })
1390
1233
  };
@@ -1409,8 +1252,6 @@ function toRemirrorNode(child) {
1409
1252
  return convertUlToRemirror(child);
1410
1253
  case "ol":
1411
1254
  return convertOlToRemirror(child);
1412
- case "br":
1413
- return convertParagraphToRemirror([child]);
1414
1255
  default:
1415
1256
  {
1416
1257
  var _exhaustiveCheck = child;
@@ -1424,7 +1265,7 @@ function convertHeadingToRemirror(level, children) {
1424
1265
  attrs: {
1425
1266
  level: level
1426
1267
  },
1427
- content: children.flatMap(convertStringOrSpanNodeToRemirror)
1268
+ content: children.flatMap(convertInlineToRemirror)
1428
1269
  };
1429
1270
  }
1430
1271
  function convertStringToRemirror(child) {
@@ -1433,14 +1274,23 @@ function convertStringToRemirror(child) {
1433
1274
  text: child
1434
1275
  };
1435
1276
  }
1436
- function convertStringOrSpanNodeToRemirror(child) {
1277
+ function convertInlineToRemirror(child) {
1437
1278
  if (typeof child === "string") {
1438
1279
  return [convertStringToRemirror(child)];
1439
1280
  }
1281
+ if (child.tag === "img") {
1282
+ return [convertImageNodeToRemirror(child)];
1283
+ }
1284
+ if (child.tag === "br") {
1285
+ return [createRemirrorBr()];
1286
+ }
1287
+ if (child.tag === "a") {
1288
+ return convertLinkNodeToRemirror(child);
1289
+ }
1440
1290
  return convertSpanNodeToRemirror(child);
1441
1291
  }
1442
1292
  function convertSpanNodeToRemirror(spanNode) {
1443
- if (spanNode.classes.length === 0 && spanNode.children.length === 1) {
1293
+ if (spanNode.styles.length === 0 && spanNode.children.length === 1) {
1444
1294
  if (typeof spanNode.children[0] === "string") {
1445
1295
  return [convertStringToRemirror(spanNode.children[0])];
1446
1296
  } else {
@@ -1448,12 +1298,13 @@ function convertSpanNodeToRemirror(spanNode) {
1448
1298
  throw Error("Unexpected amount of children in span node: " + JSON.stringify(spanNode, null, 2));
1449
1299
  }
1450
1300
  }
1301
+ var uniqueStyles = Array.from(new Set(spanNode.styles));
1451
1302
  return spanNode.children.map(function (child) {
1452
1303
  return {
1453
1304
  type: "text",
1454
1305
  text: child,
1455
- marks: spanNode.classes.map(function (className) {
1456
- switch (className) {
1306
+ marks: uniqueStyles.map(function (style) {
1307
+ switch (style) {
1457
1308
  case "bold":
1458
1309
  return {
1459
1310
  type: "bold"
@@ -1468,7 +1319,7 @@ function convertSpanNodeToRemirror(spanNode) {
1468
1319
  };
1469
1320
  default:
1470
1321
  {
1471
- var _exhaustiveCheck = className;
1322
+ var _exhaustiveCheck = style;
1472
1323
  throw Error("Unexpected span class: " + _exhaustiveCheck);
1473
1324
  }
1474
1325
  }
@@ -1508,9 +1359,12 @@ function createRemirrorBr() {
1508
1359
  };
1509
1360
  }
1510
1361
  function convertLinkNodeToRemirror(linkNode) {
1511
- return linkNode.children.flatMap(convertStringOrSpanNodeToRemirror).map(function (remirrorText) {
1512
- return _objectSpread2(_objectSpread2({}, remirrorText), {}, {
1513
- marks: (remirrorText.marks || []).concat({
1362
+ return linkNode.children.flatMap(convertInlineToRemirror).map(function (remirrorNode) {
1363
+ if (remirrorNode.type !== "text") {
1364
+ return remirrorNode;
1365
+ }
1366
+ return _objectSpread2(_objectSpread2({}, remirrorNode), {}, {
1367
+ marks: (remirrorNode.marks || []).concat({
1514
1368
  type: "link",
1515
1369
  attrs: {
1516
1370
  href: linkNode.href,
@@ -1522,12 +1376,18 @@ function convertLinkNodeToRemirror(linkNode) {
1522
1376
  });
1523
1377
  }
1524
1378
  function convertImageNodeToRemirror(imageNode) {
1379
+ var _fileSource$metadata, _fileSource$metadata2;
1380
+ var fileSource = imageNode.src;
1381
+ if (!(VAL_EXTENSION in fileSource) || fileSource[VAL_EXTENSION] !== "file") {
1382
+ throw Error("Expected file source in image node");
1383
+ }
1384
+ var fileVal = Internal.convertFileSource(fileSource);
1525
1385
  return {
1526
1386
  type: "image",
1527
1387
  attrs: {
1528
- height: imageNode.height,
1529
- width: imageNode.width,
1530
- 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?
1388
+ height: (_fileSource$metadata = fileSource.metadata) === null || _fileSource$metadata === void 0 ? void 0 : _fileSource$metadata.height,
1389
+ width: (_fileSource$metadata2 = fileSource.metadata) === null || _fileSource$metadata2 === void 0 ? void 0 : _fileSource$metadata2.width,
1390
+ src: fileVal.url
1531
1391
  }
1532
1392
  };
1533
1393
  }
@@ -1569,24 +1429,6 @@ function convertListItemToRemirrorParagraph(rtChildren) {
1569
1429
  lastChildContent.push(convertStringToRemirror(child));
1570
1430
  } else {
1571
1431
  switch (child.tag) {
1572
- case "a":
1573
- {
1574
- lastChildContent.push.apply(lastChildContent, _toConsumableArray(convertLinkNodeToRemirror(child)));
1575
- break;
1576
- }
1577
- case "br":
1578
- {
1579
- children.push({
1580
- type: "paragraph",
1581
- content: [] // create new paragraph
1582
- });
1583
- break;
1584
- }
1585
- case "span":
1586
- {
1587
- lastChildContent.push.apply(lastChildContent, _toConsumableArray(convertSpanNodeToRemirror(child)));
1588
- break;
1589
- }
1590
1432
  case "ol":
1591
1433
  {
1592
1434
  children.push(convertOlToRemirror(child));
@@ -1597,6 +1439,20 @@ function convertListItemToRemirrorParagraph(rtChildren) {
1597
1439
  children.push(convertUlToRemirror(child));
1598
1440
  break;
1599
1441
  }
1442
+ case "p":
1443
+ {
1444
+ var newChild = convertParagraphToRemirror(child.children);
1445
+ if (lastChild.type === "paragraph" && newChild.type === "paragraph") {
1446
+ if (newChild.content) {
1447
+ lastChildContent.push.apply(lastChildContent, _toConsumableArray(newChild.content));
1448
+ } else {
1449
+ // no content - skip
1450
+ }
1451
+ } else {
1452
+ children.push(newChild);
1453
+ }
1454
+ break;
1455
+ }
1600
1456
  default:
1601
1457
  {
1602
1458
  var _exhaustiveCheck = child;
@@ -2327,4 +2183,4 @@ function createSubscriberId(paths) {
2327
2183
  return paths.slice().sort().join("&");
2328
2184
  }
2329
2185
 
2330
- export { EXT_TO_MIME_TYPES, MIME_TYPES_TO_EXT, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValStore, filenameToMimeType, getMimeType, mimeTypeToFileExt, parseRichTextSource, remirrorToRichTextSource, richTextToRemirror };
2186
+ export { EXT_TO_MIME_TYPES, MIME_TYPES_TO_EXT, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValStore, filenameToMimeType, getMimeType, mimeTypeToFileExt, remirrorToRichTextSource, richTextToRemirror };