@markdoc/markdoc 0.2.2 → 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,8 +6967,8 @@ 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;
6965
6973
  var WRAPPING_TYPES = ["strong", "em", "s"];
6966
6974
  var max = (a, b) => Math.max(a, b);
@@ -7179,10 +7187,10 @@ function* formatNode(n, o = {}) {
7179
7187
  break;
7180
7188
  }
7181
7189
  case "list": {
7182
- const prefix = n.attributes.ordered ? OL : UL;
7183
- for (const child of n.children) {
7184
- const d = format(child, increment(no, prefix.length)).trim();
7185
- 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;
7186
7194
  }
7187
7195
  yield NL;
7188
7196
  break;
@@ -7196,15 +7204,15 @@ function* formatNode(n, o = {}) {
7196
7204
  break;
7197
7205
  }
7198
7206
  case "strong": {
7199
- yield "**";
7207
+ yield n.attributes.marker ?? "**";
7200
7208
  yield* formatInline(formatChildren(n, no));
7201
- yield "**";
7209
+ yield n.attributes.marker ?? "**";
7202
7210
  break;
7203
7211
  }
7204
7212
  case "em": {
7205
- yield "_";
7213
+ yield n.attributes.marker ?? "*";
7206
7214
  yield* formatInline(formatChildren(n, no));
7207
- yield "_";
7215
+ yield n.attributes.marker ?? "*";
7208
7216
  break;
7209
7217
  }
7210
7218
  case "code": {
@@ -7245,7 +7253,7 @@ function* formatNode(n, o = {}) {
7245
7253
  yield indent + "---";
7246
7254
  }
7247
7255
  for (const d of row) {
7248
- yield NL + indent + UL + d;
7256
+ yield NL + indent + UL + " " + d;
7249
7257
  }
7250
7258
  }
7251
7259
  }
