@tinacms/mdx 2.2.1 → 2.2.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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ import type * as Md from 'mdast';
2
+ /**
3
+ * Reshape the tree so every hard break left in it is one markdown can express.
4
+ * Acts in place.
5
+ *
6
+ * Two ordered passes, not one. Splitting a heading that still holds a trailing
7
+ * break yields a second heading containing only what followed it — for
8
+ * `### [one⏎](/x)` that is an empty link, written out as `### [](/x)`. Trimming
9
+ * the whole tree first means the split only ever sees breaks with content on
10
+ * both sides.
11
+ */
12
+ export declare const serializeBreaks: <T extends Md.Root>(tree: T) => T;
@@ -0,0 +1 @@
1
+ export {};
@@ -1225,7 +1225,7 @@ var init_acorn = __esm({
1225
1225
  }
1226
1226
  };
1227
1227
  pp$8.parseBreakContinueStatement = function(node2, keyword) {
1228
- var isBreak = keyword === "break";
1228
+ var isBreak2 = keyword === "break";
1229
1229
  this.next();
1230
1230
  if (this.eat(types$1.semi) || this.insertSemicolon()) {
1231
1231
  node2.label = null;
@@ -1239,10 +1239,10 @@ var init_acorn = __esm({
1239
1239
  for (; i2 < this.labels.length; ++i2) {
1240
1240
  var lab = this.labels[i2];
1241
1241
  if (node2.label == null || lab.name === node2.label.name) {
1242
- if (lab.kind != null && (isBreak || lab.kind === "loop")) {
1242
+ if (lab.kind != null && (isBreak2 || lab.kind === "loop")) {
1243
1243
  break;
1244
1244
  }
1245
- if (node2.label && isBreak) {
1245
+ if (node2.label && isBreak2) {
1246
1246
  break;
1247
1247
  }
1248
1248
  }
@@ -1250,7 +1250,7 @@ var init_acorn = __esm({
1250
1250
  if (i2 === this.labels.length) {
1251
1251
  this.raise(node2.start, "Unsyntactic " + keyword);
1252
1252
  }
1253
- return this.finishNode(node2, isBreak ? "BreakStatement" : "ContinueStatement");
1253
+ return this.finishNode(node2, isBreak2 ? "BreakStatement" : "ContinueStatement");
1254
1254
  };
1255
1255
  pp$8.parseDebuggerStatement = function(node2) {
1256
1256
  this.next();
@@ -34302,6 +34302,112 @@ function assertShape(value, callback, errorMessage) {
34302
34302
  }
34303
34303
  }
34304
34304
 
34305
+ // src/break-serialization.ts
34306
+ var serializeBreaks = (tree) => {
34307
+ trimTree(tree);
34308
+ splitTree(tree);
34309
+ return tree;
34310
+ };
34311
+ var isParent = (node2) => Array.isArray(node2?.children);
34312
+ var PHRASING_BLOCKS = /* @__PURE__ */ new Set(["paragraph", "heading", "tableCell"]);
34313
+ var trimTree = (node2) => {
34314
+ dropBreaksBeforeLineStartSensitive(node2.children);
34315
+ if (PHRASING_BLOCKS.has(node2.type ?? "")) {
34316
+ trimTrailingBreaks(node2);
34317
+ }
34318
+ for (const child of node2.children) {
34319
+ if (isParent(child)) {
34320
+ trimTree(child);
34321
+ }
34322
+ }
34323
+ };
34324
+ var splitTree = (node2) => {
34325
+ node2.children = node2.children.flatMap(
34326
+ (child) => isHeading(child) && child.depth > DEEPEST_SETEXT ? splitOnBreaks(child) : [child]
34327
+ );
34328
+ for (const child of node2.children) {
34329
+ if (isParent(child)) {
34330
+ splitTree(child);
34331
+ }
34332
+ }
34333
+ };
34334
+ var trimTrailingBreaks = ({ children }) => {
34335
+ let index2 = children.length;
34336
+ const trailing = [];
34337
+ while (index2 > 0) {
34338
+ const child = children[index2 - 1];
34339
+ if (isBreak(child)) {
34340
+ trailing.push(index2 - 1);
34341
+ } else if (!writesNothing(child)) {
34342
+ break;
34343
+ }
34344
+ index2--;
34345
+ }
34346
+ for (const at3 of trailing) {
34347
+ children.splice(at3, 1);
34348
+ }
34349
+ const last = children[index2 - 1];
34350
+ if (isParent(last)) {
34351
+ trimTrailingBreaks(last);
34352
+ }
34353
+ };
34354
+ var isBreak = (node2) => node2?.type === "break";
34355
+ var writesNothing = (node2) => node2?.type === "text" && node2.value === "";
34356
+ var dropBreaksBeforeLineStartSensitive = (children) => {
34357
+ for (let index2 = children.length - 1; index2 >= 0; index2--) {
34358
+ if (!isBreak(children[index2])) {
34359
+ continue;
34360
+ }
34361
+ const next = children.slice(index2 + 1).find((sibling2) => !writesNothing(sibling2));
34362
+ const type = next?.type;
34363
+ if (type === "html") {
34364
+ const before = children[index2 - 1];
34365
+ const spaced = before?.type === "text" && /[ \t]$/.test(before.value);
34366
+ children.splice(
34367
+ index2,
34368
+ 1,
34369
+ ...spaced ? [] : [{ type: "text", value: " " }]
34370
+ );
34371
+ } else if (type === "mdxJsxTextElement") {
34372
+ children.splice(index2, 1);
34373
+ }
34374
+ }
34375
+ };
34376
+ var isHeading = (node2) => isParent(node2) && node2.type === "heading";
34377
+ var DEEPEST_SETEXT = 2;
34378
+ var splitOnBreaks = (heading2) => splitInlines(heading2.children).map((children) => children.filter((child) => !writesNothingDeep(child))).filter((children) => children.length).map((children) => ({ ...heading2, children }));
34379
+ var writesNothingDeep = (node2) => {
34380
+ if (isBreak(node2)) {
34381
+ return false;
34382
+ }
34383
+ if (isParent(node2)) {
34384
+ return node2.children.length > 0 && node2.children.every(writesNothingDeep);
34385
+ }
34386
+ return writesNothing(node2);
34387
+ };
34388
+ var splitInlines = (nodes) => {
34389
+ const segments = [[]];
34390
+ const push2 = (node2) => segments[segments.length - 1]?.push(node2);
34391
+ for (const node2 of nodes) {
34392
+ if (isBreak(node2)) {
34393
+ segments.push([]);
34394
+ continue;
34395
+ }
34396
+ if (!isParent(node2)) {
34397
+ push2(node2);
34398
+ continue;
34399
+ }
34400
+ const inner = splitInlines(node2.children);
34401
+ inner.forEach((children, index2) => {
34402
+ if (index2 > 0) {
34403
+ segments.push([]);
34404
+ }
34405
+ push2({ ...node2, children });
34406
+ });
34407
+ }
34408
+ return segments;
34409
+ };
34410
+
34305
34411
  // src/extensions/tina-shortcodes/to-markdown.ts
34306
34412
  var own6 = {}.hasOwnProperty;
34307
34413
  var directiveToMarkdown = (patterns) => ({
@@ -35059,7 +35165,7 @@ var toTinaMarkdown = (tree, field) => {
35059
35165
  }
35060
35166
  return text4(node2, parent, context, safeOptions);
35061
35167
  };
35062
- return toMarkdown(tree, {
35168
+ return toMarkdown(serializeBreaks(tree), {
35063
35169
  extensions: [
35064
35170
  directiveToMarkdown(patterns),
35065
35171
  mdxJsxToMarkdown(),
@@ -36288,7 +36394,7 @@ var toTinaMarkdown2 = (tree, field) => {
36288
36394
  }
36289
36395
  return text4(node2, parent, context, safeOptions);
36290
36396
  };
36291
- return toMarkdown(tree, {
36397
+ return toMarkdown(serializeBreaks(tree), {
36292
36398
  extensions: [mdxJsxToMarkdown2({ patterns }), gfmToMarkdown()],
36293
36399
  listItemIndent: "one",
36294
36400
  handlers
@@ -37074,6 +37180,9 @@ var fromMarkdown2 = (value, field) => {
37074
37180
  };
37075
37181
 
37076
37182
  // src/sanitize-url.ts
37183
+ var IGNORED_IN_SCHEME = /[\p{Cc}\p{Cf}]/gu;
37184
+ var SCHEME_PREFIX = /^[a-zA-Z][a-zA-Z0-9+.\-]*:/;
37185
+ var namesAScheme = (url) => SCHEME_PREFIX.test(url.replace(IGNORED_IN_SCHEME, "").trim());
37077
37186
  var sanitizeUrl = (url) => {
37078
37187
  const allowedSchemes = ["http", "https", "mailto", "tel", "xref"];
37079
37188
  if (!url) return "";
@@ -37081,6 +37190,10 @@ var sanitizeUrl = (url) => {
37081
37190
  try {
37082
37191
  parsedUrl = new URL(url);
37083
37192
  } catch (error) {
37193
+ if (namesAScheme(url)) {
37194
+ console.warn(`Invalid URL scheme detected ${url}`);
37195
+ return "";
37196
+ }
37084
37197
  return url;
37085
37198
  }
37086
37199
  const scheme = parsedUrl.protocol.slice(0, -1);
@@ -37807,10 +37920,13 @@ var remarkToSlate = (root2, field, imageCallback, raw, skipMDXProcess) => {
37807
37920
  ) ?? mdxJsxElement2(content4, field, imageCallback);
37808
37921
  case "text":
37809
37922
  return text7(content4);
37923
+ case "break":
37924
+ return breakContent();
37810
37925
  case "inlineCode":
37811
37926
  case "emphasis":
37812
37927
  case "image":
37813
37928
  case "strong":
37929
+ case "delete":
37814
37930
  return phrashingMark(content4);
37815
37931
  case "html":
37816
37932
  return html_inline(content4);
package/dist/index.js CHANGED
@@ -1225,7 +1225,7 @@ var init_acorn = __esm({
1225
1225
  }
1226
1226
  };
1227
1227
  pp$8.parseBreakContinueStatement = function(node2, keyword) {
1228
- var isBreak = keyword === "break";
1228
+ var isBreak2 = keyword === "break";
1229
1229
  this.next();
1230
1230
  if (this.eat(types$1.semi) || this.insertSemicolon()) {
1231
1231
  node2.label = null;
@@ -1239,10 +1239,10 @@ var init_acorn = __esm({
1239
1239
  for (; i2 < this.labels.length; ++i2) {
1240
1240
  var lab = this.labels[i2];
1241
1241
  if (node2.label == null || lab.name === node2.label.name) {
1242
- if (lab.kind != null && (isBreak || lab.kind === "loop")) {
1242
+ if (lab.kind != null && (isBreak2 || lab.kind === "loop")) {
1243
1243
  break;
1244
1244
  }
1245
- if (node2.label && isBreak) {
1245
+ if (node2.label && isBreak2) {
1246
1246
  break;
1247
1247
  }
1248
1248
  }
@@ -1250,7 +1250,7 @@ var init_acorn = __esm({
1250
1250
  if (i2 === this.labels.length) {
1251
1251
  this.raise(node2.start, "Unsyntactic " + keyword);
1252
1252
  }
1253
- return this.finishNode(node2, isBreak ? "BreakStatement" : "ContinueStatement");
1253
+ return this.finishNode(node2, isBreak2 ? "BreakStatement" : "ContinueStatement");
1254
1254
  };
1255
1255
  pp$8.parseDebuggerStatement = function(node2) {
1256
1256
  this.next();
@@ -36168,6 +36168,112 @@ function assertShape(value, callback, errorMessage) {
36168
36168
  }
36169
36169
  }
36170
36170
 
36171
+ // src/break-serialization.ts
36172
+ var serializeBreaks = (tree) => {
36173
+ trimTree(tree);
36174
+ splitTree(tree);
36175
+ return tree;
36176
+ };
36177
+ var isParent = (node2) => Array.isArray(node2?.children);
36178
+ var PHRASING_BLOCKS = /* @__PURE__ */ new Set(["paragraph", "heading", "tableCell"]);
36179
+ var trimTree = (node2) => {
36180
+ dropBreaksBeforeLineStartSensitive(node2.children);
36181
+ if (PHRASING_BLOCKS.has(node2.type ?? "")) {
36182
+ trimTrailingBreaks(node2);
36183
+ }
36184
+ for (const child of node2.children) {
36185
+ if (isParent(child)) {
36186
+ trimTree(child);
36187
+ }
36188
+ }
36189
+ };
36190
+ var splitTree = (node2) => {
36191
+ node2.children = node2.children.flatMap(
36192
+ (child) => isHeading(child) && child.depth > DEEPEST_SETEXT ? splitOnBreaks(child) : [child]
36193
+ );
36194
+ for (const child of node2.children) {
36195
+ if (isParent(child)) {
36196
+ splitTree(child);
36197
+ }
36198
+ }
36199
+ };
36200
+ var trimTrailingBreaks = ({ children }) => {
36201
+ let index2 = children.length;
36202
+ const trailing = [];
36203
+ while (index2 > 0) {
36204
+ const child = children[index2 - 1];
36205
+ if (isBreak(child)) {
36206
+ trailing.push(index2 - 1);
36207
+ } else if (!writesNothing(child)) {
36208
+ break;
36209
+ }
36210
+ index2--;
36211
+ }
36212
+ for (const at3 of trailing) {
36213
+ children.splice(at3, 1);
36214
+ }
36215
+ const last = children[index2 - 1];
36216
+ if (isParent(last)) {
36217
+ trimTrailingBreaks(last);
36218
+ }
36219
+ };
36220
+ var isBreak = (node2) => node2?.type === "break";
36221
+ var writesNothing = (node2) => node2?.type === "text" && node2.value === "";
36222
+ var dropBreaksBeforeLineStartSensitive = (children) => {
36223
+ for (let index2 = children.length - 1; index2 >= 0; index2--) {
36224
+ if (!isBreak(children[index2])) {
36225
+ continue;
36226
+ }
36227
+ const next = children.slice(index2 + 1).find((sibling2) => !writesNothing(sibling2));
36228
+ const type = next?.type;
36229
+ if (type === "html") {
36230
+ const before = children[index2 - 1];
36231
+ const spaced = before?.type === "text" && /[ \t]$/.test(before.value);
36232
+ children.splice(
36233
+ index2,
36234
+ 1,
36235
+ ...spaced ? [] : [{ type: "text", value: " " }]
36236
+ );
36237
+ } else if (type === "mdxJsxTextElement") {
36238
+ children.splice(index2, 1);
36239
+ }
36240
+ }
36241
+ };
36242
+ var isHeading = (node2) => isParent(node2) && node2.type === "heading";
36243
+ var DEEPEST_SETEXT = 2;
36244
+ var splitOnBreaks = (heading2) => splitInlines(heading2.children).map((children) => children.filter((child) => !writesNothingDeep(child))).filter((children) => children.length).map((children) => ({ ...heading2, children }));
36245
+ var writesNothingDeep = (node2) => {
36246
+ if (isBreak(node2)) {
36247
+ return false;
36248
+ }
36249
+ if (isParent(node2)) {
36250
+ return node2.children.length > 0 && node2.children.every(writesNothingDeep);
36251
+ }
36252
+ return writesNothing(node2);
36253
+ };
36254
+ var splitInlines = (nodes) => {
36255
+ const segments = [[]];
36256
+ const push2 = (node2) => segments[segments.length - 1]?.push(node2);
36257
+ for (const node2 of nodes) {
36258
+ if (isBreak(node2)) {
36259
+ segments.push([]);
36260
+ continue;
36261
+ }
36262
+ if (!isParent(node2)) {
36263
+ push2(node2);
36264
+ continue;
36265
+ }
36266
+ const inner = splitInlines(node2.children);
36267
+ inner.forEach((children, index2) => {
36268
+ if (index2 > 0) {
36269
+ segments.push([]);
36270
+ }
36271
+ push2({ ...node2, children });
36272
+ });
36273
+ }
36274
+ return segments;
36275
+ };
36276
+
36171
36277
  // src/extensions/tina-shortcodes/to-markdown.ts
36172
36278
  var own7 = {}.hasOwnProperty;
36173
36279
  var directiveToMarkdown = (patterns) => ({
@@ -36925,7 +37031,7 @@ var toTinaMarkdown = (tree, field) => {
36925
37031
  }
36926
37032
  return text4(node2, parent, context, safeOptions);
36927
37033
  };
36928
- return toMarkdown(tree, {
37034
+ return toMarkdown(serializeBreaks(tree), {
36929
37035
  extensions: [
36930
37036
  directiveToMarkdown(patterns),
36931
37037
  mdxJsxToMarkdown(),
@@ -38154,7 +38260,7 @@ var toTinaMarkdown2 = (tree, field) => {
38154
38260
  }
38155
38261
  return text4(node2, parent, context, safeOptions);
38156
38262
  };
38157
- return toMarkdown(tree, {
38263
+ return toMarkdown(serializeBreaks(tree), {
38158
38264
  extensions: [mdxJsxToMarkdown2({ patterns }), gfmToMarkdown()],
38159
38265
  listItemIndent: "one",
38160
38266
  handlers
@@ -38940,6 +39046,9 @@ var fromMarkdown2 = (value, field) => {
38940
39046
  };
38941
39047
 
38942
39048
  // src/sanitize-url.ts
39049
+ var IGNORED_IN_SCHEME = /[\p{Cc}\p{Cf}]/gu;
39050
+ var SCHEME_PREFIX = /^[a-zA-Z][a-zA-Z0-9+.\-]*:/;
39051
+ var namesAScheme = (url) => SCHEME_PREFIX.test(url.replace(IGNORED_IN_SCHEME, "").trim());
38943
39052
  var sanitizeUrl = (url) => {
38944
39053
  const allowedSchemes = ["http", "https", "mailto", "tel", "xref"];
38945
39054
  if (!url) return "";
@@ -38947,6 +39056,10 @@ var sanitizeUrl = (url) => {
38947
39056
  try {
38948
39057
  parsedUrl = new URL(url);
38949
39058
  } catch (error) {
39059
+ if (namesAScheme(url)) {
39060
+ console.warn(`Invalid URL scheme detected ${url}`);
39061
+ return "";
39062
+ }
38950
39063
  return url;
38951
39064
  }
38952
39065
  const scheme = parsedUrl.protocol.slice(0, -1);
@@ -39673,10 +39786,13 @@ var remarkToSlate = (root2, field, imageCallback, raw, skipMDXProcess) => {
39673
39786
  ) ?? mdxJsxElement2(content4, field, imageCallback);
39674
39787
  case "text":
39675
39788
  return text7(content4);
39789
+ case "break":
39790
+ return breakContent();
39676
39791
  case "inlineCode":
39677
39792
  case "emphasis":
39678
39793
  case "image":
39679
39794
  case "strong":
39795
+ case "delete":
39680
39796
  return phrashingMark(content4);
39681
39797
  case "html":
39682
39798
  return html_inline(content4);
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,6 @@
1
+ const IGNORED_IN_SCHEME = /[\p{Cc}\p{Cf}]/gu;
2
+ const SCHEME_PREFIX = /^[a-zA-Z][a-zA-Z0-9+.\-]*:/;
3
+ const namesAScheme = (url) => SCHEME_PREFIX.test(url.replace(IGNORED_IN_SCHEME, "").trim());
1
4
  const sanitizeUrl = (url) => {
2
5
  const allowedSchemes = ["http", "https", "mailto", "tel", "xref"];
3
6
  if (!url) return "";
@@ -5,6 +8,10 @@ const sanitizeUrl = (url) => {
5
8
  try {
6
9
  parsedUrl = new URL(url);
7
10
  } catch (error) {
11
+ if (namesAScheme(url)) {
12
+ console.warn(`Invalid URL scheme detected ${url}`);
13
+ return "";
14
+ }
8
15
  return url;
9
16
  }
10
17
  const scheme = parsedUrl.protocol.slice(0, -1);
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinacms/mdx",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -58,14 +58,14 @@
58
58
  "unist-util-stringify-position": "3.0.3",
59
59
  "unist-util-visit": "4.1.2",
60
60
  "vfile-message": "3.1.4",
61
- "@tinacms/schema-tools": "^2.9.0"
61
+ "@tinacms/schema-tools": "^2.10.0"
62
62
  },
63
63
  "publishConfig": {
64
64
  "registry": "https://registry.npmjs.org"
65
65
  },
66
66
  "repository": {
67
67
  "url": "https://github.com/tinacms/tinacms.git",
68
- "directory": "packages/tina-graphql"
68
+ "directory": "packages/@tinacms/mdx"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@types/estree": "1.0.0",