@markdoc/markdoc 0.1.12 → 0.2.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.
Files changed (53) hide show
  1. package/README.md +9 -7
  2. package/dist/index.d.ts +3 -3
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +83 -53
  5. package/dist/index.js.map +3 -3
  6. package/dist/index.mjs +83 -53
  7. package/dist/index.mjs.map +3 -3
  8. package/dist/react.js +16 -5
  9. package/dist/react.js.map +3 -3
  10. package/dist/src/formatter.d.ts.map +1 -1
  11. package/dist/src/parser.d.ts.map +1 -1
  12. package/dist/src/renderers/html.d.ts.map +1 -1
  13. package/dist/src/renderers/react/react.d.ts +1 -1
  14. package/dist/src/renderers/react/react.d.ts.map +1 -1
  15. package/dist/src/renderers/react/static.d.ts +1 -1
  16. package/dist/src/renderers/react/static.d.ts.map +1 -1
  17. package/dist/src/schema-types/id.d.ts +5 -0
  18. package/dist/src/schema-types/id.d.ts.map +1 -0
  19. package/dist/src/tag.d.ts +1 -0
  20. package/dist/src/tag.d.ts.map +1 -1
  21. package/dist/src/tags/conditional.d.ts +1 -1
  22. package/dist/src/tags/conditional.d.ts.map +1 -1
  23. package/dist/src/tags/partial.d.ts.map +1 -1
  24. package/dist/src/tags/table.d.ts.map +1 -1
  25. package/dist/src/transformer.d.ts.map +1 -1
  26. package/dist/src/types.d.ts +2 -1
  27. package/dist/src/types.d.ts.map +1 -1
  28. package/dist/src/utils.d.ts +1 -1
  29. package/dist/src/utils.d.ts.map +1 -1
  30. package/dist/src/validator.d.ts.map +1 -1
  31. package/index.ts +3 -3
  32. package/package.json +10 -3
  33. package/src/formatter.test.ts +109 -6
  34. package/src/formatter.ts +51 -27
  35. package/src/parser.ts +2 -1
  36. package/src/renderers/html.test.ts +17 -6
  37. package/src/renderers/html.ts +4 -2
  38. package/src/renderers/react/react.test.ts +5 -12
  39. package/src/renderers/react/react.ts +21 -4
  40. package/src/renderers/react/static.ts +21 -3
  41. package/src/schema-types/id.ts +15 -0
  42. package/src/tag.ts +4 -0
  43. package/src/tags/conditional.ts +11 -2
  44. package/src/tags/partial.ts +1 -0
  45. package/src/tags/table.ts +1 -0
  46. package/src/transformer.ts +14 -3
  47. package/src/types.ts +2 -1
  48. package/src/utils.ts +2 -2
  49. package/src/validator.test.ts +67 -0
  50. package/src/validator.ts +9 -6
  51. package/dist/src/renderers/react/shared.d.ts +0 -5
  52. package/dist/src/renderers/react/shared.d.ts.map +0 -1
  53. package/src/renderers/react/shared.ts +0 -16
package/dist/index.mjs CHANGED
@@ -6653,6 +6653,9 @@ var Tag = class {
6653
6653
  this.children = children;
6654
6654
  }
6655
6655
  };
6656
+ Tag.isTag = (tag) => {
6657
+ return "$$mdtype" in tag && tag.$$mdtype === "Tag";
6658
+ };
6656
6659
 
6657
6660
  // src/schema-types/class.ts
6658
6661
  var Class = class {
@@ -6678,6 +6681,21 @@ var Class = class {
6678
6681
  }
6679
6682
  };
6680
6683
 
6684
+ // src/schema-types/id.ts
6685
+ var Id = class {
6686
+ validate(value) {
6687
+ if (typeof value === "string" && value.match(/^[a-zA-Z]/))
6688
+ return [];
6689
+ return [
6690
+ {
6691
+ id: "attribute-value-invalid",
6692
+ level: "error",
6693
+ message: "The 'id' attribute must start with a letter"
6694
+ }
6695
+ ];
6696
+ }
6697
+ };
6698
+
6681
6699
  // src/utils.ts
6682
6700
  var import_tag = __toModule(require_tag());
6683
6701
 
@@ -6719,7 +6737,7 @@ var OPEN = "{%";
6719
6737
  var CLOSE = "%}";
6720
6738
  var IDENTIFIER_REGEX = /^[a-zA-Z0-9_-]+$/;
6721
6739
  function isIdentifier(s2) {
6722
- return IDENTIFIER_REGEX.test(s2);
6740
+ return typeof s2 === "string" && IDENTIFIER_REGEX.test(s2);
6723
6741
  }
