@tinacms/mdx 0.0.0-20220718181631 → 0.60.3

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 (90) hide show
  1. package/dist/index.cjs +887 -738
  2. package/dist/index.d.ts +21 -1
  3. package/dist/index.es.js +546 -406
  4. package/dist/{parse/plate-spec.d.ts → index.js} +4 -2
  5. package/dist/mdx.es.js +1092 -0
  6. package/dist/mdx.umd.js +1097 -0
  7. package/dist/parse/acorn.d.ts +2 -3
  8. package/dist/parse/acorn.js +201 -0
  9. package/dist/parse/index.d.ts +8 -2
  10. package/dist/parse/index.js +144 -0
  11. package/dist/parse/mdx.js +75 -0
  12. package/dist/parse/plate.d.ts +15 -2
  13. package/dist/parse/plate.js +1 -0
  14. package/dist/parse/remarkToPlate.d.ts +16 -1
  15. package/dist/parse/remarkToPlate.js +380 -0
  16. package/dist/stringify/acorn.d.ts +7 -2
  17. package/dist/stringify/acorn.js +241 -0
  18. package/dist/stringify/index.d.ts +15 -0
  19. package/dist/stringify/index.js +195 -0
  20. package/dist/stringify/marks.js +250 -0
  21. package/dist/tests/autotest/_config.d.ts +4 -0
  22. package/dist/tests/autotest/_config.js +159 -0
  23. package/dist/tests/autotest/_runner.test.d.ts +1 -0
  24. package/dist/tests/autotest/_runner.test.js +77 -0
  25. package/dist/tests/autotest/autoformat block elements in list items.test.d.ts +1 -0
  26. package/dist/tests/autotest/autoformat block elements in list items.test.js +33 -0
  27. package/dist/tests/autotest/autoformat blockquote.result.test.d.ts +1 -0
  28. package/dist/tests/autotest/autoformat blockquote.result.test.js +18 -0
  29. package/dist/tests/autotest/autoformat blockquote.test.d.ts +1 -0
  30. package/dist/tests/autotest/autoformat blockquote.test.js +18 -0
  31. package/dist/tests/autotest/autoformat list item.test.d.ts +1 -0
  32. package/dist/tests/autotest/autoformat list item.test.js +31 -0
  33. package/dist/tests/autotest/autoformat mdx with nested null children.test.d.ts +1 -0
  34. package/dist/tests/autotest/autoformat mdx with nested null children.test.js +23 -0
  35. package/dist/tests/autotest/autoformat syntax changes.test.d.ts +1 -0
  36. package/dist/tests/autotest/autoformat syntax changes.test.js +90 -0
  37. package/dist/tests/autotest/blockquote.test.d.ts +1 -0
  38. package/dist/tests/autotest/blockquote.test.js +17 -0
  39. package/dist/tests/autotest/break.test.d.ts +1 -0
  40. package/dist/tests/autotest/break.test.js +26 -0
  41. package/dist/tests/autotest/code block.test.d.ts +1 -0
  42. package/dist/tests/autotest/code block.test.js +27 -0
  43. package/dist/tests/autotest/image.test.d.ts +1 -0
  44. package/dist/tests/autotest/image.test.js +52 -0
  45. package/dist/tests/autotest/invalid mdx with a closing tag.test.d.ts +1 -0
  46. package/dist/tests/autotest/invalid mdx with a closing tag.test.js +27 -0
  47. package/dist/tests/autotest/invalid mdx with a const expression.test.d.ts +1 -0
  48. package/dist/tests/autotest/invalid mdx with a const expression.test.js +27 -0
  49. package/dist/tests/autotest/invalid mdx with an expression {{}}.test.d.ts +1 -0
  50. package/dist/tests/autotest/invalid mdx with an expression {{}}.test.js +27 -0
  51. package/dist/tests/autotest/invalid mdx with an expression.test.d.ts +1 -0
  52. package/dist/tests/autotest/invalid mdx with an expression.test.js +27 -0
  53. package/dist/tests/autotest/invalid mdx with an import statment.test.d.ts +1 -0
  54. package/dist/tests/autotest/invalid mdx with an import statment.test.js +27 -0
  55. package/dist/tests/autotest/kitchen sink.test.d.ts +1 -0
  56. package/dist/tests/autotest/kitchen sink.test.js +144 -0
  57. package/dist/tests/autotest/links.test.d.ts +1 -0
  58. package/dist/tests/autotest/links.test.js +29 -0
  59. package/dist/tests/autotest/lists.test.d.ts +1 -0
  60. package/dist/tests/autotest/lists.test.js +110 -0
  61. package/dist/tests/autotest/marks.test.d.ts +1 -0
  62. package/dist/tests/autotest/marks.test.js +102 -0
  63. package/dist/tests/autotest/mdx field with a scalar field as a list.test.d.ts +1 -0
  64. package/dist/tests/autotest/mdx field with a scalar field as a list.test.js +22 -0
  65. package/dist/tests/autotest/mdx which hasnt been registered returns html.test.d.ts +1 -0
  66. package/dist/tests/autotest/mdx which hasnt been registered returns html.test.js +26 -0
  67. package/dist/tests/autotest/mdx with a boolean field.test.d.ts +1 -0
  68. package/dist/tests/autotest/mdx with a boolean field.test.js +22 -0
  69. package/dist/tests/autotest/mdx with a number field.test.d.ts +1 -0
  70. package/dist/tests/autotest/mdx with a number field.test.js +22 -0
  71. package/dist/tests/autotest/mdx with a srtring field.test.d.ts +1 -0
  72. package/dist/tests/autotest/mdx with a srtring field.test.js +28 -0
  73. package/dist/tests/autotest/mdx with multiple rich-text fields.test.d.ts +1 -0
  74. package/dist/tests/autotest/mdx with multiple rich-text fields.test.js +53 -0
  75. package/dist/tests/autotest/mdx with nested children.test.d.ts +1 -0
  76. package/dist/tests/autotest/mdx with nested children.test.js +35 -0
  77. package/dist/tests/autotest/mdx with nested null children.test.d.ts +1 -0
  78. package/dist/tests/autotest/mdx with nested null children.test.js +23 -0
  79. package/dist/tests/autotest/mdx with number list field.test.d.ts +1 -0
  80. package/dist/tests/autotest/mdx with number list field.test.js +22 -0
  81. package/dist/tests/autotest/mdx with object list field.test.d.ts +1 -0
  82. package/dist/tests/autotest/mdx with object list field.test.js +25 -0
  83. package/dist/tests/autotest/mdx with object with templates.test.d.ts +1 -0
  84. package/dist/tests/autotest/mdx with object with templates.test.js +41 -0
  85. package/dist/tests/autotest/shortcodes.test.d.ts +1 -0
  86. package/dist/tests/autotest/shortcodes.test.js +51 -0
  87. package/dist/tests/setup.d.ts +14 -0
  88. package/dist/tests/setup.js +70 -0
  89. package/package.json +13 -14
  90. package/dist/stringify/mdx.d.ts +0 -38
