@markdoc/markdoc 0.3.0 → 0.3.2

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
@@ -6659,14 +6659,14 @@ Tag.isTag = (tag) => {
6659
6659
 
6660
6660
  // src/schema-types/class.ts
6661
6661
  var Class = class {
6662
- validate(value) {
6662
+ validate(value, _config, key) {
6663
6663
  if (typeof value === "string" || typeof value === "object")
6664
6664
  return [];
6665
6665
  return [
6666
6666
  {
6667
6667
  id: "attribute-type-invalid",
6668
6668
  level: "error",
6669
- message: `Attribute 'class' must be type 'string | object'`
6669
+ message: `Attribute '${key}' must be type 'string | object'`
6670
6670
  }
6671
6671
  ];
6672
6672
  }
@@ -6870,6 +6870,8 @@ var transformer_default = {
6870
6870
  }
6871
6871
  if (schema.slots) {
6872
6872
  for (const [key, slot2] of Object.entries(schema.slots)) {
6873
+ if (slot2.render === false)
6874
+ continue;
6873
6875
  const name = typeof slot2.render === "string" ? slot2.render : key;
6874
6876
  if (node2.slots[key])
6875
6877
  output[name] = this.node(node2.slots[key], config);
@@ -6988,6 +6990,9 @@ function* formatTableRow(items) {
6988
6990
  yield `| ${items.join(" | ")} |`;
6989
6991
  }
6990
6992
  function formatScalar(v) {
6993
+ if (v === void 0) {
6994
+ return void 0;
6995
+ }
6991
6996
  if (ast_default.isAst(v)) {
6992
6997
  return format(v);
6993
6998
  }
@@ -7003,13 +7008,16 @@ function formatScalar(v) {
7003
7008
  return JSON.stringify(v);
7004
7009
  }
7005
7010
  function formatAnnotationValue(a) {
7011
+ const formattedValue = formatScalar(a.value);
7012
+ if (formattedValue === void 0)
7013
+ return void 0;
7006
7014
  if (a.name === "primary")
7007
- return formatScalar(a.value);
7015
+ return formattedValue;
7008
7016
  if (a.name === "id" && typeof a.value === "string" && isIdentifier(a.value))
7009
7017
  return "#" + a.value;
7010
7018
  if (a.type === "class" && isIdentifier(a.name))
7011
7019
  return "." + a.name;
7012
- return `${a.name}=${formatScalar(a.value)}`;
7020
+ return `${a.name}=${formattedValue}`;
7013
7021
  }
7014
7022
  function* formatAttributes(n) {
7015
7023
  for (const [key, value] of Object.entries(n.attributes)) {
@@ -7170,7 +7178,8 @@ function* formatNode(n, o = {}) {
7170
7178
  yield indent;
7171
7179
  }
7172
7180
  const open = OPEN + SPACE;
7173
- const tag = [open + n.tag, ...formatAttributes(n)];
7181
+ const attributes = [...formatAttributes(n)].filter((v) => v !== void 0);
7182
+ const tag = [open + n.tag, ...attributes];
7174
7183
  const inlineTag = tag.join(SPACE);
7175
7184
  const isLongTagOpening = inlineTag.length + open.length * 2 > (o.maxTagOpeningWidth || MAX_TAG_OPENING_WIDTH);
7176
7185
  yield (!n.inline && isLongTagOpening ? tag.join(NL + SPACE.repeat(open.length) + indent) : inlineTag) + SPACE + (n.children.length ? "" : "/") + CLOSE;
@@ -7187,9 +7196,13 @@ function* formatNode(n, o = {}) {
7187
7196
  break;
7188
7197
  }
7189
7198
  case "list": {
7199
+ const isLoose = n.children.some((n2) => n2.children.some((c) => c.type === "paragraph"));
7190
7200
  for (let i = 0; i < n.children.length; i++) {
7191
7201
  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();
7202
+ let d = format(n.children[i], increment(no, prefix.length + 1));
7203
+ if (!isLoose || i === n.children.length - 1) {
7204
+ d = d.trim();
7205
+ }
7193
7206
  yield NL + indent + prefix + " " + d;
7194
7207
  }
7195
7208
  yield NL;
@@ -7482,9 +7495,15 @@ function annotate(node2, attributes) {
7482
7495
  for (const attribute of attributes) {
7483
7496
  node2.annotations.push(attribute);
7484
7497
  const { name, value, type } = attribute;
7485
- if (type === "attribute")
7498
+ if (type === "attribute") {
7499
+ if (node2.attributes[name] !== void 0)
7500
+ node2.errors.push({
7501
+ id: "duplicate-attribute",
7502
+ level: "warning",
7503
+ message: `Attribute '${name}' already set`
7504
+ });
7486
7505
  node2.attributes[name] = value;
7487
- else if (type === "class")
7506
+ } else if (type === "class")
7488
7507
  if (node2.attributes.class)
7489
7508
  node2.attributes.class[name] = value;
7490
7509
  else
@@ -7609,7 +7628,8 @@ function handleToken(token, nodes, file, handleSlots, inlineParent) {
7609
7628
  nodes.push(node2);
7610
7629
  if (!Array.isArray(token.children))
7611
7630
  return;
7612
- inlineParent = parent;
7631
+ if (node2.type === "inline")
7632
+ inlineParent = parent;
7613
7633
  nodes.push(node2);
7614
7634
  const isLeafNode = typeName === "image";
7615
7635
  if (!isLeafNode) {
@@ -7781,16 +7801,18 @@ var td = {
7781
7801
  "hr"
7782
7802
  ],
7783
7803
  attributes: {
7784
- colspan: { type: Number },
7785
- rowspan: { type: Number },
7786
- align: { type: String }
7804
+ align: { type: String },
7805
+ colspan: { type: Number, render: "colSpan" },
7806
+ rowspan: { type: Number, render: "rowSpan" }
7787
7807
  }
7788
7808
  };
7789
7809
  var th = {
7790
7810
  render: "th",
7791
7811
  attributes: {
7792
7812
  width: { type: Number },
7793
- align: { type: String }
7813
+ align: { type: String },
7814
+ colspan: { type: Number, render: "colSpan" },
7815
+ rowspan: { type: Number, render: "rowSpan" }
7794
7816
  }
7795
7817
  };
7796
7818
  var tr = {
@@ -7911,7 +7933,7 @@ function render(node2) {
7911
7933
  return render(children);
7912
7934
  let output = `<${name}`;
7913
7935
  for (const [k, v] of Object.entries(attributes ?? {}))
7914
- output += ` ${k}="${escapeHtml(String(v))}"`;
7936
+ output += ` ${k.toLowerCase()}="${escapeHtml(String(v))}"`;
7915
7937
  output += ">";
7916
7938
  if (voidElements.has(name))
7917
7939
  return output;
@@ -8111,7 +8133,7 @@ function block(state, startLine, endLine, silent) {
8111
8133
  return false;
8112
8134
  const contentStart = start + OPEN.length;
8113
8135
  const content = state.src.slice(contentStart, tagEnd).trim();
8114
- const lines = content.split("\n").length;
8136
+ const lines = state.src.slice(start, tagEnd + CLOSE.length).split("\n").length;
8115
8137
  if (content[0] === "$")
8116
8138
  return false;
8117
8139
  if (silent)
@@ -8264,7 +8286,7 @@ var TypeMappings = {
8264
8286
  Object,
8265
8287
  Boolean
8266
8288
  };
8267
- function validateType(type, value, config) {
8289
+ function validateType(type, value, config, key) {
8268
8290
  if (!type)
8269
8291
  return true;
8270
8292
  if (ast_default.isFunction(value) && config.validation?.validateFunctions) {
@@ -8274,13 +8296,13 @@ function validateType(type, value, config) {
8274
8296
  if (ast_default.isAst(value))
8275
8297
  return true;
8276
8298
  if (Array.isArray(type))
8277
- return type.some((t) => validateType(t, value, config));
8299
+ return type.some((t) => validateType(t, value, config, key));
8278
8300
  if (typeof type === "string")
8279
8301
  type = TypeMappings[type];
8280
8302
  if (typeof type === "function") {
8281
8303
  const instance = new type();
8282
8304
  if (instance.validate) {
8283
- return instance.validate(value, config);
8305
+ return instance.validate(value, config, key);
8284
8306
  }
8285
8307
  }
8286
8308
  return value != null && value.constructor === type;
@@ -8319,7 +8341,7 @@ function validateFunction(fn, config) {
8319
8341
  if (ast_default.isAst(value) && !ast_default.isFunction(value))
8320
8342
  continue;
8321
8343
  if (param.type) {
8322
- const valid = validateType(param.type, value, config);
8344
+ const valid = validateType(param.type, value, config, key);
8323
8345
  if (valid === false) {
8324
8346
  errors.push({
8325
8347
  id: "parameter-type-invalid",
@@ -8419,7 +8441,7 @@ function validator(node2, config) {
8419
8441
  }
8420
8442
  value = value;
8421
8443
  if (type) {
8422
- const valid = validateType(type, value, config);
8444
+ const valid = validateType(type, value, config, key);
8423
8445
  if (valid === false) {
8424
8446
  errors.push({
8425
8447
  id: "attribute-type-invalid",
@@ -8446,7 +8468,7 @@ function validator(node2, config) {
8446
8468
  message: `Attribute '${key}' must match ${matches}. Got '${value}' instead.`
8447
8469
  });
8448
8470
  if (typeof attrib.validate === "function") {
8449
- const attribErrors = attrib.validate(value, config);
8471
+ const attribErrors = attrib.validate(value, config, key);
8450
8472
  if (Array.isArray(attribErrors))
8451
8473
  errors.push(...attribErrors);
8452
8474
  }
@@ -8484,6 +8506,29 @@ function validator(node2, config) {
8484
8506
  }
8485
8507
  return errors;
8486
8508
  }
8509
+ function* walkWithParents(node2, parents = []) {
8510
+ yield [node2, parents];
8511
+ for (const child of [...Object.values(node2.slots), ...node2.children])
8512
+ yield* walkWithParents(child, [...parents, node2]);
8513
+ }
8514
+ function validateTree(content, config) {
8515
+ const output = [...walkWithParents(content)].map(([node2, parents]) => {
8516
+ const { type, lines, location } = node2;
8517
+ const updatedConfig = {
8518
+ ...config,
8519
+ validation: { ...config.validation, parents }
8520
+ };
8521
+ const errors = validator(node2, updatedConfig);
8522
+ if (isPromise(errors)) {
8523
+ return errors.then((e) => e.map((error2) => ({ type, lines, location, error: error2 })));
8524
+ }
8525
+ return errors.map((error2) => ({ type, lines, location, error: error2 }));
8526
+ });
8527
+ if (output.some(isPromise)) {
8528
+ return Promise.all(output).then((o) => o.flat());
8529
+ }
8530
+ return output.flat();
8531
+ }
8487
8532
 
8488
8533
  // index.ts
8489
8534
  var tokenizer = new Tokenizer();
@@ -8523,18 +8568,7 @@ function transform2(nodes, options) {
8523
8568
  }
8524
8569
  function validate(content, options) {
8525
8570
  const config = mergeConfig(options);
8526
- const output = [content, ...content.walk()].map((node2) => {
8527
- const { type, lines, location } = node2;
8528
- const errors = validator(node2, config);
8529
- if (isPromise(errors)) {
8530
- return errors.then((e) => e.map((error2) => ({ type, lines, location, error: error2 })));
8531
- }
8532
- return errors.map((error2) => ({ type, lines, location, error: error2 }));
8533
- });
8534
- if (output.some(isPromise)) {
8535
- return Promise.all(output).then((o) => o.flat());
8536
- }
8537
- return output.flat();
8571
+ return validateTree(content, config);
8538
8572
  }
8539
8573
  function createElement(name, attributes = {}, ...children) {
8540
8574
  return { name, attributes, children };