@markdoc/markdoc 0.2.1 → 0.3.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.mjs CHANGED
@@ -6868,6 +6868,13 @@ var transformer_default = {
6868
6868
  continue;
6869
6869
  output[name] = value;
6870
6870
  }
6871
+ if (schema.slots) {
6872
+ for (const [key, slot2] of Object.entries(schema.slots)) {
6873
+ const name = typeof slot2.render === "string" ? slot2.render : key;
6874
+ if (node2.slots[key])
6875
+ output[name] = this.node(node2.slots[key], config);
6876
+ }
6877
+ }
6871
6878
  return output;
6872
6879
  },
6873
6880
  children(node2, config = {}) {
@@ -6904,9 +6911,10 @@ var Node = class {
6904
6911
  this.type = type;
6905
6912
  this.tag = tag;
6906
6913
  this.annotations = [];
6914
+ this.slots = {};
6907
6915
  }
6908
6916
  *walk() {
6909
- for (const child of this.children) {
6917
+ for (const child of [...Object.values(this.slots), ...this.children]) {
6910
6918
  yield child;
6911
6919
  yield* child.walk();
6912
6920
  }
@@ -6959,9 +6967,10 @@ var ast_default = {
6959
6967
  var SPACE = " ";
6960
6968
  var SEP = ", ";
6961
6969
  var NL = "\n";
6962
- var OL = "1. ";
6963
- var UL = "- ";
6970
+ var OL = ".";
6971
+ var UL = "-";
6964
6972
  var MAX_TAG_OPENING_WIDTH = 80;
6973
+ var WRAPPING_TYPES = ["strong", "em", "s"];
6965
6974
  var max = (a, b) => Math.max(a, b);
6966
6975
  var increment = (o, n = 2) => ({
6967
6976
  ...o,
@@ -6983,7 +6992,7 @@ function formatScalar(v) {
6983
6992
  return format(v);
6984
6993
  }
6985
6994
  if (v === null) {
6986
- return "";
6995
+ return "null";
6987
6996
  }
6988
6997
  if (Array.isArray(v)) {
6989
6998
  return "[" + v.map(formatScalar).join(SEP) + "]";
@@ -7111,18 +7120,22 @@ function* formatNode(n, o = {}) {
7111
7120
  }
7112
7121
  case "text": {
7113
7122
  const { content } = n.attributes;
7114
- if (ast_default.isAst(content))
7123
+ if (ast_default.isAst(content)) {
7115
7124
  yield OPEN + SPACE;
7116
- yield* typeof content === "string" ? escapeMarkdownCharacters(content, /[_*~]/g) : formatValue(content, no);
7117
- if (ast_default.isAst(content))
7125
+ yield* formatValue(content, no);
7118
7126
  yield SPACE + CLOSE;
7127
+ } else {
7128
+ if (o.parent && WRAPPING_TYPES.includes(o.parent.type)) {
7129
+ yield* escapeMarkdownCharacters(content, /[*_~]/g);
7130
+ } else {
7131
+ yield* escapeMarkdownCharacters(content, /^[*>#]/);
7132
+ }
7133
+ }
7119
7134
  break;
7120
7135
  }
7121
7136
  case "blockquote": {
7122
- yield NL;
7123
- yield indent;
7124
- yield "> ";
7125
- yield* trimStart(formatChildren(n, no));
7137
+ const prefix = ">" + SPACE;
7138
+ yield n.children.map((child) => format(child, no).trimStart()).map((d) => NL + indent + prefix + d).join(indent + prefix);
7126
7139
  break;
7127
7140
  }
7128
7141
  case "hr": {
@@ -7174,10 +7187,10 @@ function* formatNode(n, o = {}) {
7174
7187
  break;
7175
7188
  }
7176
7189
  case "list": {
7177
- const prefix = n.attributes.ordered ? OL : UL;
7178
- for (const child of n.children) {
7179
- const d = format(child, increment(no, prefix.length)).trim();
7180
- yield NL + indent + prefix + d;
7190
+ for (let i = 0; i < n.children.length; i++) {
7191
+ const prefix = n.attributes.ordered ? `${i === 0 ? n.attributes.start ?? "1" : "1"}${n.attributes.marker ?? OL}` : n.attributes.marker ?? UL;
7192
+ const d = format(n.children[i], increment(no, prefix.length + 1)).trim();
7193
+ yield NL + indent + prefix + " " + d;
7181
7194
  }
7182
7195
  yield NL;
7183
7196
  break;
@@ -7191,15 +7204,15 @@ function* formatNode(n, o = {}) {
7191
7204
  break;
7192
7205
  }
7193
7206
  case "strong": {
7194
- yield "**";
7207
+ yield n.attributes.marker ?? "**";
7195
7208
  yield* formatInline(formatChildren(n, no));
7196
- yield "**";
7209
+ yield n.attributes.marker ?? "**";
7197
7210
  break;
7198
7211
  }
7199
7212
  case "em": {
7200
- yield "_";
7213
+ yield n.attributes.marker ?? "*";
7201
7214
  yield* formatInline(formatChildren(n, no));
7202
- yield "_";
7215
+ yield n.attributes.marker ?? "*";
7203
7216
  break;
7204
7217
  }
7205
7218
  case "code": {
@@ -7240,7 +7253,7 @@ function* formatNode(n, o = {}) {
7240
7253
  yield indent + "---";
7241
7254
  }
7242
7255
  for (const d of row) {
7243
- yield NL + indent + UL + d;
7256
+ yield NL + indent + UL + " " + d;
7244
7257
  }
7245
7258
  }
7246
7259
  }
@@ -7482,8 +7495,11 @@ function handleAttrs(token, type) {
7482
7495
  switch (type) {
7483
7496
  case "heading":
7484
7497
  return { level: Number(token.tag.replace("h", "")) };
7485
- case "list":
7486
- return { ordered: token.type.startsWith("ordered") };
7498
+ case "list": {
7499
+ const attrs = token.attrs ? Object.fromEntries(token.attrs) : void 0;
7500
+ const ordered = token.type.startsWith("ordered");
7501
+ return ordered && attrs?.start ? { ordered: true, start: attrs.start, marker: token.markup } : { ordered, marker: token.markup };
7502
+ }
7487
7503
  case "link": {
7488
7504
  const attrs = Object.fromEntries(token.attrs);
7489
7505
  return attrs.title ? { href: attrs.href, title: attrs.title } : { href: attrs.href };
@@ -7492,6 +7508,9 @@ function handleAttrs(token, type) {
7492
7508
  const attrs = Object.fromEntries(token.attrs);
7493
7509
  return attrs.title ? { alt: token.content, src: attrs.src, title: attrs.title } : { alt: token.content, src: attrs.src };
7494
7510
  }
7511
+ case "em":
7512
+ case "strong":
7513
+ return { marker: token.markup };
7495
7514
  case "text":
7496
7515
  case "code":
7497
7516
  case "comment":
@@ -7524,7 +7543,7 @@ function handleAttrs(token, type) {
7524
7543
  return {};
7525
7544
  }
7526
7545
  }
7527
- function handleToken(token, nodes, file, inlineParent) {
7546
+ function handleToken(token, nodes, file, handleSlots, inlineParent) {
7528
7547
  if (token.type === "frontmatter") {
7529
7548
  nodes[0].attributes.frontmatter = token.content;
7530
7549
  return;
@@ -7551,7 +7570,7 @@ function handleToken(token, nodes, file, inlineParent) {
7551
7570
  errors.push({ id: "parse-error", level: "critical", message, location });
7552
7571
  }
7553
7572
  if (token.nesting < 0) {
7554
- if (parent.type === typeName) {
7573
+ if (parent.type === typeName && parent.tag === tag) {
7555
7574
  if (parent.lines && token.map)
7556
7575
  parent.lines.push(...token.map);
7557
7576
  return nodes.pop();
@@ -7582,7 +7601,10 @@ function handleToken(token, nodes, file, inlineParent) {
7582
7601
  node2.inline = true;
7583
7602
  if (attributes && ["tag", "fence", "image"].includes(typeName))
7584
7603
  annotate(node2, attributes);
7585
- parent.push(node2);
7604
+ if (handleSlots && tag === "slot" && typeof node2.attributes.primary === "string")
7605
+ parent.slots[node2.attributes.primary] = node2;
7606
+ else
7607
+ parent.push(node2);
7586
7608
  if (token.nesting > 0)
7587
7609
  nodes.push(node2);
7588
7610
  if (!Array.isArray(token.children))
@@ -7592,15 +7614,17 @@ function handleToken(token, nodes, file, inlineParent) {
7592
7614
  const isLeafNode = typeName === "image";
7593
7615
  if (!isLeafNode) {
7594
7616
  for (const child of token.children)
7595
- handleToken(child, nodes, file, inlineParent);
7617
+ handleToken(child, nodes, file, handleSlots, inlineParent);
7596
7618
  }
7597
7619
  nodes.pop();
7598
7620
  }
7599
- function parser(tokens, file) {
7621
+ function parser(tokens, args) {
7600
7622
  const doc = new Node("document");
7601
7623
  const nodes = [doc];
7624
+ if (typeof args === "string")
7625
+ args = { file: args };
7602
7626
  for (const token of tokens)
7603
- handleToken(token, nodes, file);
7627
+ handleToken(token, nodes, args?.file, args?.slots);
7604
7628
  if (nodes.length > 1)
7605
7629
  for (const node2 of nodes.slice(1))
7606
7630
  node2.errors.push({
@@ -7728,7 +7752,9 @@ var item = {
7728
7752
  var list = {
7729
7753
  children: ["item"],
7730
7754
  attributes: {
7731
- ordered: { type: Boolean, render: false, required: true }
7755
+ ordered: { type: Boolean, render: false, required: true },
7756
+ start: { type: Number },
7757
+ marker: { type: String, render: false }
7732
7758
  },
7733
7759
  transform(node2, config) {
7734
7760
  return new Tag(node2.attributes.ordered ? "ol" : "ul", node2.transformAttributes(config), node2.transformChildren(config));
@@ -7781,11 +7807,17 @@ var thead = {
7781
7807
  };
7782
7808
  var strong = {
7783
7809
  render: "strong",
7784
- children: ["em", "s", "link", "code", "text", "tag"]
7810
+ children: ["em", "s", "link", "code", "text", "tag"],
7811
+ attributes: {
7812
+ marker: { type: String, render: false }
7813
+ }
7785
7814
  };
7786
7815
  var em = {
7787
7816
  render: "em",
7788
- children: ["strong", "s", "link", "code", "text", "tag"]
7817
+ children: ["strong", "s", "link", "code", "text", "tag"],
7818
+ attributes: {
7819
+ marker: { type: String, render: false }
7820
+ }
7789
7821
  };
7790
7822
  var s = {
7791
7823
  render: "s",
@@ -8021,11 +8053,19 @@ var table2 = {
8021
8053
  inline: false
8022
8054
  };
8023
8055
 
8056
+ // src/tags/slot.ts
8057
+ var slot = {
8058
+ attributes: {
8059
+ primary: { type: String, required: true }
8060
+ }
8061
+ };
8062
+
8024
8063
  // src/tags/index.ts
8025
8064
  var tags_default = {
8026
- if: tagIf,
8027
8065
  else: tagElse,
8066
+ if: tagIf,
8028
8067
  partial,
8068
+ slot,
8029
8069
  table: table2
8030
8070
  };
8031
8071
 
@@ -8301,6 +8341,12 @@ function validateFunction(fn, config) {
8301
8341
  });
8302
8342
  return errors;
8303
8343
  }
8344
+ function displayMatches(matches, n) {
8345
+ if (matches.length <= n)
8346
+ return JSON.stringify(matches);
8347
+ const items = matches.slice(0, n).map((item2) => JSON.stringify(item2));
8348
+ return `[${items.join(",")}, ... ${matches.length - n} more]`;
8349
+ }
8304
8350
  function validator(node2, config) {
8305
8351
  const schema = node2.findSchema(config);
8306
8352
  const errors = [...node2.errors || []];
@@ -8328,6 +8374,15 @@ function validator(node2, config) {
8328
8374
  ...globalAttributes,
8329
8375
  ...schema.attributes
8330
8376
  };
8377
+ for (const key of Object.keys(node2.slots)) {
8378
+ const slot2 = schema.slots?.[key];
8379
+ if (!slot2)
8380
+ errors.push({
8381
+ id: "slot-undefined",
8382
+ level: "error",
8383
+ message: `Invalid slot: '${key}'`
8384
+ });
8385
+ }
8331
8386
  for (let [key, value] of Object.entries(node2.attributes)) {
8332
8387
  const attrib = attributes[key];
8333
8388
  if (!attrib) {
@@ -8382,7 +8437,7 @@ function validator(node2, config) {
8382
8437
  errors.push({
8383
8438
  id: "attribute-value-invalid",
8384
8439
  level: errorLevel || "error",
8385
- message: `Attribute '${key}' must match one of ${JSON.stringify(matches)}. Got '${value}' instead.`
8440
+ message: `Attribute '${key}' must match one of ${displayMatches(matches, 8)}. Got '${value}' instead.`
8386
8441
  });
8387
8442
  if (matches instanceof RegExp && !matches.test(value))
8388
8443
  errors.push({
@@ -8390,6 +8445,11 @@ function validator(node2, config) {
8390
8445
  level: errorLevel || "error",
8391
8446
  message: `Attribute '${key}' must match ${matches}. Got '${value}' instead.`
8392
8447
  });
8448
+ if (typeof attrib.validate === "function") {
8449
+ const attribErrors = attrib.validate(value, config);
8450
+ if (Array.isArray(attribErrors))
8451
+ errors.push(...attribErrors);
8452
+ }
8393
8453
  }
8394
8454
  for (const [key, { required }] of Object.entries(attributes))
8395
8455
  if (required && node2.attributes[key] === void 0)
@@ -8398,6 +8458,15 @@ function validator(node2, config) {
8398
8458
  level: "error",
8399
8459
  message: `Missing required attribute: '${key}'`
8400
8460
  });
8461
+ if (schema.slots) {
8462
+ for (const [key, { required }] of Object.entries(schema.slots))
8463
+ if (required && node2.slots[key] === void 0)
8464
+ errors.push({
8465
+ id: "slot-missing-required",
8466
+ level: "error",
8467
+ message: `Missing required slot: '${key}'`
8468
+ });
8469
+ }
8401
8470
  for (const { type } of node2.children) {
8402
8471
  if (schema.children && type !== "error" && !schema.children.includes(type))
8403
8472
  errors.push({
@@ -8435,10 +8504,10 @@ function mergeConfig(config = {}) {
8435
8504
  }
8436
8505
  };
8437
8506
  }
8438
- function parse3(content, file) {
8507
+ function parse3(content, args) {
8439
8508
  if (typeof content === "string")
8440
8509
  content = tokenizer.tokenize(content);
8441
- return parser(content, file);
8510
+ return parser(content, args);
8442
8511
  }
8443
8512
  function resolve2(content, config) {
8444
8513
  if (Array.isArray(content))