@@ -0,0 +1,380 @@
1
+ /**
2
+
3
+ Copyright 2021 Forestry.io Holdings, Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+
17
+ */
18
+ var __extends = (this && this.__extends) || (function () {
19
+ var extendStatics = function (d, b) {
20
+ extendStatics = Object.setPrototypeOf ||
21
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
22
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
23
+ return extendStatics(d, b);
24
+ };
25
+ return function (d, b) {
26
+ if (typeof b !== "function" && b !== null)
27
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
28
+ extendStatics(d, b);
29
+ function __() { this.constructor = d; }
30
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
31
+ };
32
+ })();
33
+ var __assign = (this && this.__assign) || function () {
34
+ __assign = Object.assign || function(t) {
35
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
36
+ s = arguments[i];
37
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
38
+ t[p] = s[p];
39
+ }
40
+ return t;
41
+ };
42
+ return __assign.apply(this, arguments);
43
+ };
44
+ var __spreadArray = (this && this.__spreadArray) || function (to, from) {
45
+ for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
46
+ to[j] = from[i];
47
+ return to;
48
+ };
49
+ import { flatten } from 'lodash-es';
50
+ import { mdxJsxElement } from './mdx';
51
+ export var remarkToSlate = function (root, field, imageCallback) {
52
+ var content = function (content) {
53
+ switch (content.type) {
54
+ case 'blockquote':
55
+ var children_1 = [];
56
+ content.children.map(function (child) {
57
+ var inlineElements = unwrapBlockContent(child);
58
+ inlineElements.forEach(function (child) {
59
+ children_1.push(child);
60
+ });
61
+ });
62
+ return {
63
+ type: 'blockquote',
64
+ children: children_1
65
+ };
66
+ case 'heading':
67
+ return heading(content);
68
+ case 'code':
69
+ return code(content);
70
+ case 'paragraph':
71
+ return paragraph(content);
72
+ case 'mdxJsxFlowElement':
73
+ return mdxJsxElement(content, field, imageCallback);
74
+ case 'thematicBreak':
75
+ return {
76
+ type: 'hr',
77
+ children: [{ type: 'text', text: '' }]
78
+ };
79
+ case 'listItem':
80
+ return {
81
+ type: 'li',
82
+ children: [
83
+ {
84
+ type: 'lic',
85
+ children: flatten(content.children.map(function (child) { return unwrapBlockContent(child); }))
86
+ },
87
+ ]
88
+ };
89
+ case 'list':
90
+ return list(content);
91
+ case 'html':
92
+ return html(content);
93
+ // @ts-ignore
94
+ case 'mdxFlowExpression':
95
+ // @ts-ignore
96
+ case 'mdxjsEsm':
97
+ // @ts-ignore
98
+ throw new RichTextParseError(
99
+ // @ts-ignore
100
+ "Unexpected expression " + content.value + ".",
101
+ // @ts-ignore
102
+ content.position);
103
+ default:
104
+ throw new RichTextParseError("Content: " + content.type + " is not yet supported",
105
+ // @ts-ignore
106
+ content.position);
107
+ }
108
+ };
109
+ var html = function (content) {
110
+ return {
111
+ type: 'html',
112
+ value: content.value,
113
+ children: [{ type: 'text', text: '' }]
114
+ };
115
+ };
116
+ var html_inline = function (content) {
117
+ return {
118
+ type: 'html_inline',
119
+ value: content.value,
120
+ children: [{ type: 'text', text: '' }]
121
+ };
122
+ };
123
+ var list = function (content) {
124
+ return {
125
+ type: content.ordered ? 'ol' : 'ul',
126
+ children: content.children.map(function (child) { return listItem(child); })
127
+ };
128
+ };
129
+ var listItem = function (content) {
130
+ /**
131
+ * lic (list item content) maps 1-1 with a paragraph element
132
+ * but in plate we don't support other block-level elements from being list items
133
+ * In remark, a list item contains "FlowContent" https://github.com/syntax-tree/mdast#flowcontent
134
+ * Blockquote | Code | Heading | HTML | List | ThematicBreak | Content
135
+ *
136
+ * But we only support paragraph-like blocks ("LIC"), other and text nodes
137
+ *
138
+ * Another thing is that in remark nested lists are wrapped in their ul/li `List` parent
139
+ * but in Plate we don't have that, so we can't have a ol inside a ul in plate
140
+ */
141
+ return {
142
+ type: 'li',
143
+ children: content.children.map(function (child) {
144
+ switch (child.type) {
145
+ case 'list':
146
+ return list(child);
147
+ case 'heading':
148
+ case 'paragraph':
149
+ return {
150
+ type: 'lic',
151
+ children: flatten(child.children.map(function (child) { return phrasingContent(child); }))
152
+ };
153
+ case 'blockquote': {
154
+ return __assign(__assign({}, blockquote(child)), { type: 'lic' });
155
+ }
156
+ case 'mdxJsxFlowElement':
157
+ return {
158
+ type: 'lic',
159
+ children: [
160
+ // @ts-ignore casting a flow element to a paragraph
161
+ mdxJsxElement(__assign(__assign({}, child), { type: 'mdxJsxTextElement' }), field, imageCallback),
162
+ ]
163
+ };
164
+ case 'code':
165
+ case 'thematicBreak':
166
+ case 'table':
167
+ case 'html':
168
+ throw new Error(content.type + " inside list item is not supported");
169
+ default:
170
+ throw new Error("Unknown list item of type " + content.type);
171
+ }
172
+ })
173
+ };
174
+ };
175
+ var unwrapBlockContent = function (content) {
176
+ var flattenPhrasingContent = function (children) {
177
+ var children2 = children.map(function (child) { return phrasingContent(child); });
178
+ return flatten(Array.isArray(children2) ? children2 : [children2]);
179
+ };
180
+ switch (content.type) {
181
+ case 'heading':
182
+ case 'paragraph':
183
+ return flattenPhrasingContent(content.children);
184
+ default:
185
+ throw new Error("UnwrapBlock: Unknown block content of type " + content.type);
186
+ }
187
+ };
188
+ var code = function (content) {
189
+ var extra = {};
190
+ if (content.lang)
191
+ extra['lang'] = content.lang;
192
+ return __assign(__assign({ type: 'code_block' }, extra), { value: content.value, children: [{ type: 'text', text: '' }] });
193
+ };
194
+ var link = function (content) {
195
+ return {
196
+ type: 'a',
197
+ url: content.url,
198
+ title: content.title,
199
+ children: flatten(content.children.map(function (child) { return staticPhrasingContent(child); }))
200
+ };
201
+ };
202
+ var heading = function (content) {
203
+ return {
204
+ type: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'][content.depth - 1],
205
+ children: flatten(content.children.map(phrasingContent))
206
+ };
207
+ };
208
+ var staticPhrasingContent = function (content) {
209
+ switch (content.type) {
210
+ case 'mdxJsxTextElement':
211
+ return mdxJsxElement(content, field, imageCallback);
212
+ case 'text':
213
+ return text(content);
214
+ case 'inlineCode':
215
+ case 'emphasis':
216
+ case 'image':
217
+ case 'strong':
218
+ return phrashingMark(content);
219
+ default:
220
+ throw new Error("StaticPhrasingContent: " + content.type + " is not yet supported");
221
+ }
222
+ };
223
+ var phrasingContent = function (content) {
224
+ switch (content.type) {
225
+ case 'text':
226
+ return text(content);
227
+ case 'link':
228
+ return link(content);
229
+ case 'image':
230
+ return image(content);
231
+ case 'mdxJsxTextElement':
232
+ return mdxJsxElement(content, field, imageCallback);
233
+ case 'emphasis':
234
+ return phrashingMark(content);
235
+ case 'strong':
236
+ return phrashingMark(content);
237
+ case 'break':
238
+ return breakContent();
239
+ case 'inlineCode':
240
+ return phrashingMark(content);
241
+ case 'html':
242
+ return html_inline(content);
243
+ // @ts-ignore
244
+ case 'mdxTextExpression':
245
+ throw new RichTextParseError(
246
+ // @ts-ignore
247
+ "Unexpected expression " + content.value + ".",
248
+ // @ts-ignore
249
+ content.position);
250
+ default:
251
+ throw new Error("PhrasingContent: " + content.type + " is not yet supported");
252
+ }
253
+ };
254
+ var breakContent = function () {
255
+ return {
256
+ type: 'break',
257
+ children: [
258
+ {
259
+ type: 'text',
260
+ text: ''
261
+ },
262
+ ]
263
+ };
264
+ };
265
+ var phrashingMark = function (node, marks) {
266
+ if (marks === void 0) { marks = []; }
267
+ var accum = [];
268
+ switch (node.type) {
269
+ case 'emphasis': {
270
+ var children = flatten(node.children.map(function (child) {
271
+ return phrashingMark(child, __spreadArray(__spreadArray([], marks), ['italic']));
272
+ }));
273
+ children.forEach(function (child) {
274
+ accum.push(child);
275
+ });
276
+ break;
277
+ }
278
+ case 'inlineCode': {
279
+ var markProps_1 = {};
280
+ marks.forEach(function (mark) { return (markProps_1[mark] = true); });
281
+ accum.push(__assign({ type: 'text', text: node.value, code: true }, markProps_1));
282
+ break;
283
+ }
284
+ case 'strong': {
285
+ var children = flatten(node.children.map(function (child) { return phrashingMark(child, __spreadArray(__spreadArray([], marks), ['bold'])); }));
286
+ children.forEach(function (child) {
287
+ accum.push(child);
288
+ });
289
+ break;
290
+ }
291
+ case 'image': {
292
+ accum.push(image(node));
293
+ break;
294
+ }
295
+ case 'link': {
296
+ var children = flatten(node.children.map(function (child) { return phrashingMark(child, marks); }));
297
+ accum.push({ type: 'a', url: node.url, title: node.title, children: children });
298
+ break;
299
+ }
300
+ case 'text':
301
+ var markProps_2 = {};
302
+ marks.forEach(function (mark) { return (markProps_2[mark] = true); });
303
+ accum.push(__assign({ type: 'text', text: node.value }, markProps_2));
304
+ break;
305
+ default:
306
+ throw new Error("Unexpected inline element of type " + node.type);
307
+ }
308
+ return accum;
309
+ };
310
+ var image = function (content) {
311
+ return {
312
+ type: 'img',
313
+ url: imageCallback(content.url),
314
+ alt: content.alt,
315
+ caption: content.title,
316
+ children: [{ type: 'text', text: '' }]
317
+ };
318
+ };
319
+ var text = function (content) {
320
+ return {
321
+ type: 'text',
322
+ text: content.value
323
+ };
324
+ };
325
+ var blockquote = function (content) {
326
+ var children = [];
327
+ content.children.map(function (child) {
328
+ var inlineElements = unwrapBlockContent(child);
329
+ inlineElements.forEach(function (child) {
330
+ children.push(child);
331
+ });
332
+ });
333
+ return {
334
+ type: 'blockquote',
335
+ children: children
336
+ };
337
+ };
338
+ var paragraph = function (content) {
339
+ var children = flatten(content.children.map(phrasingContent));
340
+ // MDX treats <div>Hello</div> is inline even if it's isolated on one line
341
+ // If that's the case, swap it out with html
342
+ // TODO: probably need to do the same with JSX
343
+ if (children.length === 1) {
344
+ if (children[0]) {
345
+ if (children[0].type === 'html_inline') {
346
+ return __assign(__assign({}, children[0]), { type: 'html' });
347
+ }
348
+ }
349
+ }
350
+ return {
351
+ type: 'p',
352
+ children: children
353
+ };
354
+ };
355
+ return {
356
+ type: 'root',
357
+ children: root.children.map(function (child) {
358
+ // @ts-ignore child from MDX elements aren't shared with MDAST types
359
+ return content(child);
360
+ })
361
+ };
362
+ };
363
+ var RichTextParseError = /** @class */ (function (_super) {
364
+ __extends(RichTextParseError, _super);
365
+ function RichTextParseError(message, position) {
366
+ var _this =
367
+ // Pass remaining arguments (including vendor specific ones) to parent constructor
368
+ _super.call(this, message) || this;
369
+ // Maintains proper stack trace for where our error was thrown (only available on V8)
370
+ if (Error.captureStackTrace) {
371
+ Error.captureStackTrace(_this, RichTextParseError);
372
+ }
373
+ _this.name = 'RichTextParseError';
374
+ // Custom debugging information
375
+ _this.position = position;
376
+ return _this;
377
+ }
378
+ return RichTextParseError;
379
+ }(Error));
380
+ export { RichTextParseError };
@@ -1,12 +1,17 @@
1
1
  import type { RichTypeInner } from '@tinacms/schema-tools';
