@pretextbook/ptxast-util-to-mdast 0.0.2 → 0.0.4
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 +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +250 -0
- package/dist/lib/ptxast-to-mdast.d.ts +4 -0
- package/dist/lib/ptxast-util-to-mdast.d.ts +7 -0
- package/index.cjs +1 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +3 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("@pretextbook/ptxast"),k=require("mdast-util-to-markdown"),x=require("mdast-util-directive"),v=require("mdast-util-math"),b=new Set(["chapter","section","subsection","subsubsection","paragraphs","appendix"]),T={chapter:1,section:2,subsection:3,subsubsection:4,paragraphs:5,appendix:1},w=new Set(["pretext","book","article","frontmatter","backmatter","part"]),P=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"}]]),M=new Set(["theorem-like","definition-like"]);function g(e){return{type:"root",children:d(e.children,2)}}function d(e,t){const n=[];for(const r of e){if(r.type!=="element")continue;const o=r;if(w.has(o.name))n.push(...d(o.children,t));else if(b.has(o.name)){const l=T[o.name]??t;n.push(...E(o,l))}else{const l=u(o);l!==null&&n.push(l)}}return n}function E(e,t){const n=[],r=e.attributes??{},o=e.children,l=o.find(a=>a.type==="element"&&a.name==="title"),s=o.filter(a=>!(a.type==="element"&&a.name==="title")),i={type:"heading",depth:t,children:l?l.children.map(h).filter(m):[],...r["xml:id"]?{data:{id:r["xml:id"],hProperties:{id:r["xml:id"]}}}:{}};n.push(i);const c=Math.min(t+1,6);return n.push(...d(s,c)),n}function u(e){switch(e.name){case"p":return S(e);case"blockquote":return C(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 I(e);default:{const t=P.get(e.name);return t?j(e,t):null}}}function S(e){return{type:"paragraph",children:e.children.map(h).filter(m)}}function C(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=>q(n))}}function q(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 I(e){const t=e.children;return t.length>0&&t[0].type==="text"?{type:"math",value:p.getPtxTextContent(e)}:{type:"math",value:t.filter(r=>r.type==="element"&&r.name==="mrow").map(r=>p.getPtxTextContent(r)).join(` \\\\
|
|
2
|
+
`)}}function j(e,t){const n=e.attributes??{},r=e.children,o={};n["xml:id"]&&(o.id=n["xml:id"]);for(const[i,c]of Object.entries(n))i==="xml:id"||c==null||(o[i]=c);const l=r.find(i=>i.type==="element"&&i.name==="title"),s=[];if(l){const i={type:"paragraph",data:{directiveLabel:!0},children:l.children.map(h).filter(m)};s.push(i)}if(M.has(t.category))for(const i of r){if(i.type!=="element")continue;const c=i;if(c.name!=="title")if(c.name==="statement")for(const a of c.children){if(a.type!=="element")continue;const f=u(a);f&&s.push(f)}else{const a=u(c);a&&s.push(a)}}else for(const i of r){if(i.type!=="element")continue;const c=i;if(c.name==="title")continue;const a=u(c);a&&s.push(a)}return{type:"containerDirective",name:t.name,attributes:Object.keys(o).length>0?o: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 N(e){const t=g(e);return k.toMarkdown(t,{extensions:[x.directiveToMarkdown(),v.mathToMarkdown()]})}exports.ptxastToMarkdown=N;exports.ptxastToMdast=g;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { getPtxTextContent as u } from "@pretextbook/ptxast";
|
|
2
|
+
import { toMarkdown as g } from "mdast-util-to-markdown";
|
|
3
|
+
import { directiveToMarkdown as k } from "mdast-util-directive";
|
|
4
|
+
import { mathToMarkdown as v } from "mdast-util-math";
|
|
5
|
+
const x = /* @__PURE__ */ new Set([
|
|
6
|
+
"chapter",
|
|
7
|
+
"section",
|
|
8
|
+
"subsection",
|
|
9
|
+
"subsubsection",
|
|
10
|
+
"paragraphs",
|
|
11
|
+
// appendix is division-like at the same level as chapter
|
|
12
|
+
"appendix"
|
|
13
|
+
]), b = {
|
|
14
|
+
chapter: 1,
|
|
15
|
+
section: 2,
|
|
16
|
+
subsection: 3,
|
|
17
|
+
subsubsection: 4,
|
|
18
|
+
paragraphs: 5,
|
|
19
|
+
appendix: 1
|
|
20
|
+
}, T = /* @__PURE__ */ new Set([
|
|
21
|
+
"pretext",
|
|
22
|
+
"book",
|
|
23
|
+
"article",
|
|
24
|
+
"frontmatter",
|
|
25
|
+
"backmatter",
|
|
26
|
+
"part"
|
|
27
|
+
]), w = /* @__PURE__ */ new Map([
|
|
28
|
+
// theorem-like
|
|
29
|
+
["theorem", { name: "theorem", category: "theorem-like" }],
|
|
30
|
+
["lemma", { name: "lemma", category: "theorem-like" }],
|
|
31
|
+
["corollary", { name: "corollary", category: "theorem-like" }],
|
|
32
|
+
["proposition", { name: "proposition", category: "theorem-like" }],
|
|
33
|
+
["claim", { name: "claim", category: "theorem-like" }],
|
|
34
|
+
["fact", { name: "fact", category: "theorem-like" }],
|
|
35
|
+
["conjecture", { name: "conjecture", category: "theorem-like" }],
|
|
36
|
+
["axiom", { name: "axiom", category: "theorem-like" }],
|
|
37
|
+
["principle", { name: "principle", category: "theorem-like" }],
|
|
38
|
+
["hypothesis", { name: "hypothesis", category: "theorem-like" }],
|
|
39
|
+
["algorithm", { name: "algorithm", category: "theorem-like" }],
|
|
40
|
+
// definition-like
|
|
41
|
+
["definition", { name: "definition", category: "definition-like" }],
|
|
42
|
+
["notation", { name: "notation", category: "definition-like" }],
|
|
43
|
+
// remark-like
|
|
44
|
+
["remark", { name: "remark", category: "remark-like" }],
|
|
45
|
+
["note", { name: "note", category: "remark-like" }],
|
|
46
|
+
["observation", { name: "observation", category: "remark-like" }],
|
|
47
|
+
["warning", { name: "warning", category: "remark-like" }],
|
|
48
|
+
["insight", { name: "insight", category: "remark-like" }],
|
|
49
|
+
["assemblage", { name: "assemblage", category: "remark-like" }],
|
|
50
|
+
// example-like
|
|
51
|
+
["example", { name: "example", category: "example-like" }],
|
|
52
|
+
["question", { name: "question", category: "example-like" }],
|
|
53
|
+
["problem", { name: "problem", category: "example-like" }],
|
|
54
|
+
["exercise", { name: "exercise", category: "example-like" }],
|
|
55
|
+
["activity", { name: "activity", category: "example-like" }],
|
|
56
|
+
["exploration", { name: "exploration", category: "example-like" }],
|
|
57
|
+
["investigation", { name: "investigation", category: "example-like" }],
|
|
58
|
+
["project", { name: "project", category: "example-like" }],
|
|
59
|
+
// proof-like
|
|
60
|
+
["proof", { name: "proof", category: "proof-like" }],
|
|
61
|
+
["case", { name: "case", category: "proof-like" }],
|
|
62
|
+
// solution-like
|
|
63
|
+
["solution", { name: "solution", category: "solution-like" }],
|
|
64
|
+
["hint", { name: "hint", category: "solution-like" }],
|
|
65
|
+
["answer", { name: "answer", category: "solution-like" }]
|
|
66
|
+
]), E = /* @__PURE__ */ new Set(["theorem-like", "definition-like"]);
|
|
67
|
+
function P(e) {
|
|
68
|
+
return {
|
|
69
|
+
type: "root",
|
|
70
|
+
children: f(e.children, 2)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function f(e, t) {
|
|
74
|
+
const n = [];
|
|
75
|
+
for (const r of e) {
|
|
76
|
+
if (r.type !== "element") continue;
|
|
77
|
+
const o = r;
|
|
78
|
+
if (T.has(o.name))
|
|
79
|
+
n.push(...f(o.children, t));
|
|
80
|
+
else if (x.has(o.name)) {
|
|
81
|
+
const l = b[o.name] ?? t;
|
|
82
|
+
n.push(...M(o, l));
|
|
83
|
+
} else {
|
|
84
|
+
const l = p(o);
|
|
85
|
+
l !== null && n.push(l);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return n;
|
|
89
|
+
}
|
|
90
|
+
function M(e, t) {
|
|
91
|
+
const n = [], r = e.attributes ?? {}, o = e.children, l = o.find(
|
|
92
|
+
(a) => a.type === "element" && a.name === "title"
|
|
93
|
+
), s = o.filter(
|
|
94
|
+
(a) => !(a.type === "element" && a.name === "title")
|
|
95
|
+
), i = {
|
|
96
|
+
type: "heading",
|
|
97
|
+
depth: t,
|
|
98
|
+
children: l ? l.children.map(h).filter(m) : [],
|
|
99
|
+
...r["xml:id"] ? { data: { id: r["xml:id"], hProperties: { id: r["xml:id"] } } } : {}
|
|
100
|
+
};
|
|
101
|
+
n.push(i);
|
|
102
|
+
const c = Math.min(t + 1, 6);
|
|
103
|
+
return n.push(...f(s, c)), n;
|
|
104
|
+
}
|
|
105
|
+
function p(e) {
|
|
106
|
+
switch (e.name) {
|
|
107
|
+
case "p":
|
|
108
|
+
return I(e);
|
|
109
|
+
case "blockquote":
|
|
110
|
+
return S(e);
|
|
111
|
+
case "ol":
|
|
112
|
+
return y(e, !0);
|
|
113
|
+
case "ul":
|
|
114
|
+
return y(e, !1);
|
|
115
|
+
case "program":
|
|
116
|
+
return N(e);
|
|
117
|
+
case "me":
|
|
118
|
+
case "men":
|
|
119
|
+
case "md":
|
|
120
|
+
case "mdn":
|
|
121
|
+
return j(e);
|
|
122
|
+
default: {
|
|
123
|
+
const t = w.get(e.name);
|
|
124
|
+
return t ? _(e, t) : null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function I(e) {
|
|
129
|
+
return {
|
|
130
|
+
type: "paragraph",
|
|
131
|
+
children: e.children.map(h).filter(m)
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function S(e) {
|
|
135
|
+
return {
|
|
136
|
+
type: "blockquote",
|
|
137
|
+
children: e.children.filter((t) => t.type === "element").map((t) => p(t)).filter(m)
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function y(e, t) {
|
|
141
|
+
return {
|
|
142
|
+
type: "list",
|
|
143
|
+
ordered: t,
|
|
144
|
+
spread: !1,
|
|
145
|
+
children: e.children.filter((n) => n.type === "element").map((n) => D(n))
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function D(e) {
|
|
149
|
+
return {
|
|
150
|
+
type: "listItem",
|
|
151
|
+
spread: !1,
|
|
152
|
+
children: e.children.filter((t) => t.type === "element").map((t) => p(t)).filter(m)
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function N(e) {
|
|
156
|
+
return {
|
|
157
|
+
type: "code",
|
|
158
|
+
lang: (e.attributes ?? {}).language ?? null,
|
|
159
|
+
value: u(e)
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function j(e) {
|
|
163
|
+
const t = e.children;
|
|
164
|
+
return t.length > 0 && t[0].type === "text" ? { type: "math", value: u(e) } : { type: "math", value: t.filter((r) => r.type === "element" && r.name === "mrow").map((r) => u(r)).join(` \\\\
|
|
165
|
+
`) };
|
|
166
|
+
}
|
|
167
|
+
function _(e, t) {
|
|
168
|
+
const n = e.attributes ?? {}, r = e.children, o = {};
|
|
169
|
+
n["xml:id"] && (o.id = n["xml:id"]);
|
|
170
|
+
for (const [i, c] of Object.entries(n))
|
|
171
|
+
i === "xml:id" || c == null || (o[i] = c);
|
|
172
|
+
const l = r.find(
|
|
173
|
+
(i) => i.type === "element" && i.name === "title"
|
|
174
|
+
), s = [];
|
|
175
|
+
if (l) {
|
|
176
|
+
const i = {
|
|
177
|
+
type: "paragraph",
|
|
178
|
+
data: { directiveLabel: !0 },
|
|
179
|
+
children: l.children.map(h).filter(m)
|
|
180
|
+
};
|
|
181
|
+
s.push(i);
|
|
182
|
+
}
|
|
183
|
+
if (E.has(t.category))
|
|
184
|
+
for (const i of r) {
|
|
185
|
+
if (i.type !== "element") continue;
|
|
186
|
+
const c = i;
|
|
187
|
+
if (c.name !== "title")
|
|
188
|
+
if (c.name === "statement")
|
|
189
|
+
for (const a of c.children) {
|
|
190
|
+
if (a.type !== "element") continue;
|
|
191
|
+
const d = p(a);
|
|
192
|
+
d && s.push(d);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
const a = p(c);
|
|
196
|
+
a && s.push(a);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
else
|
|
200
|
+
for (const i of r) {
|
|
201
|
+
if (i.type !== "element") continue;
|
|
202
|
+
const c = i;
|
|
203
|
+
if (c.name === "title") continue;
|
|
204
|
+
const a = p(c);
|
|
205
|
+
a && s.push(a);
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
type: "containerDirective",
|
|
209
|
+
name: t.name,
|
|
210
|
+
attributes: Object.keys(o).length > 0 ? o : void 0,
|
|
211
|
+
children: s
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function h(e) {
|
|
215
|
+
if (e.type === "text")
|
|
216
|
+
return { type: "text", value: e.value ?? "" };
|
|
217
|
+
if (e.type !== "element") return null;
|
|
218
|
+
const t = e;
|
|
219
|
+
switch (t.name) {
|
|
220
|
+
case "em":
|
|
221
|
+
return {
|
|
222
|
+
type: "emphasis",
|
|
223
|
+
children: t.children.map(h).filter(m)
|
|
224
|
+
};
|
|
225
|
+
case "alert":
|
|
226
|
+
return {
|
|
227
|
+
type: "strong",
|
|
228
|
+
children: t.children.map(h).filter(m)
|
|
229
|
+
};
|
|
230
|
+
case "c":
|
|
231
|
+
return { type: "inlineCode", value: u(t) };
|
|
232
|
+
case "m":
|
|
233
|
+
return { type: "inlineMath", value: u(t) };
|
|
234
|
+
default:
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function m(e) {
|
|
239
|
+
return e !== null;
|
|
240
|
+
}
|
|
241
|
+
function O(e) {
|
|
242
|
+
const t = P(e);
|
|
243
|
+
return g(t, {
|
|
244
|
+
extensions: [k(), v()]
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
export {
|
|
248
|
+
O as ptxastToMarkdown,
|
|
249
|
+
P as ptxastToMdast
|
|
250
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Root } from '@pretextbook/ptxast';
|
|
2
|
+
export { ptxastToMdast } from './ptxast-to-mdast.js';
|
|
3
|
+
/**
|
|
4
|
+
* Convert an xast Root (PreTeXt document) to a markdown string using the directive
|
|
5
|
+
* syntax for PreTeXt block environments.
|
|
6
|
+
*/
|
|
7
|
+
export declare function ptxastToMarkdown(root: Root): string;
|
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require('
|
|
1
|
+
module.exports = require('./dist/index.cjs');
|
package/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from
|
|
1
|
+
export * from "./dist/index.d.ts";
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '
|
|
1
|
+
export * from './dist/index.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pretextbook/ptxast-util-to-mdast",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
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",
|
|
@@ -32,14 +32,14 @@
|
|
|
32
32
|
"*.cjs",
|
|
33
33
|
"*.d.ts",
|
|
34
34
|
"*.d.cts",
|
|
35
|
-
"
|
|
35
|
+
"dist"
|
|
36
36
|
],
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "vite build",
|
|
39
39
|
"test": "vitest run"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@pretextbook/ptxast": "^0.0.
|
|
42
|
+
"@pretextbook/ptxast": "^0.0.4",
|
|
43
43
|
"@types/mdast": "^4.0.0",
|
|
44
44
|
"mdast-util-directive": "^3.0.0",
|
|
45
45
|
"mdast-util-math": "^3.0.0",
|