@pretextbook/ptxast-util-to-mdast 0.0.9 → 0.1.0

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,7 +1,7 @@
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(`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("@pretextbook/ptxast"),k=require("mdast-util-to-markdown"),S=require("mdast-util-directive"),E=require("mdast-util-math"),x=new Set([...m.DIVISION_HIERARCHY,...m.EXTRA_DIVISION_TYPES.filter(e=>!m.isTitlelessDivisionType(e))]),v=new Set(m.TITLELESS_DIVISION_TYPES),b=new Set(["pretext","book","article","slideshow","frontmatter","backmatter"]),w=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 T(e){return{type:"root",children:y(e.children,1)}}function I(e){for(const t of e){if(t.type!=="element")continue;const n=t;if(x.has(n.name)||v.has(n.name)){const r=n.attributes??{},i={};return r["xml:id"]&&(i.xmlid=r["xml:id"]),r.label&&(i.label=r.label),r.component&&(i.component=r.component),{name:n.name,attributes:i}}if(b.has(n.name)){const r=I(n.children);if(r)return r}}}function y(e,t){const n=[];for(const r of e){if(r.type!=="element")continue;const i=r;if(b.has(i.name)||v.has(i.name))n.push(...y(i.children,t));else if(x.has(i.name))n.push(...M(i,Math.min(t,6)));else{const a=p(i);a!==null&&n.push(a)}}return n}function M(e,t){const n=[],r=e.attributes??{},i=e.children,a=i.find(f=>f.type==="element"&&f.name==="title"),c=i.filter(f=>!(f.type==="element"&&f.name==="title")),{"xml:id":o,...l}=r,s={type:"heading",depth:t,children:a?a.children.map(h).filter(u):[],...o?{data:{id:o,attributes:l,hProperties:{id:o}}}:{}};n.push(s);const d=Math.min(t+1,6);return n.push(...y(c,d)),n}function p(e){switch(e.name){case"p":return D(e);case"blockquote":return _(e);case"ol":return g(e,!0);case"ul":return g(e,!1);case"program":return j(e);case"me":case"men":case"md":case"mdn":return $(e);default:{const t=w.get(e.name);return t?C(e,t):null}}}function D(e){return{type:"paragraph",children:e.children.map(h).filter(u)}}function _(e){return{type:"blockquote",children:e.children.filter(t=>t.type==="element").map(t=>p(t)).filter(u)}}function g(e,t){return{type:"list",ordered:t,spread:!1,children:e.children.filter(n=>n.type==="element").map(n=>N(n))}}function N(e){return{type:"listItem",spread:!1,children:e.children.filter(t=>t.type==="element").map(t=>p(t)).filter(u)}}function j(e){return{type:"code",lang:(e.attributes??{}).language??null,value:m.getPtxTextContent(e)}}function $(e){const t=e.children;return t.length>0&&t[0].type==="text"?{type:"math",value:m.getPtxTextContent(e)}:{type:"math",value:t.filter(r=>r.type==="element"&&r.name==="mrow").map(r=>m.getPtxTextContent(r)).join(` \\\\
2
+ `)}}function C(e,t){const n=e.attributes??{},r=e.children,i={};n["xml:id"]&&(i.id=n["xml:id"]);for(const[o,l]of Object.entries(n))o==="xml:id"||l==null||(i[o]=l);const a=r.find(o=>o.type==="element"&&o.name==="title"),c=[];if(a){const o={type:"paragraph",data:{directiveLabel:!0},children:a.children.map(h).filter(u)};c.push(o)}if(P.has(t.category))for(const o of r){if(o.type!=="element")continue;const l=o;if(l.name!=="title")if(l.name==="statement")for(const s of l.children){if(s.type!=="element")continue;const d=p(s);d&&c.push(d)}else{const s=p(l);s&&c.push(s)}}else for(const o of r){if(o.type!=="element")continue;const l=o;if(l.name==="title")continue;const s=p(l);s&&c.push(s)}return{type:"containerDirective",name:t.name,attributes:Object.keys(i).length>0?i:void 0,children:c}}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:m.getPtxTextContent(t)};case"m":return{type:"inlineMath",value:m.getPtxTextContent(t)};case"xref":return O(t);default:return null}}function O(e){const t=e.attributes??{},n=t.ref;return!n||/[\s,]/.test(n)||Object.keys(t).length>1?null:{type:"link",url:`#${n}`,children:e.children.map(h).filter(u)}}function u(e){return e!==null}function A(e){return(t,n,r,i)=>{const a=k.defaultHandlers.heading(t,n,r,i),c=t.data;if(!c?.id||t===e)return a;const o=Object.entries(c.attributes??{}).map(([l,s])=>` ${l}="${s}"`);return`${a} {#${c.id}${o.join("")}}`}}function q(e){const t=T(e),n=t.children.find(c=>c.type==="heading"),r=k.toMarkdown(t,{extensions:[S.directiveToMarkdown(),E.mathToMarkdown()],handlers:{heading:A(n)}}),i=I(e.children);if(!i)return r;const a=[];return i.name!=="chapter"&&a.push(`division: ${i.name}`),i.attributes.xmlid&&a.push(`xmlid: ${i.attributes.xmlid}`),i.attributes.label&&a.push(`label: ${i.attributes.label}`),i.attributes.component&&a.push(`component: ${i.attributes.component}`),a.length===0?r:`---
3
+ ${a.join(`
4
4
  `)}
5
5
  ---
6
6
 
7
- ${i}`}exports.ptxastToMarkdown=q;exports.ptxastToMdast=v;
7
+ ${r}`}exports.ptxastToMarkdown=q;exports.ptxastToMdast=T;
package/dist/index.js CHANGED
@@ -1,20 +1,20 @@
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
- const g = /* @__PURE__ */ new Set([
6
- ...b,
7
- ...T.filter((e) => !I(e))
8
- ]), k = new Set(
9
- S
10
- ), v = /* @__PURE__ */ new Set([
1
+ import { DIVISION_HIERARCHY as T, EXTRA_DIVISION_TYPES as I, isTitlelessDivisionType as S, TITLELESS_DIVISION_TYPES as E, getPtxTextContent as h } from "@pretextbook/ptxast";
2
+ import { toMarkdown as w, defaultHandlers as _ } from "mdast-util-to-markdown";
3
+ import { directiveToMarkdown as P } from "mdast-util-directive";
4
+ import { mathToMarkdown as D } from "mdast-util-math";
5
+ const k = /* @__PURE__ */ new Set([
6
+ ...T,
7
+ ...I.filter((e) => !S(e))
8
+ ]), v = new Set(
9
+ E
10
+ ), b = /* @__PURE__ */ new Set([
11
11
  "pretext",
12
12
  "book",
13
13
  "article",
14
14
  "slideshow",
15
15
  "frontmatter",
16
16
  "backmatter"
17
- ]), P = /* @__PURE__ */ new Map([
17
+ ]), N = /* @__PURE__ */ new Map([
18
18
  // theorem-like
19
19
  ["theorem", { name: "theorem", category: "theorem-like" }],
20
20
  ["lemma", { name: "lemma", category: "theorem-like" }],
@@ -53,172 +53,172 @@ const g = /* @__PURE__ */ new Set([
53
53
  ["solution", { name: "solution", category: "solution-like" }],
54
54
  ["hint", { name: "hint", category: "solution-like" }],
55
55
  ["answer", { name: "answer", category: "solution-like" }]
56
- ]), D = /* @__PURE__ */ new Set(["theorem-like", "definition-like"]);
57
- function N(e) {
56
+ ]), $ = /* @__PURE__ */ new Set(["theorem-like", "definition-like"]);
57
+ function M(e) {
58
58
  return {
59
59
  type: "root",
60
- children: h(e.children, 1)
60
+ children: y(e.children, 1)
61
61
  };
62
62
  }
63
63
  function x(e) {
64
64
  for (const t of e) {
65
65
  if (t.type !== "element") continue;
66
- const i = t;
67
- if (g.has(i.name) || k.has(i.name)) {
68
- const n = i.attributes ?? {}, r = {};
69
- 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 };
66
+ const n = t;
67
+ if (k.has(n.name) || v.has(n.name)) {
68
+ const r = n.attributes ?? {}, i = {};
69
+ return r["xml:id"] && (i.xmlid = r["xml:id"]), r.label && (i.label = r.label), r.component && (i.component = r.component), { name: n.name, attributes: i };
70
70
  }
71
- if (v.has(i.name)) {
72
- const n = x(i.children);
73
- if (n) return n;
71
+ if (b.has(n.name)) {
72
+ const r = x(n.children);
73
+ if (r) return r;
74
74
  }
75
75
  }
76
76
  }
77
- function h(e, t) {
78
- const i = [];
79
- for (const n of e) {
80
- if (n.type !== "element") continue;
81
- const r = n;
82
- if (v.has(r.name) || k.has(r.name))
83
- i.push(...h(r.children, t));
84
- else if (g.has(r.name))
85
- i.push(
86
- ...M(r, Math.min(t, 6))
77
+ function y(e, t) {
78
+ const n = [];
79
+ for (const r of e) {
80
+ if (r.type !== "element") continue;
81
+ const i = r;
82
+ if (b.has(i.name) || v.has(i.name))
83
+ n.push(...y(i.children, t));
84
+ else if (k.has(i.name))
85
+ n.push(
86
+ ...j(i, Math.min(t, 6))
87
87
  );
88
88
  else {
89
- const c = u(r);
90
- c !== null && i.push(c);
89
+ const a = u(i);
90
+ a !== null && n.push(a);
91
91
  }
92
92
  }
93
- return i;
93
+ return n;
94
94
  }
95
- function M(e, t) {
96
- const i = [], n = e.attributes ?? {}, r = e.children, c = r.find(
97
- (a) => a.type === "element" && a.name === "title"
98
- ), s = r.filter(
99
- (a) => !(a.type === "element" && a.name === "title")
100
- ), o = {
95
+ function j(e, t) {
96
+ const n = [], r = e.attributes ?? {}, i = e.children, a = i.find(
97
+ (f) => f.type === "element" && f.name === "title"
98
+ ), c = i.filter(
99
+ (f) => !(f.type === "element" && f.name === "title")
100
+ ), { "xml:id": o, ...l } = r, s = {
101
101
  type: "heading",
102
102
  depth: t,
103
- children: c ? c.children.map(f).filter(m) : [],
104
- ...n["xml:id"] ? { data: { id: n["xml:id"], hProperties: { id: n["xml:id"] } } } : {}
103
+ children: a ? a.children.map(p).filter(m) : [],
104
+ ...o ? { data: { id: o, attributes: l, hProperties: { id: o } } } : {}
105
105
  };
106
- i.push(o);
107
- const l = Math.min(t + 1, 6);
108
- return i.push(...h(s, l)), i;
106
+ n.push(s);
107
+ const d = Math.min(t + 1, 6);
108
+ return n.push(...y(c, d)), n;
109
109
  }
110
110
  function u(e) {
111
111
  switch (e.name) {
112
112
  case "p":
113
113
  return L(e);
114
114
  case "blockquote":
115
- return A(e);
115
+ return O(e);
116
116
  case "ol":
117
- return y(e, !0);
117
+ return g(e, !0);
118
118
  case "ul":
119
- return y(e, !1);
119
+ return g(e, !1);
120
120
  case "program":
121
- return j(e);
121
+ return R(e);
122
122
  case "me":
123
123
  case "men":
124
124
  case "md":
125
125
  case "mdn":
126
- return R(e);
126
+ return Y(e);
127
127
  default: {
128
- const t = P.get(e.name);
129
- return t ? Y(e, t) : null;
128
+ const t = N.get(e.name);
129
+ return t ? C(e, t) : null;
130
130
  }
131
131
  }
132
132
  }
133
133
  function L(e) {
134
134
  return {
135
135
  type: "paragraph",
136
- children: e.children.map(f).filter(m)
136
+ children: e.children.map(p).filter(m)
137
137
  };
138
138
  }
139
- function A(e) {
139
+ function O(e) {
140
140
  return {
141
141
  type: "blockquote",
142
142
  children: e.children.filter((t) => t.type === "element").map((t) => u(t)).filter(m)
143
143
  };
144
144
  }
145
- function y(e, t) {
145
+ function g(e, t) {
146
146
  return {
147
147
  type: "list",
148
148
  ordered: t,
149
149
  spread: !1,
150
- children: e.children.filter((i) => i.type === "element").map((i) => O(i))
150
+ children: e.children.filter((n) => n.type === "element").map((n) => A(n))
151
151
  };
152
152
  }
153
- function O(e) {
153
+ function A(e) {
154
154
  return {
155
155
  type: "listItem",
156
156
  spread: !1,
157
157
  children: e.children.filter((t) => t.type === "element").map((t) => u(t)).filter(m)
158
158
  };
159
159
  }
160
- function j(e) {
160
+ function R(e) {
161
161
  return {
162
162
  type: "code",
163
163
  lang: (e.attributes ?? {}).language ?? null,
164
- value: p(e)
164
+ value: h(e)
165
165
  };
166
166
  }
167
- function R(e) {
167
+ function Y(e) {
168
168
  const t = e.children;
169
- return t.length > 0 && t[0].type === "text" ? { type: "math", value: p(e) } : { type: "math", value: t.filter(
170
- (n) => n.type === "element" && n.name === "mrow"
171
- ).map((n) => p(n)).join(` \\\\
169
+ return t.length > 0 && t[0].type === "text" ? { type: "math", value: h(e) } : { type: "math", value: t.filter(
170
+ (r) => r.type === "element" && r.name === "mrow"
171
+ ).map((r) => h(r)).join(` \\\\
172
172
  `) };
173
173
  }
174
- function Y(e, t) {
175
- const i = e.attributes ?? {}, n = e.children, r = {};
176
- i["xml:id"] && (r.id = i["xml:id"]);
177
- for (const [o, l] of Object.entries(i))
178
- o === "xml:id" || l == null || (r[o] = l);
179
- const c = n.find(
174
+ function C(e, t) {
175
+ const n = e.attributes ?? {}, r = e.children, i = {};
176
+ n["xml:id"] && (i.id = n["xml:id"]);
177
+ for (const [o, l] of Object.entries(n))
178
+ o === "xml:id" || l == null || (i[o] = l);
179
+ const a = r.find(
180
180
  (o) => o.type === "element" && o.name === "title"
181
- ), s = [];
182
- if (c) {
181
+ ), c = [];
182
+ if (a) {
183
183
  const o = {
184
184
  type: "paragraph",
185
185
  data: { directiveLabel: !0 },
186
- children: c.children.map(f).filter(m)
186
+ children: a.children.map(p).filter(m)
187
187
  };
188
- s.push(o);
188
+ c.push(o);
189
189
  }
190
- if (D.has(t.category))
191
- for (const o of n) {
190
+ if ($.has(t.category))
191
+ for (const o of r) {
192
192
  if (o.type !== "element") continue;
193
193
  const l = o;
194
194
  if (l.name !== "title")
195
195
  if (l.name === "statement")
196
- for (const a of l.children) {
197
- if (a.type !== "element") continue;
198
- const d = u(a);
199
- d && s.push(d);
196
+ for (const s of l.children) {
197
+ if (s.type !== "element") continue;
198
+ const d = u(s);
199
+ d && c.push(d);
200
200
  }
201
201
  else {
202
- const a = u(l);
203
- a && s.push(a);
202
+ const s = u(l);
203
+ s && c.push(s);
204
204
  }
205
205
  }
206
206
  else
207
- for (const o of n) {
207
+ for (const o of r) {
208
208
  if (o.type !== "element") continue;
209
209
  const l = o;
210
210
  if (l.name === "title") continue;
211
- const a = u(l);
212
- a && s.push(a);
211
+ const s = u(l);
212
+ s && c.push(s);
213
213
  }
214
214
  return {
215
215
  type: "containerDirective",
216
216
  name: t.name,
217
- attributes: Object.keys(r).length > 0 ? r : void 0,
218
- children: s
217
+ attributes: Object.keys(i).length > 0 ? i : void 0,
218
+ children: c
219
219
  };
220
220
  }
221
- function f(e) {
221
+ function p(e) {
222
222
  if (e.type === "text")
223
223
  return {
224
224
  type: "text",
@@ -230,38 +230,59 @@ function f(e) {
230
230
  case "em":
231
231
  return {
232
232
  type: "emphasis",
233
- children: t.children.map(f).filter(m)
233
+ children: t.children.map(p).filter(m)
234
234
  };
235
235
  case "alert":
236
236
  return {
237
237
  type: "strong",
238
- children: t.children.map(f).filter(m)
238
+ children: t.children.map(p).filter(m)
239
239
  };
240
240
  case "c":
241
- return { type: "inlineCode", value: p(t) };
241
+ return { type: "inlineCode", value: h(t) };
242
242
  case "m":
243
- return { type: "inlineMath", value: p(t) };
243
+ return { type: "inlineMath", value: h(t) };
244
+ case "xref":
245
+ return V(t);
244
246
  default:
245
247
  return null;
246
248
  }
247
249
  }
250
+ function V(e) {
251
+ const t = e.attributes ?? {}, n = t.ref;
252
+ return !n || /[\s,]/.test(n) || Object.keys(t).length > 1 ? null : {
253
+ type: "link",
254
+ url: `#${n}`,
255
+ children: e.children.map(p).filter(m)
256
+ };
257
+ }
248
258
  function m(e) {
249
259
  return e !== null;
250
260
  }
251
- function H(e) {
252
- const t = N(e), i = E(t, {
253
- extensions: [w(), _()]
254
- }), n = x(e.children);
255
- if (!n) return i;
256
- const r = [];
257
- 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 : `---
258
- ${r.join(`
261
+ function q(e) {
262
+ return (t, n, r, i) => {
263
+ const a = _.heading(t, n, r, i), c = t.data;
264
+ if (!c?.id || t === e) return a;
265
+ const o = Object.entries(c.attributes ?? {}).map(
266
+ ([l, s]) => ` ${l}="${s}"`
267
+ );
268
+ return `${a} {#${c.id}${o.join("")}}`;
269
+ };
270
+ }
271
+ function z(e) {
272
+ const t = M(e), n = t.children.find((c) => c.type === "heading"), r = w(t, {
273
+ extensions: [P(), D()],
274
+ handlers: { heading: q(n) }
275
+ }), i = x(e.children);
276
+ if (!i) return r;
277
+ const a = [];
278
+ return i.name !== "chapter" && a.push(`division: ${i.name}`), i.attributes.xmlid && a.push(`xmlid: ${i.attributes.xmlid}`), i.attributes.label && a.push(`label: ${i.attributes.label}`), i.attributes.component && a.push(`component: ${i.attributes.component}`), a.length === 0 ? r : `---
279
+ ${a.join(`
259
280
  `)}
260
281
  ---
261
282
 
262
- ${i}`;
283
+ ${r}`;
263
284
  }
264
285
  export {
265
- H as ptxastToMarkdown,
266
- N as ptxastToMdast
286
+ z as ptxastToMarkdown,
287
+ M as ptxastToMdast
267
288
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretextbook/ptxast-util-to-mdast",
3
- "version": "0.0.9",
3
+ "version": "0.1.0",
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.1.0",
42
+ "@pretextbook/ptxast": "^0.2.0",
43
43
  "@types/mdast": "^4.0.0",
44
44
  "mdast-util-directive": "^3.0.0",
45
45
  "mdast-util-math": "^3.0.0",