@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.js CHANGED
@@ -6959,6 +6959,7 @@ var ast_default = {
6959
6959
 
6960
6960
  // src/formatter.ts
6961
6961
  var SPACE = " ";
6962
+ var SEP = ", ";
6962
6963
  var NL = "\n";
6963
6964
  var OL = "1. ";
6964
6965
  var UL = "- ";
@@ -6968,63 +6969,63 @@ var increment = (o, n = 2) => ({
6968
6969
  ...o,
6969
6970
  indent: (o.indent || 0) + n
6970
6971
  });
6971
- function* renderChildren(a, options) {
6972
+ function* formatChildren(a, options) {
6972
6973
  for (const child of a.children) {
6973
- yield* render(child, options);
6974
+ yield* formatValue(child, options);
6974
6975
  }
6975
6976
  }
6976
- function* renderTableRow(items) {
6977
+ function* formatTableRow(items) {
6977
6978
  yield `| ${items.join(" | ")} |`;
6978
6979
  }
6979
- function renderValue(v) {
6980
- if (v === null) {
6981
- return "";
6982
- }
6980
+ function formatScalar(v) {
6983
6981
  if (ast_default.isAst(v)) {
6984
6982
  return format(v);
6985
6983
  }
6984
+ if (v === null) {
6985
+ return "";
6986
+ }
6986
6987
  if (Array.isArray(v)) {
6987
- return "[" + v.map(renderValue).join(", ") + "]";
6988
+ return "[" + v.map(formatScalar).join(SEP) + "]";
6988
6989
  }
6989
6990
  if (typeof v === "object") {
6990
- return "{" + Object.entries(v).map(([key, value]) => `${key}: ${renderValue(value)}`).join(", ") + "}";
6991
+ return "{" + Object.entries(v).map(([key, value]) => `${key}: ${formatScalar(value)}`).join(SEP) + "}";
6991
6992
  }
6992
6993
  return JSON.stringify(v);
6993
6994
  }
6994
- function renderAnnotationValue(a) {
6995
+ function formatAnnotationValue(a) {
6995
6996
  if (a.name === "primary")
6996
6997
  return a.value;
6997
6998
  if (a.name === "id" && typeof a.value === "string")
6998
6999
  return "#" + a.value;
6999
7000
  if (a.type === "class")
7000
7001
  return "." + a.name;
7001
- return `${a.name}=${renderValue(a.value)}`;
7002
+ return `${a.name}=${formatScalar(a.value)}`;
7002
7003
  }
7003
- function* renderAttributes(n) {
7004
+ function* formatAttributes(n) {
7004
7005
  for (const [key, value] of Object.entries(n.attributes)) {
7005
7006
  if (key === "class" && !ast_default.isAst(value))
7006
7007
  for (const name of Object.keys(value)) {
7007
- yield renderAnnotationValue({ type: "class", name, value });
7008
+ yield formatAnnotationValue({ type: "class", name, value });
7008
7009
  }
7009
7010
  else
7010
- yield renderAnnotationValue({ type: "attribute", name: key, value });
7011
+ yield formatAnnotationValue({ type: "attribute", name: key, value });
7011
7012
  }
7012
7013
  }
7013
- function* renderAnnotations(n) {
7014
+ function* formatAnnotations(n) {
7014
7015
  if (n.annotations.length) {
7015
7016
  yield OPEN + SPACE;
7016
- yield n.annotations.map(renderAnnotationValue).join(SPACE);
7017
+ yield n.annotations.map(formatAnnotationValue).join(SPACE);
7017
7018
  yield SPACE + CLOSE;
7018
7019
  }
7019
7020
  }
7020
- function* renderVariable(v) {
7021
+ function* formatVariable(v) {
7021
7022
  yield "$";
7022
7023
  yield v.path.join(".");
7023
7024
  }
7024
- function* renderFunction(f) {
7025
+ function* formatFunction(f) {
7025
7026
  yield f.name;
7026
7027
  yield "(";
7027
- yield Object.values(f.parameters).map(renderValue).join(", ");
7028
+ yield Object.values(f.parameters).map(formatScalar).join(SEP);
7028
7029
  yield ")";
7029
7030
  }
7030
7031
  function* trimStart(g) {
@@ -7035,7 +7036,7 @@ function* trimStart(g) {
7035
7036
  yield n;
7036
7037
  yield* g;
7037
7038
  }
7038
- function* renderNode(n, o = {}) {
7039
+ function* formatNode(n, o = {}) {
7039
7040
  const no = { ...o, parent: n };
7040
7041
  const indent = SPACE.repeat(no.indent || 0);
7041
7042
  switch (n.type) {
@@ -7043,7 +7044,7 @@ function* renderNode(n, o = {}) {
7043
7044
  if (n.attributes.frontmatter && n.attributes.frontmatter.length) {
7044
7045
  yield "---" + NL + n.attributes.frontmatter + NL + "---" + NL + NL;
7045
7046
  }
7046
- yield* trimStart(renderChildren(n, no));
7047
+ yield* trimStart(formatChildren(n, no));
7047
7048
  break;
7048
7049
  }
7049
7050
  case "heading": {
@@ -7051,36 +7052,36 @@ function* renderNode(n, o = {}) {
7051
7052
  yield indent;
7052
7053
  yield "#".repeat(n.attributes.level || 1);
7053
7054
  yield SPACE;
7054
- yield* renderChildren(n, no);
7055
- yield* renderAnnotations(n);
7055
+ yield* formatChildren(n, no);
7056
+ yield* formatAnnotations(n);
7056
7057
  yield NL;
7057
7058
  break;
7058
7059
  }
7059
7060
  case "paragraph": {
7060
7061
  yield NL;
7061
7062
  yield indent;
7062
- yield* renderChildren(n, no);
7063
- yield* renderAnnotations(n);
7063
+ yield* formatChildren(n, no);
7064
+ yield* formatAnnotations(n);
7064
7065
  yield NL;
7065
7066
  break;
7066
7067
  }
7067
7068
  case "inline": {
7068
- yield* renderChildren(n, no);
7069
+ yield* formatChildren(n, no);
7069
7070
  break;
7070
7071
  }
7071
7072
  case "link": {
7072
7073
  yield "[";
7073
- yield* renderChildren(n, no);
7074
+ yield* formatChildren(n, no);
7074
7075
  yield "]";
7075
7076
  yield "(";
7076
- yield* render(n.attributes.href, no);
7077
+ yield* formatValue(n.attributes.href, no);
7077
7078
  yield ")";
7078
7079
  break;
7079
7080
  }
7080
7081
  case "text": {
7081
7082
  if (ast_default.isAst(n.attributes.content))
7082
7083
  yield OPEN + SPACE;
7083
- yield* render(n.attributes.content, no);
7084
+ yield* formatValue(n.attributes.content, no);
7084
7085
  if (ast_default.isAst(n.attributes.content))
7085
7086
  yield SPACE + CLOSE;
7086
7087
  break;
@@ -7089,7 +7090,7 @@ function* renderNode(n, o = {}) {
7089
7090
  yield NL;
7090
7091
  yield indent;
7091
7092
  yield "> ";
7092
- yield* trimStart(renderChildren(n, no));
7093
+ yield* trimStart(formatChildren(n, no));
7093
7094
  break;
7094
7095
  }
7095
7096
  case "hr": {
@@ -7102,10 +7103,10 @@ function* renderNode(n, o = {}) {
7102
7103
  case "image": {
7103
7104
  yield "!";
7104
7105
  yield "[";
7105
- yield* render(n.attributes.alt, no);
7106
+ yield* formatValue(n.attributes.alt, no);
7106
7107
  yield "]";
7107
7108
  yield "(";
7108
- yield* render(n.attributes.src, no);
7109
+ yield* formatValue(n.attributes.src, no);
7109
7110
  yield ")";
7110
7111
  break;
7111
7112
  }
@@ -7116,10 +7117,10 @@ function* renderNode(n, o = {}) {
7116
7117
  yield (n.attributes.language || "").toLowerCase();
7117
7118
  if (n.annotations.length)
7118
7119
  yield SPACE;
7119
- yield* renderAnnotations(n);
7120
+ yield* formatAnnotations(n);
7120
7121
  yield NL;
7121
7122
  yield indent;
7122
- yield* renderChildren(n, no);
7123
+ yield* formatChildren(n, no);
7123
7124
  yield indent;
7124
7125
  yield "```";
7125
7126
  yield NL;
@@ -7131,16 +7132,16 @@ function* renderNode(n, o = {}) {
7131
7132
  yield indent;
7132
7133
  }
7133
7134
  const open = OPEN + SPACE;
7134
- const tag = [open + n.tag, ...renderAttributes(n)];
7135
+ const tag = [open + n.tag, ...formatAttributes(n)];
7135
7136
  const inlineTag = tag.join(SPACE);
7136
7137
  if (inlineTag.length + open.length * 2 > MAX_TAG_HEAD_LENGTH) {
7137
- yield tag.join("\n" + SPACE.repeat(open.length) + indent);
7138
+ yield tag.join(NL + SPACE.repeat(open.length) + indent);
7138
7139
  } else {
7139
7140
  yield inlineTag;
7140
7141
  }
7141
7142
  yield SPACE + (n.children.length ? "" : "/") + CLOSE;
7142
7143
  if (n.children.length) {
7143
- yield* renderChildren(n, no.allowIndentation ? increment(no) : no);
7144
+ yield* formatChildren(n, no.allowIndentation ? increment(no) : no);
7144
7145
  if (!n.inline) {
7145
7146
  yield indent;
7146
7147
  }
@@ -7154,48 +7155,46 @@ function* renderNode(n, o = {}) {
7154
7155
  break;
7155
7156
  }
7156
7157
  case "list": {
7158
+ const prefix = n.attributes.ordered ? OL : UL;
7157
7159
  yield NL;
7158
- for (let i = 0; i < n.children.length; i++) {
7159
- yield indent;
7160
- const prefix = n.attributes.ordered ? OL : UL;
7161
- yield prefix;
7162
- yield* render(n.children[i], increment(no, prefix.length));
7163
- if (!no.indent)
7164
- yield NL;
7160
+ for (const child of n.children) {
7161
+ const d = format(child, increment(no, prefix.length)).trim();
7162
+ yield indent + prefix + d;
7163
+ yield NL;
7165
7164
  }
7166
7165
  break;
7167
7166
  }
7168
7167
  case "item": {
7169
- yield* trimStart(renderChildren(n, no));
7170
- yield* renderAnnotations(n);
7168
+ yield* formatChildren(n, no);
7169
+ yield* formatAnnotations(n);
7171
7170
  break;
7172
7171
  }
7173
7172
  case "strong": {
7174
7173
  yield "**";
7175
- yield* renderChildren(n, no);
7174
+ yield* formatChildren(n, no);
7176
7175
  yield "**";
7177
7176
  break;
7178
7177
  }
7179
7178
  case "em": {
7180
7179
  yield "_";
7181
- yield* renderChildren(n, no);
7180
+ yield* formatChildren(n, no);
7182
7181
  yield "_";
7183
7182
  break;
7184
7183
  }
7185
7184
  case "code": {
7186
7185
  yield "`";
7187
- yield* render(n.attributes.content, no);
7186
+ yield* formatValue(n.attributes.content, no);
7188
7187
  yield "`";
7189
7188
  break;
7190
7189
  }
7191
7190
  case "s": {
7192
7191
  yield "~~";
7193
- yield* renderChildren(n, no);
7192
+ yield* formatChildren(n, no);
7194
7193
  yield "~~";
7195
7194
  break;
7196
7195
  }
7197
7196
  case "hardbreak": {
7198
- yield "\\\n";
7197
+ yield "\\" + NL;
7199
7198
  yield indent;
7200
7199
  break;
7201
7200
  }
@@ -7205,7 +7204,7 @@ function* renderNode(n, o = {}) {
7205
7204
  break;
7206
7205
  }
7207
7206
  case "table": {
7208
- const table3 = [...renderChildren(n, increment(no))];
7207
+ const table3 = [...formatChildren(n, increment(no))];
7209
7208
  if (o.parent && o.parent.type === "tag" && o.parent.tag === "table") {
7210
7209
  for (const row of table3) {
7211
7210
  yield NL;
@@ -7221,33 +7220,33 @@ function* renderNode(n, o = {}) {
7221
7220
  yield NL;
7222
7221
  const [head, ...rows] = table3;
7223
7222
  const ml = table3.map((arr) => arr.map((s2) => s2.length).reduce(max)).reduce(max);
7224
- yield* renderTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
7223
+ yield* formatTableRow(head.map((h) => h + SPACE.repeat(ml - h.length)));
7225
7224
  yield NL;
7226
- yield* renderTableRow(head.map(() => "-".repeat(ml)));
7225
+ yield* formatTableRow(head.map(() => "-".repeat(ml)));
7227
7226
  yield NL;
7228
7227
  for (const row of rows) {
7229
- yield* renderTableRow(row.map((r) => r + SPACE.repeat(ml - r.length)));
7228
+ yield* formatTableRow(row.map((r) => r + SPACE.repeat(ml - r.length)));
7230
7229
  yield NL;
7231
7230
  }
7232
7231
  }
7233
7232
  break;
7234
7233
  }
7235
7234
  case "thead": {
7236
- const [head] = [...renderChildren(n, no)];
7235
+ const [head] = [...formatChildren(n, no)];
7237
7236
  yield head || [];
7238
7237
  break;
7239
7238
  }
7240
7239
  case "tr": {
7241
- yield [...renderChildren(n, no)];
7240
+ yield [...formatChildren(n, no)];
7242
7241
  break;
7243
7242
  }
7244
7243
  case "td":
7245
7244
  case "th": {
7246
- yield [...renderChildren(n, no), ...renderAnnotations(n)].join("");
7245
+ yield [...formatChildren(n, no), ...formatAnnotations(n)].join("");
7247
7246
  break;
7248
7247
  }
7249
7248
  case "tbody": {
7250
- yield* renderChildren(n, no);
7249
+ yield* formatChildren(n, no);
7251
7250
  break;
7252
7251
  }
7253
7252
  case "error": {
@@ -7258,7 +7257,7 @@ function* renderNode(n, o = {}) {
7258
7257
  break;
7259
7258
  }
7260
7259
  }
7261
- function* render(v, o = {}) {
7260
+ function* formatValue(v, o = {}) {
7262
7261
  switch (typeof v) {
7263
7262
  case "undefined":
7264
7263
  break;
@@ -7272,21 +7271,20 @@ function* render(v, o = {}) {
7272
7271
  if (v === null)
7273
7272
  break;
7274
7273
  if (Array.isArray(v)) {
7275
- for (const n of v) {
7276
- yield* render(n, o);
7277
- }
7274
+ for (const n of v)
7275
+ yield* formatValue(n, o);
7278
7276
  break;
7279
7277
  }
7280
7278
  switch (v.$$mdtype) {
7281
7279
  case "Function": {
7282
- yield* renderFunction(v);
7280
+ yield* formatFunction(v);
7283
7281
  break;
7284
7282
  }
7285
7283
  case "Node":
7286
- yield* renderNode(v, o);
7284
+ yield* formatNode(v, o);
7287
7285
  break;
7288
7286
  case "Variable": {
7289
- yield* renderVariable(v);
7287
+ yield* formatVariable(v);
7290
7288
  break;
7291
7289
  }
7292
7290
  default:
@@ -7295,11 +7293,10 @@ function* render(v, o = {}) {
7295
7293
  break;
7296
7294
  }
7297
7295
  }
7298
- return true;
7299
7296
  }
7300
7297
  function format(v, options) {
7301
7298
  let doc = "";
7302
- for (const s2 of trimStart(render(v, options)))
7299
+ for (const s2 of formatValue(v, options))
7303
7300
  doc += s2;
7304
7301
  return doc;
7305
7302
  }
@@ -7831,16 +7828,16 @@ var voidElements = new Set([
7831
7828
  "track",
7832
7829
  "wbr"
7833
7830
  ]);
7834
- function render2(node2) {
7831
+ function render(node2) {
7835
7832
  if (typeof node2 === "string")
7836
7833
  return escapeHtml(node2);
7837
7834
  if (Array.isArray(node2))
7838
- return node2.map(render2).join("");
7835
+ return node2.map(render).join("");
7839
7836
  if (node2 === null || typeof node2 !== "object")
7840
7837
  return "";
7841
7838
  const { name, attributes, children = [] } = node2;
7842
7839
  if (!name)
7843
- return render2(children);
7840
+ return render(children);
7844
7841
  let output = `<${name}`;
7845
7842
  for (const [k, v] of Object.entries(attributes ?? {}))
7846
7843
  output += ` ${k}="${escapeHtml(String(v))}"`;
@@ -7848,7 +7845,7 @@ function render2(node2) {
7848
7845
  if (voidElements.has(name))
7849
7846
  return output;
7850
7847
  if (children.length)
7851
- output += render2(children);
7848
+ output += render(children);
7852
7849
  output += `</${name}>`;
7853
7850
  return output;
7854
7851
  }
@@ -7866,7 +7863,7 @@ function dynamic(node2, React, { components = {} } = {}) {
7866
7863
  if (Array.isArray(value))
7867
7864
  return value.map((item2) => deepRender2(item2));
7868
7865
  if (value.$$mdtype === "Tag")
7869
- return render4(value);
7866
+ return render3(value);
7870
7867
  if (typeof value !== "object")
7871
7868
  return value;
7872
7869
  const output = {};
@@ -7874,9 +7871,9 @@ function dynamic(node2, React, { components = {} } = {}) {
7874
7871
  output[k] = deepRender2(v);
7875
7872
  return output;
7876
7873
  }
7877
- function render4(node3) {
7874
+ function render3(node3) {
7878
7875
  if (Array.isArray(node3))
7879
- return React.createElement(React.Fragment, null, ...node3.map(render4));
7876
+ return React.createElement(React.Fragment, null, ...node3.map(render3));
7880
7877
  if (node3 === null || typeof node3 !== "object")
7881
7878
  return node3;
7882
7879
  const {
@@ -7886,14 +7883,14 @@ function dynamic(node2, React, { components = {} } = {}) {
7886
7883
  } = node3;
7887
7884
  if (className)
7888
7885
  attrs.className = className;
7889
- return React.createElement(tagName(name, components), Object.keys(attrs).length == 0 ? null : deepRender2(attrs), ...children.map(render4));
7886
+ return React.createElement(tagName(name, components), Object.keys(attrs).length == 0 ? null : deepRender2(attrs), ...children.map(render3));
7890
7887
  }
7891
- return render4(node2);
7888
+ return render3(node2);
7892
7889
  }
7893
7890
 
7894
7891
  // src/renderers/react/static.ts
7895
7892
  function renderArray(children) {
7896
- return children.map(render3).join(", ");
7893
+ return children.map(render2).join(", ");
7897
7894
  }
7898
7895
  function deepRender(value) {
7899
7896
  if (value == null || typeof value !== "object")
@@ -7901,13 +7898,13 @@ function deepRender(value) {
7901
7898
  if (Array.isArray(value))
7902
7899
  return `[${value.map((item2) => deepRender(item2)).join(", ")}]`;
7903
7900
  if (value.$$mdtype === "Tag")
7904
- return render3(value);
7901
+ return render2(value);
7905
7902
  if (typeof value !== "object")
7906
7903
  return JSON.stringify(value);
7907
7904
  const object = Object.entries(value).map(([k, v]) => [JSON.stringify(k), deepRender(v)].join(": ")).join(", ");
7908
7905
  return `{${object}}`;
7909
7906
  }
7910
- function render3(node2) {
7907
+ function render2(node2) {
7911
7908
  if (Array.isArray(node2))
7912
7909
  return `React.createElement(React.Fragment, null, ${renderArray(node2)})`;
7913
7910
  if (node2 === null || typeof node2 !== "object")
@@ -7928,13 +7925,13 @@ function reactStatic(node2) {
7928
7925
  return `
7929
7926
  (({components = {}} = {}) => {
7930
7927
  ${tagName}
7931
- return ${render3(node2)};
7928
+ return ${render2(node2)};
7932
7929
  })
7933
7930
  `;
7934
7931
  }
7935
7932
 
7936
7933
  // src/renderers/index.ts
7937
- var renderers_default = { html: render2, react: dynamic, reactStatic };
7934
+ var renderers_default = { html: render, react: dynamic, reactStatic };
7938
7935
 
7939
7936
  // src/tags/partial.ts
7940
7937
  var PartialFile = class {