@tinacms/mdx 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -244330,29 +244330,6 @@ function mdxJsxElement(node, field, imageCallback) {
244330
244330
  children: [{ type: "text", text: "" }]
244331
244331
  };
244332
244332
  }
244333
- if (template.match) {
244334
- const firstChild = node == null ? void 0 : node.children[0];
244335
- if ((firstChild == null ? void 0 : firstChild.type) === "inlineCode") {
244336
- const value = firstChild.value;
244337
- if (node.type === "mdxJsxFlowElement") {
244338
- return {
244339
- type: node.type,
244340
- name: node.name,
244341
- children: [{ type: "text", text: "" }],
244342
- props: { text: value }
244343
- };
244344
- } else {
244345
- return {
244346
- type: node.type,
244347
- name: node.name,
244348
- children: [{ type: "text", text: "" }],
244349
- props: { text: value }
244350
- };
244351
- }
244352
- } else {
244353
- throw new Error(`Unable to parse value for template match pattern - start: ${template.match.start}, end: ${template.match.end}`);
244354
- }
244355
- }
244356
244333
  const props = extractAttributes(node.attributes, template.fields, imageCallback);
244357
244334
  const childField = template.fields.find((field2) => field2.name === "children");
244358
244335
  if (childField) {
@@ -244679,6 +244656,17 @@ var RichTextParseError = class extends Error {
244679
244656
  }
244680
244657
  };
244681
244658
 
244659
+ // src/parse/parseShortcode.ts
244660
+ function parseShortcode(preprocessedString, template) {
244661
+ const match = template.match;
244662
+ const unkeyedAttributes = !!template.fields.find((t) => t.name === "_value");
244663
+ const hasChildren = !!template.fields.find((t) => t.name == "children");
244664
+ const replacement = `<${template.name} ${unkeyedAttributes ? '_value="$1"' : "$1"}>${hasChildren ? "$2" : "\n"}</${template.name}>`;
244665
+ const endRegex = `((?:.|\\n)*)${match.start}\\s/\\s*${match.name || template.name}[\\s]*${match.end}`;
244666
+ const regex = `${match.start}\\s*${match.name || template.name}[\\s]+${unkeyedAttributes ? `['"]?(.*?)['"]?` : "(.*?)"}[\\s]*${match.end}${hasChildren ? endRegex : ""}`;
244667
+ return replaceAll(preprocessedString, regex, replacement);
244668
+ }
244669
+
244682
244670
  // src/parse/index.ts
244683
244671
  var markdownToAst = (value, field) => {
244684
244672
  var _a;
@@ -244690,8 +244678,7 @@ var markdownToAst = (value, field) => {
244690
244678
  }
244691
244679
  if (template.match) {
244692
244680
  if (preprocessedString) {
244693
- preprocessedString = replaceAll(preprocessedString, template.match.start, `<${template.name}>\``);
244694
- preprocessedString = replaceAll(preprocessedString, template.match.end, `\`</${template.name}>`);
244681
+ preprocessedString = parseShortcode(preprocessedString, template);
244695
244682
  }
244696
244683
  }
244697
244684
  });
@@ -244702,6 +244689,7 @@ var markdownToAst = (value, field) => {
244702
244689
  }
244703
244690
  return tree;
244704
244691
  } catch (e) {
244692
+ console.error("error parsing file: ", e);
244705
244693
  throw new RichTextParseError(e, e.position);
244706
244694
  }
244707
244695
  };
@@ -244922,12 +244910,20 @@ ${val}
244922
244910
  }
244923
244911
  });
244924
244912
  if (template.match) {
244925
- if (attributes[0] && typeof attributes[0].value === "string") {
244926
- return {
244927
- attributes: [],
244928
- children: [{ type: "inlineCode", value: attributes[0].value }]
244929
- };
244930
- }
244913
+ return {
244914
+ attributes,
244915
+ children: children && children.length ? children : [
244916
+ {
244917
+ type: "paragraph",
244918
+ children: [
244919
+ {
244920
+ type: "text",
244921
+ value: ""
244922
+ }
244923
+ ]
244924
+ }
244925
+ ]
244926
+ };
244931
244927
  }
244932
244928
  return { attributes, children };
244933
244929
  }
@@ -245141,6 +245137,19 @@ var cleanNode = (node, mark) => {
245141
245137
  return cleanedNode;
245142
245138
  };
245143
245139
 
245140
+ // src/stringify/stringifyShortcode.ts
245141
+ function stringifyShortcode(preprocessedString, template) {
245142
+ const match = template.match;
245143
+ const unkeyedAttributes = !!template.fields.find((t) => t.name == "_value");
245144
+ const regex = `<[\\s]*${template.name}[\\s]*${unkeyedAttributes ? "(?:_value=(.*?))?" : "(.+?)?"}[\\s]*>[\\s]*((?:.|
245145
+ )*?)[\\s]*</[\\s]*${template.name}[\\s]*>`;
245146
+ const closingRegex = `
245147
+ $2
245148
+ ${match.start} /${match.name || template.name} ${match.end}`;
245149
+ const replace = `${match.start} ${match.name || template.name} $1 ${match.end}${template.fields.find((t) => t.name == "children") ? closingRegex : ""}`;
245150
+ return replaceAll(preprocessedString, regex, replace);
245151
+ }
245152
+
245144
245153
  // src/stringify/index.ts
245145
245154
  var stringifyMDX = (value, field, imageCallback) => {
245146
245155
  var _a;
@@ -245166,10 +245175,7 @@ var stringifyMDX = (value, field, imageCallback) => {
245166
245175
  throw new Error("Global templates are not supported");
245167
245176
  }
245168
245177
  if (template.match) {
245169
- preprocessedString = replaceAll(preprocessedString, `<${template.name}>[
245170
- \r\\s]*\``, `${template.match.start} `);
245171
- preprocessedString = replaceAll(preprocessedString, `\`[
245172
- \r\\s]*</${template.name}>`, ` ${template.match.end}`);
245178
+ preprocessedString = stringifyShortcode(preprocessedString, template);
245173
245179
  }
245174
245180
  });
245175
245181
  return preprocessedString;
@@ -0,0 +1,21 @@
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 { Template } from '@tinacms/schema-tools';
19
+ export declare function parseShortcode(preprocessedString: string, template: Template<false> & {
20
+ inline?: boolean;
21
+ }): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
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 { Template } from '@tinacms/schema-tools';
19
+ export declare function stringifyShortcode(preprocessedString: string, template: Template<false> & {
20
+ inline?: boolean;
21
+ }): string;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinacms/mdx",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "typings": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.js",
@@ -19,7 +19,7 @@
19
19
  ]
20
20
  },
21
21
  "dependencies": {
22
- "@tinacms/schema-tools": "1.1.0",
22
+ "@tinacms/schema-tools": "1.2.0",
23
23
  "acorn": "^8.7.1",
24
24
  "concat-md": "^0.5.0",
25
25
  "lodash-es": "^4.17.21",
@@ -43,7 +43,7 @@
43
43
  "devDependencies": {
44
44
  "@swc/core": "^1.2.210",
45
45
  "@swc/jest": "^0.2.21",
46
- "@tinacms/scripts": "1.0.0",
46
+ "@tinacms/scripts": "1.0.1",
47
47
  "@types/cors": "^2.8.7",
48
48
  "@types/estree": "^0.0.50",
49
49
  "@types/estree-jsx": "^1.0.0",