@pretextbook/ptxast-util-to-mdast 0.0.10 → 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 +4 -4
- package/dist/index.js +123 -102
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
`)}}function
|
|
3
|
-
${
|
|
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
|
-
${
|
|
7
|
+
${r}`}exports.ptxastToMarkdown=q;exports.ptxastToMdast=T;
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { DIVISION_HIERARCHY as
|
|
2
|
-
import { toMarkdown as
|
|
3
|
-
import { directiveToMarkdown as
|
|
4
|
-
import { mathToMarkdown as
|
|
5
|
-
const
|
|
6
|
-
...
|
|
7
|
-
...
|
|
8
|
-
]),
|
|
9
|
-
|
|
10
|
-
),
|
|
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
|
-
]),
|
|
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
|
-
]),
|
|
57
|
-
function
|
|
56
|
+
]), $ = /* @__PURE__ */ new Set(["theorem-like", "definition-like"]);
|
|
57
|
+
function M(e) {
|
|
58
58
|
return {
|
|
59
59
|
type: "root",
|
|
60
|
-
children:
|
|
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
|
|
67
|
-
if (
|
|
68
|
-
const
|
|
69
|
-
return
|
|
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 (
|
|
72
|
-
const
|
|
73
|
-
if (
|
|
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
|
|
78
|
-
const
|
|
79
|
-
for (const
|
|
80
|
-
if (
|
|
81
|
-
const
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
else if (
|
|
85
|
-
|
|
86
|
-
...
|
|
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
|
|
90
|
-
|
|
89
|
+
const a = u(i);
|
|
90
|
+
a !== null && n.push(a);
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
|
-
return
|
|
93
|
+
return n;
|
|
94
94
|
}
|
|
95
|
-
function
|
|
96
|
-
const
|
|
97
|
-
(
|
|
98
|
-
),
|
|
99
|
-
(
|
|
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:
|
|
104
|
-
...
|
|
103
|
+
children: a ? a.children.map(p).filter(m) : [],
|
|
104
|
+
...o ? { data: { id: o, attributes: l, hProperties: { id: o } } } : {}
|
|
105
105
|
};
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
return
|
|
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
|
|
115
|
+
return O(e);
|
|
116
116
|
case "ol":
|
|
117
|
-
return
|
|
117
|
+
return g(e, !0);
|
|
118
118
|
case "ul":
|
|
119
|
-
return
|
|
119
|
+
return g(e, !1);
|
|
120
120
|
case "program":
|
|
121
|
-
return
|
|
121
|
+
return R(e);
|
|
122
122
|
case "me":
|
|
123
123
|
case "men":
|
|
124
124
|
case "md":
|
|
125
125
|
case "mdn":
|
|
126
|
-
return
|
|
126
|
+
return Y(e);
|
|
127
127
|
default: {
|
|
128
|
-
const t =
|
|
129
|
-
return t ?
|
|
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(
|
|
136
|
+
children: e.children.map(p).filter(m)
|
|
137
137
|
};
|
|
138
138
|
}
|
|
139
|
-
function
|
|
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
|
|
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((
|
|
150
|
+
children: e.children.filter((n) => n.type === "element").map((n) => A(n))
|
|
151
151
|
};
|
|
152
152
|
}
|
|
153
|
-
function
|
|
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
|
|
160
|
+
function R(e) {
|
|
161
161
|
return {
|
|
162
162
|
type: "code",
|
|
163
163
|
lang: (e.attributes ?? {}).language ?? null,
|
|
164
|
-
value:
|
|
164
|
+
value: h(e)
|
|
165
165
|
};
|
|
166
166
|
}
|
|
167
|
-
function
|
|
167
|
+
function Y(e) {
|
|
168
168
|
const t = e.children;
|
|
169
|
-
return t.length > 0 && t[0].type === "text" ? { type: "math", value:
|
|
170
|
-
(
|
|
171
|
-
).map((
|
|
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
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
for (const [o, l] of Object.entries(
|
|
178
|
-
o === "xml:id" || l == null || (
|
|
179
|
-
const
|
|
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
|
-
),
|
|
182
|
-
if (
|
|
181
|
+
), c = [];
|
|
182
|
+
if (a) {
|
|
183
183
|
const o = {
|
|
184
184
|
type: "paragraph",
|
|
185
185
|
data: { directiveLabel: !0 },
|
|
186
|
-
children:
|
|
186
|
+
children: a.children.map(p).filter(m)
|
|
187
187
|
};
|
|
188
|
-
|
|
188
|
+
c.push(o);
|
|
189
189
|
}
|
|
190
|
-
if (
|
|
191
|
-
for (const o of
|
|
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
|
|
197
|
-
if (
|
|
198
|
-
const d = u(
|
|
199
|
-
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
|
|
203
|
-
|
|
202
|
+
const s = u(l);
|
|
203
|
+
s && c.push(s);
|
|
204
204
|
}
|
|
205
205
|
}
|
|
206
206
|
else
|
|
207
|
-
for (const o of
|
|
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
|
|
212
|
-
|
|
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(
|
|
218
|
-
children:
|
|
217
|
+
attributes: Object.keys(i).length > 0 ? i : void 0,
|
|
218
|
+
children: c
|
|
219
219
|
};
|
|
220
220
|
}
|
|
221
|
-
function
|
|
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(
|
|
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(
|
|
238
|
+
children: t.children.map(p).filter(m)
|
|
239
239
|
};
|
|
240
240
|
case "c":
|
|
241
|
-
return { type: "inlineCode", value:
|
|
241
|
+
return { type: "inlineCode", value: h(t) };
|
|
242
242
|
case "m":
|
|
243
|
-
return { type: "inlineMath", value:
|
|
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
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
${
|
|
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
|
-
${
|
|
283
|
+
${r}`;
|
|
263
284
|
}
|
|
264
285
|
export {
|
|
265
|
-
|
|
266
|
-
|
|
286
|
+
z as ptxastToMarkdown,
|
|
287
|
+
M as ptxastToMdast
|
|
267
288
|
};
|