@pretextbook/ptxast-util-to-mdast 0.0.5 → 0.0.7

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.cjs CHANGED
@@ -1,6 +1,7 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("@pretextbook/ptxast"),T=require("mdast-util-to-markdown"),b=require("mdast-util-directive"),w=require("mdast-util-math"),g=new Set([...p.DIVISION_HIERARCHY,"appendix"]),k=new Set(["pretext","book","article","frontmatter","backmatter"]),M=new Map([["theorem",{name:"theorem",category:"theorem-like"}],["lemma",{name:"lemma",category:"theorem-like"}],["corollary",{name:"corollary",category:"theorem-like"}],["proposition",{name:"proposition",category:"theorem-like"}],["claim",{name:"claim",category:"theorem-like"}],["fact",{name:"fact",category:"theorem-like"}],["conjecture",{name:"conjecture",category:"theorem-like"}],["axiom",{name:"axiom",category:"theorem-like"}],["principle",{name:"principle",category:"theorem-like"}],["hypothesis",{name:"hypothesis",category:"theorem-like"}],["algorithm",{name:"algorithm",category:"theorem-like"}],["definition",{name:"definition",category:"definition-like"}],["notation",{name:"notation",category:"definition-like"}],["remark",{name:"remark",category:"remark-like"}],["note",{name:"note",category:"remark-like"}],["observation",{name:"observation",category:"remark-like"}],["warning",{name:"warning",category:"remark-like"}],["insight",{name:"insight",category:"remark-like"}],["assemblage",{name:"assemblage",category:"remark-like"}],["example",{name:"example",category:"example-like"}],["question",{name:"question",category:"example-like"}],["problem",{name:"problem",category:"example-like"}],["exercise",{name:"exercise",category:"example-like"}],["activity",{name:"activity",category:"example-like"}],["exploration",{name:"exploration",category:"example-like"}],["investigation",{name:"investigation",category:"example-like"}],["project",{name:"project",category:"example-like"}],["proof",{name:"proof",category:"proof-like"}],["case",{name:"case",category:"proof-like"}],["solution",{name:"solution",category:"solution-like"}],["hint",{name:"hint",category:"solution-like"}],["answer",{name:"answer",category:"solution-like"}]]),P=new Set(["theorem-like","definition-like"]);function x(e){return{type:"root",children:d(e.children,1)}}function v(e){for(const t of e){if(t.type!=="element")continue;const n=t;if(g.has(n.name))return n.name;if(k.has(n.name)){const i=v(n.children);if(i)return i}}}function d(e,t){const n=[];for(const i of e){if(i.type!=="element")continue;const a=i;if(k.has(a.name))n.push(...d(a.children,t));else if(g.has(a.name))n.push(...E(a,Math.min(t,6)));else{const l=u(a);l!==null&&n.push(l)}}return n}function E(e,t){const n=[],i=e.attributes??{},a=e.children,l=a.find(o=>o.type==="element"&&o.name==="title"),s=a.filter(o=>!(o.type==="element"&&o.name==="title")),r={type:"heading",depth:t,children:l?l.children.map(h).filter(m):[],...i["xml:id"]?{data:{id:i["xml:id"],hProperties:{id:i["xml:id"]}}}:{}};n.push(r);const c=Math.min(t+1,6);return n.push(...d(s,c)),n}function u(e){switch(e.name){case"p":return I(e);case"blockquote":return S(e);case"ol":return y(e,!0);case"ul":return y(e,!1);case"program":return D(e);case"me":case"men":case"md":case"mdn":return N(e);default:{const t=M.get(e.name);return t?q(e,t):null}}}function I(e){return{type:"paragraph",children:e.children.map(h).filter(m)}}function S(e){return{type:"blockquote",children:e.children.filter(t=>t.type==="element").map(t=>u(t)).filter(m)}}function y(e,t){return{type:"list",ordered:t,spread:!1,children:e.children.filter(n=>n.type==="element").map(n=>C(n))}}function C(e){return{type:"listItem",spread:!1,children:e.children.filter(t=>t.type==="element").map(t=>u(t)).filter(m)}}function D(e){return{type:"code",lang:(e.attributes??{}).language??null,value:p.getPtxTextContent(e)}}function N(e){const t=e.children;return t.length>0&&t[0].type==="text"?{type:"math",value:p.getPtxTextContent(e)}:{type:"math",value:t.filter(i=>i.type==="element"&&i.name==="mrow").map(i=>p.getPtxTextContent(i)).join(` \\\\
2
- `)}}function q(e,t){const n=e.attributes??{},i=e.children,a={};n["xml:id"]&&(a.id=n["xml:id"]);for(const[r,c]of Object.entries(n))r==="xml:id"||c==null||(a[r]=c);const l=i.find(r=>r.type==="element"&&r.name==="title"),s=[];if(l){const r={type:"paragraph",data:{directiveLabel:!0},children:l.children.map(h).filter(m)};s.push(r)}if(P.has(t.category))for(const r of i){if(r.type!=="element")continue;const c=r;if(c.name!=="title")if(c.name==="statement")for(const o of c.children){if(o.type!=="element")continue;const f=u(o);f&&s.push(f)}else{const o=u(c);o&&s.push(o)}}else for(const r of i){if(r.type!=="element")continue;const c=r;if(c.name==="title")continue;const o=u(c);o&&s.push(o)}return{type:"containerDirective",name:t.name,attributes:Object.keys(a).length>0?a:void 0,children:s}}function h(e){if(e.type==="text")return{type:"text",value:e.value??""};if(e.type!=="element")return null;const t=e;switch(t.name){case"em":return{type:"emphasis",children:t.children.map(h).filter(m)};case"alert":return{type:"strong",children:t.children.map(h).filter(m)};case"c":return{type:"inlineCode",value:p.getPtxTextContent(t)};case"m":return{type:"inlineMath",value:p.getPtxTextContent(t)};default:return null}}function m(e){return e!==null}function j(e){const t=x(e),n=T.toMarkdown(t,{extensions:[b.directiveToMarkdown(),w.mathToMarkdown()]}),i=v(e.children);return!i||i==="chapter"?n:`---
3
- division: ${i}
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@pretextbook/ptxast"),T=require("mdast-util-to-markdown"),I=require("mdast-util-directive"),S=require("mdast-util-math"),g=new Set([...c.DIVISION_HIERARCHY,...c.EXTRA_DIVISION_TYPES.filter(e=>!c.isTitlelessDivisionType(e))]),k=new Set(c.TITLELESS_DIVISION_TYPES),x=new Set(["pretext","book","article","slideshow","frontmatter","backmatter"]),E=new Map([["theorem",{name:"theorem",category:"theorem-like"}],["lemma",{name:"lemma",category:"theorem-like"}],["corollary",{name:"corollary",category:"theorem-like"}],["proposition",{name:"proposition",category:"theorem-like"}],["claim",{name:"claim",category:"theorem-like"}],["fact",{name:"fact",category:"theorem-like"}],["conjecture",{name:"conjecture",category:"theorem-like"}],["axiom",{name:"axiom",category:"theorem-like"}],["principle",{name:"principle",category:"theorem-like"}],["hypothesis",{name:"hypothesis",category:"theorem-like"}],["algorithm",{name:"algorithm",category:"theorem-like"}],["definition",{name:"definition",category:"definition-like"}],["notation",{name:"notation",category:"definition-like"}],["remark",{name:"remark",category:"remark-like"}],["note",{name:"note",category:"remark-like"}],["observation",{name:"observation",category:"remark-like"}],["warning",{name:"warning",category:"remark-like"}],["insight",{name:"insight",category:"remark-like"}],["assemblage",{name:"assemblage",category:"remark-like"}],["example",{name:"example",category:"example-like"}],["question",{name:"question",category:"example-like"}],["problem",{name:"problem",category:"example-like"}],["exercise",{name:"exercise",category:"example-like"}],["activity",{name:"activity",category:"example-like"}],["exploration",{name:"exploration",category:"example-like"}],["investigation",{name:"investigation",category:"example-like"}],["project",{name:"project",category:"example-like"}],["proof",{name:"proof",category:"proof-like"}],["case",{name:"case",category:"proof-like"}],["solution",{name:"solution",category:"solution-like"}],["hint",{name:"hint",category:"solution-like"}],["answer",{name:"answer",category:"solution-like"}]]),w=new Set(["theorem-like","definition-like"]);function v(e){return{type:"root",children:f(e.children,1)}}function b(e){for(const t of e){if(t.type!=="element")continue;const i=t;if(g.has(i.name)||k.has(i.name)){const n=i.attributes??{},r={};return n["xml:id"]&&(r.xmlid=n["xml:id"]),n.label&&(r.label=n.label),n.component&&(r.component=n.component),{name:i.name,attributes:r}}if(x.has(i.name)){const n=b(i.children);if(n)return n}}}function f(e,t){const i=[];for(const n of e){if(n.type!=="element")continue;const r=n;if(x.has(r.name)||k.has(r.name))i.push(...f(r.children,t));else if(g.has(r.name))i.push(...P(r,Math.min(t,6)));else{const s=p(r);s!==null&&i.push(s)}}return i}function P(e,t){const i=[],n=e.attributes??{},r=e.children,s=r.find(a=>a.type==="element"&&a.name==="title"),m=r.filter(a=>!(a.type==="element"&&a.name==="title")),o={type:"heading",depth:t,children:s?s.children.map(h).filter(u):[],...n["xml:id"]?{data:{id:n["xml:id"],hProperties:{id:n["xml:id"]}}}:{}};i.push(o);const l=Math.min(t+1,6);return i.push(...f(m,l)),i}function p(e){switch(e.name){case"p":return M(e);case"blockquote":return D(e);case"ol":return y(e,!0);case"ul":return y(e,!1);case"program":return N(e);case"me":case"men":case"md":case"mdn":return C(e);default:{const t=E.get(e.name);return t?j(e,t):null}}}function M(e){return{type:"paragraph",children:e.children.map(h).filter(u)}}function D(e){return{type:"blockquote",children:e.children.filter(t=>t.type==="element").map(t=>p(t)).filter(u)}}function y(e,t){return{type:"list",ordered:t,spread:!1,children:e.children.filter(i=>i.type==="element").map(i=>_(i))}}function _(e){return{type:"listItem",spread:!1,children:e.children.filter(t=>t.type==="element").map(t=>p(t)).filter(u)}}function N(e){return{type:"code",lang:(e.attributes??{}).language??null,value:c.getPtxTextContent(e)}}function C(e){const t=e.children;return t.length>0&&t[0].type==="text"?{type:"math",value:c.getPtxTextContent(e)}:{type:"math",value:t.filter(n=>n.type==="element"&&n.name==="mrow").map(n=>c.getPtxTextContent(n)).join(` \\\\
2
+ `)}}function j(e,t){const i=e.attributes??{},n=e.children,r={};i["xml:id"]&&(r.id=i["xml:id"]);for(const[o,l]of Object.entries(i))o==="xml:id"||l==null||(r[o]=l);const s=n.find(o=>o.type==="element"&&o.name==="title"),m=[];if(s){const o={type:"paragraph",data:{directiveLabel:!0},children:s.children.map(h).filter(u)};m.push(o)}if(w.has(t.category))for(const o of n){if(o.type!=="element")continue;const l=o;if(l.name!=="title")if(l.name==="statement")for(const a of l.children){if(a.type!=="element")continue;const d=p(a);d&&m.push(d)}else{const a=p(l);a&&m.push(a)}}else for(const o of n){if(o.type!=="element")continue;const l=o;if(l.name==="title")continue;const a=p(l);a&&m.push(a)}return{type:"containerDirective",name:t.name,attributes:Object.keys(r).length>0?r:void 0,children:m}}function h(e){if(e.type==="text")return{type:"text",value:e.value??""};if(e.type!=="element")return null;const t=e;switch(t.name){case"em":return{type:"emphasis",children:t.children.map(h).filter(u)};case"alert":return{type:"strong",children:t.children.map(h).filter(u)};case"c":return{type:"inlineCode",value:c.getPtxTextContent(t)};case"m":return{type:"inlineMath",value:c.getPtxTextContent(t)};default:return null}}function u(e){return e!==null}function q(e){const t=v(e),i=T.toMarkdown(t,{extensions:[I.directiveToMarkdown(),S.mathToMarkdown()]}),n=b(e.children);if(!n)return i;const r=[];return n.name!=="chapter"&&r.push(`division: ${n.name}`),n.attributes.xmlid&&r.push(`xmlid: ${n.attributes.xmlid}`),n.attributes.label&&r.push(`label: ${n.attributes.label}`),n.attributes.component&&r.push(`component: ${n.attributes.component}`),r.length===0?i:`---
3
+ ${r.join(`
4
+ `)}
4
5
  ---
5
6
 
6
- ${n}`}exports.ptxastToMarkdown=j;exports.ptxastToMdast=x;
7
+ ${i}`}exports.ptxastToMarkdown=q;exports.ptxastToMdast=v;
package/dist/index.js CHANGED
@@ -1,17 +1,18 @@
1
- import { DIVISION_HIERARCHY as x, getPtxTextContent as p } from "@pretextbook/ptxast";
2
- import { toMarkdown as b } from "mdast-util-to-markdown";
3
- import { directiveToMarkdown as T } from "mdast-util-directive";
4
- import { mathToMarkdown as w } from "mdast-util-math";
1
+ import { DIVISION_HIERARCHY as b, EXTRA_DIVISION_TYPES as T, isTitlelessDivisionType as I, TITLELESS_DIVISION_TYPES as S, getPtxTextContent as p } from "@pretextbook/ptxast";
2
+ import { toMarkdown as E } from "mdast-util-to-markdown";
3
+ import { directiveToMarkdown as w } from "mdast-util-directive";
4
+ import { mathToMarkdown as _ } from "mdast-util-math";
5
5
  const g = /* @__PURE__ */ new Set([
6
- ...x,
7
- "appendix"
8
- ]), k = /* @__PURE__ */ new Set([
6
+ ...b,
7
+ ...T.filter((e) => !I(e))
8
+ ]), k = new Set(S), v = /* @__PURE__ */ new Set([
9
9
  "pretext",
10
10
  "book",
11
11
  "article",
12
+ "slideshow",
12
13
  "frontmatter",
13
14
  "backmatter"
14
- ]), E = /* @__PURE__ */ new Map([
15
+ ]), P = /* @__PURE__ */ new Map([
15
16
  // theorem-like
16
17
  ["theorem", { name: "theorem", category: "theorem-like" }],
17
18
  ["lemma", { name: "lemma", category: "theorem-like" }],
@@ -50,88 +51,91 @@ const g = /* @__PURE__ */ new Set([
50
51
  ["solution", { name: "solution", category: "solution-like" }],
51
52
  ["hint", { name: "hint", category: "solution-like" }],
52
53
  ["answer", { name: "answer", category: "solution-like" }]
53
- ]), I = /* @__PURE__ */ new Set(["theorem-like", "definition-like"]);
54
- function M(e) {
54
+ ]), D = /* @__PURE__ */ new Set(["theorem-like", "definition-like"]);
55
+ function N(e) {
55
56
  return {
56
57
  type: "root",
57
58
  children: h(e.children, 1)
58
59
  };
59
60
  }
60
- function v(e) {
61
+ function x(e) {
61
62
  for (const t of e) {
62
63
  if (t.type !== "element") continue;
63
- const n = t;
64
- if (g.has(n.name)) return n.name;
65
- if (k.has(n.name)) {
66
- const r = v(n.children);
67
- if (r) return r;
64
+ const i = t;
65
+ if (g.has(i.name) || k.has(i.name)) {
66
+ const n = i.attributes ?? {}, r = {};
67
+ return n["xml:id"] && (r.xmlid = n["xml:id"]), n.label && (r.label = n.label), n.component && (r.component = n.component), { name: i.name, attributes: r };
68
+ }
69
+ if (v.has(i.name)) {
70
+ const n = x(i.children);
71
+ if (n) return n;
68
72
  }
69
73
  }
70
74
  }
71
75
  function h(e, t) {
72
- const n = [];
73
- for (const r of e) {
74
- if (r.type !== "element") continue;
75
- const a = r;
76
- if (k.has(a.name))
77
- n.push(...h(a.children, t));
78
- else if (g.has(a.name))
79
- n.push(...P(a, Math.min(t, 6)));
76
+ const i = [];
77
+ for (const n of e) {
78
+ if (n.type !== "element") continue;
79
+ const r = n;
80
+ if (v.has(r.name) || k.has(r.name))
81
+ i.push(...h(r.children, t));
82
+ else if (g.has(r.name))
83
+ i.push(...M(r, Math.min(t, 6)));
80
84
  else {
81
- const l = u(a);
82
- l !== null && n.push(l);
85
+ const c = u(r);
86
+ c !== null && i.push(c);
83
87
  }
84
88
  }
85
- return n;
89
+ return i;
86
90
  }
87
- function P(e, t) {
88
- const n = [], r = e.attributes ?? {}, a = e.children, l = a.find(
89
- (o) => o.type === "element" && o.name === "title"
90
- ), m = a.filter(
91
- (o) => !(o.type === "element" && o.name === "title")
92
- ), i = {
91
+ function M(e, t) {
92
+ const i = [], n = e.attributes ?? {}, r = e.children, c = r.find(
93
+ (a) => a.type === "element" && a.name === "title"
94
+ ), s = r.filter(
95
+ (a) => !(a.type === "element" && a.name === "title")
96
+ ), o = {
93
97
  type: "heading",
94
98
  depth: t,
95
- children: l ? l.children.map(f).filter(s) : [],
96
- ...r["xml:id"] ? { data: { id: r["xml:id"], hProperties: { id: r["xml:id"] } } } : {}
99
+ children: c ? c.children.map(f).filter(m) : [],
100
+ ...n["xml:id"] ? { data: { id: n["xml:id"], hProperties: { id: n["xml:id"] } } } : {}
97
101
  };
98
- n.push(i);
99
- const c = Math.min(t + 1, 6);
100
- return n.push(...h(m, c)), n;
102
+ i.push(o);
103
+ const l = Math.min(t + 1, 6);
104
+ return i.push(...h(s, l)), i;
101
105
  }
102
106
  function u(e) {
103
107
  switch (e.name) {
104
108
  case "p":
105
- return D(e);
109
+ return L(e);
106
110
  case "blockquote":
107
- return N(e);
111
+ return A(e);
108
112
  case "ol":
109
113
  return y(e, !0);
110
114
  case "ul":
111
115
  return y(e, !1);
112
116
  case "program":
113
- return A(e);
117
+ return j(e);
114
118
  case "me":
115
119
  case "men":
116
120
  case "md":
117
121
  case "mdn":
118
- return _(e);
122
+ return R(e);
119
123
  default: {
120
- const t = E.get(e.name);
121
- return t ? j(e, t) : null;
124
+ const t = P.get(e.name);
125
+ return t ? Y(e, t) : null;
122
126
  }
123
127
  }
124
128
  }
125
- function D(e) {
129
+ function L(e) {
126
130
  return {
127
131
  type: "paragraph",
128
- children: e.children.map(f).filter(s)
132
+ children: e.children.map(f).filter(m)
129
133
  };
130
134
  }
131
- function N(e) {
135
+ function A(e) {
132
136
  return {
133
137
  type: "blockquote",
134
- children: e.children.filter((t) => t.type === "element").map((t) => u(t)).filter(s)
138
+ children: e.children.filter((t) => t.type === "element").map((t) => u(t)).filter(m)
135
139
  };
136
140
  }
137
141
  function y(e, t) {
@@ -139,73 +143,73 @@ function y(e, t) {
139
143
  type: "list",
140
144
  ordered: t,
141
145
  spread: !1,
142
- children: e.children.filter((n) => n.type === "element").map((n) => S(n))
146
+ children: e.children.filter((i) => i.type === "element").map((i) => O(i))
143
147
  };
144
148
  }
145
- function S(e) {
149
+ function O(e) {
146
150
  return {
147
151
  type: "listItem",
148
152
  spread: !1,
149
- children: e.children.filter((t) => t.type === "element").map((t) => u(t)).filter(s)
153
+ children: e.children.filter((t) => t.type === "element").map((t) => u(t)).filter(m)
150
154
  };
151
155
  }
152
- function A(e) {
156
+ function j(e) {
153
157
  return {
154
158
  type: "code",
155
159
  lang: (e.attributes ?? {}).language ?? null,
156
160
  value: p(e)
157
161
  };
158
162
  }
159
- function _(e) {
163
+ function R(e) {
160
164
  const t = e.children;
161
- return t.length > 0 && t[0].type === "text" ? { type: "math", value: p(e) } : { type: "math", value: t.filter((r) => r.type === "element" && r.name === "mrow").map((r) => p(r)).join(` \\\\
165
+ return t.length > 0 && t[0].type === "text" ? { type: "math", value: p(e) } : { type: "math", value: t.filter((n) => n.type === "element" && n.name === "mrow").map((n) => p(n)).join(` \\\\
162
166
  `) };
163
167
  }
164
- function j(e, t) {
165
- const n = e.attributes ?? {}, r = e.children, a = {};
166
- n["xml:id"] && (a.id = n["xml:id"]);
167
- for (const [i, c] of Object.entries(n))
168
- i === "xml:id" || c == null || (a[i] = c);
169
- const l = r.find(
170
- (i) => i.type === "element" && i.name === "title"
171
- ), m = [];
172
- if (l) {
173
- const i = {
168
+ function Y(e, t) {
169
+ const i = e.attributes ?? {}, n = e.children, r = {};
170
+ i["xml:id"] && (r.id = i["xml:id"]);
171
+ for (const [o, l] of Object.entries(i))
172
+ o === "xml:id" || l == null || (r[o] = l);
173
+ const c = n.find(
174
+ (o) => o.type === "element" && o.name === "title"
175
+ ), s = [];
176
+ if (c) {
177
+ const o = {
174
178
  type: "paragraph",
175
179
  data: { directiveLabel: !0 },
176
- children: l.children.map(f).filter(s)
180
+ children: c.children.map(f).filter(m)
177
181
  };
178
- m.push(i);
182
+ s.push(o);
179
183
  }
180
- if (I.has(t.category))
181
- for (const i of r) {
182
- if (i.type !== "element") continue;
183
- const c = i;
184
- if (c.name !== "title")
185
- if (c.name === "statement")
186
- for (const o of c.children) {
187
- if (o.type !== "element") continue;
188
- const d = u(o);
189
- d && m.push(d);
184
+ if (D.has(t.category))
185
+ for (const o of n) {
186
+ if (o.type !== "element") continue;
187
+ const l = o;
188
+ if (l.name !== "title")
189
+ if (l.name === "statement")
190
+ for (const a of l.children) {
191
+ if (a.type !== "element") continue;
192
+ const d = u(a);
193
+ d && s.push(d);
190
194
  }
191
195
  else {
192
- const o = u(c);
193
- o && m.push(o);
196
+ const a = u(l);
197
+ a && s.push(a);
194
198
  }
195
199
  }
196
200
  else
197
- for (const i of r) {
198
- if (i.type !== "element") continue;
199
- const c = i;
200
- if (c.name === "title") continue;
201
- const o = u(c);
202
- o && m.push(o);
201
+ for (const o of n) {
202
+ if (o.type !== "element") continue;
203
+ const l = o;
204
+ if (l.name === "title") continue;
205
+ const a = u(l);
206
+ a && s.push(a);
203
207
  }
204
208
  return {
205
209
  type: "containerDirective",
206
210
  name: t.name,
207
- attributes: Object.keys(a).length > 0 ? a : void 0,
208
- children: m
211
+ attributes: Object.keys(r).length > 0 ? r : void 0,
212
+ children: s
209
213
  };
210
214
  }
211
215
  function f(e) {
@@ -217,12 +221,12 @@ function f(e) {
217
221
  case "em":
218
222
  return {
219
223
  type: "emphasis",
220
- children: t.children.map(f).filter(s)
224
+ children: t.children.map(f).filter(m)
221
225
  };
222
226
  case "alert":
223
227
  return {
224
228
  type: "strong",
225
- children: t.children.map(f).filter(s)
229
+ children: t.children.map(f).filter(m)
226
230
  };
227
231
  case "c":
228
232
  return { type: "inlineCode", value: p(t) };
@@ -232,20 +236,23 @@ function f(e) {
232
236
  return null;
233
237
  }
234
238
  }
235
- function s(e) {
239
+ function m(e) {
236
240
  return e !== null;
237
241
  }
238
- function O(e) {
239
- const t = M(e), n = b(t, {
240
- extensions: [T(), w()]
241
- }), r = v(e.children);
242
- return !r || r === "chapter" ? n : `---
243
- division: ${r}
242
+ function H(e) {
243
+ const t = N(e), i = E(t, {
244
+ extensions: [w(), _()]
245
+ }), n = x(e.children);
246
+ if (!n) return i;
247
+ const r = [];
248
+ return n.name !== "chapter" && r.push(`division: ${n.name}`), n.attributes.xmlid && r.push(`xmlid: ${n.attributes.xmlid}`), n.attributes.label && r.push(`label: ${n.attributes.label}`), n.attributes.component && r.push(`component: ${n.attributes.component}`), r.length === 0 ? i : `---
249
+ ${r.join(`
250
+ `)}
244
251
  ---
245
252
 
246
- ${n}`;
253
+ ${i}`;
247
254
  }
248
255
  export {
249
- O as ptxastToMarkdown,
250
- M as ptxastToMdast
256
+ H as ptxastToMarkdown,
257
+ N as ptxastToMdast
251
258
  };
@@ -1,6 +1,7 @@
1
1
  import { Root as MdastRoot } from 'mdast';
2
2
  import { Root, ElementContent } from '@pretextbook/ptxast';
3
3
  export declare const DIVISION_TYPE_NAMES: Set<string>;
4
+ export declare const TITLELESS_DIVISION_TYPES: Set<string>;
4
5
  /**
5
6
  * Structural container types whose content should be recursed into
6
7
  * without emitting a heading, and without changing the current depth.
@@ -10,6 +11,23 @@ export declare const DIVISION_TYPE_NAMES: Set<string>;
10
11
  export declare const TRANSPARENT_TYPES: Set<string>;
11
12
  /** Convert an xast Root (PreTeXt document) to an mdast Root. */
12
13
  export declare function ptxastToMdast(root: Root): MdastRoot;
14
+ /** The outermost division's name plus the subset of its attributes that
15
+ * round-trip through markdown frontmatter (`xmlid`, `label`, `component`). */
16
+ export interface TopLevelDivisionInfo {
17
+ name: string;
18
+ attributes: {
19
+ xmlid?: string;
20
+ label?: string;
21
+ component?: string;
22
+ };
23
+ }
24
+ /**
25
+ * Find the outermost division in a ptxast tree (skipping transparent
26
+ * structural wrappers), i.e. the division a depth-1 heading would represent
27
+ * if this tree were flattened to markdown — along with its `xml:id`/
28
+ * `label`/`component` attributes, for round-tripping into frontmatter.
29
+ */
30
+ export declare function findTopLevelDivisionInfo(nodes: ElementContent[]): TopLevelDivisionInfo | undefined;
13
31
  /**
14
32
  * Find the name of the outermost division in a ptxast tree (skipping
15
33
  * transparent structural wrappers), i.e. the division a depth-1 heading
@@ -4,8 +4,10 @@ export { ptxastToMdast } from './ptxast-to-mdast.js';
4
4
  * Convert an xast Root (PreTeXt document) to a markdown string using the directive
5
5
  * syntax for PreTeXt block environments.
6
6
  *
7
- * When the document's outermost division isn't `chapter` (the default a
8
- * depth-1 heading is assumed to mean), a `division:` frontmatter block is
9
- * prepended so the markdown can be converted back to PreTeXt unambiguously.
7
+ * A leading frontmatter block is prepended when the outermost division
8
+ * carries information that markdown can't otherwise represent: a
9
+ * `division:` field when it isn't `chapter` (the default a depth-1 heading
10
+ * is assumed to mean), and `xmlid:`/`label:`/`component:` fields mirroring
11
+ * that division's `xml:id`/`label`/`component` attributes.
10
12
  */
11
13
  export declare function ptxastToMarkdown(root: Root): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretextbook/ptxast-util-to-mdast",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Convert ptxast to mdast (markdown AST) and markdown strings",
5
5
  "author": "Oscar Levin",
6
6
  "license": "GPL-3.0-or-later",
@@ -39,7 +39,7 @@
39
39
  "test": "vitest run"
40
40
  },
41
41
  "dependencies": {
42
- "@pretextbook/ptxast": "^0.0.5",
42
+ "@pretextbook/ptxast": "^0.0.7",
43
43
  "@types/mdast": "^4.0.0",
44
44
  "mdast-util-directive": "^3.0.0",
45
45
  "mdast-util-math": "^3.0.0",