@tinacms/mdx 1.3.0 → 1.3.2

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 (72) hide show
  1. package/dist/index.es.js +2232 -1308
  2. package/dist/index.js +15747 -11148
  3. package/dist/next/index.d.ts +2 -0
  4. package/dist/next/parse/index.d.ts +2 -0
  5. package/dist/next/parse/markdown.d.ts +2 -0
  6. package/dist/next/parse/post-processing.d.ts +3 -0
  7. package/dist/next/shortcodes/index.d.ts +2 -0
  8. package/dist/next/shortcodes/lib/factory-tag.d.ts +4 -0
  9. package/dist/next/shortcodes/lib/jsx-flow.d.ts +3 -0
  10. package/dist/next/shortcodes/lib/jsx-text.d.ts +3 -0
  11. package/dist/next/shortcodes/lib/syntax.d.ts +17 -0
  12. package/dist/next/shortcodes/lib/util.d.ts +3 -0
  13. package/dist/next/shortcodes/mdast/index.d.ts +106 -0
  14. package/dist/next/stringify/acorn.d.ts +21 -0
  15. package/dist/next/stringify/index.d.ts +3 -0
  16. package/dist/next/stringify/marks.d.ts +8 -0
  17. package/dist/next/stringify/pre-processing.d.ts +9 -0
  18. package/dist/next/stringify/to-markdown.d.ts +3 -0
  19. package/dist/next/tests/block-level-1/field.d.ts +2 -0
  20. package/dist/next/tests/block-level-2/field.d.ts +2 -0
  21. package/dist/next/tests/block-level-2/index.test.d.ts +1 -0
  22. package/dist/next/tests/block-level-3/field.d.ts +2 -0
  23. package/dist/next/tests/block-level-3/index.test.d.ts +1 -0
  24. package/dist/next/tests/block-level-nested/field.d.ts +2 -0
  25. package/dist/next/tests/block-level-nested/index.test.d.ts +1 -0
  26. package/dist/next/tests/block-level-unclosed/field.d.ts +2 -0
  27. package/dist/next/tests/block-level-unclosed/index.test.d.ts +1 -0
  28. package/dist/next/tests/expressions-in-text/field.d.ts +2 -0
  29. package/dist/next/tests/expressions-in-text/index.test.d.ts +1 -0
  30. package/dist/next/tests/image-callback/field.d.ts +2 -0
  31. package/dist/next/tests/image-callback/index.test.d.ts +1 -0
  32. package/dist/next/tests/inline/field.d.ts +2 -0
  33. package/dist/next/tests/inline/index.test.d.ts +1 -0
  34. package/dist/next/tests/inline-with-children/field.d.ts +2 -0
  35. package/dist/next/tests/inline-with-children/index.test.d.ts +1 -0
  36. package/dist/next/tests/invalid/field.d.ts +2 -0
  37. package/dist/next/tests/invalid/index.test.d.ts +1 -0
  38. package/dist/next/tests/invalid-2/field.d.ts +2 -0
  39. package/dist/next/tests/invalid-2/index.test.d.ts +1 -0
  40. package/dist/next/tests/invalid-3/field.d.ts +2 -0
  41. package/dist/next/tests/invalid-3/index.test.d.ts +1 -0
  42. package/dist/next/tests/invalid-4/field.d.ts +2 -0
  43. package/dist/next/tests/invalid-4/index.test.d.ts +1 -0
  44. package/dist/next/tests/lists/field.d.ts +2 -0
  45. package/dist/next/tests/lists/index.test.d.ts +1 -0
  46. package/dist/next/tests/markdoc/field.d.ts +2 -0
  47. package/dist/next/tests/markdoc/index.test.d.ts +1 -0
  48. package/dist/next/tests/unrecognized-shortcodes/field.d.ts +2 -0
  49. package/dist/next/tests/unrecognized-shortcodes/index.test.d.ts +1 -0
  50. package/dist/next/tests/util.d.ts +4 -0
  51. package/dist/next/tests/wordpress-style/field.d.ts +2 -0
  52. package/dist/next/tests/wordpress-style/index.test.d.ts +1 -0
  53. package/dist/next/tests/wordpress-style-2/field.d.ts +2 -0
  54. package/dist/next/tests/wordpress-style-2/index.test.d.ts +1 -0
  55. package/dist/next/tests/wordpress-style-2-block-with-children/field.d.ts +2 -0
  56. package/dist/next/tests/wordpress-style-2-block-with-children/index.test.d.ts +1 -0
  57. package/dist/next/tests/wordpress-style-2-with-children/field.d.ts +2 -0
  58. package/dist/next/tests/wordpress-style-2-with-children/index.test.d.ts +1 -0
  59. package/dist/next/util.d.ts +3 -0
  60. package/dist/parse/acorn.d.ts +2 -2
  61. package/dist/parse/index.d.ts +3 -3
  62. package/dist/parse/mdx.d.ts +4 -4
  63. package/dist/parse/parseShortcode.d.ts +2 -4
  64. package/dist/parse/remarkToPlate.d.ts +2 -2
  65. package/dist/stringify/acorn.d.ts +4 -4
  66. package/dist/stringify/index.d.ts +5 -5
  67. package/dist/stringify/marks.d.ts +2 -2
  68. package/dist/stringify/stringifyShortcode.d.ts +2 -4
  69. package/dist/tests/autotest/_config.d.ts +2 -2
  70. package/dist/tests/setup.d.ts +4 -4
  71. package/package.json +53 -47
  72. /package/dist/{tests/temp/shortcodes.test.d.ts → next/tests/block-level-1/index.test.d.ts} +0 -0
