@markdoc/markdoc 0.1.5 → 0.1.6

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
@@ -6935,6 +6935,7 @@ var ast_default = {
6935
6935
 
6936
6936
  // src/formatter.ts
6937
6937
  var SPACE = " ";
6938
+ var SEP = ", ";
6938
6939
  var NL = "\n";
6939
6940
  var OL = "1. ";
6940
6941
  var UL = "- ";
@@ -6944,63 +6945,63 @@ var increment = (o, n = 2) => ({
6944
6945
  ...o,
6945
6946
  indent: (o.indent || 0) + n
6946
6947
  });
6947
- function* renderChildren(a, options) {
6948
+ function* formatChildren(a, options) {
6948
6949
  for (const child of a.children) {
6949
- yield* render(child, options);
6950
+ yield* formatValue(child, options);
6950
6951
  }
6951
6952
  }
6952
- function* renderTableRow(items) {
6953
+ function* formatTableRow(items) {
6953
6954
  yield `| ${items.join(" | ")} |`;
6954
6955
  }
6955
- function renderValue(v) {
6956
- if (v === null) {
6957
- return "";
6958
- }
6956
+ function formatScalar(v) {
6959
6957
  if (ast_default.isAst(v)) {
6960
6958
  return format(v);
6961
6959
  }
6960
+ if (v === null) {
6961
+ return "";
6962
+ }
6962
6963
  if (Array.isArray(v)) {
6963
- return "[" + v.map(renderValue).join(", ") + "]";
6964
+ return "[" + v.map(formatScalar).join(SEP) + "]";
6964
6965
  }
6965
6966
  if (typeof v === "object") {
6966
- return "{" + Object.entries(v).map(([key, value]) => `${key}: ${renderValue(value)}`).join(", ") + "}";
6967
+ return "{" + Object.entries(v).map(([key, value]) => `${key}: ${formatScalar(value)}`).join(SEP) + "}";
6967
6968
  }
6968
6969
  return JSON.stringify(v);
6969
6970
  }
6970
- function renderAnnotationValue(a) {
6971
+ function formatAnnotationValue(a) {
6971
6972
  if (a.name === "primary")
6972
6973
  return a.value;
6973
6974
  if (a.name === "id" && typeof a.value === "string")
6974
6975
  return "#" + a.value;
6975
6976
  if (a.type === "class")
6976
6977
  return "." + a.name;
6977
- return `${a.name}=${renderValue(a.value)}`;
6978
+ return `${a.name}=${formatScalar(a.value)}`;
6978
6979
  }
6979
- function* renderAttributes(n) {
6980
+ function* formatAttributes(n) {
6980
6981
  for (const [key, value] of Object.entries(n.attributes)) {
6981
6982
  if (key === "class" && !ast_default.isAst(value))
6982
6983
  for (const name of Object.keys(value)) {
6983
- yield renderAnnotationValue({ type: "class", name, value });
6984
+ yield formatAnnotationValue({ type: "class", name, value });
6984
6985
  }
6985
6986
  else
6986
- yield renderAnnotationValue({ type: "attribute", name: key, value });
6987
+ yield formatAnnotationValue({ type: "attribute", name: key, value });
6987
6988
  }
6988
6989
  }
6989
- function* renderAnnotations(n) {
6990
+ function* formatAnnotations(n) {
6990
6991
  if (n.annotations.length) {
6991
6992
  yield OPEN + SPACE;
6992
- yield n.annotations.map(renderAnnotationValue).join(SPACE);
6993
+ yield n.annotations.map(formatAnnotationValue).join(SPACE);
6993
6994
  yield SPACE + CLOSE;
6994
6995
  }
6995
6996
  }
6996
- function* renderVariable(v) {
6997
+ function* formatVariable(v) {
6997
6998
  yield "$";
6998
6999
  yield v.path.join(".");
6999
7000
  }
7000
- function* renderFunction(f) {
7001
+ function* formatFunction(f) {
7001
7002
  yield f.name;
7002
7003
  yield "(";
7003
- yield Object.values(f.parameters).map(renderValue).join(", ");
7004
+ yield Object.values(f.parameters).map(formatScalar).join(SEP);
7004
7005
  yield ")";
7005
7006
  }
7006
7007
  function* trimStart(g) {
@@ -7011,7 +7012,7 @@ function* trimStart(g) {
7011
7012
  yield n;
7012
7013
  yield* g;
7013
7014
  }
7014
- function* renderNode(n, o = {}) {
7015
+ function* formatNode(n, o = {}) {
7015
7016
  const no = { ...o, parent: n };
7016
7017
  const indent = SPACE.repeat(no.indent || 0);
7017
7018
  switch (n.type) {
@@ -7019,7 +7020,7 @@ function* renderNode(n, o = {}) {
7019
7020
  if (n.attributes.frontmatter && n.attributes.frontmatter.length) {
7020
7021
  yield "---" + NL + n.attributes.frontmatter + NL + "---" + NL + NL;
7021
7022
  }
7022
- yield* trimStart(renderChildren(n, no));
7023
+ yield* trimStart(formatChildren(n, no));
7023
7024
  break;
7024
7025
  }
7025
7026
  case "heading": {
@@ -7027,36 +7028,36 @@ function* renderNode(n, o = {}) {
7027
7028
  yield indent;
7028
7029
  yield "#".repeat(n.attributes.level || 1);
7029
7030
  yield SPACE;
7030
- yield* renderChildren(n, no);
7031
- yield* renderAnnotations(n);
7031
+ yield* formatChildren(n, no);
7032
+ yield* formatAnnotations(n);
7032
7033
  yield NL;
7033
7034
  break;
7034
7035
  }
7035
7036
  case "paragraph": {
7036
7037
  yield NL;
7037
7038
  yield indent;
7038
- yield* renderChildren(n, no);
7039
- yield* renderAnnotations(n);
7039
+ yield* formatChildren(n, no);
7040
+ yield* formatAnnotations(n);
7040
7041
  yield NL;
7041
7042
  break;
7042
7043
  }
7043
7044
  case "inline": {
7044
- yield* renderChildren(n, no);
7045
+ yield* formatChildren(n, no);
7045
7046
  break;
7046
7047
  }
7047
7048
  case "link": {
7048
7049
  yield "[";
7049
- yield* renderChildren(n, no);
7050
+ yield* formatChildren(n, no);
7050
7051
  yield "]";
7051
7052
  yield "(";
7052
- yield* render(n.attributes.href, no);
7053
+ yield* formatValue(n.attributes.href, no);
7053
7054
  yield ")";
7054
7055
  break;
7055
7056
  }
7056
7057
  case "text": {
7057
7058
  if (ast_default.isAst(n.attributes.content))
7058
7059
  yield OPEN + SPACE;
7059
- yield* render(n.attributes.content, no);
7060
+ yield* formatValue(n.attributes.content, no);
7060
7061
  if (ast_default.isAst(n.attributes.content))
7061
7062
  yield SPACE + CLOSE;
7062
7063
  break;
@@ -7065,7 +7066,7 @@ function* renderNode(n, o = {}) {
7065
7066
  yield NL;
7066
7067
  yield indent;
7067
7068
  yield "> ";
7068
- yield* trimStart(renderChildren(n, no));
7069
+ yield* trimStart(formatChildren(n, no));
7069
7070
  break;
7070
7071
  }
7071
7072
  case "hr": {
@@ -7078,10 +7079,10 @@ function* renderNode(n, o = {}) {
7078
7079
  case "image": {
7079
7080
  yield "!";
7080
7081
  yield "[";
7081
- yield* render(n.attributes.alt, no);
7082
+ yield* formatValue(n.attributes.alt, no);
7082
7083
  yield "]";
7083
7084
  yield "(";
7084
- yield* render(n.attributes.src, no);
7085
+ yield* formatValue(n.attributes.src, no);
7085
7086
  yield ")";
7086
7087
  break;
7087
7088
  }
@@ -7092,10 +7093,10 @@ function* renderNode(n, o = {}) {
7092
7093
  yield (n.attributes.language || "").toLowerCase();
7093
7094
  if (n.annotations.length)
7094
7095
  yield SPACE;
7095
- yield* renderAnnotations(n);
7096
+ yield* formatAnnotations(n);
7096
7097
  yield NL;
7097
7098
  yield indent;
7098
- yield* renderChildren(n, no);
7099
+ yield* formatChildren(n, no);
7099
7100
  yield indent;
7100
7101
  yield "```";
7101
7102
  yield NL;
@@ -7107,16 +7108,16 @@ function* renderNode(n, o = {}) {
7107
7108
  yield indent;
7108
7109
  }
7109
7110
  const open = OPEN + SPACE;
7110
- const tag = [open + n.tag, ...renderAttributes(n)];
7111
+ const tag = [open + n.tag, ...formatAttributes(n)];
7111
7112
  const inlineTag = tag.join(SPACE);
7112
7113
  if (inlineTag.length + open.length * 2 > MAX_TAG_HEAD_LENGTH) {
7113
- yield tag.join("\n" + SPACE.repeat(open.length) + indent);
7114
+ yield tag.join(NL + SPACE.repeat(open.length) + indent);
7114
7115
  } else {
7115
7116
  yield inlineTag;
7116
7117
  }
7117
7118
  yield SPACE + (n.children.length ? "" : "/") + CLOSE;
7118
7119
  if (n.children.length) {
7119
- yield* renderChildren(n, no.allowIndentation ? increment(no) : no);
7120
+ yield* formatChildren(n, no.allowIndentation ? increment(no) : no);
7120
7121
  if (!n.inline) {
7121
7122
  yield indent;
7122
7123
  }
@@ -7130,48 +7131,46 @@ function* renderNode(n, o = {}) {
7130
7131
  break;
7131
7132
  }
7132
7133
  case "list": {
7134
+ const prefix = n.attributes.ordered ? OL : UL;
7133
7135
  yield NL;
7134
- for (let i = 0; i < n.children.length; i++) {
7135
- yield indent;
7136
- const prefix = n.attributes.ordered ? OL : UL;
7137
- yield prefix;
7138
- yield* render(n.children[i], increment(no, prefix.length));
7139
- if (!no.indent)
7140
- yield NL;
7136
+ for (const child of n.children) {
7137
+ const d = format(child, increment(no, prefix.length)).trim();
7138
+ yield indent + prefix + d;
7139
+ yield NL;
7141
7140
  }
7142
7141
  break;
7143
7142
  }
7144
7143
  case "item": {
7145
- yield* trimStart(renderChildren(n, no));
7146
- yield* renderAnnotations(n);
7144
+ yield* formatChildren(n, no);
7145
+ yield* formatAnnotations(n);
7147
7146
  break;
7148
7147
  }
7149
7148
  case "strong": {
7150
7149
  yield "**";
7151
- yield* renderChildren(n, no);
7150
+ yield* formatChildren(n, no);
7152
7151
  yield "**";
7153
7152
  break;
7154
7153
  }
7155
7154
  case "em": {
7156
7155
  yield "_";
7157
- yield* renderChildren(n, no);
7156
+ yield* formatChildren(n, no);
7158
7157
  yield "_";
7159
7158
  break;
7160
7159
  }
7161
7160
  case "code": {
7162
7161
  yield "`";
7163
- yield* render(n.attributes.content, no);
7162
+ yield* formatValue(n.attributes.content, no);
7164
7163
  yield "`";
7165
7164
  break;
7166
7165
  }
7167
7166
  case "s": {
7168
7167
  yield "~~";
7169
- yield* renderChildren(n, no);
7168
+ yield* formatChildren(n, no);
7170
7169
  yield "~~";
7171
7170
  break;
7172
7171
  }
7173
7172
  case "hardbreak": {
7174
- yield "\\\n";
7173
+ yield "\\" + NL;
7175
7174
  yield indent;
7176
7175
  break;
7177
7176
  }
@@ -7181,7 +7180,7 @@ function* renderNode(n, o = {}) {
7181
7180
  break;
7182
7181
  }
7183
7182
  case "table": {
7184
- const table3 = [...renderChildren(n, increment(no))];
7183
+ const table3 = [...formatChildren(n, increment(no))];
7185
7184
  if (o.parent && o.parent.type === "tag" && o.parent.tag === "table") {
7186
7185
  for (const row of table3) {
7187
7186
  yield NL;
@@ -7197,33 +7196,33 @@ function* renderNode(n, o = {}) {
7197
7196
  yield NL;
7198
7197
  const [head, ...rows] = table3;
7199
7198
  const ml = table3.map((arr) => arr.map((s2) => s2.length).reduce(max)).reduce(max);
7200
- yield* renderTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
7199
+ yield* formatTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
7201
7200
  yield NL;
7202
- yield* renderTableRow(head.map(() => "-".repeat(ml)));
7201
+ yield* formatTableRow(head.map(() => "-".repeat(ml)));
7203
7202
  yield NL;
7204
7203
  for (const row of rows) {
7205
- yield* renderTableRow(row.map((r) => r + SPACE.repeat(ml - r.length)));
7204
+ yield* formatTableRow(row.map((r) => r + SPACE.repeat(ml - r.length)));
7206
7205
  yield NL;
7207
7206
  }
7208
7207
  }
7209
7208
  break;
7210
7209
  }
7211
7210
  case "thead": {
7212
- const [head] = [...renderChildren(n, no)];
7211
+ const [head] = [...formatChildren(n, no)];
7213
7212
  yield head || [];
7214
7213
  break;
7215
7214
  }
7216
7215
  case "tr": {
7217
- yield [...renderChildren(n, no)];
7216
+ yield [...formatChildren(n, no)];
7218
7217
  break;
7219
7218
  }
7220
7219
  case "td":
7221
7220
  case "th": {
7222
- yield [...renderChildren(n, no), ...renderAnnotations(n)].join("");
7221
+ yield [...formatChildren(n, no), ...formatAnnotations(n)].join("");
7223
7222
  break;
7224
7223
  }
7225
7224
  case "tbody": {
7226
- yield* renderChildren(n, no);
7225
+ yield* formatChildren(n, no);
7227
7226
  break;
7228
7227
  }
7229
7228
  case "error": {
@@ -7234,7 +7233,7 @@ function* renderNode(n, o = {}) {
7234
7233
  break;
7235
7234
  }
7236
7235
  }
7237
- function* render(v, o = {}) {
7236
+ function* formatValue(v, o = {}) {
7238
7237
  switch (typeof v) {
7239
7238
  case "undefined":
7240
7239
  break;
@@ -7248,21 +7247,20 @@ function* render(v, o = {}) {
7248
7247
  if (v === null)
7249
7248
  break;
7250
7249
  if (Array.isArray(v)) {
7251
- for (const n of v) {
7252
- yield* render(n, o);
7253
- }
7250
+ for (const n of v)
7251
+ yield* formatValue(n, o);
7254
7252
  break;
7255
7253
  }
7256
7254
  switch (v.$$mdtype) {
7257
7255
  case "Function": {
7258
- yield* renderFunction(v);
7256
+ yield* formatFunction(v);
7259
7257
  break;
7260
7258
  }
7261
7259
  case "Node":
7262
- yield* renderNode(v, o);
7260
+ yield* formatNode(v, o);
7263
7261
  break;
7264
7262
  case "Variable": {
7265
- yield* renderVariable(v);
7263
+ yield* formatVariable(v);
7266
7264
  break;
7267
7265
  }
7268
7266
  default:
@@ -7271,11 +7269,10 @@ function* render(v, o = {}) {
7271
7269
  break;
7272
7270
  }
7273
7271
  }
7274
- return true;
7275
7272
  }
7276
7273
  function format(v, options) {
7277
7274
  let doc = "";
7278
- for (const s2 of trimStart(render(v, options)))
7275
+ for (const s2 of formatValue(v, options))
7279
7276
  doc += s2;
7280
7277
  return doc;
7281
7278
  }
@@ -7807,16 +7804,16 @@ var voidElements = new Set([
7807
7804
  "track",
7808
7805
  "wbr"
7809
7806
  ]);
7810
- function render2(node2) {
7807
+ function render(node2) {
7811
7808
  if (typeof node2 === "string")
7812
7809
  return escapeHtml(node2);
7813
7810
  if (Array.isArray(node2))
7814
- return node2.map(render2).join("");
7811
+ return node2.map(render).join("");
7815
7812
  if (node2 === null || typeof node2 !== "object")
7816
7813
  return "";
7817
7814
  const { name, attributes, children = [] } = node2;
7818
7815
  if (!name)
7819
- return render2(children);
7816
+ return render(children);
7820
7817
  let output = `<${name}`;
7821
7818
  for (const [k, v] of Object.entries(attributes ?? {}))
7822
7819
  output += ` ${k}="${escapeHtml(String(v))}"`;
@@ -7824,7 +7821,7 @@ function render2(node2) {
7824
7821
  if (voidElements.has(name))
7825
7822
  return output;
7826
7823
  if (children.length)
7827
- output += render2(children);
7824
+ output += render(children);
7828
7825
  output += `</${name}>`;
7829
7826
  return output;
7830
7827
  }
@@ -7842,7 +7839,7 @@ function dynamic(node2, React, { components = {} } = {}) {
7842
7839
  if (Array.isArray(value))
7843
7840
  return value.map((item2) => deepRender2(item2));
7844
7841
  if (value.$$mdtype === "Tag")
7845
- return render4(value);
7842
+ return render3(value);
7846
7843
  if (typeof value !== "object")
7847
7844
  return value;
7848
7845
  const output = {};
@@ -7850,9 +7847,9 @@ function dynamic(node2, React, { components = {} } = {}) {
7850
7847
  output[k] = deepRender2(v);
7851
7848
  return output;
7852
7849
  }
7853
- function render4(node3) {
7850
+ function render3(node3) {
7854
7851
  if (Array.isArray(node3))
7855
- return React.createElement(React.Fragment, null, ...node3.map(render4));
7852
+ return React.createElement(React.Fragment, null, ...node3.map(render3));
7856
7853
  if (node3 === null || typeof node3 !== "object")
7857
7854
  return node3;
7858
7855
  const {
@@ -7862,14 +7859,14 @@ function dynamic(node2, React, { components = {} } = {}) {
7862
7859
  } = node3;
7863
7860
  if (className)
7864
7861
  attrs.className = className;
7865
- return React.createElement(tagName(name, components), Object.keys(attrs).length == 0 ? null : deepRender2(attrs), ...children.map(render4));
7862
+ return React.createElement(tagName(name, components), Object.keys(attrs).length == 0 ? null : deepRender2(attrs), ...children.map(render3));
7866
7863
  }
7867
- return render4(node2);
7864
+ return render3(node2);
7868
7865
  }
7869
7866
 
7870
7867
  // src/renderers/react/static.ts
7871
7868
  function renderArray(children) {
7872
- return children.map(render3).join(", ");
7869
+ return children.map(render2).join(", ");
7873
7870
  }
7874
7871
  function deepRender(value) {
7875
7872
  if (value == null || typeof value !== "object")
@@ -7877,13 +7874,13 @@ function deepRender(value) {
7877
7874
  if (Array.isArray(value))
7878
7875
  return `[${value.map((item2) => deepRender(item2)).join(", ")}]`;
7879
7876
  if (value.$$mdtype === "Tag")
7880
- return render3(value);
7877
+ return render2(value);
7881
7878
  if (typeof value !== "object")
7882
7879
  return JSON.stringify(value);
7883
7880
  const object = Object.entries(value).map(([k, v]) => [JSON.stringify(k), deepRender(v)].join(": ")).join(", ");
7884
7881
  return `{${object}}`;
7885
7882
  }
7886
- function render3(node2) {
7883
+ function render2(node2) {
7887
7884
  if (Array.isArray(node2))
7888
7885
  return `React.createElement(React.Fragment, null, ${renderArray(node2)})`;
7889
7886
  if (node2 === null || typeof node2 !== "object")
@@ -7904,13 +7901,13 @@ function reactStatic(node2) {
7904
7901
  return `
7905
7902
  (({components = {}} = {}) => {
7906
7903
  ${tagName}
7907
- return ${render3(node2)};
7904
+ return ${render2(node2)};
7908
7905
  })
7909
7906
  `;
7910
7907
  }
7911
7908
 
7912
7909
  // src/renderers/index.ts
7913
- var renderers_default = { html: render2, react: dynamic, reactStatic };
7910
+ var renderers_default = { html: render, react: dynamic, reactStatic };
7914
7911
 
7915
7912
  // src/tags/partial.ts
7916
7913
  var PartialFile = class {