6724
6742
  function isPromise(a) {
6725
6743
  return a && typeof a === "object" && typeof a.then === "function";
@@ -6824,7 +6842,7 @@ function parseTags(content, firstLine = 0) {
6824
6842
  // src/transformer.ts
6825
6843
  var globalAttributes = {
6826
6844
  class: { type: Class, render: true },
6827
- id: { type: String, render: true }
6845
+ id: { type: Id, render: true }
6828
6846
  };
6829
6847
  var transformer_default = {
6830
6848
  findSchema(node2, { nodes = {}, tags = {} } = {}) {
@@ -6954,6 +6972,9 @@ function* formatChildren(a, options) {
6954
6972
  yield* formatValue(child, options);
6955
6973
  }
6956
6974
  }
6975
+ function* formatInline(g) {
6976
+ yield [...g].join("").trim();
6977
+ }
6957
6978
  function* formatTableRow(items) {
6958
6979
  yield `| ${items.join(" | ")} |`;
6959
6980
  }
@@ -7003,8 +7024,10 @@ function* formatVariable(v) {
7003
7024
  yield v.path.map((p, i) => {
7004
7025
  if (i === 0)
7005
7026
  return p;
7006
- if (isIdentifier(String(p)))
7027
+ if (isIdentifier(p))
7007
7028
  return "." + p;
7029
+ if (typeof p === "number")
7030
+ return `[${p}]`;
7008
7031
  return `["${p}"]`;
7009
7032
  }).join("");
7010
7033
  }
@@ -7025,6 +7048,9 @@ function* trimStart(g) {
7025
7048
  yield n;
7026
7049
  yield* g;
7027
7050
  }
7051
+ function* escapeMarkdownCharacters(s2, characters) {
7052
+ yield s2.replace(characters, "\\$&").replace(new RegExp("\xA0", "g"), " ");
7053
+ }
7028
7054
  function* formatNode(n, o = {}) {
7029
7055
  const no = { ...o, parent: n };
7030
7056
  const indent = SPACE.repeat(no.indent || 0);
@@ -7041,20 +7067,20 @@ function* formatNode(n, o = {}) {
7041
7067
  yield indent;
7042
7068
  yield "#".repeat(n.attributes.level || 1);
7043
7069
  yield SPACE;
7044
- yield* formatChildren(n, no);
7070
+ yield* trimStart(formatChildren(n, no));
7045
7071
  yield* formatAnnotations(n);
7046
7072
  yield NL;
7047
7073
  break;
7048
7074
  }
7049
7075
  case "paragraph": {
7050
7076
  yield NL;
7051
- yield indent;
7052
7077
  yield* formatChildren(n, no);
7053
7078
  yield* formatAnnotations(n);
7054
7079
  yield NL;
7055
7080
  break;
7056
7081
  }
7057
7082
  case "inline": {
7083
+ yield indent;
7058
7084
  yield* formatChildren(n, no);
7059
7085
  break;
7060
7086
  }
@@ -7064,7 +7090,7 @@ function* formatNode(n, o = {}) {
7064
7090
  yield* formatValue(n.attributes.alt, no);
7065
7091
  yield "]";
7066
7092
  yield "(";
7067
- yield* formatValue(n.attributes.src, no);
7093
+ yield* typeof n.attributes.src === "string" ? escapeMarkdownCharacters(n.attributes.src, /[()]/) : formatValue(n.attributes.src, no);
7068
7094
  if (n.attributes.title) {
7069
7095
  yield SPACE + `"${n.attributes.title}"`;
7070
7096
  }
@@ -7076,7 +7102,7 @@ function* formatNode(n, o = {}) {
7076
7102
  yield* formatChildren(n, no);
7077
7103
  yield "]";
7078
7104
  yield "(";
7079
- yield* formatValue(n.attributes.href, no);
7105
+ yield* typeof n.attributes.href === "string" ? escapeMarkdownCharacters(n.attributes.href, /[()]/g) : formatValue(n.attributes.href, no);
7080
7106
  if (n.attributes.title) {
7081
7107
  yield SPACE + `"${n.attributes.title}"`;
7082
7108
  }
@@ -7084,10 +7110,11 @@ function* formatNode(n, o = {}) {
7084
7110
  break;
7085
7111
  }
7086
7112
  case "text": {
7087
- if (ast_default.isAst(n.attributes.content))
7113
+ const { content } = n.attributes;
7114
+ if (ast_default.isAst(content))
7088
7115
  yield OPEN + SPACE;
7089
- yield* formatValue(n.attributes.content, no);
7090
- if (ast_default.isAst(n.attributes.content))
7116
+ yield* typeof content === "string" ? escapeMarkdownCharacters(content, /[_*~]/g) : formatValue(content, no);
7117
+ if (ast_default.isAst(content))
7091
7118
  yield SPACE + CLOSE;
7092
7119
  break;
7093
7120
  }
@@ -7108,15 +7135,19 @@ function* formatNode(n, o = {}) {
7108
7135
  case "fence": {
7109
7136
  yield NL;
7110
7137
  yield indent;
7111
- yield "```";
7112
- yield (n.attributes.language || "").toLowerCase();
7138
+ const innerFence = n.attributes.content.match(/`{3,}/g) || [];
7139
+ const innerFenceLength = innerFence.map((s2) => s2.length).reduce(max, 0);
7140
+ const boundary = "`".repeat(innerFenceLength ? innerFenceLength + 1 : 3);
7141
+ yield boundary;
7142
+ if (n.attributes.language)
7143
+ yield n.attributes.language;
7113
7144
  if (n.annotations.length)
7114
7145
  yield SPACE;
7115
7146
  yield* formatAnnotations(n);
7116
7147
  yield NL;
7117
7148
  yield indent;
7118
7149
  yield n.attributes.content.split(NL).join(NL + indent);
7119
- yield "```";
7150
+ yield boundary;
7120
7151
  yield NL;
7121
7152
  break;
7122
7153
  }
@@ -7129,7 +7160,7 @@ function* formatNode(n, o = {}) {
7129
7160
  const tag = [open + n.tag, ...formatAttributes(n)];
7130
7161
  const inlineTag = tag.join(SPACE);
7131
7162
  const isLongTagOpening = inlineTag.length + open.length * 2 > (o.maxTagOpeningWidth || MAX_TAG_OPENING_WIDTH);
7132
- yield (isLongTagOpening ? tag.join(NL + SPACE.repeat(open.length) + indent) : inlineTag) + SPACE + (n.children.length ? "" : "/") + CLOSE;
7163
+ yield (!n.inline && isLongTagOpening ? tag.join(NL + SPACE.repeat(open.length) + indent) : inlineTag) + SPACE + (n.children.length ? "" : "/") + CLOSE;
7133
7164
  if (n.children.length) {
7134
7165
  yield* formatChildren(n, no.allowIndentation ? increment(no) : no);
7135
7166
  if (!n.inline) {
@@ -7144,18 +7175,16 @@ function* formatNode(n, o = {}) {
7144
7175
  }
7145
7176
  case "list": {
7146
7177
  const prefix = n.attributes.ordered ? OL : UL;
7147
- yield NL;
7148
7178
  for (const child of n.children) {
7149
7179
  const d = format(child, increment(no, prefix.length)).trim();
7150
- yield indent + prefix + d;
7151
- yield NL;
7180
+ yield NL + indent + prefix + d;
7152
7181
  }
7182
+ yield NL;
7153
7183
  break;
7154
7184
  }
7155
7185
  case "item": {
7156
7186
  for (let i = 0; i < n.children.length; i++) {
7157
- const child = n.children[i];
7158
- yield* formatValue(child, no);
7187
+ yield* formatValue(n.children[i], no);
7159
7188
  if (i === 0)
7160
7189
  yield* formatAnnotations(n);
7161
7190
  }
@@ -7163,25 +7192,25 @@ function* formatNode(n, o = {}) {
7163
7192
  }
7164
7193
  case "strong": {
7165
7194
  yield "**";
7166
- yield* formatChildren(n, no);
7195
+ yield* formatInline(formatChildren(n, no));
7167
7196
  yield "**";
7168
7197
  break;
7169
7198
  }
7170
7199
  case "em": {
7171
7200
  yield "_";
7172
- yield* formatChildren(n, no);
7201
+ yield* formatInline(formatChildren(n, no));
7173
7202
  yield "_";
7174
7203
  break;
7175
7204
  }
7176
7205
  case "code": {
7177
7206
  yield "`";
7178
- yield* formatValue(n.attributes.content, no);
7207
+ yield* formatInline(formatValue(n.attributes.content, no));
7179
7208
  yield "`";
7180
7209
  break;
7181
7210
  }
7182
7211
  case "s": {
7183
7212
  yield "~~";
7184
- yield* formatChildren(n, no);
7213
+ yield* formatInline(formatChildren(n, no));
7185
7214
  yield "~~";
7186
7215
  break;
7187
7216
  }
@@ -7210,10 +7239,8 @@ function* formatNode(n, o = {}) {
7210
7239
  yield NL;
7211
7240
  yield indent + "---";
7212
7241
  }
7213
- for (let j = 0; j < row.length; j++) {
7214
- const d = row[j];
7215
- yield NL;
7216
- yield indent + UL + d.trim();
7242
+ for (const d of row) {
7243
+ yield NL + indent + UL + d;
7217
7244
  }
7218
7245
  }
7219
7246
  }
@@ -7244,7 +7271,7 @@ function* formatNode(n, o = {}) {
7244
7271
  }
7245
7272
  case "td":
7246
7273
  case "th": {
7247
- yield [...formatChildren(n, no), ...formatAnnotations(n)].join("");
7274
+ yield [...formatChildren(n, no), ...formatAnnotations(n)].join("").trim();
7248
7275
  break;
7249
7276
  }
7250
7277
  case "tbody": {
@@ -7471,7 +7498,7 @@ function handleAttrs(token, type) {
7471
7498
  return { content: (token.meta || {}).variable || token.content };
7472
7499
  case "fence": {
7473
7500
  const [language] = token.info.split(" ", 1);
7474
- return language === "" ? { content: token.content } : { content: token.content, language };
7501
+ return language === "" || language === OPEN ? { content: token.content } : { content: token.content, language };
7475
7502
  }
7476
7503
  case "td":
7477
7504
  case "th": {
@@ -7841,11 +7868,11 @@ var voidElements = new Set([
7841
7868
  "wbr"
7842
7869
  ]);
7843
7870
  function render(node2) {
7844
- if (typeof node2 === "string")
7845
- return escapeHtml(node2);
7871
+ if (typeof node2 === "string" || typeof node2 === "number")
7872
+ return escapeHtml(String(node2));
7846
7873
  if (Array.isArray(node2))
7847
7874
  return node2.map(render).join("");
7848
- if (node2 === null || typeof node2 !== "object")
7875
+ if (node2 === null || typeof node2 !== "object" || !Tag.isTag(node2))
7849
7876
  return "";
7850
7877
  const { name, attributes, children = [] } = node2;
7851
7878
  if (!name)
@@ -7862,12 +7889,10 @@ function render(node2) {
7862
7889
  return output;
7863
7890
  }
7864
7891
 
7865
- // src/renderers/react/shared.ts
7892
+ // src/renderers/react/react.ts
7866
7893
  function tagName(name, components) {
7867
- return typeof name !== "string" ? "Fragment" : name[0] !== name[0].toUpperCase() ? name : components instanceof Function ? components(name) : components[name];
7894
+ return typeof name !== "string" ? name : name[0] !== name[0].toUpperCase() ? name : components instanceof Function ? components(name) : components[name];
7868
7895
  }
7869
-
7870
- // src/renderers/react/react.ts
7871
7896
  function dynamic(node2, React, { components = {} } = {}) {
7872
7897
  function deepRender2(value) {
7873
7898
  if (value == null || typeof value !== "object")
@@ -7886,7 +7911,7 @@ function dynamic(node2, React, { components = {} } = {}) {
7886
7911
  function render3(node3) {
7887
7912
  if (Array.isArray(node3))
7888
7913
  return React.createElement(React.Fragment, null, ...node3.map(render3));
7889
- if (node3 === null || typeof node3 !== "object")
7914
+ if (node3 === null || typeof node3 !== "object" || !Tag.isTag(node3))
7890
7915
  return node3;
7891
7916
  const {
7892
7917
  name,
@@ -7901,6 +7926,9 @@ function dynamic(node2, React, { components = {} } = {}) {
7901
7926
  }
7902
7927
 
7903
7928
  // src/renderers/react/static.ts
7929
+ function tagName2(name, components) {
7930
+ return typeof name !== "string" ? "Fragment" : name[0] !== name[0].toUpperCase() ? name : components instanceof Function ? components(name) : components[name];
7931
+ }
7904
7932
  function renderArray(children) {
7905
7933
  return children.map(render2).join(", ");
7906
7934
  }
@@ -7919,7 +7947,7 @@ function deepRender(value) {
7919
7947
  function render2(node2) {
7920
7948
  if (Array.isArray(node2))
7921
7949
  return `React.createElement(React.Fragment, null, ${renderArray(node2)})`;
7922
- if (node2 === null || typeof node2 !== "object")
7950
+ if (node2 === null || typeof node2 !== "object" || !Tag.isTag(node2))
7923
7951
  return JSON.stringify(node2);
7924
7952
  const {
7925
7953
  name,
@@ -7936,7 +7964,7 @@ function render2(node2) {
7936
7964
  function reactStatic(node2) {
7937
7965
  return `
7938
7966
  (({components = {}} = {}) => {
7939
- ${tagName}
7967
+ ${tagName2}
7940
7968
  return ${render2(node2)};
7941
7969
  })
7942
7970
  `;
@@ -7962,6 +7990,7 @@ var PartialFile = class {
7962
7990
  }
7963
7991
  };
7964
7992
  var partial = {
7993
+ inline: false,
7965
7994
  selfClosing: true,
7966
7995
  attributes: {
7967
7996
  file: { type: PartialFile, render: false, required: true },
@@ -7988,7 +8017,8 @@ var partial = {
7988
8017
 
7989
8018
  // src/tags/table.ts
7990
8019
  var table2 = {
7991
- children: ["table"]
8020
+ children: ["table"],
8021
+ inline: false
7992
8022
  };
7993
8023
 
7994
8024
  // src/tags/index.ts
@@ -8003,10 +8033,10 @@ var tags_default = {
8003
8033
  var import_lib = __toModule(require_lib());
8004
8034
 
8005
8035
  // src/tokenizer/plugins/annotations.ts
8006
- var import_tag4 = __toModule(require_tag());
8036
+ var import_tag7 = __toModule(require_tag());
8007
8037
  function createToken(state, content, contentStart) {
8008
8038
  try {
8009
- const { type, meta, nesting = 0 } = (0, import_tag4.parse)(content, { Variable, Function: Function2 });
8039
+ const { type, meta, nesting = 0 } = (0, import_tag7.parse)(content, { Variable, Function: Function2 });
8010
8040
  const token = state.push(type, "", nesting);
8011
8041
  token.info = content;
8012
8042
  token.meta = meta;
@@ -8015,7 +8045,7 @@ function createToken(state, content, contentStart) {
8015
8045
  }
8016
8046
  return token;
8017
8047
  } catch (error2) {
8018
- if (!(error2 instanceof import_tag4.SyntaxError))
8048
+ if (!(error2 instanceof import_tag7.SyntaxError))
8019
8049
  throw error2;
8020
8050
  const {
8021
8051
  message,
@@ -8073,10 +8103,10 @@ function core(state) {
8073
8103
  const end = findTagEnd(token.info, start);
8074
8104
  const content = token.info.slice(start + OPEN.length, end);
8075
8105
  try {
8076
- const { meta } = (0, import_tag4.parse)(content.trim(), { Variable, Function: Function2 });
8106
+ const { meta } = (0, import_tag7.parse)(content.trim(), { Variable, Function: Function2 });
8077
8107
  token.meta = meta;
8078
8108
  } catch (error2) {
8079
- if (!(error2 instanceof import_tag4.SyntaxError))
8109
+ if (!(error2 instanceof import_tag7.SyntaxError))
8080
8110
  throw error2;
8081
8111
  if (!token.errors)
8082
8112
  token.errors = [];
@@ -8282,6 +8312,12 @@ function validator(node2, config) {
8282
8312
  });
8283
8313
  return errors;
8284
8314
  }
8315
+ if (schema.inline != void 0 && node2.inline !== schema.inline)
8316
+ errors.push({
8317
+ id: "tag-placement-invalid",
8318
+ level: "critical",
8319
+ message: `'${node2.tag}' tag should be ${schema.inline ? "inline" : "block"}`
8320
+ });
8285
8321
  if (schema.selfClosing && node2.children.length > 0)
8286
8322
  errors.push({
8287
8323
  id: "tag-selfclosing-has-children",
@@ -8327,12 +8363,6 @@ function validator(node2, config) {
8327
8363
  continue;
8328
8364
  }
8329
8365
  value = value;
8330
- if (key === "id" && value.match(/^[0-9]/))
8331
- errors.push({
8332
- id: "attribute-value-invalid",
8333
- level: "error",
8334
- message: "The id attribute must not start with a number"
8335
- });
8336
8366
  if (type) {
8337
8367
  const valid = validateType(type, value, config);
8338
8368
  if (valid === false) {
@@ -8466,14 +8496,14 @@ Markdoc.transform = transform2;
8466
8496
  Markdoc.validate = validate;
8467
8497
  Markdoc.createElement = createElement;
8468
8498
  Markdoc.truthy = truthy;
8469
- Markdoc.__EXPERIMENTAL__format = format;
8499
+ Markdoc.format = format;
8470
8500
  export {
8471
8501
  ast_default as Ast,
8472
8502
  Tag,
8473
8503
  Tokenizer,
8474
- format as __EXPERIMENTAL__format,
8475
8504
  createElement,
8476
8505
  Markdoc as default,
8506
+ format,
8477
8507
  functions_default as functions,
8478
8508
  globalAttributes,
8479
8509
  schema_exports as nodes,