package/dist/index.es.js CHANGED
@@ -8,209 +8,471 @@ var __publicField = (obj, key, value) => {
8
8
  // src/parse/index.ts
9
9
  import { remark } from "remark";
10
10
  import remarkMdx from "remark-mdx";
11
- import { fromMarkdown } from "mdast-util-from-markdown";
12
11
 
13
- // src/parse/remarkToPlate.ts
14
- import { flatten } from "lodash-es";
12
+ // src/next/stringify/to-markdown.ts
13
+ import { toMarkdown } from "mdast-util-to-markdown";
14
+ import { text } from "mdast-util-to-markdown/lib/handle/text";
15
15
 
16
- // src/parse/acorn.ts
17
- var extractAttributes = (attributes2, fields, imageCallback) => {
18
- const properties = {};
19
- attributes2?.forEach((attribute) => {
20
- assertType(attribute, "mdxJsxAttribute");
21
- const field = fields.find((field2) => field2.name === attribute.name);
22
- if (!field) {
23
- throw new Error(`Unable to find field definition for property "${attribute.name}"`);
24
- }
25
- try {
26
- properties[attribute.name] = extractAttribute(attribute, field, imageCallback);
27
- } catch (e) {
28
- if (e instanceof Error) {
29
- throw new Error(`Unable to parse field value for field "${field.name}" (type: ${field.type}). ${e.message}`);
16
+ // src/next/shortcodes/mdast/index.ts
17
+ import { ccount } from "ccount";
18
+ import { parseEntities } from "parse-entities";
19
+ import { stringifyPosition } from "unist-util-stringify-position";
20
+ import { VFileMessage } from "vfile-message";
21
+ import { stringifyEntitiesLight } from "stringify-entities";
22
+ import { containerFlow } from "mdast-util-to-markdown/lib/util/container-flow.js";
23
+ import { containerPhrasing } from "mdast-util-to-markdown/lib/util/container-phrasing.js";
24
+ import { indentLines } from "mdast-util-to-markdown/lib/util/indent-lines.js";
25
+ import { track } from "mdast-util-to-markdown/lib/util/track.js";
26
+ function mdxJsxFromMarkdown({ patterns }) {
27
+ const buffer = function() {
28
+ this.buffer();
29
+ };
30
+ const data = function(token) {
31
+ this.config?.enter?.data?.call(this, token);
32
+ this.config?.exit?.data?.call(this, token);
33
+ };
34
+ const enterMdxJsxTag = function(token) {
35
+ const tag = {
36
+ name: void 0,
37
+ attributes: [],
38
+ close: false,
39
+ selfClosing: false,
40
+ start: token.start,
41
+ end: token.end
42
+ };
43
+ if (!this.getData("mdxJsxTagStack"))
44
+ this.setData("mdxJsxTagStack", []);
45
+ this.setData("mdxJsxTag", tag);
46
+ this.buffer();
47
+ };
48
+ const enterMdxJsxTagClosingMarker = function(token) {
49
+ const stack = this.getData("mdxJsxTagStack");
50
+ const tag = this.getData("mdxJsxTag");
51
+ if (stack?.length === 0) {
52
+ if (tag) {
53
+ tag.shouldFallback = true;
30
54
  }
31
- throw e;
32
55
  }
33
- });
34
- return properties;
35
- };
36
- var extractAttribute = (attribute, field, imageCallback) => {
37
- switch (field.type) {
38
- case "boolean":
39
- case "number":
40
- return extractScalar(extractExpression(attribute), field);
41
- case "datetime":
42
- case "string":
43
- if (field.list) {
44
- return extractScalar(extractExpression(attribute), field);
45
- } else {
46
- return extractString(attribute, field);
56
+ };
57
+ const enterMdxJsxTagAnyAttribute = function(token) {
58
+ const tag = this.getData("mdxJsxTag");
59
+ };
60
+ const enterMdxJsxTagSelfClosingMarker = function(token) {
61
+ const tag = this.getData("mdxJsxTag");
62
+ if (tag?.close) {
63
+ throw new VFileMessage("Unexpected self-closing slash `/` in closing tag, expected the end of the tag", { start: token.start, end: token.end }, "mdast-util-mdx-jsx:unexpected-self-closing-slash");
64
+ }
65
+ };
66
+ const exitMdxJsxTagClosingMarker = function() {
67
+ const tag = this.getData("mdxJsxTag");
68
+ if (tag) {
69
+ tag.close = true;
70
+ }
71
+ };
72
+ const exitMdxJsxTagNamePrimary = function(token) {
73
+ const tag = this.getData("mdxJsxTag");
74
+ if (tag) {
75
+ tag.name = this.sliceSerialize(token);
76
+ }
77
+ };
78
+ const exitMdxJsxTagNameMember = function(token) {
79
+ const tag = this.getData("mdxJsxTag");
80
+ if (tag) {
81
+ tag.name += "." + this.sliceSerialize(token);
82
+ }
83
+ };
84
+ const exitMdxJsxTagNameLocal = function(token) {
85
+ const tag = this.getData("mdxJsxTag");
86
+ if (tag) {
87
+ tag.name += ":" + this.sliceSerialize(token);
88
+ }
89
+ };
90
+ const enterMdxJsxTagAttribute = function(token) {
91
+ const tag = this.getData("mdxJsxTag");
92
+ enterMdxJsxTagAnyAttribute.call(this, token);
93
+ if (tag) {
94
+ tag.attributes.push({ type: "mdxJsxAttribute", name: "", value: null });
95
+ }
96
+ };
97
+ const enterMdxJsxTagExpressionAttribute = function(token) {
98
+ const tag = this.getData("mdxJsxTag");
99
+ enterMdxJsxTagAnyAttribute.call(this, token);
100
+ if (tag) {
101
+ tag.attributes.push({ type: "mdxJsxExpressionAttribute", value: "" });
102
+ }
103
+ this.buffer();
104
+ };
105
+ const exitMdxJsxTagExpressionAttribute = function(token) {
106
+ const tag = this.getData("mdxJsxTag");
107
+ if (tag) {
108
+ const tail = tag.attributes[tag.attributes.length - 1];
109
+ const estree = token.estree;
110
+ if (tail) {
111
+ tail.value = this.resume();
112
+ if (estree) {
113
+ tail.data = { estree };
114
+ }
47
115
  }
48
- case "image":
49
- if (field.list) {
50
- const values2 = extractScalar(extractExpression(attribute), field);
51
- return values2.split(",").map((value) => imageCallback(value));
52
- } else {
53
- const value = extractString(attribute, field);
54
- return imageCallback(value);
116
+ }
117
+ };
118
+ const exitMdxJsxTagAttributeNamePrimary = function(token) {
119
+ const tag = this.getData("mdxJsxTag");
120
+ if (tag) {
121
+ const node = tag.attributes[tag.attributes.length - 1];
122
+ if (node) {
123
+ node.name = this.sliceSerialize(token);
55
124
  }
56
- case "reference":
57
- if (field.list) {
58
- return extractScalar(extractExpression(attribute), field);
59
- } else {
60
- return extractString(attribute, field);
125
+ }
126
+ };
127
+ const exitMdxJsxTagAttributeNameLocal = function(token) {
128
+ const tag = this.getData("mdxJsxTag");
129
+ if (tag) {
130
+ const node = tag.attributes[tag.attributes.length - 1];
131
+ if (node) {
132
+ node.name += ":" + this.sliceSerialize(token);
61
133
  }
62
- case "object":
63
- return extractObject(extractExpression(attribute), field);
64
- case "rich-text":
65
- const JSXString = extractRaw(attribute);
66
- if (JSXString) {
67
- return parseMDX(JSXString, field, imageCallback);
68
- } else {
69
- return {};
134
+ }
135
+ };
136
+ const exitMdxJsxTagAttributeValueLiteral = function() {
137
+ const tag = this.getData("mdxJsxTag");
138
+ if (tag) {
139
+ const attribute = tag.attributes[tag.attributes.length - 1];
140
+ if (attribute) {
141
+ if (attribute.name === "") {
142
+ attribute.name = "_value";
143
+ }
144
+ attribute.value = parseEntities(this.resume(), { nonTerminated: false });
70
145
  }
71
- default:
72
- throw new Error(`Extract attribute: Unhandled field type ${field.type}`);
73
- }
74
- };
75
- var extractScalar = (attribute, field) => {
76
- if (field.list) {
77
- assertType(attribute.expression, "ArrayExpression");
78
- return attribute.expression.elements.map((element) => {
79
- assertHasType(element);
80
- assertType(element, "Literal");
81
- return element.value;
82
- });
83
- } else {
84
- assertType(attribute.expression, "Literal");
85
- return attribute.expression.value;
86
- }
87
- };
88
- var extractObject = (attribute, field) => {
89
- if (field.list) {
90
- assertType(attribute.expression, "ArrayExpression");
91
- return attribute.expression.elements.map((element) => {
92
- assertHasType(element);
93
- assertType(element, "ObjectExpression");
94
- return extractObjectExpression(element, field);
95
- });
96
- } else {
97
- assertType(attribute.expression, "ObjectExpression");
98
- return extractObjectExpression(attribute.expression, field);
99
- }
100
- };
101
- var extractObjectExpression = (expression, field) => {
102
- const properties = {};
103
- expression.properties?.forEach((property) => {
104
- assertType(property, "Property");
105
- const { key, value } = extractKeyValue(property, field);
106
- properties[key] = value;
107
- });
108
- return properties;
109
- };
110
- var getField = (objectField, name) => {
111
- if (objectField.fields) {
112
- if (typeof objectField.fields === "string") {
113
- throw new Error("Global templates not supported");
114
146
  }
115
- return objectField.fields.find((f) => f.name === name);
116
- }
117
- };
118
- var extractKeyValue = (property, parentField) => {
119
- assertType(property.key, "Identifier");
120
- const key = property.key.name;
121
- const field = getField(parentField, key);
122
- if (field?.type === "object") {
123
- if (field.list) {
124
- assertType(property.value, "ArrayExpression");
125
- const value = property.value.elements.map((element) => {
126
- assertHasType(element);
127
- assertType(element, "ObjectExpression");
128
- return extractObjectExpression(element, field);
129
- });
130
- return { key, value };
131
- } else {
132
- assertType(property.value, "ObjectExpression");
133
- const value = extractObjectExpression(property.value, field);
134
- return { key, value };
147
+ };
148
+ const exitMdxJsxTagAttributeValueExpression = function(token) {
149
+ const tag = this.getData("mdxJsxTag");
150
+ if (!tag)
151
+ return;
152
+ const tail = tag.attributes[tag.attributes.length - 1];
153
+ const node = {
154
+ type: "mdxJsxAttributeValueExpression",
155
+ value: this.resume()
156
+ };
157
+ const estree = token.estree;
158
+ if (estree) {
159
+ node.data = { estree };
135
160
  }
136
- } else {
137
- assertType(property.value, "Literal");
138
- return { key, value: property.value.value };
139
- }
140
- };
141
- var extractStatement = (attribute) => {
142
- const body = attribute.data?.estree?.body;
143
- if (body) {
144
- if (body[0]) {
145
- assertType(body[0], "ExpressionStatement");
146
- return body[0];
161
+ if (tail) {
162
+ tail.value = node;
147
163
  }
148
- }
149
- throw new Error(`Unable to extract body from expression`);
150
- };
151
- var extractString = (attribute, field) => {
152
- if (attribute.type === "mdxJsxAttribute") {
153
- if (typeof attribute.value === "string") {
154
- return attribute.value;
164
+ };
165
+ const exitMdxJsxTagSelfClosingMarker = function() {
166
+ const tag = this.getData("mdxJsxTag");
167
+ if (tag) {
168
+ tag.selfClosing = true;
155
169
  }
156
- }
157
- return extractScalar(extractExpression(attribute), field);
158
- };
159
- var extractExpression = (attribute) => {
160
- assertType(attribute, "mdxJsxAttribute");
161
- assertHasType(attribute.value);
162
- assertType(attribute.value, "mdxJsxAttributeValueExpression");
163
- return extractStatement(attribute.value);
164
- };
165
- var extractRaw = (attribute) => {
166
- assertType(attribute, "mdxJsxAttribute");
167
- assertHasType(attribute.value);
168
- assertType(attribute.value, "mdxJsxAttributeValueExpression");
169
- const rawValue = attribute.value.value;
170
- return trimFragments(rawValue);
171
- };
172
- function assertType(val, type) {
173
- if (val.type !== type) {
174
- throw new Error(`Expected type to be ${type} but received ${val.type}. ${MDX_PARSE_ERROR_MSG}`);
175
- }
176
- }
177
- function assertHasType(val) {
178
- if (val) {
179
- if (typeof val !== "string") {
170
+ };
171
+ const exitMdxJsxTag = function(token) {
172
+ const tag = this.getData("mdxJsxTag");
173
+ const stack = this.getData("mdxJsxTagStack");
174
+ if (!stack)
180
175
  return;
176
+ const tail = stack[stack.length - 1];
177
+ if (!tag)
178
+ return;
179
+ if (tail && tag.close && tail.name !== tag.name) {
180
+ throw new VFileMessage("Unexpected closing tag `" + serializeAbbreviatedTag(tag) + "`, expected corresponding closing tag for `" + serializeAbbreviatedTag(tail) + "` (" + stringifyPosition(tail) + ")", { start: token.start, end: token.end }, "mdast-util-mdx-jsx:end-tag-mismatch");
181
+ }
182
+ this.resume();
183
+ if (tag.close) {
184
+ stack.pop();
185
+ } else {
186
+ const pattern = patterns.find((pattern2) => pattern2.name === tag.name);
187
+ const tagName = pattern?.templateName || tag.name;
188
+ this.enter({
189
+ type: token.type === "mdxJsxTextTag" ? "mdxJsxTextElement" : "mdxJsxFlowElement",
190
+ name: tagName || null,
191
+ attributes: tag.attributes,
192
+ children: []
193
+ }, token, (left, right) => {
194
+ this.exit(right);
195
+ });
196
+ }
197
+ if (tag.selfClosing || tag.close) {
198
+ if (tag.shouldFallback) {
199
+ if (token.type === "mdxJsxFlowTag") {
200
+ this.enter({
201
+ type: "paragraph",
202
+ children: [{ type: "text", value: this.sliceSerialize(token) }]
203
+ }, token);
204
+ this.exit(token);
205
+ } else {
206
+ this.enter({
207
+ type: "text",
208
+ value: this.sliceSerialize(token)
209
+ }, token);
210
+ this.exit(token);
211
+ }
212
+ } else {
213
+ this.exit(token, onErrorLeftIsTag);
214
+ }
215
+ } else {
216
+ stack.push(tag);
181
217
  }
218
+ };
219
+ const onErrorRightIsTag = function(closing, open) {
220
+ const tag = this.getData("mdxJsxTag");
221
+ if (!tag)
222
+ return;
223
+ const place = closing ? " before the end of `" + closing.type + "`" : "";
224
+ const position = closing ? { start: closing.start, end: closing.end } : void 0;
225
+ throw new VFileMessage("Expected a closing tag for `" + serializeAbbreviatedTag(tag) + "` (" + stringifyPosition({ start: open.start, end: open.end }) + ")" + place, position, "mdast-util-mdx-jsx:end-tag-mismatch");
226
+ };
227
+ const onErrorLeftIsTag = function(a, b) {
228
+ const tag = this.getData("mdxJsxTag");
229
+ };
230
+ function serializeAbbreviatedTag(tag) {
231
+ return "<" + (tag.close ? "/" : "") + (tag.name || "") + ">";
182
232
  }
183
- throw new Error(`Expect value to be an object with property "type"`);
233
+ return {
234
+ canContainEols: ["mdxJsxTextElement"],
235
+ enter: {
236
+ mdxJsxFlowTag: enterMdxJsxTag,
237
+ mdxJsxFlowTagClosingMarker: enterMdxJsxTagClosingMarker,
238
+ mdxJsxFlowTagAttribute: enterMdxJsxTagAttribute,
239
+ mdxJsxFlowTagExpressionAttribute: enterMdxJsxTagExpressionAttribute,
240
+ mdxJsxFlowTagAttributeValueLiteral: buffer,
241
+ mdxJsxFlowTagAttributeValueExpression: buffer,
242
+ mdxJsxFlowTagSelfClosingMarker: enterMdxJsxTagSelfClosingMarker,
243
+ mdxJsxTextTag: enterMdxJsxTag,
244
+ mdxJsxTextTagClosingMarker: enterMdxJsxTagClosingMarker,
245
+ mdxJsxTextTagAttribute: enterMdxJsxTagAttribute,
246
+ mdxJsxTextTagExpressionAttribute: enterMdxJsxTagExpressionAttribute,
247
+ mdxJsxTextTagAttributeValueLiteral: buffer,
248
+ mdxJsxTextTagAttributeValueExpression: buffer,
249
+ mdxJsxTextTagSelfClosingMarker: enterMdxJsxTagSelfClosingMarker
250
+ },
251
+ exit: {
252
+ mdxJsxFlowTagClosingMarker: exitMdxJsxTagClosingMarker,
253
+ mdxJsxFlowTagNamePrimary: exitMdxJsxTagNamePrimary,
254
+ mdxJsxFlowTagNameMember: exitMdxJsxTagNameMember,
255
+ mdxJsxFlowTagNameLocal: exitMdxJsxTagNameLocal,
256
+ mdxJsxFlowTagExpressionAttribute: exitMdxJsxTagExpressionAttribute,
257
+ mdxJsxFlowTagExpressionAttributeValue: data,
258
+ mdxJsxFlowTagAttributeNamePrimary: exitMdxJsxTagAttributeNamePrimary,
259
+ mdxJsxFlowTagAttributeNameLocal: exitMdxJsxTagAttributeNameLocal,
260
+ mdxJsxFlowTagAttributeValueLiteral: exitMdxJsxTagAttributeValueLiteral,
261
+ mdxJsxFlowTagAttributeValueLiteralValue: data,
262
+ mdxJsxFlowTagAttributeValueExpression: exitMdxJsxTagAttributeValueExpression,
263
+ mdxJsxFlowTagAttributeValueExpressionValue: data,
264
+ mdxJsxFlowTagSelfClosingMarker: exitMdxJsxTagSelfClosingMarker,
265
+ mdxJsxFlowTag: exitMdxJsxTag,
266
+ mdxJsxTextTagClosingMarker: exitMdxJsxTagClosingMarker,
267
+ mdxJsxTextTagNamePrimary: exitMdxJsxTagNamePrimary,
268
+ mdxJsxTextTagNameMember: exitMdxJsxTagNameMember,
269
+ mdxJsxTextTagNameLocal: exitMdxJsxTagNameLocal,
270
+ mdxJsxTextTagExpressionAttribute: exitMdxJsxTagExpressionAttribute,
271
+ mdxJsxTextTagExpressionAttributeValue: data,
272
+ mdxJsxTextTagAttributeNamePrimary: exitMdxJsxTagAttributeNamePrimary,
273
+ mdxJsxTextTagAttributeNameLocal: exitMdxJsxTagAttributeNameLocal,
274
+ mdxJsxTextTagAttributeValueLiteral: exitMdxJsxTagAttributeValueLiteral,
275
+ mdxJsxTextTagAttributeValueLiteralValue: data,
276
+ mdxJsxTextTagAttributeValueExpression: exitMdxJsxTagAttributeValueExpression,
277
+ mdxJsxTextTagAttributeValueExpressionValue: data,
278
+ mdxJsxTextTagSelfClosingMarker: exitMdxJsxTagSelfClosingMarker,
279
+ mdxJsxTextTag: exitMdxJsxTag
280
+ }
281
+ };
184
282
  }
185
- var trimFragments = (string) => {
186
- const rawArr = string.split("\n");
187
- let openingFragmentIndex = null;
188
- let closingFragmentIndex = null;
189
- rawArr.forEach((item, index) => {
190
- if (item.trim() === "<>") {
191
- if (!openingFragmentIndex) {
192
- openingFragmentIndex = index + 1;
283
+ var mdxJsxToMarkdown = function(options) {
284
+ const patterns = options.patterns || [];
285
+ const options_ = options || {};
286
+ const quote = options_.quote || '"';
287
+ const quoteSmart = options_.quoteSmart || false;
288
+ const tightSelfClosing = options_.tightSelfClosing || false;
289
+ const printWidth = options_.printWidth || Number.POSITIVE_INFINITY;
290
+ const alternative = quote === '"' ? "'" : '"';
291
+ if (quote !== '"' && quote !== "'") {
292
+ throw new Error("Cannot serialize attribute values with `" + quote + "` for `options.quote`, expected `\"`, or `'`");
293
+ }
294
+ const mdxElement = function(node, _, context, safeOptions) {
295
+ const pattern = patterns.find((p) => p.templateName === node.name);
296
+ if (!pattern) {
297
+ return "";
298
+ }
299
+ const patternName = pattern.name || pattern?.templateName;
300
+ const tracker = track(safeOptions);
301
+ const selfClosing = pattern.leaf;
302
+ const exit = context.enter(node.type);
303
+ let index = -1;
304
+ const serializedAttributes = [];
305
+ let value = tracker.move(pattern.start + " " + (patternName || ""));
306
+ if (node.attributes && node.attributes.length > 0) {
307
+ if (!node.name) {
308
+ throw new Error("Cannot serialize fragment w/ attributes");
309
+ }
310
+ while (++index < node.attributes.length) {
311
+ const attribute = node.attributes[index];
312
+ let result;
313
+ if (attribute?.type === "mdxJsxExpressionAttribute") {
314
+ result = "{" + (attribute.value || "") + "}";
315
+ } else {
316
+ if (!attribute?.name) {
317
+ throw new Error("Cannot serialize attribute w/o name");
318
+ }
319
+ const value2 = attribute.value;
320
+ const left = attribute.name;
321
+ let right = "";
322
+ if (value2 === void 0 || value2 === null) {
323
+ } else if (typeof value2 === "object") {
324
+ right = "{" + (value2.value || "") + "}";
325
+ } else {
326
+ const appliedQuote = quoteSmart && ccount(value2, quote) > ccount(value2, alternative) ? alternative : quote;
327
+ right = appliedQuote + stringifyEntitiesLight(value2, { subset: [appliedQuote] }) + appliedQuote;
328
+ }
329
+ if (left === "_value") {
330
+ result = right;
331
+ } else {
332
+ result = left + (right ? "=" : "") + right;
333
+ }
334
+ }
335
+ serializedAttributes.push(result);
193
336
  }
194
337
  }
195
- });
196
- rawArr.reverse().forEach((item, index) => {
197
- if (item.trim() === "</>") {
198
- const length = rawArr.length - 1;
199
- if (!closingFragmentIndex) {
200
- closingFragmentIndex = length - index;
338
+ let attributesOnTheirOwnLine = false;
339
+ const attributesOnOneLine = serializedAttributes.join(" ");
340
+ if (node.type === "mdxJsxFlowElement" && (/\r?\n|\r/.test(attributesOnOneLine) || tracker.current().now.column + attributesOnOneLine.length + (selfClosing ? tightSelfClosing ? 2 : 3 : 1) > printWidth)) {
341
+ attributesOnTheirOwnLine = true;
342
+ }
343
+ if (attributesOnTheirOwnLine) {
344
+ value += tracker.move("\n" + indentLines(serializedAttributes.join("\n"), map));
345
+ } else if (attributesOnOneLine) {
346
+ value += tracker.move(" " + attributesOnOneLine);
347
+ }
348
+ if (attributesOnTheirOwnLine) {
349
+ value += tracker.move("\n");
350
+ }
351
+ if (selfClosing) {
352
+ value += tracker.move(tightSelfClosing || attributesOnTheirOwnLine ? "" : "");
353
+ }
354
+ value += tracker.move(" " + pattern.end);
355
+ if (node.children) {
356
+ if (node.type === "mdxJsxFlowElement") {
357
+ const emptyChildren = node.children.length === 1 && node.children[0]?.type === "paragraph" && node.children[0].children[0]?.type === "text" && node.children[0].children[0].value === "";
358
+ if (!emptyChildren) {
359
+ tracker.shift(2);
360
+ value += tracker.move("\n");
361
+ value += tracker.move(containerFlow(node, context, tracker.current()));
362
+ value += tracker.move("\n");
363
+ }
364
+ } else {
365
+ value += tracker.move(containerPhrasing(node, context, {
366
+ ...tracker.current(),
367
+ before: "<",
368
+ after: ">"
369
+ }));
201
370
  }
202
371
  }
203
- });
204
- const value = rawArr.reverse().slice(openingFragmentIndex || 0, closingFragmentIndex || rawArr.length - 1).join("\n");
205
- return value;
372
+ if (!selfClosing) {
373
+ value += tracker.move(pattern.start + " /" + (patternName || " ") + " " + pattern.end);
374
+ }
375
+ exit();
376
+ return value;
377
+ };
378
+ const map = function(line, _, blank) {
379
+ return (blank ? "" : " ") + line;
380
+ };
381
+ const peekElement = function() {
382
+ return "<";
383
+ };
384
+ mdxElement.peek = peekElement;
385
+ return {
386
+ ...options,
387
+ handlers: {
388
+ mdxJsxFlowElement: mdxElement,
389
+ mdxJsxTextElement: mdxElement
390
+ },
391
+ unsafe: [
392
+ { character: "<", inConstruct: ["phrasing"] },
393
+ { atBreak: true, character: "<" }
394
+ ],
395
+ fences: true,
396
+ resourceLink: true
397
+ };
206
398
  };
207
399
 
208
- // src/stringify/index.ts
209
- import { toMarkdown } from "mdast-util-to-markdown";
210
- import { text as text2 } from "mdast-util-to-markdown/lib/handle/text";
211
- import {
212
- mdxJsxToMarkdown
213
- } from "mdast-util-mdx-jsx";
400
+ // src/next/util.ts
401
+ var getFieldPatterns = (field) => {
402
+ const patterns = [];
403
+ const templates = [];
404
+ hoistAllTemplates(field, templates);
405
+ templates?.forEach((template) => {
406
+ if (typeof template === "string") {
407
+ throw new Error("Global templates not supported");
408
+ }
409
+ if (template.match) {
410
+ patterns.push({
411
+ start: template.match.start,
412
+ end: template.match.end,
413
+ name: template.match.name || template.name,
414
+ templateName: template.name,
415
+ type: template.inline ? "inline" : "flow",
416
+ leaf: !template.fields.some((f) => f.name === "children")
417
+ });
418
+ }
419
+ });
420
+ return patterns;
421
+ };
422
+ var hoistAllTemplates = (field, templates = []) => {
423
+ field.templates?.forEach((template) => {
424
+ if (typeof template === "string") {
425
+ throw new Error("Global templates not supported");
426
+ }
427
+ templates.push(template);
428
+ template.fields.forEach((field2) => {
429
+ if (field2.type === "rich-text") {
430
+ hoistAllTemplates(field2, templates);
431
+ }
432
+ });
433
+ });
434
+ return templates;
435
+ };
436
+
437
+ // src/next/stringify/to-markdown.ts
438
+ var toTinaMarkdown = (tree, field) => {
439
+ const patterns = getFieldPatterns(field);
440
+ const handlers = {};
441
+ handlers["text"] = (node, parent, context, safeOptions) => {
442
+ context.unsafe = context.unsafe.filter((unsafeItem) => {
443
+ if (unsafeItem.character === " " && unsafeItem.inConstruct === "phrasing") {
444
+ return false;
445
+ }
446
+ return true;
447
+ });
448
+ if (field.parser?.type === "markdown") {
449
+ if (field.parser.skipEscaping === "all") {
450
+ return node.value;
451
+ }
452
+ if (field.parser.skipEscaping === "html") {
453
+ context.unsafe = context.unsafe.filter((unsafeItem) => {
454
+ if (unsafeItem.character === "<") {
455
+ return false;
456
+ }
457
+ return true;
458
+ });
459
+ }
460
+ }
461
+ return text(node, parent, context, safeOptions);
462
+ };
463
+ return toMarkdown(tree, {
464
+ extensions: [mdxJsxToMarkdown({ patterns })],
465
+ listItemIndent: "one",
466
+ handlers
467
+ });
468
+ };
469
+
470
+ // src/stringify/index.ts
471
+ import { toMarkdown as toMarkdown2 } from "mdast-util-to-markdown";
472
+ import { text as text3 } from "mdast-util-to-markdown/lib/handle/text";
473
+ import {
474
+ mdxJsxToMarkdown as mdxJsxToMarkdown2
475
+ } from "mdast-util-mdx-jsx";
214
476
 
215
477
  // src/stringify/acorn.ts
216
478
  import { format } from "prettier";
@@ -509,12 +771,12 @@ var inlineElementExceptLink = (content, field, imageCallback) => {
509
771
  }
510
772
  default:
511
773
  if (!content.type && typeof content.text === "string") {
512
- return text(content);
774
+ return text2(content);
513
775
  }
514
776
  throw new Error(`InlineElement: ${content.type} is not supported`);
515
777
  }
516
778
  };
517
- var text = (content) => {
779
+ var text2 = (content) => {
518
780
  return {
519
781
  type: "text",
520
782
  value: content.text
@@ -547,11 +809,11 @@ var eat = (c, field, imageCallback) => {
547
809
  if (marks.length === 0) {
548
810
  if (first.linkifyTextNode) {
549
811
  return [
550
- first.linkifyTextNode(text(first)),
812
+ first.linkifyTextNode(text2(first)),
551
813
  ...eat(content.slice(1), field, imageCallback)
552
814
  ];
553
815
  } else {
554
- return [text(first), ...eat(content.slice(1), field, imageCallback)];
816
+ return [text2(first), ...eat(content.slice(1), field, imageCallback)];
555
817
  }
556
818
  }
557
819
  let nonMatchingSiblingIndex = 0;
@@ -587,7 +849,7 @@ var eat = (c, field, imageCallback) => {
587
849
  }
588
850
  });
589
851
  if (!markToProcess) {
590
- return [text(first), ...eat(content.slice(1), field, imageCallback)];
852
+ return [text2(first), ...eat(content.slice(1), field, imageCallback)];
591
853
  }
592
854
  if (markToProcess === "inlineCode") {
593
855
  if (nonMatchingSiblingIndex) {
@@ -634,11 +896,11 @@ var cleanNode = (node, mark) => {
634
896
  };
635
897
 
636
898
  // src/extensions/tina-shortcodes/to-markdown.ts
637
- import { stringifyEntitiesLight } from "stringify-entities";
638
- import { containerFlow } from "mdast-util-to-markdown/lib/util/container-flow";
639
- import { containerPhrasing } from "mdast-util-to-markdown/lib/util/container-phrasing";
899
+ import { stringifyEntitiesLight as stringifyEntitiesLight2 } from "stringify-entities";
900
+ import { containerFlow as containerFlow2 } from "mdast-util-to-markdown/lib/util/container-flow";
901
+ import { containerPhrasing as containerPhrasing2 } from "mdast-util-to-markdown/lib/util/container-phrasing";
640
902
  import { checkQuote } from "mdast-util-to-markdown/lib/util/check-quote";
641
- import { track } from "mdast-util-to-markdown/lib/util/track";
903
+ import { track as track2 } from "mdast-util-to-markdown/lib/util/track";
642
904
  var own = {}.hasOwnProperty;
643
905
  var directiveToMarkdown = (patterns) => ({
644
906
  unsafe: [
@@ -666,12 +928,12 @@ var directiveToMarkdown = (patterns) => ({
666
928
  });
667
929
  var handleDirective = function(patterns) {
668
930
  const handleDirective2 = function(node, _, state, safeOptions) {
669
- const tracker = track(safeOptions);
670
- const exit2 = state.enter(node.type);
931
+ const tracker = track2(safeOptions);
932
+ const exit = state.enter(node.type);
671
933
  const pattern = patterns.find((p) => p.name === node.name || p.templateName === node.name);
672
934
  if (!pattern) {
673
935
  console.log("no pattern found for directive", node.name);
674
- exit2();
936
+ exit();
675
937
  return "";
676
938
  }
677
939
  const patternName = pattern.name || pattern.templateName;
@@ -679,18 +941,18 @@ var handleDirective = function(patterns) {
679
941
  let value = tracker.move(sequence + " " + patternName);
680
942
  let label;
681
943
  if (label && label.children && label.children.length > 0) {
682
- const exit3 = state.enter("label");
944
+ const exit2 = state.enter("label");
683
945
  const labelType = `${node.type}Label`;
684
946
  const subexit = state.enter(labelType);
685
947
  value += tracker.move("[");
686
- value += tracker.move(containerPhrasing(label, state, {
948
+ value += tracker.move(containerPhrasing2(label, state, {
687
949
  ...tracker.current(),
688
950
  before: value,
689
951
  after: "]"
690
952
  }));
691
953
  value += tracker.move("]");
692
954
  subexit();
693
- exit3();
955
+ exit2();
694
956
  }
695
957
  value += tracker.move(" ");
696
958
  value += tracker.move(attributes(node, state));
@@ -703,12 +965,12 @@ var handleDirective = function(patterns) {
703
965
  }
704
966
  if (shallow && shallow.children && shallow.children.length > 0) {
705
967
  value += tracker.move("\n");
706
- value += tracker.move(containerFlow(shallow, state, tracker.current()));
968
+ value += tracker.move(containerFlow2(shallow, state, tracker.current()));
707
969
  }
708
970
  value += tracker.move("\n" + sequence);
709
- value += tracker.move(" \\" + patternName + " " + pattern.end);
971
+ value += tracker.move(" /" + patternName + " " + pattern.end);
710
972
  }
711
- exit2();
973
+ exit();
712
974
  return value;
713
975
  };
714
976
  handleDirective2.peek = peekDirective;
@@ -721,17 +983,17 @@ function attributes(node, state) {
721
983
  const quote = checkQuote(state);
722
984
  const subset = node.type === "textDirective" ? [quote] : [quote, "\n", "\r"];
723
985
  const attrs = node.attributes || {};
724
- const values2 = [];
986
+ const values3 = [];
725
987
  let key;
726
988
  for (key in attrs) {
727
989
  if (own.call(attrs, key) && attrs[key] !== void 0 && attrs[key] !== null) {
728
990
  const value = String(attrs[key]);
729
- values2.push(quoted(key, value));
991
+ values3.push(quoted(key, value));
730
992
  }
731
993
  }
732
- return values2.length > 0 ? values2.join(" ") + " " : "";
994
+ return values3.length > 0 ? values3.join(" ") + " " : "";
733
995
  function quoted(key2, value) {
734
- const v = quote + stringifyEntitiesLight(value, { subset }) + quote;
996
+ const v = quote + stringifyEntitiesLight2(value, { subset }) + quote;
735
997
  if (key2 === "_value") {
736
998
  return v;
737
999
  }
@@ -757,6 +1019,9 @@ ${match.start} /${match.name || template.name} ${match.end}`;
757
1019
 
758
1020
  // src/stringify/index.ts
759
1021
  var stringifyMDX = (value, field, imageCallback) => {
1022
+ if (field.parser?.type === "markdown") {
1023
+ return stringifyMDX2(value, field, imageCallback);
1024
+ }
760
1025
  if (!value) {
761
1026
  return;
762
1027
  }
@@ -769,7 +1034,7 @@ var stringifyMDX = (value, field, imageCallback) => {
769
1034
  }
770
1035
  }
771
1036
  const tree = rootElement(value, field, imageCallback);
772
- const res = toTinaMarkdown(tree, field);
1037
+ const res = toTinaMarkdown2(tree, field);
773
1038
  const templatesWithMatchers = field.templates?.filter((template) => template.match);
774
1039
  let preprocessedString = res;
775
1040
  templatesWithMatchers?.forEach((template) => {
@@ -782,7 +1047,7 @@ var stringifyMDX = (value, field, imageCallback) => {
782
1047
  });
783
1048
  return preprocessedString;
784
1049
  };
785
- var toTinaMarkdown = (tree, field) => {
1050
+ var toTinaMarkdown2 = (tree, field) => {
786
1051
  const patterns = [];
787
1052
  field.templates?.forEach((template) => {
788
1053
  if (typeof template === "string") {
@@ -815,10 +1080,10 @@ var toTinaMarkdown = (tree, field) => {
815
1080
  });
816
1081
  }
817
1082
  }
818
- return text2(node, parent, context, safeOptions);
1083
+ return text3(node, parent, context, safeOptions);
819
1084
  };
820
- return toMarkdown(tree, {
821
- extensions: [directiveToMarkdown(patterns), mdxJsxToMarkdown()],
1085
+ return toMarkdown2(tree, {
1086
+ extensions: [directiveToMarkdown(patterns), mdxJsxToMarkdown2()],
822
1087
  listItemIndent: "one",
823
1088
  handlers
824
1089
  });
@@ -996,1172 +1261,1852 @@ var getMarks = (content) => {
996
1261
  return marks;
997
1262
  };
998
1263
 
999
- // src/parse/mdx.ts
1000
- import { source } from "unist-util-source";
1001
- function mdxJsxElement(node, field, imageCallback) {
1002
- try {
1003
- const template = field.templates?.find((template2) => {
1004
- const templateName = typeof template2 === "string" ? template2 : template2.name;
1005
- return templateName === node.name;
1006
- });
1007
- if (typeof template === "string") {
1008
- throw new Error("Global templates not yet supported");
1009
- }
1010
- if (!template) {
1011
- const string = toTinaMarkdown({ type: "root", children: [node] }, field);
1012
- return {
1013
- type: node.type === "mdxJsxFlowElement" ? "html" : "html_inline",
1014
- value: string.trim(),
1015
- children: [{ type: "text", text: "" }]
1016
- };
1017
- }
1018
- const props = extractAttributes(node.attributes, template.fields, imageCallback);
1019
- const childField = template.fields.find((field2) => field2.name === "children");
1020
- if (childField) {
1021
- if (childField.type === "rich-text") {
1022
- props.children = remarkToSlate(node, childField, imageCallback);
1023
- }
1024
- }
1025
- return {
1026
- type: node.type,
1027
- name: node.name,
1028
- children: [{ type: "text", text: "" }],
1029
- props
1030
- };
1031
- } catch (e) {
1032
- if (e instanceof Error) {
1033
- throw new RichTextParseError(e.message, node.position);
1034
- }
1035
- throw e;
1036
- }
1037
- }
1038
- var directiveElement = (node, field, imageCallback, raw) => {
1264
+ // src/next/stringify/acorn.ts
1265
+ import { format as format2 } from "prettier";
1266
+ var stringifyPropsInline2 = (element, field, imageCallback) => {
1267
+ return stringifyProps2(element, field, true, imageCallback);
1268
+ };
1269
+ function stringifyProps2(element, parentField, flatten2, imageCallback) {
1270
+ const attributes2 = [];
1271
+ const children = [];
1039
1272
  let template;
1040
- template = field.templates?.find((template2) => {
1041
- const templateName = typeof template2 === "string" ? template2 : template2.name;
1042
- return templateName === node.name;
1273
+ let useDirective = false;
1274
+ let directiveType = "leaf";
1275
+ template = parentField.templates?.find((template2) => {
1276
+ if (typeof template2 === "string") {
1277
+ throw new Error("Global templates not supported");
1278
+ }
1279
+ return template2.name === element.name;
1043
1280
  });
1044
- if (typeof template === "string") {
1045
- throw new Error("Global templates not yet supported");
1046
- }
1047
1281
  if (!template) {
1048
- template = field.templates?.find((template2) => {
1282
+ template = parentField.templates?.find((template2) => {
1049
1283
  const templateName = template2?.match?.name;
1050
- return templateName === node.name;
1284
+ return templateName === element.name;
1051
1285
  });
1052
1286
  }
1053
- if (!template) {
1054
- return {
1055
- type: "p",
1056
- children: [{ type: "text", text: source(node, raw || "") || "" }]
1057
- };
1287
+ if (!template || typeof template === "string") {
1288
+ throw new Error(`Unable to find template for JSX element ${element.name}`);
1058
1289
  }
1059
- if (typeof template === "string") {
1060
- throw new Error(`Global templates not supported`);
1290
+ if (template.fields.find((f) => f.name === "children")) {
1291
+ directiveType = "block";
1061
1292
  }
1062
- const props = node.attributes || {};
1063
- const childField = template.fields.find((field2) => field2.name === "children");
1064
- if (childField) {
1065
- if (childField.type === "rich-text") {
1066
- if (node.type === "containerDirective") {
1067
- props.children = remarkToSlate(node, childField, imageCallback, raw);
1068
- }
1293
+ useDirective = !!template.match;
1294
+ Object.entries(element.props).forEach(([name, value]) => {
1295
+ if (typeof template === "string") {
1296
+ throw new Error(`Unable to find template for JSX element ${name}`);
1069
1297
  }
1070
- }
1071
- return {
1072
- type: "mdxJsxFlowElement",
1073
- name: template.name,
1074
- props,
1075
- children: [{ type: "text", text: "" }]
1076
- };
1077
- };
1078
-
1079
- // src/parse/remarkToPlate.ts
1080
- var remarkToSlate = (root, field, imageCallback, raw) => {
1081
- const content = (content2) => {
1082
- switch (content2.type) {
1083
- case "blockquote":
1084
- const children = [];
1085
- content2.children.map((child) => {
1086
- const inlineElements = unwrapBlockContent(child);
1087
- inlineElements.forEach((child2) => {
1088
- children.push(child2);
1089
- });
1090
- });
1091
- return {
1092
- type: "blockquote",
1093
- children
1094
- };
1095
- case "heading":
1096
- return heading(content2);
1097
- case "code":
1098
- return code(content2);
1099
- case "paragraph":
1100
- return paragraph(content2);
1101
- case "mdxJsxFlowElement":
1102
- return mdxJsxElement(content2, field, imageCallback);
1103
- case "thematicBreak":
1104
- return {
1105
- type: "hr",
1106
- children: [{ type: "text", text: "" }]
1107
- };
1108
- case "listItem":
1109
- return {
1110
- type: "li",
1111
- children: [
1112
- {
1113
- type: "lic",
1114
- children: flatten(content2.children.map((child) => unwrapBlockContent(child)))
1115
- }
1116
- ]
1117
- };
1118
- case "list":
1119
- return list(content2);
1120
- case "html":
1121
- return html(content2);
1122
- case "mdxFlowExpression":
1123
- case "mdxjsEsm":
1124
- throw new RichTextParseError(`Unexpected expression ${content2.value}.`, content2.position);
1125
- case "leafDirective": {
1126
- return directiveElement(content2, field, imageCallback, raw);
1127
- }
1128
- case "containerDirective": {
1129
- return directiveElement(content2, field, imageCallback, raw);
1298
+ const field = template?.fields?.find((field2) => field2.name === name);
1299
+ if (!field) {
1300
+ if (name === "children") {
1301
+ return;
1130
1302
  }
1131
- default:
1132
- throw new RichTextParseError(`Content: ${content2.type} is not yet supported`, content2.position);
1303
+ return;
1133
1304
  }
1134
- };
1135
- const html = (content2) => {
1136
- return {
1137
- type: "p",
1138
- children: [{ type: "text", text: content2.value }]
1139
- };
1140
- };
1141
- const html_inline = (content2) => {
1142
- return { type: "text", text: content2.value };
1143
- };
1144
- const list = (content2) => {
1145
- return {
1146
- type: content2.ordered ? "ol" : "ul",
1147
- children: content2.children.map((child) => listItem(child))
1148
- };
1149
- };
1150
- const listItem = (content2) => {
1151
- return {
1152
- type: "li",
1153
- children: content2.children.map((child) => {
1154
- switch (child.type) {
1155
- case "list":
1156
- return list(child);
1157
- case "heading":
1158
- case "paragraph":
1159
- return {
1160
- type: "lic",
1161
- children: flatten(child.children.map((child2) => phrasingContent(child2)))
1162
- };
1163
- case "blockquote": {
1164
- return {
1165
- ...blockquote(child),
1166
- type: "lic"
1167
- };
1305
+ switch (field.type) {
1306
+ case "reference":
1307
+ if (field.list) {
1308
+ if (Array.isArray(value)) {
1309
+ attributes2.push({
1310
+ type: "mdxJsxAttribute",
1311
+ name,
1312
+ value: {
1313
+ type: "mdxJsxAttributeValueExpression",
1314
+ value: `[${value.map((item) => `"${item}"`).join(", ")}]`
1315
+ }
1316
+ });
1168
1317
  }
1169
- case "mdxJsxFlowElement":
1170
- return {
1171
- type: "lic",
1172
- children: [
1173
- mdxJsxElement({ ...child, type: "mdxJsxTextElement" }, field, imageCallback)
1174
- ]
1175
- };
1176
- case "html":
1177
- return {
1178
- type: "lic",
1179
- children: html_inline(child)
1180
- };
1181
- case "leafDirective": {
1182
- return {
1183
- type: "lic",
1184
- children: [directiveElement(child, field, imageCallback)]
1185
- };
1318
+ } else {
1319
+ if (typeof value === "string") {
1320
+ attributes2.push({
1321
+ type: "mdxJsxAttribute",
1322
+ name,
1323
+ value
1324
+ });
1325
+ }
1326
+ }
1327
+ break;
1328
+ case "datetime":
1329
+ case "string":
1330
+ if (field.list) {
1331
+ if (Array.isArray(value)) {
1332
+ attributes2.push({
1333
+ type: "mdxJsxAttribute",
1334
+ name,
1335
+ value: {
1336
+ type: "mdxJsxAttributeValueExpression",
1337
+ value: `[${value.map((item) => `"${item}"`).join(", ")}]`
1338
+ }
1339
+ });
1340
+ }
1341
+ } else {
1342
+ if (typeof value === "string") {
1343
+ attributes2.push({
1344
+ type: "mdxJsxAttribute",
1345
+ name,
1346
+ value
1347
+ });
1348
+ } else {
1349
+ throw new Error(`Expected string for attribute on field ${field.name}`);
1350
+ }
1351
+ }
1352
+ break;
1353
+ case "image":
1354
+ if (field.list) {
1355
+ if (Array.isArray(value)) {
1356
+ attributes2.push({
1357
+ type: "mdxJsxAttribute",
1358
+ name,
1359
+ value: {
1360
+ type: "mdxJsxAttributeValueExpression",
1361
+ value: `[${value.map((item) => `"${imageCallback(item)}"`).join(", ")}]`
1362
+ }
1363
+ });
1364
+ }
1365
+ } else {
1366
+ attributes2.push({
1367
+ type: "mdxJsxAttribute",
1368
+ name,
1369
+ value: imageCallback(String(value))
1370
+ });
1371
+ }
1372
+ break;
1373
+ case "number":
1374
+ case "boolean":
1375
+ if (field.list) {
1376
+ if (Array.isArray(value)) {
1377
+ attributes2.push({
1378
+ type: "mdxJsxAttribute",
1379
+ name,
1380
+ value: {
1381
+ type: "mdxJsxAttributeValueExpression",
1382
+ value: `[${value.map((item) => `${item}`).join(", ")}]`
1383
+ }
1384
+ });
1385
+ }
1386
+ } else {
1387
+ attributes2.push({
1388
+ type: "mdxJsxAttribute",
1389
+ name,
1390
+ value: {
1391
+ type: "mdxJsxAttributeValueExpression",
1392
+ value: String(value)
1393
+ }
1394
+ });
1395
+ }
1396
+ break;
1397
+ case "object":
1398
+ attributes2.push({
1399
+ type: "mdxJsxAttribute",
1400
+ name,
1401
+ value: {
1402
+ type: "mdxJsxAttributeValueExpression",
1403
+ value: stringifyObj2(value, flatten2)
1404
+ }
1405
+ });
1406
+ break;
1407
+ case "rich-text":
1408
+ if (typeof value === "string") {
1409
+ throw new Error(`Unexpected string for rich-text, ensure the value has been properly parsed`);
1410
+ }
1411
+ if (field.list) {
1412
+ throw new Error(`Rich-text list is not supported`);
1413
+ } else {
1414
+ const joiner = flatten2 ? " " : "\n";
1415
+ let val = "";
1416
+ assertShape2(value, (value2) => value2.type === "root" && Array.isArray(value2.children), `Nested rich-text element is not a valid shape for field ${field.name}`);
1417
+ if (field.name === "children") {
1418
+ const root = rootElement2(value, field, imageCallback);
1419
+ root.children.forEach((child) => {
1420
+ children.push(child);
1421
+ });
1422
+ return;
1423
+ } else {
1424
+ const stringValue = stringifyMDX2(value, field, imageCallback);
1425
+ if (stringValue) {
1426
+ val = stringValue.trim().split("\n").map((str) => ` ${str.trim()}`).join(joiner);
1427
+ }
1428
+ }
1429
+ if (flatten2) {
1430
+ attributes2.push({
1431
+ type: "mdxJsxAttribute",
1432
+ name,
1433
+ value: {
1434
+ type: "mdxJsxAttributeValueExpression",
1435
+ value: `<>${val.trim()}</>`
1436
+ }
1437
+ });
1438
+ } else {
1439
+ attributes2.push({
1440
+ type: "mdxJsxAttribute",
1441
+ name,
1442
+ value: {
1443
+ type: "mdxJsxAttributeValueExpression",
1444
+ value: `<>
1445
+ ${val}
1446
+ </>`
1447
+ }
1448
+ });
1449
+ }
1450
+ }
1451
+ break;
1452
+ default:
1453
+ throw new Error(`Stringify props: ${field.type} not yet supported`);
1454
+ }
1455
+ });
1456
+ if (template.match) {
1457
+ return {
1458
+ useDirective,
1459
+ directiveType,
1460
+ attributes: attributes2,
1461
+ children: children && children.length ? children : [
1462
+ {
1463
+ type: "paragraph",
1464
+ children: [
1465
+ {
1466
+ type: "text",
1467
+ value: ""
1468
+ }
1469
+ ]
1470
+ }
1471
+ ]
1472
+ };
1473
+ }
1474
+ return { attributes: attributes2, children, useDirective, directiveType };
1475
+ }
1476
+ function stringifyObj2(obj, flatten2) {
1477
+ if (typeof obj === "object" && obj !== null) {
1478
+ const dummyFunc = `const dummyFunc = `;
1479
+ const res = format2(`${dummyFunc}${JSON.stringify(obj)}`, {
1480
+ parser: "acorn",
1481
+ trailingComma: "none",
1482
+ semi: false
1483
+ }).trim().replace(dummyFunc, "");
1484
+ return flatten2 ? res.replaceAll("\n", "").replaceAll(" ", " ") : res;
1485
+ } else {
1486
+ throw new Error(`stringifyObj must be passed an object or an array of objects, received ${typeof obj}`);
1487
+ }
1488
+ }
1489
+ function assertShape2(value, callback, errorMessage) {
1490
+ if (!callback(value)) {
1491
+ throw new Error(errorMessage || `Failed to assert shape`);
1492
+ }
1493
+ }
1494
+
1495
+ // src/next/stringify/marks.ts
1496
+ var matches2 = (a, b) => {
1497
+ return a.some((v) => b.includes(v));
1498
+ };
1499
+ var replaceLinksWithTextNodes2 = (content) => {
1500
+ const newItems = [];
1501
+ content?.forEach((item) => {
1502
+ if (item.type === "a") {
1503
+ if (item.children.length === 1) {
1504
+ const firstChild = item.children[0];
1505
+ if (firstChild?.type === "text") {
1506
+ newItems.push({
1507
+ ...firstChild,
1508
+ linkifyTextNode: (a) => {
1509
+ return {
1510
+ type: "link",
1511
+ url: item.url,
1512
+ title: item.title,
1513
+ children: [a]
1514
+ };
1515
+ }
1516
+ });
1517
+ } else {
1518
+ newItems.push(item);
1519
+ }
1520
+ } else {
1521
+ newItems.push(item);
1522
+ }
1523
+ } else {
1524
+ newItems.push(item);
1525
+ }
1526
+ });
1527
+ return newItems;
1528
+ };
1529
+ var inlineElementExceptLink2 = (content, field, imageCallback) => {
1530
+ switch (content.type) {
1531
+ case "a":
1532
+ throw new Error(`Unexpected node of type "a", link elements should be processed after all inline elements have resolved`);
1533
+ case "img":
1534
+ return {
1535
+ type: "image",
1536
+ url: imageCallback(content.url),
1537
+ alt: content.alt,
1538
+ title: content.caption
1539
+ };
1540
+ case "break":
1541
+ return {
1542
+ type: "break"
1543
+ };
1544
+ case "mdxJsxTextElement": {
1545
+ const { attributes: attributes2, children } = stringifyPropsInline2(content, field, imageCallback);
1546
+ let c = children;
1547
+ if (children.length) {
1548
+ const firstChild = children[0];
1549
+ if (firstChild && firstChild.type === "paragraph") {
1550
+ c = firstChild.children;
1551
+ }
1552
+ }
1553
+ return {
1554
+ type: "mdxJsxTextElement",
1555
+ name: content.name,
1556
+ attributes: attributes2,
1557
+ children: c
1558
+ };
1559
+ }
1560
+ case "html_inline": {
1561
+ return {
1562
+ type: "html",
1563
+ value: content.value
1564
+ };
1565
+ }
1566
+ default:
1567
+ if (!content.type && typeof content.text === "string") {
1568
+ return text4(content);
1569
+ }
1570
+ throw new Error(`InlineElement: ${content.type} is not supported`);
1571
+ }
1572
+ };
1573
+ var text4 = (content) => {
1574
+ return {
1575
+ type: "text",
1576
+ value: content.text
1577
+ };
1578
+ };
1579
+ var eat2 = (c, field, imageCallback) => {
1580
+ const content = replaceLinksWithTextNodes2(c);
1581
+ const first = content[0];
1582
+ if (!first) {
1583
+ return [];
1584
+ }
1585
+ if (first && first?.type !== "text") {
1586
+ if (first.type === "a") {
1587
+ return [
1588
+ {
1589
+ type: "link",
1590
+ url: first.url,
1591
+ title: first.title,
1592
+ children: eat2(first.children, field, imageCallback)
1593
+ },
1594
+ ...eat2(content.slice(1), field, imageCallback)
1595
+ ];
1596
+ }
1597
+ return [
1598
+ inlineElementExceptLink2(first, field, imageCallback),
1599
+ ...eat2(content.slice(1), field, imageCallback)
1600
+ ];
1601
+ }
1602
+ const marks = getMarks(first);
1603
+ if (marks.length === 0) {
1604
+ if (first.linkifyTextNode) {
1605
+ return [
1606
+ first.linkifyTextNode(text4(first)),
1607
+ ...eat2(content.slice(1), field, imageCallback)
1608
+ ];
1609
+ } else {
1610
+ return [text4(first), ...eat2(content.slice(1), field, imageCallback)];
1611
+ }
1612
+ }
1613
+ let nonMatchingSiblingIndex = 0;
1614
+ if (content.slice(1).every((content2, index) => {
1615
+ if (matches2(marks, getMarks(content2))) {
1616
+ return true;
1617
+ } else {
1618
+ nonMatchingSiblingIndex = index;
1619
+ return false;
1620
+ }
1621
+ })) {
1622
+ nonMatchingSiblingIndex = content.length - 1;
1623
+ }
1624
+ const matchingSiblings = content.slice(1, nonMatchingSiblingIndex + 1);
1625
+ const markCounts = {};
1626
+ marks.forEach((mark) => {
1627
+ let count2 = 1;
1628
+ matchingSiblings.every((sibling, index) => {
1629
+ if (getMarks(sibling).includes(mark)) {
1630
+ count2 = index + 1;
1631
+ return true;
1632
+ }
1633
+ });
1634
+ markCounts[mark] = count2;
1635
+ });
1636
+ let count = 0;
1637
+ let markToProcess = null;
1638
+ Object.entries(markCounts).forEach(([mark, markCount]) => {
1639
+ const m = mark;
1640
+ if (markCount > count) {
1641
+ count = markCount;
1642
+ markToProcess = m;
1643
+ }
1644
+ });
1645
+ if (!markToProcess) {
1646
+ return [text4(first), ...eat2(content.slice(1), field, imageCallback)];
1647
+ }
1648
+ if (markToProcess === "inlineCode") {
1649
+ if (nonMatchingSiblingIndex) {
1650
+ throw new Error(`Marks inside inline code are not supported`);
1651
+ }
1652
+ const node = {
1653
+ type: markToProcess,
1654
+ value: first.text
1655
+ };
1656
+ return [
1657
+ first.linkifyTextNode?.(node) ?? node,
1658
+ ...eat2(content.slice(nonMatchingSiblingIndex + 1), field, imageCallback)
1659
+ ];
1660
+ }
1661
+ return [
1662
+ {
1663
+ type: markToProcess,
1664
+ children: eat2([
1665
+ ...[first, ...matchingSiblings].map((sibling) => cleanNode2(sibling, markToProcess))
1666
+ ], field, imageCallback)
1667
+ },
1668
+ ...eat2(content.slice(nonMatchingSiblingIndex + 1), field, imageCallback)
1669
+ ];
1670
+ };
1671
+ var cleanNode2 = (node, mark) => {
1672
+ if (!mark) {
1673
+ return node;
1674
+ }
1675
+ const cleanedNode = {};
1676
+ const markToClear = {
1677
+ strong: "bold",
1678
+ emphasis: "italic",
1679
+ inlineCode: "code"
1680
+ }[mark];
1681
+ Object.entries(node).map(([key, value]) => {
1682
+ if (key !== markToClear) {
1683
+ cleanedNode[key] = value;
1684
+ }
1685
+ });
1686
+ if (node.linkifyTextNode) {
1687
+ cleanedNode.callback = node.linkifyTextNode;
1688
+ }
1689
+ return cleanedNode;
1690
+ };
1691
+
1692
+ // src/next/stringify/pre-processing.ts
1693
+ var preProcess = (tree, field, imageCallback) => {
1694
+ const ast = rootElement2(tree, field, imageCallback);
1695
+ return ast;
1696
+ };
1697
+ var rootElement2 = (content, field, imageCallback) => {
1698
+ const children = [];
1699
+ content.children?.forEach((child) => {
1700
+ const value = blockElement2(child, field, imageCallback);
1701
+ if (value) {
1702
+ children.push(value);
1703
+ }
1704
+ });
1705
+ return {
1706
+ type: "root",
1707
+ children
1708
+ };
1709
+ };
1710
+ var blockElement2 = (content, field, imageCallback) => {
1711
+ switch (content.type) {
1712
+ case "h1":
1713
+ case "h2":
1714
+ case "h3":
1715
+ case "h4":
1716
+ case "h5":
1717
+ case "h6":
1718
+ return {
1719
+ type: "heading",
1720
+ depth: { h1: 1, h2: 2, h3: 3, h4: 4, h5: 5, h6: 6 }[content.type],
1721
+ children: eat2(content.children, field, imageCallback)
1722
+ };
1723
+ case "p":
1724
+ if (content.children.length === 1) {
1725
+ const onlyChild = content.children[0];
1726
+ if (onlyChild && (onlyChild.type === "text" || !onlyChild.type) && onlyChild.text === "") {
1727
+ return null;
1728
+ }
1729
+ }
1730
+ return {
1731
+ type: "paragraph",
1732
+ children: eat2(content.children, field, imageCallback)
1733
+ };
1734
+ case "code_block":
1735
+ return {
1736
+ type: "code",
1737
+ lang: content.lang,
1738
+ value: content.value
1739
+ };
1740
+ case "mdxJsxFlowElement":
1741
+ const { children, attributes: attributes2, useDirective, directiveType } = stringifyProps2(content, field, false, imageCallback);
1742
+ return {
1743
+ type: "mdxJsxFlowElement",
1744
+ name: content.name,
1745
+ attributes: attributes2,
1746
+ children
1747
+ };
1748
+ case "blockquote":
1749
+ return {
1750
+ type: "blockquote",
1751
+ children: [
1752
+ {
1753
+ type: "paragraph",
1754
+ children: eat2(content.children, field, imageCallback)
1186
1755
  }
1187
- case "code":
1188
- case "thematicBreak":
1189
- case "table":
1190
- throw new RichTextParseError(`${child.type} inside list item is not supported`, child.position);
1191
- default:
1192
- throw new RichTextParseError(`Unknown list item of type ${child.type}`, child.position);
1756
+ ]
1757
+ };
1758
+ case "hr":
1759
+ return {
1760
+ type: "thematicBreak"
1761
+ };
1762
+ case "ol":
1763
+ case "ul":
1764
+ return {
1765
+ type: "list",
1766
+ ordered: content.type === "ol",
1767
+ spread: false,
1768
+ children: content.children.map((child) => listItemElement2(child, field, imageCallback))
1769
+ };
1770
+ case "html": {
1771
+ return {
1772
+ type: "html",
1773
+ value: content.value
1774
+ };
1775
+ }
1776
+ case "img":
1777
+ return {
1778
+ type: "image",
1779
+ url: imageCallback(content.url),
1780
+ alt: content.alt,
1781
+ title: content.caption
1782
+ };
1783
+ default:
1784
+ throw new Error(`BlockElement: ${content.type} is not yet supported`);
1785
+ }
1786
+ };
1787
+ var listItemElement2 = (content, field, imageCallback) => {
1788
+ return {
1789
+ type: "listItem",
1790
+ spread: false,
1791
+ children: content.children.map((child) => {
1792
+ if (child.type === "lic") {
1793
+ return {
1794
+ type: "paragraph",
1795
+ children: eat2(child.children, field, imageCallback)
1796
+ };
1797
+ }
1798
+ return blockContentElement2(child, field, imageCallback);
1799
+ })
1800
+ };
1801
+ };
1802
+ var blockContentElement2 = (content, field, imageCallback) => {
1803
+ switch (content.type) {
1804
+ case "blockquote":
1805
+ return {
1806
+ type: "blockquote",
1807
+ children: content.children.map((child) => blockContentElement2(child, field, imageCallback))
1808
+ };
1809
+ case "p":
1810
+ return {
1811
+ type: "paragraph",
1812
+ children: eat2(content.children, field, imageCallback)
1813
+ };
1814
+ case "ol":
1815
+ case "ul":
1816
+ return {
1817
+ type: "list",
1818
+ ordered: content.type === "ol",
1819
+ spread: false,
1820
+ children: content.children.map((child) => listItemElement2(child, field, imageCallback))
1821
+ };
1822
+ default:
1823
+ throw new Error(`BlockContentElement: ${content.type} is not yet supported`);
1824
+ }
1825
+ };
1826
+
1827
+ // src/next/stringify/index.ts
1828
+ var stringifyMDX2 = (value, field, imageCallback) => {
1829
+ if (!value) {
1830
+ return;
1831
+ }
1832
+ const mdTree = preProcess(value, field, imageCallback);
1833
+ return toTinaMarkdown(mdTree, field);
1834
+ };
1835
+
1836
+ // src/next/parse/markdown.ts
1837
+ import { fromMarkdown as mdastFromMarkdown } from "mdast-util-from-markdown";
1838
+
1839
+ // src/next/shortcodes/lib/factory-tag.ts
1840
+ import { ok as assert } from "uvu/assert";
1841
+ import {
1842
+ start as idStart,
1843
+ cont as idCont
1844
+ } from "estree-util-is-identifier-name";
1845
+ import { factoryMdxExpression } from "micromark-factory-mdx-expression";
1846
+ import { factorySpace } from "micromark-factory-space";
1847
+ import {
1848
+ markdownLineEnding,
1849
+ markdownLineEndingOrSpace,
1850
+ markdownSpace,
1851
+ unicodeWhitespace
1852
+ } from "micromark-util-character";
1853
+ import { codes as codes2 } from "micromark-util-symbol/codes.js";
1854
+ import { constants } from "micromark-util-symbol/constants.js";
1855
+ import { types } from "micromark-util-symbol/types.js";
1856
+ import { VFileMessage as VFileMessage2 } from "vfile-message";
1857
+
1858
+ // src/next/shortcodes/lib/util.ts
1859
+ import { codes } from "micromark-util-symbol/codes";
1860
+ import { values } from "micromark-util-symbol/values";
1861
+ var findValue = (string) => {
1862
+ let lookupValue = null;
1863
+ Object.entries(values).forEach(([key, value]) => {
1864
+ if (value === string) {
1865
+ lookupValue = key;
1866
+ }
1867
+ });
1868
+ return lookupValue;
1869
+ };
1870
+ var findCode = (string) => {
1871
+ if (!string) {
1872
+ return null;
1873
+ }
1874
+ const lookup = findValue(string);
1875
+ let lookupValue = null;
1876
+ if (lookup) {
1877
+ Object.entries(codes).forEach(([key, value]) => {
1878
+ if (key === lookup) {
1879
+ lookupValue = value;
1880
+ }
1881
+ });
1882
+ }
1883
+ return lookupValue;
1884
+ };
1885
+
1886
+ // src/next/shortcodes/lib/factory-tag.ts
1887
+ function factoryTag(effects, ok, nok, acorn2, acornOptions, addResult, allowLazy, tagType, tagMarkerType, tagClosingMarkerType, tagSelfClosingMarker, tagNameType, tagNamePrimaryType, tagNameMemberMarkerType, tagNameMemberType, tagNamePrefixMarkerType, tagNameLocalType, tagExpressionAttributeType, tagExpressionAttributeMarkerType, tagExpressionAttributeValueType, tagAttributeType, tagAttributeNameType, tagAttributeNamePrimaryType, tagAttributeNamePrefixMarkerType, tagAttributeNameLocalType, tagAttributeInitializerMarkerType, tagAttributeValueLiteralType, tagAttributeValueLiteralMarkerType, tagAttributeValueLiteralValueType, tagAttributeValueExpressionType, tagAttributeValueExpressionMarkerType, tagAttributeValueExpressionValueType, pattern) {
1888
+ const self = this;
1889
+ let returnState;
1890
+ let marker;
1891
+ let startPoint;
1892
+ let tagOpenerIndex = 1;
1893
+ let tagCloserIndex = 1;
1894
+ let nameIndex = 1;
1895
+ const start = function(code) {
1896
+ startPoint = self.now();
1897
+ effects.enter(tagType);
1898
+ effects.enter(tagMarkerType);
1899
+ effects.consume(code);
1900
+ if (pattern.start.length === 1) {
1901
+ effects.exit(tagMarkerType);
1902
+ return afterStart;
1903
+ }
1904
+ return tagOpenerSequence;
1905
+ };
1906
+ const tagOpenerSequence = function(code) {
1907
+ const character = findCode(pattern.start[tagOpenerIndex]);
1908
+ if (code === character) {
1909
+ effects.consume(code);
1910
+ if (pattern.start.length - 1 === tagOpenerIndex) {
1911
+ effects.exit(tagMarkerType);
1912
+ return afterStart;
1913
+ }
1914
+ tagOpenerIndex++;
1915
+ return tagOpenerSequence;
1916
+ }
1917
+ return nok;
1918
+ };
1919
+ const afterStart = function(code) {
1920
+ returnState = beforeName;
1921
+ return optionalEsWhitespace(code);
1922
+ };
1923
+ const beforeName = function(code) {
1924
+ if (code === codes2.slash) {
1925
+ effects.enter(tagClosingMarkerType);
1926
+ effects.consume(code);
1927
+ effects.exit(tagClosingMarkerType);
1928
+ returnState = beforeClosingTagName;
1929
+ return optionalEsWhitespace;
1930
+ }
1931
+ if (code === codes2.greaterThan) {
1932
+ return tagEnd(code);
1933
+ }
1934
+ if (code !== codes2.eof && idStart(code) && findCode(pattern.name[0]) === code) {
1935
+ effects.enter(tagNameType);
1936
+ effects.enter(tagNamePrimaryType);
1937
+ effects.consume(code);
1938
+ return primaryName;
1939
+ }
1940
+ return nok(code);
1941
+ };
1942
+ const beforeClosingTagName = function(code) {
1943
+ if (code === codes2.greaterThan) {
1944
+ return tagEnd(code);
1945
+ }
1946
+ if (code !== codes2.eof && idStart(code)) {
1947
+ effects.enter(tagNameType);
1948
+ effects.enter(tagNamePrimaryType);
1949
+ effects.consume(code);
1950
+ return primaryName;
1951
+ }
1952
+ return nok(code);
1953
+ };
1954
+ const primaryName = function(code) {
1955
+ const nextCharacterInName = pattern.name[nameIndex];
1956
+ const nextCodeInName = nextCharacterInName ? findCode(nextCharacterInName) : null;
1957
+ if (nextCodeInName === code) {
1958
+ effects.consume(code);
1959
+ nameIndex++;
1960
+ return primaryName;
1961
+ }
1962
+ nameIndex = 0;
1963
+ if (code === codes2.dot || code === codes2.slash || code === codes2.colon || code === codes2.greaterThan || code === findCode(pattern.end[0]) || markdownLineEndingOrSpace(code) || unicodeWhitespace(code)) {
1964
+ effects.exit(tagNamePrimaryType);
1965
+ returnState = afterPrimaryName;
1966
+ return optionalEsWhitespace(code);
1967
+ }
1968
+ return nok(code);
1969
+ };
1970
+ const afterPrimaryName = function(code) {
1971
+ if (code === codes2.dot) {
1972
+ effects.enter(tagNameMemberMarkerType);
1973
+ effects.consume(code);
1974
+ effects.exit(tagNameMemberMarkerType);
1975
+ returnState = beforeMemberName;
1976
+ return optionalEsWhitespace;
1977
+ }
1978
+ if (code === codes2.colon) {
1979
+ effects.enter(tagNamePrefixMarkerType);
1980
+ effects.consume(code);
1981
+ effects.exit(tagNamePrefixMarkerType);
1982
+ returnState = beforeLocalName;
1983
+ return optionalEsWhitespace;
1984
+ }
1985
+ if (code === findCode(pattern.end[0])) {
1986
+ const tagCloserSequence = function(code2) {
1987
+ const character = findCode(pattern.end[tagCloserIndex]);
1988
+ if (code2 === character) {
1989
+ if (pattern.end.length - 1 === tagCloserIndex) {
1990
+ effects.exit(tagNameType);
1991
+ return beforeAttribute(code2);
1992
+ }
1993
+ tagCloserIndex++;
1994
+ effects.consume(code2);
1995
+ return tagCloserSequence;
1193
1996
  }
1194
- })
1195
- };
1997
+ tagCloserIndex = 0;
1998
+ return nok;
1999
+ };
2000
+ if (pattern.end.length === 1) {
2001
+ effects.exit(tagNameType);
2002
+ return beforeAttribute(code);
2003
+ } else {
2004
+ effects.consume(code);
2005
+ return tagCloserSequence;
2006
+ }
2007
+ }
2008
+ if (code === codes2.slash || code === codes2.greaterThan || code === codes2.leftCurlyBrace || code !== codes2.eof && idStart(code)) {
2009
+ effects.exit(tagNameType);
2010
+ return beforeAttribute(code);
2011
+ }
2012
+ if (code === codes2.quotationMark) {
2013
+ effects.exit(tagNameType);
2014
+ effects.enter(tagAttributeType);
2015
+ effects.enter(tagAttributeNameType);
2016
+ effects.enter(tagAttributeNamePrimaryType);
2017
+ effects.exit(tagAttributeNamePrimaryType);
2018
+ effects.exit(tagAttributeNameType);
2019
+ effects.enter(tagAttributeInitializerMarkerType);
2020
+ effects.exit(tagAttributeInitializerMarkerType);
2021
+ return beforeAttributeValue(code);
2022
+ }
2023
+ return nok(code);
2024
+ crash(code, "after name", "a character that can start an attribute name, such as a letter, `$`, or `_`; whitespace before attributes; or the end of the tag");
1196
2025
  };
1197
- const unwrapBlockContent = (content2) => {
1198
- const flattenPhrasingContent = (children) => {
1199
- const children2 = children.map((child) => phrasingContent(child));
1200
- return flatten(Array.isArray(children2) ? children2 : [children2]);
1201
- };
1202
- switch (content2.type) {
1203
- case "heading":
1204
- case "paragraph":
1205
- return flattenPhrasingContent(content2.children);
1206
- case "html":
1207
- return [html_inline(content2)];
1208
- case "blockquote":
1209
- default:
1210
- throw new RichTextParseError(`UnwrapBlock: Unknown block content of type ${content2.type}`, content2.position);
2026
+ const beforeMemberName = function(code) {
2027
+ if (code !== codes2.eof && idStart(code)) {
2028
+ effects.enter(tagNameMemberType);
2029
+ effects.consume(code);
2030
+ return memberName;
1211
2031
  }
2032
+ return nok(code);
2033
+ crash(code, "before member name", "a character that can start an attribute name, such as a letter, `$`, or `_`; whitespace before attributes; or the end of the tag");
1212
2034
  };
1213
- const code = (content2) => {
1214
- const extra = {};
1215
- if (content2.lang)
1216
- extra["lang"] = content2.lang;
1217
- return {
1218
- type: "code_block",
1219
- ...extra,
1220
- value: content2.value,
1221
- children: [{ type: "text", text: "" }]
1222
- };
2035
+ const memberName = function(code) {
2036
+ if (code === codes2.dash || code !== codes2.eof && idCont(code)) {
2037
+ effects.consume(code);
2038
+ return memberName;
2039
+ }
2040
+ if (code === codes2.dot || code === codes2.slash || code === codes2.greaterThan || code === codes2.leftCurlyBrace || markdownLineEndingOrSpace(code) || unicodeWhitespace(code)) {
2041
+ effects.exit(tagNameMemberType);
2042
+ returnState = afterMemberName;
2043
+ return optionalEsWhitespace(code);
2044
+ }
2045
+ crash(code, "in member name", "a name character such as letters, digits, `$`, or `_`; whitespace before attributes; or the end of the tag" + (code === codes2.atSign ? " (note: to create a link in MDX, use `[text](url)`)" : ""));
1223
2046
  };
1224
- const link = (content2) => {
1225
- return {
1226
- type: "a",
1227
- url: content2.url,
1228
- title: content2.title,
1229
- children: flatten(content2.children.map((child) => staticPhrasingContent(child)))
1230
- };
2047
+ const afterMemberName = function(code) {
2048
+ if (code === codes2.dot) {
2049
+ effects.enter(tagNameMemberMarkerType);
2050
+ effects.consume(code);
2051
+ effects.exit(tagNameMemberMarkerType);
2052
+ returnState = beforeMemberName;
2053
+ return optionalEsWhitespace;
2054
+ }
2055
+ if (code === codes2.slash || code === codes2.greaterThan || code === codes2.leftCurlyBrace || code !== codes2.eof && idStart(code)) {
2056
+ effects.exit(tagNameType);
2057
+ return beforeAttribute(code);
2058
+ }
2059
+ return nok(code);
2060
+ };
2061
+ const beforeLocalName = function(code) {
2062
+ if (code !== codes2.eof && idStart(code)) {
2063
+ effects.enter(tagNameLocalType);
2064
+ effects.consume(code);
2065
+ return localName;
2066
+ }
2067
+ crash(code, "before local name", "a character that can start a name, such as a letter, `$`, or `_`" + (code === codes2.plusSign || code !== null && code > codes2.dot && code < codes2.colon ? " (note: to create a link in MDX, use `[text](url)`)" : ""));
2068
+ };
2069
+ const localName = function(code) {
2070
+ if (code === codes2.dash || code !== codes2.eof && idCont(code)) {
2071
+ effects.consume(code);
2072
+ return localName;
2073
+ }
2074
+ if (code === codes2.slash || code === codes2.greaterThan || code === codes2.leftCurlyBrace || markdownLineEndingOrSpace(code) || unicodeWhitespace(code)) {
2075
+ effects.exit(tagNameLocalType);
2076
+ returnState = afterLocalName;
2077
+ return optionalEsWhitespace(code);
2078
+ }
2079
+ crash(code, "in local name", "a name character such as letters, digits, `$`, or `_`; whitespace before attributes; or the end of the tag");
2080
+ };
2081
+ const afterLocalName = function(code) {
2082
+ if (code === codes2.slash || code === codes2.greaterThan || code === codes2.leftCurlyBrace || code !== codes2.eof && idStart(code)) {
2083
+ effects.exit(tagNameType);
2084
+ return beforeAttribute(code);
2085
+ }
2086
+ if (code === findCode(pattern.end)) {
2087
+ effects.exit(tagNameType);
2088
+ return beforeAttribute(code);
2089
+ }
2090
+ crash(code, "after local name", "a character that can start an attribute name, such as a letter, `$`, or `_`; whitespace before attributes; or the end of the tag");
2091
+ };
2092
+ const beforeAttribute = function(code) {
2093
+ if (code === findCode(pattern.end[0])) {
2094
+ const tagCloserSequence = function(code2) {
2095
+ const character = findCode(pattern.end[tagCloserIndex]);
2096
+ if (code2 === character) {
2097
+ if (pattern.end.length - 1 === tagCloserIndex) {
2098
+ return beforeAttribute(code2);
2099
+ }
2100
+ tagCloserIndex++;
2101
+ effects.consume(code2);
2102
+ return tagCloserSequence;
2103
+ }
2104
+ tagCloserIndex = 0;
2105
+ return nok;
2106
+ };
2107
+ if (pattern.end.length === 1) {
2108
+ if (pattern.leaf) {
2109
+ effects.enter(tagSelfClosingMarker);
2110
+ effects.exit(tagSelfClosingMarker);
2111
+ returnState = selfClosing;
2112
+ return optionalEsWhitespace;
2113
+ } else {
2114
+ return tagEnd(code);
2115
+ }
2116
+ } else {
2117
+ effects.consume(code);
2118
+ return tagCloserSequence;
2119
+ }
2120
+ }
2121
+ if (code === findCode(pattern.end[pattern.end.length - 1])) {
2122
+ if (pattern.leaf) {
2123
+ effects.enter(tagSelfClosingMarker);
2124
+ effects.exit(tagSelfClosingMarker);
2125
+ returnState = selfClosing;
2126
+ return optionalEsWhitespace;
2127
+ } else {
2128
+ return tagEnd(code);
2129
+ }
2130
+ }
2131
+ if (code === codes2.greaterThan) {
2132
+ return tagEnd(code);
2133
+ }
2134
+ if (code === codes2.leftCurlyBrace) {
2135
+ assert(startPoint, "expected `startPoint` to be defined");
2136
+ return factoryMdxExpression.call(self, effects, afterAttributeExpression, tagExpressionAttributeType, tagExpressionAttributeMarkerType, tagExpressionAttributeValueType, acorn2, acornOptions, addResult, true, false, allowLazy, startPoint.column)(code);
2137
+ }
2138
+ if (code !== codes2.eof && idStart(code)) {
2139
+ effects.enter(tagAttributeType);
2140
+ effects.enter(tagAttributeNameType);
2141
+ effects.enter(tagAttributeNamePrimaryType);
2142
+ effects.consume(code);
2143
+ return attributePrimaryName;
2144
+ }
2145
+ return nok;
2146
+ };
2147
+ const afterAttributeExpression = function(code) {
2148
+ returnState = beforeAttribute;
2149
+ return optionalEsWhitespace(code);
2150
+ };
2151
+ const attributePrimaryName = function(code) {
2152
+ if (code === codes2.dash || code !== codes2.eof && idCont(code)) {
2153
+ effects.consume(code);
2154
+ return attributePrimaryName;
2155
+ }
2156
+ if (code === codes2.slash || code === codes2.colon || code === codes2.equalsTo || code === codes2.greaterThan || code === codes2.leftCurlyBrace || markdownLineEndingOrSpace(code) || unicodeWhitespace(code)) {
2157
+ effects.exit(tagAttributeNamePrimaryType);
2158
+ returnState = afterAttributePrimaryName;
2159
+ return optionalEsWhitespace(code);
2160
+ }
2161
+ return nok(code);
2162
+ };
2163
+ const afterAttributePrimaryName = function(code) {
2164
+ if (code === codes2.colon) {
2165
+ effects.enter(tagAttributeNamePrefixMarkerType);
2166
+ effects.consume(code);
2167
+ effects.exit(tagAttributeNamePrefixMarkerType);
2168
+ returnState = beforeAttributeLocalName;
2169
+ return optionalEsWhitespace;
2170
+ }
2171
+ if (code === codes2.equalsTo) {
2172
+ effects.exit(tagAttributeNameType);
2173
+ effects.enter(tagAttributeInitializerMarkerType);
2174
+ effects.consume(code);
2175
+ effects.exit(tagAttributeInitializerMarkerType);
2176
+ returnState = beforeAttributeValue;
2177
+ return optionalEsWhitespace;
2178
+ }
2179
+ if (code === codes2.slash || code === codes2.greaterThan || code === codes2.leftCurlyBrace || markdownLineEndingOrSpace(code) || unicodeWhitespace(code) || code !== codes2.eof && idStart(code)) {
2180
+ effects.exit(tagAttributeNameType);
2181
+ effects.exit(tagAttributeType);
2182
+ returnState = beforeAttribute;
2183
+ return optionalEsWhitespace(code);
2184
+ }
2185
+ return nok(code);
2186
+ };
2187
+ const beforeAttributeLocalName = function(code) {
2188
+ if (code !== codes2.eof && idStart(code)) {
2189
+ effects.enter(tagAttributeNameLocalType);
2190
+ effects.consume(code);
2191
+ return attributeLocalName;
2192
+ }
2193
+ crash(code, "before local attribute name", "a character that can start an attribute name, such as a letter, `$`, or `_`; `=` to initialize a value; or the end of the tag");
1231
2194
  };
1232
- const heading = (content2) => {
1233
- return {
1234
- type: ["h1", "h2", "h3", "h4", "h5", "h6"][content2.depth - 1],
1235
- children: flatten(content2.children.map(phrasingContent))
1236
- };
2195
+ const attributeLocalName = function(code) {
2196
+ if (code === codes2.dash || code !== codes2.eof && idCont(code)) {
2197
+ effects.consume(code);
2198
+ return attributeLocalName;
2199
+ }
2200
+ if (code === codes2.slash || code === codes2.equalsTo || code === codes2.greaterThan || code === codes2.leftCurlyBrace || markdownLineEndingOrSpace(code) || unicodeWhitespace(code)) {
2201
+ effects.exit(tagAttributeNameLocalType);
2202
+ effects.exit(tagAttributeNameType);
2203
+ returnState = afterAttributeLocalName;
2204
+ return optionalEsWhitespace(code);
2205
+ }
2206
+ crash(code, "in local attribute name", "an attribute name character such as letters, digits, `$`, or `_`; `=` to initialize a value; whitespace before attributes; or the end of the tag");
1237
2207
  };
1238
- const staticPhrasingContent = (content2) => {
1239
- switch (content2.type) {
1240
- case "mdxJsxTextElement":
1241
- return mdxJsxElement(content2, field, imageCallback);
1242
- case "text":
1243
- return text3(content2);
1244
- case "inlineCode":
1245
- case "emphasis":
1246
- case "image":
1247
- case "strong":
1248
- return phrashingMark(content2);
1249
- case "html":
1250
- return html_inline(content2);
1251
- default:
1252
- throw new Error(`StaticPhrasingContent: ${content2.type} is not yet supported`);
2208
+ const afterAttributeLocalName = function(code) {
2209
+ if (code === codes2.equalsTo) {
2210
+ effects.enter(tagAttributeInitializerMarkerType);
2211
+ effects.consume(code);
2212
+ effects.exit(tagAttributeInitializerMarkerType);
2213
+ returnState = beforeAttributeValue;
2214
+ return optionalEsWhitespace;
1253
2215
  }
2216
+ if (code === codes2.slash || code === codes2.greaterThan || code === codes2.leftCurlyBrace || code !== codes2.eof && idStart(code)) {
2217
+ effects.exit(tagAttributeType);
2218
+ return beforeAttribute(code);
2219
+ }
2220
+ crash(code, "after local attribute name", "a character that can start an attribute name, such as a letter, `$`, or `_`; `=` to initialize a value; or the end of the tag");
1254
2221
  };
1255
- const phrasingContent = (content2) => {
1256
- switch (content2.type) {
1257
- case "text":
1258
- return text3(content2);
1259
- case "link":
1260
- return link(content2);
1261
- case "image":
1262
- return image(content2);
1263
- case "mdxJsxTextElement":
1264
- return mdxJsxElement(content2, field, imageCallback);
1265
- case "emphasis":
1266
- return phrashingMark(content2);
1267
- case "strong":
1268
- return phrashingMark(content2);
1269
- case "break":
1270
- return breakContent();
1271
- case "inlineCode":
1272
- return phrashingMark(content2);
1273
- case "html":
1274
- return html_inline(content2);
1275
- case "mdxTextExpression":
1276
- throw new RichTextParseError(`Unexpected expression ${content2.value}.`, content2.position);
1277
- default:
1278
- throw new Error(`PhrasingContent: ${content2.type} is not yet supported`);
2222
+ const beforeAttributeValue = function(code) {
2223
+ if (code === codes2.quotationMark || code === codes2.apostrophe) {
2224
+ effects.enter(tagAttributeValueLiteralType);
2225
+ effects.enter(tagAttributeValueLiteralMarkerType);
2226
+ effects.consume(code);
2227
+ effects.exit(tagAttributeValueLiteralMarkerType);
2228
+ marker = code;
2229
+ return attributeValueQuotedStart;
2230
+ }
2231
+ if (code === codes2.leftCurlyBrace) {
2232
+ assert(startPoint, "expected `startPoint` to be defined");
2233
+ return factoryMdxExpression.call(self, effects, afterAttributeValueExpression, tagAttributeValueExpressionType, tagAttributeValueExpressionMarkerType, tagAttributeValueExpressionValueType, acorn2, acornOptions, addResult, false, false, allowLazy, startPoint.column)(code);
1279
2234
  }
2235
+ return nok(code);
1280
2236
  };
1281
- const breakContent = () => {
1282
- return {
1283
- type: "break",
1284
- children: [
1285
- {
1286
- type: "text",
1287
- text: ""
1288
- }
1289
- ]
1290
- };
2237
+ const afterAttributeValueExpression = function(code) {
2238
+ effects.exit(tagAttributeType);
2239
+ returnState = beforeAttribute;
2240
+ return optionalEsWhitespace(code);
1291
2241
  };
1292
- const phrashingMark = (node, marks = []) => {
1293
- const accum = [];
1294
- switch (node.type) {
1295
- case "emphasis": {
1296
- const children = flatten(node.children.map((child) => phrashingMark(child, [...marks, "italic"])));
1297
- children.forEach((child) => {
1298
- accum.push(child);
1299
- });
1300
- break;
1301
- }
1302
- case "inlineCode": {
1303
- const markProps2 = {};
1304
- marks.forEach((mark) => markProps2[mark] = true);
1305
- accum.push({
1306
- type: "text",
1307
- text: node.value,
1308
- code: true,
1309
- ...markProps2
1310
- });
1311
- break;
1312
- }
1313
- case "strong": {
1314
- const children = flatten(node.children.map((child) => phrashingMark(child, [...marks, "bold"])));
1315
- children.forEach((child) => {
1316
- accum.push(child);
1317
- });
1318
- break;
1319
- }
1320
- case "image": {
1321
- accum.push(image(node));
1322
- break;
1323
- }
1324
- case "link": {
1325
- const children = flatten(node.children.map((child) => phrashingMark(child, marks)));
1326
- accum.push({ type: "a", url: node.url, title: node.title, children });
1327
- break;
1328
- }
1329
- case "html":
1330
- case "text":
1331
- const markProps = {};
1332
- marks.forEach((mark) => markProps[mark] = true);
1333
- accum.push({ type: "text", text: node.value, ...markProps });
1334
- break;
1335
- case "break":
1336
- accum.push(breakContent());
1337
- break;
1338
- default:
1339
- throw new RichTextParseError(`Unexpected inline element of type ${node.type}`, node?.position);
2242
+ const attributeValueQuotedStart = function(code) {
2243
+ assert(marker !== void 0, "expected `marker` to be defined");
2244
+ if (code === codes2.eof) {
2245
+ return nok(code);
1340
2246
  }
1341
- return accum;
2247
+ if (code === marker) {
2248
+ effects.enter(tagAttributeValueLiteralMarkerType);
2249
+ effects.consume(code);
2250
+ effects.exit(tagAttributeValueLiteralMarkerType);
2251
+ effects.exit(tagAttributeValueLiteralType);
2252
+ effects.exit(tagAttributeType);
2253
+ marker = void 0;
2254
+ returnState = beforeAttribute;
2255
+ return optionalEsWhitespace;
2256
+ }
2257
+ if (markdownLineEnding(code)) {
2258
+ returnState = attributeValueQuotedStart;
2259
+ return optionalEsWhitespace(code);
2260
+ }
2261
+ effects.enter(tagAttributeValueLiteralValueType);
2262
+ return attributeValueQuoted(code);
1342
2263
  };
1343
- const image = (content2) => {
1344
- return {
1345
- type: "img",
1346
- url: imageCallback(content2.url),
1347
- alt: content2.alt || void 0,
1348
- caption: content2.title,
1349
- children: [{ type: "text", text: "" }]
1350
- };
2264
+ const attributeValueQuoted = function(code) {
2265
+ if (code === codes2.eof || code === marker || markdownLineEnding(code)) {
2266
+ effects.exit(tagAttributeValueLiteralValueType);
2267
+ return attributeValueQuotedStart(code);
2268
+ }
2269
+ effects.consume(code);
2270
+ return attributeValueQuoted;
1351
2271
  };
1352
- const text3 = (content2) => {
1353
- return {
1354
- type: "text",
1355
- text: content2.value
1356
- };
2272
+ const selfClosing = function(code) {
2273
+ if (code === findCode(pattern.end[pattern.end.length - 1])) {
2274
+ return tagEnd(code);
2275
+ }
2276
+ crash(code, "after self-closing slash", "`>` to end the tag" + (code === codes2.asterisk || code === codes2.slash ? " (note: JS comments in JSX tags are not supported in MDX)" : ""));
1357
2277
  };
1358
- const blockquote = (content2) => {
1359
- const children = [];
1360
- content2.children.map((child) => {
1361
- const inlineElements = unwrapBlockContent(child);
1362
- inlineElements.forEach((child2) => {
1363
- children.push(child2);
1364
- });
1365
- });
1366
- return {
1367
- type: "blockquote",
1368
- children
1369
- };
2278
+ const tagEnd = function(code) {
2279
+ effects.enter(tagMarkerType);
2280
+ effects.consume(code);
2281
+ effects.exit(tagMarkerType);
2282
+ effects.exit(tagType);
2283
+ return ok;
1370
2284
  };
1371
- const paragraph = (content2) => {
1372
- const children = flatten(content2.children.map(phrasingContent));
1373
- if (children.length === 1) {
1374
- if (children[0]) {
1375
- if (children[0].type === "html_inline") {
1376
- return {
1377
- ...children[0],
1378
- type: "html"
1379
- };
1380
- }
2285
+ const optionalEsWhitespace = function(code) {
2286
+ if (markdownLineEnding(code)) {
2287
+ if (allowLazy) {
2288
+ effects.enter(types.lineEnding);
2289
+ effects.consume(code);
2290
+ effects.exit(types.lineEnding);
2291
+ return factorySpace(effects, optionalEsWhitespace, types.linePrefix, constants.tabSize);
1381
2292
  }
2293
+ return effects.attempt(lazyLineEnd, factorySpace(effects, optionalEsWhitespace, types.linePrefix, constants.tabSize), crashEol)(code);
1382
2294
  }
1383
- return {
1384
- type: "p",
1385
- children
2295
+ if (markdownSpace(code) || unicodeWhitespace(code)) {
2296
+ effects.enter("esWhitespace");
2297
+ return optionalEsWhitespaceContinue(code);
2298
+ }
2299
+ return returnState(code);
2300
+ };
2301
+ const optionalEsWhitespaceContinue = function(code) {
2302
+ if (markdownLineEnding(code) || !(markdownSpace(code) || unicodeWhitespace(code))) {
2303
+ effects.exit("esWhitespace");
2304
+ return optionalEsWhitespace(code);
2305
+ }
2306
+ effects.consume(code);
2307
+ return optionalEsWhitespaceContinue;
2308
+ };
2309
+ function crashEol() {
2310
+ throw new VFileMessage2("Unexpected lazy line in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc", self.now(), "micromark-extension-mdx-jsx:unexpected-eof");
2311
+ }
2312
+ function crash(code, at, expect) {
2313
+ throw new VFileMessage2("Unexpected " + (code === codes2.eof ? "end of file" : "character `" + (code === codes2.graveAccent ? "` ` `" : String.fromCodePoint(code)) + "` (" + serializeCharCode(code) + ")") + " " + at + ", expected " + expect, self.now(), "micromark-extension-mdx-jsx:unexpected-" + (code === codes2.eof ? "eof" : "character"));
2314
+ }
2315
+ return start;
2316
+ }
2317
+ var tokenizeLazyLineEnd = function(effects, ok, nok) {
2318
+ const self = this;
2319
+ const start = function(code) {
2320
+ assert(markdownLineEnding(code), "expected eol");
2321
+ effects.enter(types.lineEnding);
2322
+ effects.consume(code);
2323
+ effects.exit(types.lineEnding);
2324
+ return lineStart;
2325
+ };
2326
+ const lineStart = function(code) {
2327
+ return self.parser.lazy[self.now().line] ? nok(code) : ok(code);
2328
+ };
2329
+ return start;
2330
+ };
2331
+ var serializeCharCode = function(code) {
2332
+ return "U+" + code.toString(constants.numericBaseHexadecimal).toUpperCase().padStart(4, "0");
2333
+ };
2334
+ var lazyLineEnd = { tokenize: tokenizeLazyLineEnd, partial: true };
2335
+
2336
+ // src/next/shortcodes/lib/jsx-text.ts
2337
+ var jsxText = function(acorn2, acornOptions, addResult, pattern) {
2338
+ const tokenizeJsxText = function(effects, ok, nok) {
2339
+ const self = this;
2340
+ return factoryTag.call(self, effects, ok, nok, acorn2, acornOptions, addResult, true, "mdxJsxTextTag", "mdxJsxTextTagMarker", "mdxJsxTextTagClosingMarker", "mdxJsxTextTagSelfClosingMarker", "mdxJsxTextTagName", "mdxJsxTextTagNamePrimary", "mdxJsxTextTagNameMemberMarker", "mdxJsxTextTagNameMember", "mdxJsxTextTagNamePrefixMarker", "mdxJsxTextTagNameLocal", "mdxJsxTextTagExpressionAttribute", "mdxJsxTextTagExpressionAttributeMarker", "mdxJsxTextTagExpressionAttributeValue", "mdxJsxTextTagAttribute", "mdxJsxTextTagAttributeName", "mdxJsxTextTagAttributeNamePrimary", "mdxJsxTextTagAttributeNamePrefixMarker", "mdxJsxTextTagAttributeNameLocal", "mdxJsxTextTagAttributeInitializerMarker", "mdxJsxTextTagAttributeValueLiteral", "mdxJsxTextTagAttributeValueLiteralMarker", "mdxJsxTextTagAttributeValueLiteralValue", "mdxJsxTextTagAttributeValueExpression", "mdxJsxTextTagAttributeValueExpressionMarker", "mdxJsxTextTagAttributeValueExpressionValue", pattern);
2341
+ };
2342
+ return { tokenize: tokenizeJsxText };
2343
+ };
2344
+
2345
+ // src/next/shortcodes/lib/jsx-flow.ts
2346
+ import { factorySpace as factorySpace2 } from "micromark-factory-space";
2347
+ import { markdownLineEnding as markdownLineEnding2 } from "micromark-util-character";
2348
+ import { codes as codes3 } from "micromark-util-symbol/codes.js";
2349
+ import { types as types2 } from "micromark-util-symbol/types.js";
2350
+ var jsxFlow = function(acorn2, acornOptions, addResult, pattern) {
2351
+ const tokenizeJsxFlow = function(effects, ok, nok) {
2352
+ const self = this;
2353
+ const start = function(code) {
2354
+ return factoryTag.call(self, effects, factorySpace2(effects, after, types2.whitespace), nok, acorn2, acornOptions, addResult, false, "mdxJsxFlowTag", "mdxJsxFlowTagMarker", "mdxJsxFlowTagClosingMarker", "mdxJsxFlowTagSelfClosingMarker", "mdxJsxFlowTagName", "mdxJsxFlowTagNamePrimary", "mdxJsxFlowTagNameMemberMarker", "mdxJsxFlowTagNameMember", "mdxJsxFlowTagNamePrefixMarker", "mdxJsxFlowTagNameLocal", "mdxJsxFlowTagExpressionAttribute", "mdxJsxFlowTagExpressionAttributeMarker", "mdxJsxFlowTagExpressionAttributeValue", "mdxJsxFlowTagAttribute", "mdxJsxFlowTagAttributeName", "mdxJsxFlowTagAttributeNamePrimary", "mdxJsxFlowTagAttributeNamePrefixMarker", "mdxJsxFlowTagAttributeNameLocal", "mdxJsxFlowTagAttributeInitializerMarker", "mdxJsxFlowTagAttributeValueLiteral", "mdxJsxFlowTagAttributeValueLiteralMarker", "mdxJsxFlowTagAttributeValueLiteralValue", "mdxJsxFlowTagAttributeValueExpression", "mdxJsxFlowTagAttributeValueExpressionMarker", "mdxJsxFlowTagAttributeValueExpressionValue", pattern)(code);
2355
+ };
2356
+ const after = function(code) {
2357
+ return code === codes3.lessThan ? start(code) : code === codes3.eof || markdownLineEnding2(code) ? ok(code) : nok(code);
1386
2358
  };
2359
+ return start;
1387
2360
  };
2361
+ return { tokenize: tokenizeJsxFlow, concrete: true };
2362
+ };
2363
+
2364
+ // src/next/shortcodes/lib/syntax.ts
2365
+ function mdxJsx(options = {}) {
2366
+ const acorn2 = options.acorn;
2367
+ let acornOptions;
2368
+ if (acorn2) {
2369
+ if (!acorn2.parse || !acorn2.parseExpressionAt) {
2370
+ throw new Error("Expected a proper `acorn` instance passed in as `options.acorn`");
2371
+ }
2372
+ acornOptions = Object.assign({ ecmaVersion: 2020, sourceType: "module" }, options.acornOptions, { locations: true });
2373
+ } else if (options.acornOptions || options.addResult) {
2374
+ throw new Error("Expected an `acorn` instance passed in as `options.acorn`");
2375
+ }
2376
+ const patterns = options.patterns || [];
2377
+ const flowRules = {};
2378
+ const textRules = {};
2379
+ patterns.forEach((pattern) => {
2380
+ const firstCharacter = findCode(pattern.start[0])?.toString();
2381
+ if (!firstCharacter) {
2382
+ return;
2383
+ }
2384
+ if (pattern.type === "flow") {
2385
+ const existing = flowRules[firstCharacter];
2386
+ flowRules[firstCharacter] = existing ? [
2387
+ ...existing,
2388
+ jsxFlow(acorn2, acornOptions, options.addResult, pattern)
2389
+ ] : [jsxFlow(acorn2, acornOptions, options.addResult, pattern)];
2390
+ } else {
2391
+ const existing = textRules[firstCharacter];
2392
+ textRules[firstCharacter] = existing ? [
2393
+ ...existing,
2394
+ jsxText(acorn2, acornOptions, options.addResult, pattern)
2395
+ ] : [jsxText(acorn2, acornOptions, options.addResult, pattern)];
2396
+ }
2397
+ });
1388
2398
  return {
1389
- type: "root",
1390
- children: root.children.map((child) => {
1391
- return content(child);
1392
- })
2399
+ flow: flowRules,
2400
+ text: textRules
1393
2401
  };
2402
+ }
2403
+
2404
+ // src/next/parse/markdown.ts
2405
+ import * as acorn from "acorn";
2406
+ var fromMarkdown = (value, field) => {
2407
+ const patterns = getFieldPatterns(field);
2408
+ const acornDefault = acorn;
2409
+ const tree = mdastFromMarkdown(value, {
2410
+ extensions: [mdxJsx({ acorn: acornDefault, patterns, addResult: true })],
2411
+ mdastExtensions: [mdxJsxFromMarkdown({ patterns })]
2412
+ });
2413
+ return tree;
1394
2414
  };
1395
- var RichTextParseError = class extends Error {
1396
- constructor(message, position) {
1397
- super(message);
1398
- __publicField(this, "position");
1399
- if (Error.captureStackTrace) {
1400
- Error.captureStackTrace(this, RichTextParseError);
2415
+
2416
+ // src/next/parse/index.ts
2417
+ import { compact } from "mdast-util-compact";
2418
+
2419
+ // src/next/parse/post-processing.ts
2420
+ import { visit } from "unist-util-visit";
2421
+
2422
+ // src/parse/remarkToPlate.ts
2423
+ import { flatten } from "lodash-es";
2424
+
2425
+ // src/parse/acorn.ts
2426
+ var extractAttributes = (attributes2, fields, imageCallback) => {
2427
+ const properties = {};
2428
+ attributes2?.forEach((attribute) => {
2429
+ assertType(attribute, "mdxJsxAttribute");
2430
+ const field = fields.find((field2) => field2.name === attribute.name);
2431
+ if (!field) {
2432
+ throw new Error(`Unable to find field definition for property "${attribute.name}"`);
1401
2433
  }
1402
- this.name = "RichTextParseError";
1403
- this.position = position;
2434
+ try {
2435
+ properties[attribute.name] = extractAttribute(attribute, field, imageCallback);
2436
+ } catch (e) {
2437
+ if (e instanceof Error) {
2438
+ throw new Error(`Unable to parse field value for field "${field.name}" (type: ${field.type}). ${e.message}`);
2439
+ }
2440
+ throw e;
2441
+ }
2442
+ });
2443
+ return properties;
2444
+ };
2445
+ var extractAttribute = (attribute, field, imageCallback) => {
2446
+ switch (field.type) {
2447
+ case "boolean":
2448
+ case "number":
2449
+ return extractScalar(extractExpression(attribute), field);
2450
+ case "datetime":
2451
+ case "string":
2452
+ if (field.list) {
2453
+ return extractScalar(extractExpression(attribute), field);
2454
+ } else {
2455
+ return extractString(attribute, field);
2456
+ }
2457
+ case "image":
2458
+ if (field.list) {
2459
+ const values3 = extractScalar(extractExpression(attribute), field);
2460
+ return values3.split(",").map((value) => imageCallback(value));
2461
+ } else {
2462
+ const value = extractString(attribute, field);
2463
+ return imageCallback(value);
2464
+ }
2465
+ case "reference":
2466
+ if (field.list) {
2467
+ return extractScalar(extractExpression(attribute), field);
2468
+ } else {
2469
+ return extractString(attribute, field);
2470
+ }
2471
+ case "object":
2472
+ return extractObject(extractExpression(attribute), field);
2473
+ case "rich-text":
2474
+ const JSXString = extractRaw(attribute);
2475
+ if (JSXString) {
2476
+ return parseMDX(JSXString, field, imageCallback);
2477
+ } else {
2478
+ return {};
2479
+ }
2480
+ default:
2481
+ throw new Error(`Extract attribute: Unhandled field type ${field.type}`);
1404
2482
  }
1405
2483
  };
1406
-
1407
- // src/extensions/tina-shortcodes/from-markdown.ts
1408
- import { parseEntities } from "parse-entities";
1409
- var enterContainer = function(token) {
1410
- enter.call(this, "containerDirective", token);
1411
- };
1412
- var enterLeaf = function(token) {
1413
- enter.call(this, "leafDirective", token);
1414
- };
1415
- var enterText = function(token) {
1416
- enter.call(this, "textDirective", token);
1417
- };
1418
- var enter = function(type, token) {
1419
- this.enter({ type, name: "", attributes: {}, children: [] }, token);
1420
- };
1421
- function exitName(token) {
1422
- const node = this.stack[this.stack.length - 1];
1423
- node.name = this.sliceSerialize(token);
1424
- }
1425
- var enterContainerLabel = function(token) {
1426
- this.enter({ type: "paragraph", data: { directiveLabel: true }, children: [] }, token);
1427
- };
1428
- var exitContainerLabel = function(token) {
1429
- this.exit(token);
1430
- };
1431
- var enterAttributes = function() {
1432
- this.setData("directiveAttributes", []);
1433
- this.buffer();
1434
- };
1435
- var exitAttributeIdValue = function(token) {
1436
- const list = this.getData("directiveAttributes");
1437
- if (list) {
1438
- list.push([
1439
- "id",
1440
- parseEntities(this.sliceSerialize(token), {
1441
- attribute: true
1442
- })
1443
- ]);
2484
+ var extractScalar = (attribute, field) => {
2485
+ if (field.list) {
2486
+ assertType(attribute.expression, "ArrayExpression");
2487
+ return attribute.expression.elements.map((element) => {
2488
+ assertHasType(element);
2489
+ assertType(element, "Literal");
2490
+ return element.value;
2491
+ });
2492
+ } else {
2493
+ assertType(attribute.expression, "Literal");
2494
+ return attribute.expression.value;
1444
2495
  }
1445
2496
  };
1446
- var exitAttributeClassValue = function(token) {
1447
- const list = this.getData("directiveAttributes");
1448
- if (list) {
1449
- list.push([
1450
- "class",
1451
- parseEntities(this.sliceSerialize(token), {
1452
- attribute: true
1453
- })
1454
- ]);
2497
+ var extractObject = (attribute, field) => {
2498
+ if (field.list) {
2499
+ assertType(attribute.expression, "ArrayExpression");
2500
+ return attribute.expression.elements.map((element) => {
2501
+ assertHasType(element);
2502
+ assertType(element, "ObjectExpression");
2503
+ return extractObjectExpression(element, field);
2504
+ });
2505
+ } else {
2506
+ assertType(attribute.expression, "ObjectExpression");
2507
+ return extractObjectExpression(attribute.expression, field);
1455
2508
  }
1456
2509
  };
1457
- var exitAttributeValue = function(token) {
1458
- const list = this.getData("directiveAttributes");
1459
- if (list) {
1460
- const item = list[list.length - 1];
1461
- if (item) {
1462
- item[1] = parseEntities(this.sliceSerialize(token), {
1463
- attribute: true
1464
- });
2510
+ var extractObjectExpression = (expression, field) => {
2511
+ const properties = {};
2512
+ expression.properties?.forEach((property) => {
2513
+ assertType(property, "Property");
2514
+ const { key, value } = extractKeyValue(property, field);
2515
+ properties[key] = value;
2516
+ });
2517
+ return properties;
2518
+ };
2519
+ var getField = (objectField, name) => {
2520
+ if (objectField.fields) {
2521
+ if (typeof objectField.fields === "string") {
2522
+ throw new Error("Global templates not supported");
1465
2523
  }
2524
+ return objectField.fields.find((f) => f.name === name);
1466
2525
  }
1467
2526
  };
1468
- var exitAttributeName = function(token) {
1469
- const list = this.getData("directiveAttributes");
1470
- if (list) {
1471
- const name = this.sliceSerialize(token);
1472
- if (!name) {
1473
- list.push(["_value", ""]);
2527
+ var extractKeyValue = (property, parentField) => {
2528
+ assertType(property.key, "Identifier");
2529
+ const key = property.key.name;
2530
+ const field = getField(parentField, key);
2531
+ if (field?.type === "object") {
2532
+ if (field.list) {
2533
+ assertType(property.value, "ArrayExpression");
2534
+ const value = property.value.elements.map((element) => {
2535
+ assertHasType(element);
2536
+ assertType(element, "ObjectExpression");
2537
+ return extractObjectExpression(element, field);
2538
+ });
2539
+ return { key, value };
1474
2540
  } else {
1475
- list.push([this.sliceSerialize(token), ""]);
2541
+ assertType(property.value, "ObjectExpression");
2542
+ const value = extractObjectExpression(property.value, field);
2543
+ return { key, value };
1476
2544
  }
2545
+ } else {
2546
+ assertType(property.value, "Literal");
2547
+ return { key, value: property.value.value };
1477
2548
  }
1478
2549
  };
1479
- function exitAttributes() {
1480
- const list = this.getData("directiveAttributes");
1481
- const cleaned = {};
1482
- let index = -1;
1483
- if (list) {
1484
- while (++index < list.length) {
1485
- const attribute = list[index];
1486
- if (attribute) {
1487
- if (attribute[0] === "class" && cleaned.class) {
1488
- cleaned.class += " " + attribute[1];
1489
- } else {
1490
- cleaned[attribute[0]] = attribute[1];
1491
- }
1492
- }
2550
+ var extractStatement = (attribute) => {
2551
+ const body = attribute.data?.estree?.body;
2552
+ if (body) {
2553
+ if (body[0]) {
2554
+ assertType(body[0], "ExpressionStatement");
2555
+ return body[0];
1493
2556
  }
1494
2557
  }
1495
- this.setData("directiveAttributes");
1496
- this.resume();
1497
- const node = this.stack[this.stack.length - 1];
1498
- node.attributes = cleaned;
1499
- }
1500
- function exit(token) {
1501
- this.exit(token);
1502
- }
1503
- var directiveFromMarkdown = {
1504
- canContainEols: ["textDirective"],
1505
- enter: {
1506
- directiveContainer: enterContainer,
1507
- directiveContainerAttributes: enterAttributes,
1508
- directiveContainerLabel: enterContainerLabel,
1509
- directiveLeaf: enterLeaf,
1510
- directiveLeafAttributes: enterAttributes,
1511
- directiveText: enterText,
1512
- directiveTextAttributes: enterAttributes
1513
- },
1514
- exit: {
1515
- directiveContainer: exit,
1516
- directiveContainerAttributeClassValue: exitAttributeClassValue,
1517
- directiveContainerAttributeIdValue: exitAttributeIdValue,
1518
- directiveContainerAttributeName: exitAttributeName,
1519
- directiveContainerAttributeValue: exitAttributeValue,
1520
- directiveContainerAttributes: exitAttributes,
1521
- directiveContainerLabel: exitContainerLabel,
1522
- directiveContainerName: exitName,
1523
- directiveLeaf: exit,
1524
- directiveLeafAttributeClassValue: exitAttributeClassValue,
1525
- directiveLeafAttributeIdValue: exitAttributeIdValue,
1526
- directiveLeafAttributeName: exitAttributeName,
1527
- directiveLeafAttributeValue: exitAttributeValue,
1528
- directiveLeafAttributes: exitAttributes,
1529
- directiveLeafName: exitName,
1530
- directiveText: exit,
1531
- directiveTextAttributeClassValue: exitAttributeClassValue,
1532
- directiveTextAttributeIdValue: exitAttributeIdValue,
1533
- directiveTextAttributeName: exitAttributeName,
1534
- directiveTextAttributeValue: exitAttributeValue,
1535
- directiveTextAttributes: exitAttributes,
1536
- directiveTextName: exitName
1537
- }
2558
+ throw new Error(`Unable to extract body from expression`);
1538
2559
  };
1539
-
1540
- // src/extensions/tina-shortcodes/shortcode-leaf.ts
1541
- import { factorySpace as factorySpace2 } from "micromark-factory-space";
1542
- import { markdownLineEnding as markdownLineEnding2, markdownSpace as markdownSpace2 } from "micromark-util-character";
1543
- import { codes as codes3 } from "micromark-util-symbol/codes";
1544
- import { values } from "micromark-util-symbol/values";
1545
- import { types as types2 } from "micromark-util-symbol/types";
1546
-
1547
- // src/extensions/tina-shortcodes/factory-attributes.ts
1548
- import { factorySpace } from "micromark-factory-space";
1549
- import { factoryWhitespace } from "micromark-factory-whitespace";
1550
- import {
1551
- asciiAlpha,
1552
- asciiAlphanumeric,
1553
- markdownLineEnding,
1554
- markdownLineEndingOrSpace,
1555
- markdownSpace
1556
- } from "micromark-util-character";
1557
- import { codes } from "micromark-util-symbol/codes";
1558
- import { types } from "micromark-util-symbol/types";
1559
- function factoryAttributes(effects, ok, nnok, attributesType, attributesMarkerType, attributeType, attributeIdType, attributeClassType, attributeNameType, attributeInitializerType, attributeValueLiteralType, attributeValueType, attributeValueMarker, attributeValueData, disallowEol) {
1560
- let type;
1561
- let marker;
1562
- const nok = function(code) {
1563
- return nnok(code);
1564
- };
1565
- const start = function(code) {
1566
- effects.enter(attributesType);
1567
- return between(code);
1568
- };
1569
- const between = function(code) {
1570
- if (code === codes.numberSign) {
1571
- type = attributeIdType;
1572
- return shortcutStart(code);
1573
- }
1574
- if (code === codes.dot) {
1575
- type = attributeClassType;
1576
- return shortcutStart(code);
1577
- }
1578
- if (code === codes.colon || code === codes.underscore || asciiAlpha(code)) {
1579
- effects.enter(attributeType);
1580
- effects.enter(attributeNameType);
1581
- effects.consume(code);
1582
- return name;
1583
- }
1584
- if (code === codes.quotationMark || code === codes.apostrophe) {
1585
- effects.enter(attributeNameType);
1586
- effects.exit(attributeNameType);
1587
- effects.enter(attributeType);
1588
- return valueBefore(code);
1589
- }
1590
- if (disallowEol && markdownSpace(code)) {
1591
- return factorySpace(effects, between, types.whitespace)(code);
1592
- }
1593
- if (!disallowEol && markdownLineEndingOrSpace(code)) {
1594
- return factoryWhitespace(effects, between)(code);
1595
- }
1596
- return end(code);
1597
- };
1598
- const shortcutStart = function(code) {
1599
- effects.enter(attributeType);
1600
- effects.enter(type);
1601
- effects.enter(type + "Marker");
1602
- effects.consume(code);
1603
- effects.exit(type + "Marker");
1604
- return shortcutStartAfter;
1605
- };
1606
- const shortcutStartAfter = function(code) {
1607
- if (code === codes.eof || code === codes.quotationMark || code === codes.numberSign || code === codes.apostrophe || code === codes.dot || code === codes.lessThan || code === codes.equalsTo || code === codes.greaterThan || code === codes.graveAccent || code === codes.rightCurlyBrace || markdownLineEndingOrSpace(code)) {
1608
- return nok(code);
1609
- }
1610
- effects.enter(type + "Value");
1611
- effects.consume(code);
1612
- return shortcut;
1613
- };
1614
- const shortcut = function(code) {
1615
- if (code === codes.eof || code === codes.quotationMark || code === codes.apostrophe || code === codes.lessThan || code === codes.equalsTo || code === codes.greaterThan || code === codes.graveAccent) {
1616
- return nok(code);
1617
- }
1618
- if (code === codes.numberSign || code === codes.dot || code === codes.rightCurlyBrace || markdownLineEndingOrSpace(code)) {
1619
- effects.exit(type + "Value");
1620
- effects.exit(type);
1621
- effects.exit(attributeType);
1622
- return between(code);
1623
- }
1624
- effects.consume(code);
1625
- return shortcut;
1626
- };
1627
- const name = function(code) {
1628
- if (code === codes.dash || code === codes.dot || code === codes.colon || code === codes.underscore || asciiAlphanumeric(code)) {
1629
- effects.consume(code);
1630
- return name;
1631
- }
1632
- effects.exit(attributeNameType);
1633
- if (disallowEol && markdownSpace(code)) {
1634
- return factorySpace(effects, nameAfter, types.whitespace)(code);
1635
- }
1636
- if (!disallowEol && markdownLineEndingOrSpace(code)) {
1637
- return factoryWhitespace(effects, nameAfter)(code);
1638
- }
1639
- return nameAfter(code);
1640
- };
1641
- const nameAfter = function(code) {
1642
- if (code === codes.equalsTo) {
1643
- effects.enter(attributeInitializerType);
1644
- effects.consume(code);
1645
- effects.exit(attributeInitializerType);
1646
- return valueBefore;
1647
- }
1648
- effects.exit(attributeType);
1649
- return between(code);
1650
- };
1651
- const valueBefore = function(code) {
1652
- if (code === codes.eof || code === codes.lessThan || code === codes.equalsTo || code === codes.greaterThan || code === codes.graveAccent || code === codes.rightCurlyBrace || disallowEol && markdownLineEnding(code)) {
1653
- return nok(code);
1654
- }
1655
- if (code === codes.quotationMark || code === codes.apostrophe) {
1656
- effects.enter(attributeValueLiteralType);
1657
- effects.enter(attributeValueMarker);
1658
- effects.consume(code);
1659
- effects.exit(attributeValueMarker);
1660
- marker = code;
1661
- return valueQuotedStart;
1662
- }
1663
- if (disallowEol && markdownSpace(code)) {
1664
- return factorySpace(effects, valueBefore, types.whitespace)(code);
1665
- }
1666
- if (!disallowEol && markdownLineEndingOrSpace(code)) {
1667
- return factoryWhitespace(effects, valueBefore)(code);
1668
- }
1669
- effects.enter(attributeValueType);
1670
- effects.enter(attributeValueData);
1671
- effects.consume(code);
1672
- marker = void 0;
1673
- return valueUnquoted;
1674
- };
1675
- const valueUnquoted = function(code) {
1676
- if (code === codes.eof || code === codes.quotationMark || code === codes.apostrophe || code === codes.lessThan || code === codes.equalsTo || code === codes.greaterThan || code === codes.graveAccent) {
1677
- return nok(code);
1678
- }
1679
- if (code === codes.rightCurlyBrace || markdownLineEndingOrSpace(code)) {
1680
- effects.exit(attributeValueData);
1681
- effects.exit(attributeValueType);
1682
- effects.exit(attributeType);
1683
- return between(code);
1684
- }
1685
- effects.consume(code);
1686
- return valueUnquoted;
1687
- };
1688
- const valueQuotedStart = function(code) {
1689
- if (code === marker) {
1690
- effects.enter(attributeValueMarker);
1691
- effects.consume(code);
1692
- effects.exit(attributeValueMarker);
1693
- effects.exit(attributeValueLiteralType);
1694
- effects.exit(attributeType);
1695
- return valueQuotedAfter;
1696
- }
1697
- effects.enter(attributeValueType);
1698
- return valueQuotedBetween(code);
1699
- };
1700
- const valueQuotedBetween = function(code) {
1701
- if (code === marker) {
1702
- effects.exit(attributeValueType);
1703
- return valueQuotedStart(code);
1704
- }
1705
- if (code === codes.eof) {
1706
- return nok(code);
2560
+ var extractString = (attribute, field) => {
2561
+ if (attribute.type === "mdxJsxAttribute") {
2562
+ if (typeof attribute.value === "string") {
2563
+ return attribute.value;
1707
2564
  }
1708
- if (markdownLineEnding(code)) {
1709
- return disallowEol ? nok(code) : factoryWhitespace(effects, valueQuotedBetween)(code);
2565
+ }
2566
+ return extractScalar(extractExpression(attribute), field);
2567
+ };
2568
+ var extractExpression = (attribute) => {
2569
+ assertType(attribute, "mdxJsxAttribute");
2570
+ assertHasType(attribute.value);
2571
+ assertType(attribute.value, "mdxJsxAttributeValueExpression");
2572
+ return extractStatement(attribute.value);
2573
+ };
2574
+ var extractRaw = (attribute) => {
2575
+ assertType(attribute, "mdxJsxAttribute");
2576
+ assertHasType(attribute.value);
2577
+ assertType(attribute.value, "mdxJsxAttributeValueExpression");
2578
+ const rawValue = attribute.value.value;
2579
+ return trimFragments(rawValue);
2580
+ };
2581
+ function assertType(val, type) {
2582
+ if (val.type !== type) {
2583
+ throw new Error(`Expected type to be ${type} but received ${val.type}. ${MDX_PARSE_ERROR_MSG}`);
2584
+ }
2585
+ }
2586
+ function assertHasType(val) {
2587
+ if (val) {
2588
+ if (typeof val !== "string") {
2589
+ return;
1710
2590
  }
1711
- effects.enter(attributeValueData);
1712
- effects.consume(code);
1713
- return valueQuoted;
1714
- };
1715
- const valueQuoted = function(code) {
1716
- if (code === marker || code === codes.eof || markdownLineEnding(code)) {
1717
- effects.exit(attributeValueData);
1718
- return valueQuotedBetween(code);
2591
+ }
2592
+ throw new Error(`Expect value to be an object with property "type"`);
2593
+ }
2594
+ var trimFragments = (string) => {
2595
+ const rawArr = string.split("\n");
2596
+ let openingFragmentIndex = null;
2597
+ let closingFragmentIndex = null;
2598
+ rawArr.forEach((item, index) => {
2599
+ if (item.trim() === "<>") {
2600
+ if (!openingFragmentIndex) {
2601
+ openingFragmentIndex = index + 1;
2602
+ }
1719
2603
  }
1720
- effects.consume(code);
1721
- return valueQuoted;
1722
- };
1723
- const valueQuotedAfter = function(code) {
1724
- return code === codes.rightCurlyBrace || markdownLineEndingOrSpace(code) ? between(code) : end(code);
1725
- };
1726
- const end = function(code) {
1727
- if (!asciiAlpha(code)) {
1728
- effects.enter(attributesMarkerType);
1729
- effects.exit(attributesMarkerType);
1730
- effects.exit(attributesType);
1731
- return ok(code);
2604
+ });
2605
+ rawArr.reverse().forEach((item, index) => {
2606
+ if (item.trim() === "</>") {
2607
+ const length = rawArr.length - 1;
2608
+ if (!closingFragmentIndex) {
2609
+ closingFragmentIndex = length - index;
2610
+ }
1732
2611
  }
1733
- return nok(code);
1734
- };
1735
- return start;
1736
- }
2612
+ });
2613
+ const value = rawArr.reverse().slice(openingFragmentIndex || 0, closingFragmentIndex || rawArr.length - 1).join("\n");
2614
+ return value;
2615
+ };
1737
2616
 
1738
- // src/extensions/tina-shortcodes/factory-name.ts
1739
- import { asciiAlpha as asciiAlpha2, asciiAlphanumeric as asciiAlphanumeric2 } from "micromark-util-character";
1740
- import { codes as codes2 } from "micromark-util-symbol/codes";
1741
- function factoryName(effects, ok, nok, type, patternName) {
1742
- const self = this;
1743
- let nameIndex = 0;
1744
- const start = function(code) {
1745
- const character = patternName[nameIndex];
1746
- if (asciiAlpha2(code) && findCode(character) === code) {
1747
- nameIndex++;
1748
- effects.enter(type);
1749
- effects.consume(code);
1750
- return name;
2617
+ // src/parse/mdx.ts
2618
+ import { source } from "unist-util-source";
2619
+ function mdxJsxElement(node, field, imageCallback) {
2620
+ try {
2621
+ const template = field.templates?.find((template2) => {
2622
+ const templateName = typeof template2 === "string" ? template2 : template2.name;
2623
+ return templateName === node.name;
2624
+ });
2625
+ if (typeof template === "string") {
2626
+ throw new Error("Global templates not yet supported");
1751
2627
  }
1752
- return nok(code);
1753
- };
1754
- const name = function(code) {
1755
- const character = patternName[nameIndex];
1756
- if (code === codes2.dash || code === codes2.underscore || asciiAlphanumeric2(code)) {
1757
- if (findCode(character) === code) {
1758
- effects.consume(code);
1759
- nameIndex++;
1760
- return name;
2628
+ if (!template) {
2629
+ const string = toTinaMarkdown2({ type: "root", children: [node] }, field);
2630
+ return {
2631
+ type: node.type === "mdxJsxFlowElement" ? "html" : "html_inline",
2632
+ value: string.trim(),
2633
+ children: [{ type: "text", text: "" }]
2634
+ };
2635
+ }
2636
+ const props = extractAttributes(node.attributes, template.fields, imageCallback);
2637
+ const childField = template.fields.find((field2) => field2.name === "children");
2638
+ if (childField) {
2639
+ if (childField.type === "rich-text") {
2640
+ props.children = remarkToSlate(node, childField, imageCallback);
1761
2641
  }
1762
- return nok(code);
1763
2642
  }
1764
- effects.exit(type);
1765
- return self.previous === codes2.dash || self.previous === codes2.underscore ? nok(code) : ok(code);
1766
- };
1767
- return start;
1768
- }
1769
-
1770
- // src/extensions/tina-shortcodes/shortcode-leaf.ts
1771
- var findValue = (string) => {
1772
- let lookupValue = null;
1773
- Object.entries(values).forEach(([key, value]) => {
1774
- if (value === string) {
1775
- lookupValue = key;
2643
+ return {
2644
+ type: node.type,
2645
+ name: node.name,
2646
+ children: [{ type: "text", text: "" }],
2647
+ props
2648
+ };
2649
+ } catch (e) {
2650
+ if (e instanceof Error) {
2651
+ throw new RichTextParseError(e.message, node.position);
1776
2652
  }
2653
+ throw e;
2654
+ }
2655
+ }
2656
+ var directiveElement = (node, field, imageCallback, raw) => {
2657
+ let template;
2658
+ template = field.templates?.find((template2) => {
2659
+ const templateName = typeof template2 === "string" ? template2 : template2.name;
2660
+ return templateName === node.name;
1777
2661
  });
1778
- return lookupValue;
1779
- };
1780
- var findCode = (string) => {
1781
- if (!string) {
1782
- return null;
2662
+ if (typeof template === "string") {
2663
+ throw new Error("Global templates not yet supported");
1783
2664
  }
1784
- const lookup = findValue(string);
1785
- let lookupValue = null;
1786
- if (lookup) {
1787
- Object.entries(codes3).forEach(([key, value]) => {
1788
- if (key === lookup) {
1789
- lookupValue = value;
1790
- }
2665
+ if (!template) {
2666
+ template = field.templates?.find((template2) => {
2667
+ const templateName = template2?.match?.name;
2668
+ return templateName === node.name;
1791
2669
  });
1792
2670
  }
1793
- return lookupValue;
1794
- };
1795
- var directiveLeaf = (pattern) => {
1796
- const tokenizeDirectiveLeaf = function(effects, ook, nnok) {
1797
- const self = this;
1798
- let startSequenceIndex = 1;
1799
- let endSequenceIndex = 0;
1800
- const ok = function(code) {
1801
- return ook(code);
1802
- };
1803
- const nok = function(code) {
1804
- return nnok(code);
1805
- };
1806
- const start = function(code) {
1807
- const firstCharacter = pattern.start[0];
1808
- if (findCode(firstCharacter) === code) {
1809
- effects.enter("directiveLeaf");
1810
- effects.enter("directiveLeafFence");
1811
- effects.enter("directiveLeafSequence");
1812
- effects.consume(code);
1813
- return sequenceOpen(code);
1814
- }
1815
- return nok(code);
1816
- };
1817
- const sequenceOpen = function(code) {
1818
- const nextCharacter = pattern.start[startSequenceIndex];
1819
- if (findCode(nextCharacter) === code) {
1820
- effects.consume(code);
1821
- startSequenceIndex++;
1822
- return sequenceOpen;
1823
- }
1824
- if (startSequenceIndex < pattern.start.length) {
1825
- return nok(code);
1826
- }
1827
- effects.exit("directiveLeafSequence");
1828
- return factorName(code);
1829
- };
1830
- const factorName = (code) => {
1831
- if (markdownSpace2(code)) {
1832
- return factorySpace2(effects, factorName, types2.whitespace)(code);
1833
- }
1834
- return factoryName.call(self, effects, afterName, nok, "directiveLeafName", pattern.name || pattern.templateName)(code);
1835
- };
1836
- const afterName = function(code) {
1837
- if (markdownSpace2(code)) {
1838
- return factorySpace2(effects, afterName, types2.whitespace)(code);
1839
- }
1840
- if (markdownLineEnding2(code)) {
1841
- return nok;
1842
- }
1843
- return startAttributes;
1844
- };
1845
- const startAttributes = function(code) {
1846
- const nextCharacter = pattern.end[endSequenceIndex];
1847
- if (findCode(nextCharacter) === code) {
1848
- return afterAttributes(code);
1849
- }
1850
- return effects.attempt(attributes2, afterAttributes, afterAttributes)(code);
1851
- };
1852
- const end = function(code) {
1853
- effects.exit("directiveLeafFence");
1854
- effects.exit("directiveLeaf");
1855
- return ok(code);
2671
+ if (!template) {
2672
+ return {
2673
+ type: "p",
2674
+ children: [{ type: "text", text: source(node, raw || "") || "" }]
1856
2675
  };
1857
- const afterAttributes = function(code) {
1858
- const nextCharacter = pattern.end[endSequenceIndex];
1859
- if (pattern.end.length === endSequenceIndex) {
1860
- return factorySpace2(effects, end, types2.whitespace)(code);
1861
- }
1862
- if (code === codes3.eof) {
1863
- return nok;
1864
- }
1865
- if (findCode(nextCharacter) === code) {
1866
- effects.consume(code);
1867
- endSequenceIndex++;
1868
- return afterAttributes;
2676
+ }
2677
+ if (typeof template === "string") {
2678
+ throw new Error(`Global templates not supported`);
2679
+ }
2680
+ const props = node.attributes || {};
2681
+ const childField = template.fields.find((field2) => field2.name === "children");
2682
+ if (childField) {
2683
+ if (childField.type === "rich-text") {
2684
+ if (node.type === "containerDirective") {
2685
+ props.children = remarkToSlate(node, childField, imageCallback, raw);
1869
2686
  }
1870
- return nok;
1871
- };
1872
- return start;
1873
- };
1874
- const tokenizeAttributes = function(effects, ok, nok) {
1875
- return factoryAttributes(effects, ok, nok, "directiveLeafAttributes", "directiveLeafAttributesMarker", "directiveLeafAttribute", "directiveLeafAttributeId", "directiveLeafAttributeClass", "directiveLeafAttributeName", "directiveLeafAttributeInitializerMarker", "directiveLeafAttributeValueLiteral", "directiveLeafAttributeValue", "directiveLeafAttributeValueMarker", "directiveLeafAttributeValueData", true);
1876
- };
1877
- const attributes2 = { tokenize: tokenizeAttributes, partial: true };
2687
+ }
2688
+ }
1878
2689
  return {
1879
- tokenize: tokenizeDirectiveLeaf
2690
+ type: "mdxJsxFlowElement",
2691
+ name: template.name,
2692
+ props,
2693
+ children: [{ type: "text", text: "" }]
1880
2694
  };
1881
2695
  };
1882
2696
 
1883
- // src/extensions/tina-shortcodes/shortcode-container.ts
1884
- import { ok as assert } from "uvu/assert";
1885
- import { factorySpace as factorySpace3 } from "micromark-factory-space";
1886
- import { markdownLineEnding as markdownLineEnding3, markdownSpace as markdownSpace3 } from "micromark-util-character";
1887
- import { codes as codes4 } from "micromark-util-symbol/codes";
1888
- import { constants } from "micromark-util-symbol/constants";
1889
- import { types as types3 } from "micromark-util-symbol/types";
1890
- var directiveContainer = (pattern) => {
1891
- const tokenizeDirectiveContainer = function(effects, ook, nnok) {
1892
- const self = this;
1893
- const tail = self.events[self.events.length - 1];
1894
- const initialSize = tail && tail[1].type === types3.linePrefix ? tail[2].sliceSerialize(tail[1], true).length : 0;
1895
- let previous;
1896
- let startSequenceIndex = 1;
1897
- let closeStartSequenceIndex = 0;
1898
- let endNameIndex = 0;
1899
- let endSequenceIndex = 0;
1900
- let closeEndSequenceIndex = 0;
1901
- const ok = function(code) {
1902
- return ook(code);
1903
- };
1904
- const nok = function(code) {
1905
- return nnok(code);
1906
- };
1907
- const start = function(code) {
1908
- const firstCharacter = pattern.start[0];
1909
- if (findCode(firstCharacter) === code) {
1910
- effects.enter("directiveContainer");
1911
- effects.enter("directiveContainerFence");
1912
- effects.enter("directiveContainerSequence");
1913
- effects.consume(code);
1914
- return sequenceOpen(code);
1915
- }
1916
- return nok(code);
1917
- };
1918
- const sequenceOpen = function(code) {
1919
- const nextCharacter = pattern.start[startSequenceIndex];
1920
- if (findCode(nextCharacter) === code) {
1921
- effects.consume(code);
1922
- startSequenceIndex++;
1923
- return sequenceOpen;
2697
+ // src/parse/remarkToPlate.ts
2698
+ var remarkToSlate = (root, field, imageCallback, raw, skipMDXProcess) => {
2699
+ const mdxJsxElement2 = skipMDXProcess ? (node) => node : mdxJsxElement;
2700
+ const content = (content2) => {
2701
+ switch (content2.type) {
2702
+ case "blockquote":
2703
+ const children = [];
2704
+ content2.children.map((child) => {
2705
+ const inlineElements = unwrapBlockContent(child);
2706
+ inlineElements.forEach((child2) => {
2707
+ children.push(child2);
2708
+ });
2709
+ });
2710
+ return {
2711
+ type: "blockquote",
2712
+ children
2713
+ };
2714
+ case "heading":
2715
+ return heading(content2);
2716
+ case "code":
2717
+ return code(content2);
2718
+ case "paragraph":
2719
+ return paragraph(content2);
2720
+ case "mdxJsxFlowElement":
2721
+ return mdxJsxElement2(content2, field, imageCallback);
2722
+ case "thematicBreak":
2723
+ return {
2724
+ type: "hr",
2725
+ children: [{ type: "text", text: "" }]
2726
+ };
2727
+ case "listItem":
2728
+ return {
2729
+ type: "li",
2730
+ children: [
2731
+ {
2732
+ type: "lic",
2733
+ children: flatten(content2.children.map((child) => unwrapBlockContent(child)))
2734
+ }
2735
+ ]
2736
+ };
2737
+ case "list":
2738
+ return list(content2);
2739
+ case "html":
2740
+ return html(content2);
2741
+ case "mdxFlowExpression":
2742
+ case "mdxjsEsm":
2743
+ throw new RichTextParseError(`Unexpected expression ${content2.value}.`, content2.position);
2744
+ case "leafDirective": {
2745
+ return directiveElement(content2, field, imageCallback, raw);
1924
2746
  }
1925
- if (startSequenceIndex < pattern.start.length) {
1926
- return nok(code);
2747
+ case "containerDirective": {
2748
+ return directiveElement(content2, field, imageCallback, raw);
1927
2749
  }
1928
- effects.exit("directiveContainerSequence");
1929
- return factorName(code);
2750
+ default:
2751
+ throw new RichTextParseError(`Content: ${content2.type} is not yet supported`, content2.position);
2752
+ }
2753
+ };
2754
+ const html = (content2) => {
2755
+ return {
2756
+ type: "p",
2757
+ children: [{ type: "text", text: content2.value }]
1930
2758
  };
1931
- const factorName = (code) => {
1932
- if (markdownSpace3(code)) {
1933
- return factorySpace3(effects, factorName, types3.whitespace)(code);
1934
- }
1935
- return factoryName.call(self, effects, afterName, nok, "directiveContainerName", pattern.name || pattern.templateName)(code);
2759
+ };
2760
+ const html_inline = (content2) => {
2761
+ return { type: "text", text: content2.value };
2762
+ };
2763
+ const list = (content2) => {
2764
+ return {
2765
+ type: content2.ordered ? "ol" : "ul",
2766
+ children: content2.children.map((child) => listItem(child))
1936
2767
  };
1937
- const afterName = function(code) {
1938
- if (markdownSpace3(code)) {
1939
- return factorySpace3(effects, afterName, types3.whitespace)(code);
1940
- }
1941
- if (markdownLineEnding3(code)) {
1942
- return nok;
1943
- }
1944
- return startAttributes;
2768
+ };
2769
+ const listItem = (content2) => {
2770
+ return {
2771
+ type: "li",
2772
+ children: content2.children.map((child) => {
2773
+ switch (child.type) {
2774
+ case "list":
2775
+ return list(child);
2776
+ case "heading":
2777
+ case "paragraph":
2778
+ return {
2779
+ type: "lic",
2780
+ children: flatten(child.children.map((child2) => phrasingContent(child2)))
2781
+ };
2782
+ case "blockquote": {
2783
+ return {
2784
+ ...blockquote(child),
2785
+ type: "lic"
2786
+ };
2787
+ }
2788
+ case "mdxJsxFlowElement":
2789
+ return {
2790
+ type: "lic",
2791
+ children: [
2792
+ mdxJsxElement2({ ...child, type: "mdxJsxTextElement" }, field, imageCallback)
2793
+ ]
2794
+ };
2795
+ case "html":
2796
+ return {
2797
+ type: "lic",
2798
+ children: html_inline(child)
2799
+ };
2800
+ case "leafDirective": {
2801
+ return {
2802
+ type: "lic",
2803
+ children: [directiveElement(child, field, imageCallback)]
2804
+ };
2805
+ }
2806
+ case "code":
2807
+ case "thematicBreak":
2808
+ case "table":
2809
+ throw new RichTextParseError(`${child.type} inside list item is not supported`, child.position);
2810
+ default:
2811
+ let position;
2812
+ if (child.type !== "containerDirective") {
2813
+ position = child.position;
2814
+ }
2815
+ throw new RichTextParseError(`Unknown list item of type ${child.type}`, position);
2816
+ }
2817
+ })
1945
2818
  };
1946
- const startAttributes = function(code) {
1947
- const nextCharacter = pattern.end[endSequenceIndex];
1948
- if (findCode(nextCharacter) === code) {
1949
- return afterAttributes(code);
1950
- }
1951
- return effects.attempt(attributes2, afterAttributes, afterAttributes)(code);
2819
+ };
2820
+ const unwrapBlockContent = (content2) => {
2821
+ const flattenPhrasingContent = (children) => {
2822
+ const children2 = children.map((child) => phrasingContent(child));
2823
+ return flatten(Array.isArray(children2) ? children2 : [children2]);
2824
+ };
2825
+ switch (content2.type) {
2826
+ case "heading":
2827
+ case "paragraph":
2828
+ return flattenPhrasingContent(content2.children);
2829
+ case "html":
2830
+ return [html_inline(content2)];
2831
+ case "blockquote":
2832
+ default:
2833
+ throw new RichTextParseError(`UnwrapBlock: Unknown block content of type ${content2.type}`, content2.position);
2834
+ }
2835
+ };
2836
+ const code = (content2) => {
2837
+ const extra = {};
2838
+ if (content2.lang)
2839
+ extra["lang"] = content2.lang;
2840
+ return {
2841
+ type: "code_block",
2842
+ ...extra,
2843
+ value: content2.value,
2844
+ children: [{ type: "text", text: "" }]
2845
+ };
2846
+ };
2847
+ const link = (content2) => {
2848
+ return {
2849
+ type: "a",
2850
+ url: content2.url,
2851
+ title: content2.title,
2852
+ children: flatten(content2.children.map((child) => staticPhrasingContent(child)))
1952
2853
  };
1953
- const afterAttributes = function(code) {
1954
- const nextCharacter = pattern.end[endSequenceIndex];
1955
- if (code === codes4.eof) {
1956
- return nok;
1957
- }
1958
- if (findCode(nextCharacter) === code) {
1959
- effects.consume(code);
1960
- endSequenceIndex++;
1961
- return afterAttributes;
1962
- }
1963
- if (pattern.end.length === endSequenceIndex) {
1964
- return factorySpace3(effects, openAfter, types3.whitespace)(code);
1965
- }
1966
- return nok;
2854
+ };
2855
+ const heading = (content2) => {
2856
+ return {
2857
+ type: ["h1", "h2", "h3", "h4", "h5", "h6"][content2.depth - 1],
2858
+ children: flatten(content2.children.map(phrasingContent))
1967
2859
  };
1968
- const openAfter = function(code) {
1969
- effects.exit("directiveContainerFence");
1970
- if (code === codes4.eof) {
1971
- return afterOpening(code);
1972
- }
1973
- if (markdownLineEnding3(code)) {
1974
- if (self.interrupt) {
1975
- return nok(code);
2860
+ };
2861
+ const staticPhrasingContent = (content2) => {
2862
+ switch (content2.type) {
2863
+ case "mdxJsxTextElement":
2864
+ return mdxJsxElement2(content2, field, imageCallback);
2865
+ case "text":
2866
+ return text5(content2);
2867
+ case "inlineCode":
2868
+ case "emphasis":
2869
+ case "image":
2870
+ case "strong":
2871
+ return phrashingMark(content2);
2872
+ case "html":
2873
+ return html_inline(content2);
2874
+ default:
2875
+ throw new Error(`StaticPhrasingContent: ${content2.type} is not yet supported`);
2876
+ }
2877
+ };
2878
+ const phrasingContent = (content2) => {
2879
+ switch (content2.type) {
2880
+ case "text":
2881
+ return text5(content2);
2882
+ case "link":
2883
+ return link(content2);
2884
+ case "image":
2885
+ return image(content2);
2886
+ case "mdxJsxTextElement":
2887
+ return mdxJsxElement2(content2, field, imageCallback);
2888
+ case "emphasis":
2889
+ return phrashingMark(content2);
2890
+ case "strong":
2891
+ return phrashingMark(content2);
2892
+ case "break":
2893
+ return breakContent();
2894
+ case "inlineCode":
2895
+ return phrashingMark(content2);
2896
+ case "html":
2897
+ return html_inline(content2);
2898
+ case "mdxTextExpression":
2899
+ throw new RichTextParseError(`Unexpected expression ${content2.value}.`, content2.position);
2900
+ default:
2901
+ throw new Error(`PhrasingContent: ${content2.type} is not yet supported`);
2902
+ }
2903
+ };
2904
+ const breakContent = () => {
2905
+ return {
2906
+ type: "break",
2907
+ children: [
2908
+ {
2909
+ type: "text",
2910
+ text: ""
1976
2911
  }
1977
- return effects.attempt(nonLazyLine, contentStart, afterOpening)(code);
1978
- }
1979
- return nok(code);
1980
- };
1981
- const afterOpening = function(code) {
1982
- return nok(code);
2912
+ ]
1983
2913
  };
1984
- const contentStart = function(code) {
1985
- if (code === codes4.eof) {
1986
- return nok(code);
2914
+ };
2915
+ const phrashingMark = (node, marks = []) => {
2916
+ const accum = [];
2917
+ switch (node.type) {
2918
+ case "emphasis": {
2919
+ const children = flatten(node.children.map((child) => phrashingMark(child, [...marks, "italic"])));
2920
+ children.forEach((child) => {
2921
+ accum.push(child);
2922
+ });
2923
+ break;
1987
2924
  }
1988
- effects.enter("directiveContainerContent");
1989
- return lineStart(code);
1990
- };
1991
- const lineStart = function(code) {
1992
- if (code === codes4.eof) {
1993
- return nok(code);
2925
+ case "inlineCode": {
2926
+ const markProps2 = {};
2927
+ marks.forEach((mark) => markProps2[mark] = true);
2928
+ accum.push({
2929
+ type: "text",
2930
+ text: node.value,
2931
+ code: true,
2932
+ ...markProps2
2933
+ });
2934
+ break;
1994
2935
  }
1995
- return effects.attempt({ tokenize: tokenizeClosingFence, partial: true }, after, initialSize ? factorySpace3(effects, chunkStart, types3.linePrefix, initialSize + 1) : chunkStart)(code);
1996
- };
1997
- const chunkStart = function(code) {
1998
- if (code === codes4.eof) {
1999
- return nok(code);
2936
+ case "strong": {
2937
+ const children = flatten(node.children.map((child) => phrashingMark(child, [...marks, "bold"])));
2938
+ children.forEach((child) => {
2939
+ accum.push(child);
2940
+ });
2941
+ break;
2000
2942
  }
2001
- const token = effects.enter(types3.chunkDocument, {
2002
- contentType: constants.contentTypeDocument,
2003
- previous
2004
- });
2005
- if (previous)
2006
- previous.next = token;
2007
- previous = token;
2008
- return contentContinue(code);
2009
- };
2010
- const contentContinue = function(code) {
2011
- if (code === codes4.eof) {
2012
- const t = effects.exit(types3.chunkDocument);
2013
- self.parser.lazy[t.start.line] = false;
2014
- return nok(code);
2943
+ case "image": {
2944
+ accum.push(image(node));
2945
+ break;
2015
2946
  }
2016
- if (markdownLineEnding3(code)) {
2017
- return effects.check(nonLazyLine, nonLazyLineAfter, lineAfter)(code);
2947
+ case "link": {
2948
+ const children = flatten(node.children.map((child) => phrashingMark(child, marks)));
2949
+ accum.push({ type: "a", url: node.url, title: node.title, children });
2950
+ break;
2018
2951
  }
2019
- effects.consume(code);
2020
- return contentContinue;
2021
- };
2022
- const nonLazyLineAfter = function(code) {
2023
- effects.consume(code);
2024
- const t = effects.exit(types3.chunkDocument);
2025
- self.parser.lazy[t.start.line] = false;
2026
- return lineStart;
2027
- };
2028
- const lineAfter = function(code) {
2029
- const t = effects.exit(types3.chunkDocument);
2030
- self.parser.lazy[t.start.line] = false;
2031
- return after(code);
2032
- };
2033
- const after = function(code) {
2034
- effects.exit("directiveContainerContent");
2035
- effects.exit("directiveContainer");
2036
- return ok(code);
2037
- };
2038
- const tokenizeClosingFence = function(effects2, ok2, nok2) {
2039
- const closingPrefixAfter = function(code) {
2040
- effects2.enter("directiveContainerFence");
2041
- effects2.enter("directiveContainerSequence");
2042
- return closingSequence(code);
2043
- };
2044
- const closingSequence = function(code) {
2045
- const nextCharacter = pattern.start[closeStartSequenceIndex];
2046
- if (findCode(nextCharacter) === code) {
2047
- effects2.consume(code);
2048
- closeStartSequenceIndex++;
2049
- return closingSequence;
2050
- }
2051
- if (closeStartSequenceIndex < pattern.end.length - 1) {
2052
- return nok2(code);
2053
- }
2054
- effects2.exit("directiveContainerSequence");
2055
- return factorySpace3(effects2, closingSequenceNameStart, types3.whitespace)(code);
2056
- };
2057
- const closingSequenceName = function(code) {
2058
- const patternName = pattern.name || pattern.templateName;
2059
- const nextCharacter = patternName[endNameIndex];
2060
- if (code === codes4.eof) {
2061
- return nok2;
2062
- }
2063
- if (markdownLineEnding3(code)) {
2064
- return nok2;
2065
- }
2066
- if (findCode(nextCharacter) === code) {
2067
- effects2.consume(code);
2068
- endNameIndex++;
2069
- return closingSequenceName;
2070
- }
2071
- if (patternName.length === endNameIndex) {
2072
- return closingSequenceEnd;
2073
- }
2074
- return nok2;
2075
- };
2076
- const closingSequenceNameStart = function(code) {
2077
- if (markdownSpace3(code)) {
2078
- return factorySpace3(effects2, closingSequenceNameStart, types3.whitespace);
2079
- }
2080
- if (code === codes4.backslash) {
2081
- effects2.consume(code);
2082
- return closingSequenceName;
2083
- }
2084
- return nok2(code);
2085
- };
2086
- const closingSequenceEnd = function(code) {
2087
- if (markdownSpace3(code)) {
2088
- return factorySpace3(effects2, closingSequenceEnd, types3.whitespace);
2089
- }
2090
- if (code === codes4.eof) {
2091
- return nok2;
2092
- }
2093
- if (pattern.end.length - 1 === closeEndSequenceIndex) {
2094
- effects2.exit("directiveContainerFence");
2095
- return ok2(code);
2096
- }
2097
- const nextCharacter = pattern.end[closeEndSequenceIndex];
2098
- if (findCode(nextCharacter) === code) {
2099
- effects2.consume(code);
2100
- closeEndSequenceIndex++;
2101
- return closingSequenceEnd;
2102
- }
2103
- return nok2(code);
2104
- };
2105
- return factorySpace3(effects2, closingPrefixAfter, types3.linePrefix, constants.tabSize);
2952
+ case "html":
2953
+ case "text":
2954
+ const markProps = {};
2955
+ marks.forEach((mark) => markProps[mark] = true);
2956
+ accum.push({ type: "text", text: node.value, ...markProps });
2957
+ break;
2958
+ case "break":
2959
+ accum.push(breakContent());
2960
+ break;
2961
+ default:
2962
+ throw new RichTextParseError(`Unexpected inline element of type ${node.type}`, node?.position);
2963
+ }
2964
+ return accum;
2965
+ };
2966
+ const image = (content2) => {
2967
+ return {
2968
+ type: "img",
2969
+ url: imageCallback(content2.url),
2970
+ alt: content2.alt || void 0,
2971
+ caption: content2.title,
2972
+ children: [{ type: "text", text: "" }]
2106
2973
  };
2107
- return start;
2108
2974
  };
2109
- const tokenizeAttributes = function(effects, ok, nok) {
2110
- return factoryAttributes(effects, ok, nok, "directiveContainerAttributes", "directiveContainerAttributesMarker", "directiveContainerAttribute", "directiveContainerAttributeId", "directiveContainerAttributeClass", "directiveContainerAttributeName", "directiveContainerAttributeInitializerMarker", "directiveContainerAttributeValueLiteral", "directiveContainerAttributeValue", "directiveContainerAttributeValueMarker", "directiveContainerAttributeValueData", true);
2975
+ const text5 = (content2) => {
2976
+ return {
2977
+ type: "text",
2978
+ text: content2.value
2979
+ };
2111
2980
  };
2112
- const tokenizeNonLazyLine = function(effects, ok, nok) {
2113
- const self = this;
2114
- const lineStart = function(code) {
2115
- return self.parser.lazy[self.now().line] ? nok(code) : ok(code);
2981
+ const blockquote = (content2) => {
2982
+ const children = [];
2983
+ content2.children.map((child) => {
2984
+ const inlineElements = unwrapBlockContent(child);
2985
+ inlineElements.forEach((child2) => {
2986
+ children.push(child2);
2987
+ });
2988
+ });
2989
+ return {
2990
+ type: "blockquote",
2991
+ children
2116
2992
  };
2117
- const start = function(code) {
2118
- assert(markdownLineEnding3(code), "expected eol");
2119
- effects.enter(types3.lineEnding);
2120
- effects.consume(code);
2121
- effects.exit(types3.lineEnding);
2122
- return lineStart;
2993
+ };
2994
+ const paragraph = (content2) => {
2995
+ const children = flatten(content2.children.map(phrasingContent));
2996
+ if (children.length === 1) {
2997
+ if (children[0]) {
2998
+ if (children[0].type === "html_inline") {
2999
+ return {
3000
+ ...children[0],
3001
+ type: "html"
3002
+ };
3003
+ }
3004
+ }
3005
+ }
3006
+ return {
3007
+ type: "p",
3008
+ children
2123
3009
  };
2124
- return start;
2125
3010
  };
2126
- const attributes2 = { tokenize: tokenizeAttributes, partial: true };
2127
- const nonLazyLine = { tokenize: tokenizeNonLazyLine, partial: true };
2128
3011
  return {
2129
- tokenize: tokenizeDirectiveContainer,
2130
- concrete: true
3012
+ type: "root",
3013
+ children: root.children.map((child) => {
3014
+ return content(child);
3015
+ })
2131
3016
  };
2132
3017
  };
3018
+ var RichTextParseError = class extends Error {
3019
+ constructor(message, position) {
3020
+ super(message);
3021
+ __publicField(this, "position");
3022
+ if (Error.captureStackTrace) {
3023
+ Error.captureStackTrace(this, RichTextParseError);
3024
+ }
3025
+ this.name = "RichTextParseError";
3026
+ this.position = position;
3027
+ }
3028
+ };
2133
3029
 
2134
- // src/extensions/tina-shortcodes/extension.ts
2135
- var tinaDirective = function(patterns) {
2136
- const rules = {};
2137
- patterns.forEach((pattern) => {
2138
- const firstKey = pattern.start[0];
2139
- if (firstKey) {
2140
- const code = findCode(firstKey);
2141
- if (code) {
2142
- if (pattern.type === "leaf") {
2143
- const directive = directiveLeaf(pattern);
2144
- if (rules[code]) {
2145
- rules[code] = [...rules[code] || [], directive];
2146
- } else {
2147
- rules[code] = [directive];
2148
- }
2149
- }
2150
- if (pattern.type === "block") {
2151
- const directive = directiveContainer(pattern);
2152
- if (rules[code]) {
2153
- rules[code] = [...rules[code] || [], directive];
2154
- } else {
2155
- rules[code] = [directive];
2156
- }
2157
- }
3030
+ // src/next/parse/post-processing.ts
3031
+ var postProcessor = (tree, field, imageCallback) => {
3032
+ const addPropsToMdxFlow = (node) => {
3033
+ const props = {};
3034
+ node.attributes.forEach((attribute) => {
3035
+ if (attribute.type === "mdxJsxAttribute") {
3036
+ props[attribute.name] = attribute.value;
3037
+ } else {
3038
+ throw new Error("HANDLE mdxJsxExpressionAttribute");
3039
+ }
3040
+ });
3041
+ if (node.children.length) {
3042
+ let tree2;
3043
+ if (node.type === "mdxJsxTextElement") {
3044
+ tree2 = postProcessor({
3045
+ type: "root",
3046
+ children: [{ type: "paragraph", children: node.children }]
3047
+ }, field, imageCallback);
3048
+ } else {
3049
+ tree2 = postProcessor({ type: "root", children: node.children }, field, imageCallback);
2158
3050
  }
3051
+ props.children = tree2;
2159
3052
  }
2160
- });
2161
- return {
2162
- flow: rules
3053
+ node.props = props;
3054
+ delete node.attributes;
3055
+ node.children = [{ type: "text", text: "" }];
2163
3056
  };
3057
+ visit(tree, "mdxJsxFlowElement", addPropsToMdxFlow);
3058
+ visit(tree, "mdxJsxTextElement", addPropsToMdxFlow);
3059
+ return remarkToSlate(tree, field, imageCallback, "", true);
3060
+ };
3061
+
3062
+ // src/next/parse/index.ts
3063
+ var parseMDX2 = (value, field, imageCallback) => {
3064
+ const backup = (v) => v;
3065
+ const callback = imageCallback || backup;
3066
+ const tree = fromMarkdown(value, field);
3067
+ return postProcess(tree, field, callback);
2164
3068
  };
3069
+ var postProcess = (tree, field, imageCallback) => {
3070
+ return postProcessor(compact(tree), field, imageCallback);
3071
+ };
3072
+
3073
+ // src/parse/index.ts
3074
+ import { fromMarkdown as fromMarkdown2 } from "mdast-util-from-markdown";
3075
+
3076
+ // src/extensions/tina-shortcodes/from-markdown.ts
3077
+ import { parseEntities as parseEntities2 } from "parse-entities";
3078
+
3079
+ // src/extensions/tina-shortcodes/shortcode-leaf.ts
3080
+ import { factorySpace as factorySpace4 } from "micromark-factory-space";
3081
+ import { markdownLineEnding as markdownLineEnding4, markdownSpace as markdownSpace3 } from "micromark-util-character";
3082
+ import { codes as codes6 } from "micromark-util-symbol/codes";
3083
+ import { values as values2 } from "micromark-util-symbol/values";
3084
+ import { types as types4 } from "micromark-util-symbol/types";
3085
+
3086
+ // src/extensions/tina-shortcodes/factory-attributes.ts
3087
+ import { factorySpace as factorySpace3 } from "micromark-factory-space";
3088
+ import { factoryWhitespace } from "micromark-factory-whitespace";
3089
+ import {
3090
+ asciiAlpha,
3091
+ asciiAlphanumeric,
3092
+ markdownLineEnding as markdownLineEnding3,
3093
+ markdownLineEndingOrSpace as markdownLineEndingOrSpace2,
3094
+ markdownSpace as markdownSpace2
3095
+ } from "micromark-util-character";
3096
+ import { codes as codes4 } from "micromark-util-symbol/codes";
3097
+ import { types as types3 } from "micromark-util-symbol/types";
3098
+
3099
+ // src/extensions/tina-shortcodes/factory-name.ts
3100
+ import { asciiAlpha as asciiAlpha2, asciiAlphanumeric as asciiAlphanumeric2 } from "micromark-util-character";
3101
+ import { codes as codes5 } from "micromark-util-symbol/codes";
3102
+
3103
+ // src/extensions/tina-shortcodes/shortcode-container.ts
3104
+ import { ok as assert2 } from "uvu/assert";
3105
+ import { factorySpace as factorySpace5 } from "micromark-factory-space";
3106
+ import { markdownLineEnding as markdownLineEnding5, markdownSpace as markdownSpace4 } from "micromark-util-character";
3107
+ import { codes as codes7 } from "micromark-util-symbol/codes";
3108
+ import { constants as constants2 } from "micromark-util-symbol/constants";
3109
+ import { types as types5 } from "micromark-util-symbol/types";
2165
3110
 
2166
3111
  // src/parse/parseShortcode.ts
2167
3112
  function parseShortcode(preprocessedString, template) {
@@ -2175,26 +3120,6 @@ function parseShortcode(preprocessedString, template) {
2175
3120
  }
2176
3121
 
2177
3122
  // src/parse/index.ts
2178
- var markdownToAst = (value, field) => {
2179
- const patterns = [];
2180
- field.templates?.forEach((template) => {
2181
- if (typeof template === "string") {
2182
- return;
2183
- }
2184
- if (template && template.match) {
2185
- patterns.push({
2186
- ...template.match,
2187
- name: template.match?.name || template.name,
2188
- templateName: template.name,
2189
- type: template.fields.find((f) => f.name === "children") ? "block" : "leaf"
2190
- });
2191
- }
2192
- });
2193
- return fromMarkdown(value, {
2194
- extensions: [tinaDirective(patterns)],
2195
- mdastExtensions: [directiveFromMarkdown]
2196
- });
2197
- };
2198
3123
  var mdxToAst = (value) => {
2199
3124
  return remark().use(remarkMdx).parse(value);
2200
3125
  };
@@ -2206,22 +3131,21 @@ var parseMDX = (value, field, imageCallback) => {
2206
3131
  let tree;
2207
3132
  try {
2208
3133
  if (field.parser?.type === "markdown") {
2209
- tree = markdownToAst(value, field);
2210
- } else {
2211
- let preprocessedString = value;
2212
- const templatesWithMatchers = field.templates?.filter((template) => template.match);
2213
- templatesWithMatchers?.forEach((template) => {
2214
- if (typeof template === "string") {
2215
- throw new Error("Global templates are not supported");
2216
- }
2217
- if (template.match) {
2218
- if (preprocessedString) {
2219
- preprocessedString = parseShortcode(preprocessedString, template);
2220
- }
2221
- }
2222
- });
2223
- tree = mdxToAst(preprocessedString);
3134
+ return parseMDX2(value, field, imageCallback);
2224
3135
  }
3136
+ let preprocessedString = value;
3137
+ const templatesWithMatchers = field.templates?.filter((template) => template.match);
3138
+ templatesWithMatchers?.forEach((template) => {
3139
+ if (typeof template === "string") {
3140
+ throw new Error("Global templates are not supported");
3141
+ }
3142
+ if (template.match) {
3143
+ if (preprocessedString) {
3144
+ preprocessedString = parseShortcode(preprocessedString, template);
3145
+ }
3146
+ }
3147
+ });
3148
+ tree = mdxToAst(preprocessedString);
2225
3149
  if (tree) {
2226
3150
  return remarkToSlate(tree, field, imageCallback, value);
2227
3151
  } else {