@@ -7487,8 +7495,11 @@ function handleAttrs(token, type) {
7487
7495
  switch (type) {
7488
7496
  case "heading":
7489
7497
  return { level: Number(token.tag.replace("h", "")) };
7490
- case "list":
7491
- 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
+ }
7492
7503
  case "link": {
7493
7504
  const attrs = Object.fromEntries(token.attrs);
7494
7505
  return attrs.title ? { href: attrs.href, title: attrs.title } : { href: attrs.href };
@@ -7497,6 +7508,9 @@ function handleAttrs(token, type) {
7497
7508
  const attrs = Object.fromEntries(token.attrs);
7498
7509
  return attrs.title ? { alt: token.content, src: attrs.src, title: attrs.title } : { alt: token.content, src: attrs.src };
7499
7510
  }
7511
+ case "em":
7512
+ case "strong":
7513
+ return { marker: token.markup };
7500
7514
  case "text":
7501
7515
  case "code":
7502
7516
  case "comment":
@@ -7529,7 +7543,7 @@ function handleAttrs(token, type) {
7529
7543
  return {};
7530
7544
  }
7531
7545
  }
7532
- function handleToken(token, nodes, file, inlineParent) {
7546
+ function handleToken(token, nodes, file, handleSlots, inlineParent) {
7533
7547
  if (token.type === "frontmatter") {
7534
7548
  nodes[0].attributes.frontmatter = token.content;
7535
7549
  return;
@@ -7556,7 +7570,7 @@ function handleToken(token, nodes, file, inlineParent) {
7556
7570
  errors.push({ id: "parse-error", level: "critical", message, location });
7557
7571
  }
7558
7572
  if (token.nesting < 0) {
7559
- if (parent.type === typeName) {
7573
+ if (parent.type === typeName && parent.tag === tag) {
7560
7574
  if (parent.lines && token.map)
7561
7575
  parent.lines.push(...token.map);
7562
7576
  return nodes.pop();
@@ -7587,7 +7601,10 @@ function handleToken(token, nodes, file, inlineParent) {
7587
7601
  node2.inline = true;
7588
7602
  if (attributes && ["tag", "fence", "image"].includes(typeName))
7589
7603
  annotate(node2, attributes);
7590
- 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);
7591
7608
  if (token.nesting > 0)
7592
7609
  nodes.push(node2);
7593
7610
  if (!Array.isArray(token.children))
@@ -7597,15 +7614,17 @@ function handleToken(token, nodes, file, inlineParent) {
7597
7614
  const isLeafNode = typeName === "image";
7598
7615
  if (!isLeafNode) {
7599
7616
  for (const child of token.children)
7600
- handleToken(child, nodes, file, inlineParent);
7617
+ handleToken(child, nodes, file, handleSlots, inlineParent);
7601
7618
  }
7602
7619
  nodes.pop();
7603
7620
  }
7604
- function parser(tokens, file) {
7621
+ function parser(tokens, args) {
7605
7622
  const doc = new Node("document");
7606
7623
  const nodes = [doc];
7624
+ if (typeof args === "string")
7625
+ args = { file: args };
7607
7626
  for (const token of tokens)
7608
- handleToken(token, nodes, file);
7627
+ handleToken(token, nodes, args?.file, args?.slots);
7609
7628
  if (nodes.length > 1)
7610
7629
  for (const node2 of nodes.slice(1))
7611
7630
  node2.errors.push({
@@ -7733,7 +7752,9 @@ var item = {
7733
7752
  var list = {
7734
7753
  children: ["item"],
7735
7754
  attributes: {
7736
- 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 }
7737
7758
  },
7738
7759
  transform(node2, config) {
7739
7760
  return new Tag(node2.attributes.ordered ? "ol" : "ul", node2.transformAttributes(config), node2.transformChildren(config));
@@ -7786,11 +7807,17 @@ var thead = {
7786
7807
  };
7787
7808
  var strong = {
7788
7809
  render: "strong",
7789
- children: ["em", "s", "link", "code", "text", "tag"]
7810
+ children: ["em", "s", "link", "code", "text", "tag"],
7811
+ attributes: {
7812
+ marker: { type: String, render: false }
7813
+ }
7790
7814
  };
7791
7815
  var em = {
7792
7816
  render: "em",
7793
- children: ["strong", "s", "link", "code", "text", "tag"]
7817
+ children: ["strong", "s", "link", "code", "text", "tag"],
7818
+ attributes: {
7819
+ marker: { type: String, render: false }
7820
+ }
7794
7821
  };
7795
7822
  var s = {
7796
7823
  render: "s",
@@ -8026,11 +8053,19 @@ var table2 = {
8026
8053
  inline: false
8027
8054
  };
8028
8055
 
8056
+ // src/tags/slot.ts
8057
+ var slot = {
8058
+ attributes: {
8059
+ primary: { type: String, required: true }
8060
+ }
8061
+ };
8062
+
8029
8063
  // src/tags/index.ts
8030
8064
  var tags_default = {
8031
- if: tagIf,
8032
8065
  else: tagElse,
8066
+ if: tagIf,
8033
8067
  partial,
8068
+ slot,
8034
8069
  table: table2
8035
8070
  };
8036
8071
 
@@ -8306,6 +8341,12 @@ function validateFunction(fn, config) {
8306
8341
  });
8307
8342
  return errors;
8308
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
+ }
8309
8350
  function validator(node2, config) {
8310
8351
  const schema = node2.findSchema(config);
8311
8352
  const errors = [...node2.errors || []];
@@ -8333,6 +8374,15 @@ function validator(node2, config) {
8333
8374
  ...globalAttributes,
8334
8375
  ...schema.attributes
8335
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
+ }
8336
8386
  for (let [key, value] of Object.entries(node2.attributes)) {
8337
8387
  const attrib = attributes[key];
8338
8388
  if (!attrib) {
@@ -8387,7 +8437,7 @@ function validator(node2, config) {
8387
8437
  errors.push({
8388
8438
  id: "attribute-value-invalid",
8389
8439
  level: errorLevel || "error",
8390
- 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.`
8391
8441
  });
8392
8442
  if (matches instanceof RegExp && !matches.test(value))
8393
8443
  errors.push({
@@ -8395,6 +8445,11 @@ function validator(node2, config) {
8395
8445
  level: errorLevel || "error",
8396
8446
  message: `Attribute '${key}' must match ${matches}. Got '${value}' instead.`
8397
8447
  });
8448
+ if (typeof attrib.validate === "function") {
8449
+ const attribErrors = attrib.validate(value, config);
8450
+ if (Array.isArray(attribErrors))
8451
+ errors.push(...attribErrors);
8452
+ }
8398
8453
  }
8399
8454
  for (const [key, { required }] of Object.entries(attributes))
8400
8455
  if (required && node2.attributes[key] === void 0)
@@ -8403,6 +8458,15 @@ function validator(node2, config) {
8403
8458
  level: "error",
8404
8459
  message: `Missing required attribute: '${key}'`
8405
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
+ }
8406
8470
  for (const { type } of node2.children) {
8407
8471
  if (schema.children && type !== "error" && !schema.children.includes(type))
8408
8472
  errors.push({
@@ -8440,10 +8504,10 @@ function mergeConfig(config = {}) {
8440
8504
  }
8441
8505
  };
8442
8506
  }
8443
- function parse3(content, file) {
8507
+ function parse3(content, args) {
8444
8508
  if (typeof content === "string")
8445
8509
  content = tokenizer.tokenize(content);
8446
- return parser(content, file);
8510
+ return parser(content, args);
8447
8511
  }
8448
8512
  function resolve2(content, config) {
8449
8513
  if (Array.isArray(content))