@markdoc/markdoc 0.3.0 → 0.3.1

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