2
2
  import type { MdxJsxAttribute } from 'mdast-util-mdx-jsx';
3
- import type * as Plate from '../parse/plate';
3
+ import * as Plate from '../parse/plate';
4
4
  import type * as Md from 'mdast';
5
5
  export declare const stringifyPropsInline: (element: Plate.MdxInlineElement, field: RichTypeInner, imageCallback: (url: string) => string) => {
6
6
  attributes: MdxJsxAttribute[];
7
7
  children: Md.PhrasingContent[];
8
8
  };
9
- export declare const stringifyProps: (element: Plate.MdxBlockElement, parentField: RichTypeInner, flatten: boolean, imageCallback: (url: string) => string) => {
9
+ export declare function stringifyProps(element: Plate.MdxInlineElement, parentField: RichTypeInner, flatten: boolean, imageCallback: (url: string) => string): {
10
+ attributes: MdxJsxAttribute[];
11
+ children: Md.PhrasingContent[];
12
+ };
13
+ export declare function stringifyProps(element: Plate.MdxBlockElement, parentField: RichTypeInner, flatten: boolean, imageCallback: (url: string) => string): {
10
14
  attributes: MdxJsxAttribute[];
11
15
  children: Md.BlockContent[];
12
16
  };
17
+ export declare function assertShape<T extends unknown>(value: unknown, callback: (item: any) => boolean, errorMessage?: string): asserts value is T;
@@ -0,0 +1,241 @@
1
+ /**
2
+
3
+ Copyright 2021 Forestry.io Holdings, Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+
17
+ */
18
+ import { format } from 'prettier';
19
+ import { rootElement, stringifyMDX } from '.';
20
+ export var stringifyPropsInline = function (element, field, imageCallback) {
21
+ return stringifyProps(element, field, true, imageCallback);
22
+ };
23
+ export function stringifyProps(element, parentField, flatten, imageCallback) {
24
+ var _a;
25
+ var attributes = [];
26
+ var children = [];
27
+ var template = (_a = parentField.templates) === null || _a === void 0 ? void 0 : _a.find(function (template) {
28
+ if (typeof template === 'string') {
29
+ throw new Error('Global templates not supported');
30
+ }
31
+ return template.name === element.name;
32
+ });
33
+ if (!template || typeof template === 'string') {
34
+ throw new Error("Unable to find template for JSX element " + element.name);
35
+ }
36
+ Object.entries(element.props).forEach(function (_a) {
37
+ var name = _a[0], value = _a[1];
38
+ var field = template.fields.find(function (field) { return field.name === name; });
39
+ if (!field) {
40
+ if (name === 'children') {
41
+ return;
42
+ }
43
+ throw new Error("No field definition found for property " + name);
44
+ }
45
+ switch (field.type) {
46
+ case 'reference':
47
+ if (field.list) {
48
+ if (Array.isArray(value)) {
49
+ attributes.push({
50
+ type: 'mdxJsxAttribute',
51
+ name: name,
52
+ value: {
53
+ type: 'mdxJsxAttributeValueExpression',
54
+ value: "[" + value.map(function (item) { return "\"" + item + "\""; }).join(', ') + "]"
55
+ }
56
+ });
57
+ }
58
+ }
59
+ else {
60
+ if (typeof value === 'string') {
61
+ attributes.push({
62
+ type: 'mdxJsxAttribute',
63
+ name: name,
64
+ value: value
65
+ });
66
+ }
67
+ }
68
+ break;
69
+ case 'datetime':
70
+ case 'string':
71
+ if (field.list) {
72
+ if (Array.isArray(value)) {
73
+ attributes.push({
74
+ type: 'mdxJsxAttribute',
75
+ name: name,
76
+ value: {
77
+ type: 'mdxJsxAttributeValueExpression',
78
+ value: "[" + value.map(function (item) { return "\"" + item + "\""; }).join(', ') + "]"
79
+ }
80
+ });
81
+ }
82
+ }
83
+ else {
84
+ if (typeof value === 'string') {
85
+ attributes.push({
86
+ type: 'mdxJsxAttribute',
87
+ name: name,
88
+ value: value
89
+ });
90
+ }
91
+ else {
92
+ throw new Error("Expected string for attribute on field " + field.name);
93
+ }
94
+ }
95
+ break;
96
+ case 'image':
97
+ if (field.list) {
98
+ if (Array.isArray(value)) {
99
+ attributes.push({
100
+ type: 'mdxJsxAttribute',
101
+ name: name,
102
+ value: {
103
+ type: 'mdxJsxAttributeValueExpression',
104
+ value: "[" + value
105
+ .map(function (item) { return "\"" + imageCallback(item) + "\""; })
106
+ .join(', ') + "]"
107
+ }
108
+ });
109
+ }
110
+ }
111
+ else {
112
+ attributes.push({
113
+ type: 'mdxJsxAttribute',
114
+ name: name,
115
+ value: imageCallback(String(value))
116
+ });
117
+ }
118
+ break;
119
+ case 'number':
120
+ case 'boolean':
121
+ if (field.list) {
122
+ if (Array.isArray(value)) {
123
+ attributes.push({
124
+ type: 'mdxJsxAttribute',
125
+ name: name,
126
+ value: {
127
+ type: 'mdxJsxAttributeValueExpression',
128
+ value: "[" + value.map(function (item) { return "" + item; }).join(', ') + "]"
129
+ }
130
+ });
131
+ }
132
+ }
133
+ else {
134
+ attributes.push({
135
+ type: 'mdxJsxAttribute',
136
+ name: name,
137
+ value: {
138
+ type: 'mdxJsxAttributeValueExpression',
139
+ value: String(value)
140
+ }
141
+ });
142
+ }
143
+ break;
144
+ case 'object':
145
+ attributes.push({
146
+ type: 'mdxJsxAttribute',
147
+ name: name,
148
+ value: {
149
+ type: 'mdxJsxAttributeValueExpression',
150
+ value: stringifyObj(value, flatten)
151
+ }
152
+ });
153
+ break;
154
+ case 'rich-text':
155
+ if (typeof value === 'string') {
156
+ throw new Error("Unexpected string for rich-text, ensure the value has been properly parsed");
157
+ }
158
+ if (field.list) {
159
+ throw new Error("Rich-text list is not supported");
160
+ }
161
+ else {
162
+ var joiner = flatten ? ' ' : '\n';
163
+ var val = '';
164
+ assertShape(value, function (value) { return value.type === 'root' && Array.isArray(value.children); }, "Nested rich-text element is not a valid shape for field " + field.name);
165
+ if (field.name === 'children') {
166
+ var root = rootElement(value, field, imageCallback);
167
+ root.children.forEach(function (child) {
168
+ children.push(child);
169
+ });
170
+ return;
171
+ }
172
+ else {
173
+ var stringValue = stringifyMDX(value, field, imageCallback);
174
+ val = stringValue
175
+ .trim()
176
+ .split('\n')
177
+ .map(function (str) { return " " + str.trim(); })
178
+ .join(joiner);
179
+ }
180
+ if (flatten) {
181
+ attributes.push({
182
+ type: 'mdxJsxAttribute',
183
+ name: name,
184
+ value: {
185
+ type: 'mdxJsxAttributeValueExpression',
186
+ value: "<>" + val.trim() + "</>"
187
+ }
188
+ });
189
+ }
190
+ else {
191
+ attributes.push({
192
+ type: 'mdxJsxAttribute',
193
+ name: name,
194
+ value: {
195
+ type: 'mdxJsxAttributeValueExpression',
196
+ value: "<>\n" + val + "\n</>"
197
+ }
198
+ });
199
+ }
200
+ }
201
+ break;
202
+ default:
203
+ // @ts-expect-error error type is never
204
+ throw new Error("Stringify props: " + field.type + " not yet supported");
205
+ }
206
+ });
207
+ if (template.match) {
208
+ if (attributes[0] && typeof attributes[0].value === 'string') {
209
+ return {
210
+ attributes: [],
211
+ children: [{ type: 'inlineCode', value: attributes[0].value }]
212
+ };
213
+ }
214
+ }
215
+ return { attributes: attributes, children: children };
216
+ }
217
+ /**
218
+ * Use prettier to determine how to format potentially large objects as strings
219
+ */
220
+ function stringifyObj(obj, flatten) {
221
+ if (typeof obj === 'object' && obj !== null) {
222
+ var dummyFunc = "const dummyFunc = ";
223
+ var res = format("" + dummyFunc + JSON.stringify(obj), {
224
+ parser: 'acorn',
225
+ trailingComma: 'none',
226
+ semi: false
227
+ })
228
+ .trim()
229
+ .replace(dummyFunc, '');
230
+ return flatten ? res.replaceAll('\n', '').replaceAll(' ', ' ') : res;
231
+ }
232
+ else {
233
+ console.log(obj);
234
+ throw new Error("stringifyObj must be passed an object or an array of objects, received " + typeof obj);
235
+ }
236
+ }
237
+ export function assertShape(value, callback, errorMessage) {
238
+ if (!callback(value)) {
239
+ throw new Error(errorMessage || "Failed to assert shape");
240
+ }
241
+ }
@@ -15,9 +15,24 @@ See the License for the specific language governing permissions and
15
15
  limitations under the License.
16
16
 
17
17
  */
18
+ import { MdxJsxTextElement, MdxJsxFlowElement } from 'mdast-util-mdx-jsx';
18
19
  import type { RichTypeInner } from '@tinacms/schema-tools';
19
20
  import type * as Md from 'mdast';
20
21
  import type * as Plate from '../parse/plate';
22
+ declare module 'mdast' {
23
+ interface StaticPhrasingContentMap {
24
+ mdxJsxTextElement: MdxJsxTextElement;
25
+ }
26
+ interface PhrasingContentMap {
27
+ mdxJsxTextElement: MdxJsxTextElement;
28
+ }
29
+ interface BlockContentMap {
30
+ mdxJsxFlowElement: MdxJsxFlowElement;
31
+ }
32
+ interface ContentMap {
33
+ mdxJsxFlowElement: MdxJsxFlowElement;
34
+ }
35
+ }
21
36
  export declare const stringifyMDX: (value: Plate.RootElement, field: RichTypeInner, imageCallback: (url: string) => string) => string;
22
37
  export declare const rootElement: (content: Plate.RootElement, field: RichTypeInner, imageCallback: (url: string) => string) => Md.Root;
23
38
  export declare const blockElement: (content: Plate.BlockElement, field: RichTypeInner, imageCallback: (url: string) => string) => Md.Content | null;