@pretextbook/ptxast-util-to-mdast 0.1.0 → 0.2.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 +6 -4
- package/dist/index.js +360 -118
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("@pretextbook/ptxast"),D=require("mdast-util-to-markdown"),Y=require("mdast-util-directive"),B=require("mdast-util-gfm-table"),H=require("mdast-util-math"),U=/["&'<>`]/g,V=/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,X=/[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g,W=/[|\\{}()[\]^$+*?.]/g,C=new WeakMap;function G(e,t){if(e=e.replace(t.subset?K(t.subset):U,r),t.subset||t.escapeOnly)return e;return e.replace(V,n).replace(X,r);function n(o,i,c){return t.format((o.charCodeAt(0)-55296)*1024+o.charCodeAt(1)-56320+65536,c.charCodeAt(i+2),t)}function r(o,i,c){return t.format(o.charCodeAt(0),c.charCodeAt(i+1),t)}}function K(e){let t=C.get(e);return t||(t=z(e),C.set(e,t)),t}function z(e){const t=[];let n=-1;for(;++n<e.length;)t.push(e[n].replace(W,"\\$&"));return new RegExp("(?:"+t.join("|")+")","g")}function J(e){return"&#x"+e.toString(16).toUpperCase()+";"}function Q(e,t){return G(e,Object.assign({format:J},t))}const Z=/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;function g(e,t,n){const r=ee(e);return n?r.replace(n,o):o(r);function o(i){return Q(i,{subset:t})}}function ee(e){return String(e||"").replace(Z,"")}const te=/]]>/g,ne=[">"];function re(e){return"<![CDATA["+g(e.value,ne,te)+"]]>"}function oe(e){return"<!--"+g(e.value,["-"])+"-->"}const ie=[" ",`
|
|
2
|
+
`," ",'"',"&","'","/","<","=",">"];function k(e){return g(e,ie)}function I(e,t){const n=String(e);if(typeof t!="string")throw new TypeError("Expected character");let r=0,o=n.indexOf(t);for(;o!==-1;)r++,o=n.indexOf(t,o+t.length);return r}function T(e,t){const n=String(e);let r=t.options.quote||'"';if(t.options.quoteSmart){const o=r==='"'?"'":'"';I(n,r)>I(n,o)&&(r=o)}return r+g(n,["<","&",r])+r}function le(e,t){const n=k(e.name),r=e.public,o=e.system;let i="<!DOCTYPE";return n!==""&&(i+=" "+n),r?i+=" PUBLIC "+T(r,t):o&&(i+=" SYSTEM"),o&&(i+=" "+T(o,t)),i+">"}const ce={}.hasOwnProperty;function ae(e,t){const n=k(e.name),r=R(e,t),o=e.attributes||{},i=r?!1:t.options.closeEmptyElements,c=[];let l;for(l in o)if(ce.call(o,l)){const a=o[l];a!=null&&c.push(k(l)+"="+T(a,t))}return"<"+n+(c.length===0?"":" "+c.join(" "))+(i?(t.options.tightClose?"":" ")+"/":"")+">"+r+(i?"":"</"+n+">")}const se=/\?>/g,ue=[">"];function fe(e){const t=k(e.name)||"x",n=g(e.value,ue,se);return"<?"+t+(n?" "+n:"")+"?>"}const me=["&","<"];function M(e){return g(e.value,me)}function he(e,t){return t.options.allowDangerousXml?e.value:M(e)}const pe={}.hasOwnProperty,P={cdata:re,comment:oe,doctype:le,element:ae,instruction:fe,raw:he,root:R,text:M};function O(e,t){const n=e&&e.type;if(!n)throw new Error("Expected node, not `"+e+"`");if(!pe.call(P,n))throw new Error("Cannot compile unknown node `"+n+"`");const r=P[n];return r(e,t)}function R(e,t){const n=e&&e.children||[];let r=-1;const o=[];for(;++r<n.length;)o[r]=O(n[r],t);return o.join("")}function v(e,t){const n={options:t||{}};if(typeof n.options.quote=="string"&&n.options.quote!=='"'&&n.options.quote!=="'")throw new Error("Invalid quote `"+n.options.quote+"`, expected `'` or `\"`");const r=Array.isArray(e)?{type:"root",children:e}:e;return O(r,n)}const j=new Set([...f.DIVISION_HIERARCHY,...f.EXTRA_DIVISION_TYPES.filter(e=>!f.isTitlelessDivisionType(e))]),q=new Set(f.TITLELESS_DIVISION_TYPES),N=new Set(["pretext","book","article","slideshow","frontmatter","backmatter"]),de=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"}],["table",{name:"table",category:"figure-like"}]]),ge=new Set(["theorem-like","definition-like"]);function _(e){return{type:"root",children:E(e.children,1)}}function L(e){for(const t of e){if(t.type!=="element")continue;const n=t;if(j.has(n.name)||q.has(n.name)){const r=n.attributes??{},o={};return r["xml:id"]&&(o.xmlid=r["xml:id"]),r.label&&(o.label=r.label),r.component&&(o.component=r.component),{name:n.name,attributes:o}}if(N.has(n.name)){const r=L(n.children);if(r)return r}}}function E(e,t){const n=[];for(const r of e){if(r.type!=="element")continue;const o=r;if(N.has(o.name)||q.has(o.name))n.push(...E(o.children,t));else if(j.has(o.name))n.push(...ye(o,Math.min(t,6)));else{const i=d(o);i!==null&&n.push(i)}}return n}function ye(e,t){const n=[],r=e.attributes??{},o=e.children,i=o.find(s=>s.type==="element"&&s.name==="title"),c=o.filter(s=>!(s.type==="element"&&s.name==="title")),{"xml:id":l,...a}=r,u={type:"heading",depth:t,children:i?i.children.map(p).filter(h):[],...l?{data:{id:l,attributes:a,hProperties:{id:l}}}:{}};n.push(u);const y=Math.min(t+1,6);return n.push(...E(c,y)),n}function d(e){switch(e.name){case"p":return xe(e);case"blockquote":return be(e);case"ol":return A(e,!0);case"ul":return A(e,!1);case"program":return ve(e);case"tabular":return we(e);case"me":case"men":case"md":case"mdn":return Pe(e);default:{const t=de.get(e.name);return t?Ae(e,t):null}}}function xe(e){return{type:"paragraph",children:e.children.map(p).filter(h)}}function be(e){return{type:"blockquote",children:e.children.filter(t=>t.type==="element").map(t=>d(t)).filter(h)}}function A(e,t){return{type:"list",ordered:t,spread:!1,children:e.children.filter(n=>n.type==="element").map(n=>ke(n))}}function ke(e){return{type:"listItem",spread:!1,children:e.children.filter(t=>t.type==="element").map(t=>d(t)).filter(h)}}function ve(e){return{type:"code",lang:(e.attributes??{}).language??null,value:f.getPtxTextContent(e)}}const $=new Set(["left","center","right"]);function we(e){return Ee(e)??{type:"html",value:Te(e)}}const w={closeEmptyElements:!0,tightClose:!0};function Te(e){const t=e.children.filter(i=>!(i.type==="text"&&/^\s*$/.test(i.value)));if(t.length===0||t.some(i=>i.type==="text"))return v(e,w);const r=v({...e,children:[]},w).replace(/\/>$/,">"),o=t.map(i=>` ${v(i,w)}`);return[r,...o,`</${e.name}>`].join(`
|
|
3
|
+
`)}function Ee(e){const{halign:t,...n}=e.attributes??{};if(!b(n)||t!=null&&!$.has(t))return null;const r=Ie(e),o=r.filter(s=>s.name==="col"),i=r.filter(s=>s.name==="row");if(i.length===0||o.length+i.length!==r.length||r.findIndex(s=>s.name==="row")!==o.length)return null;const c=[];for(const s of o){const{halign:m,...x}=s.attributes??{};if(!b(x)||m!=null&&!$.has(m))return null;c.push(m??t??null)}const l=[];let a=!1;for(const[s,m]of i.entries()){const{header:x,...F}=m.attributes??{};if(!b(F))return null;if(x==="yes"){if(s!==0)return null;a=!0}else if(x!=null&&x!=="no")return null;const S=Se(m);if(!S)return null;l.push(S)}const u=Math.max(o.length,...l.map(s=>s.children.length));if(u===0)return null;a||l.unshift({type:"tableRow",children:[]});for(const s of l)for(;s.children.length<u;)s.children.push({type:"tableCell",children:[]});return{type:"table",align:Array.from({length:u},(s,m)=>c[m]??t??null),children:l}}function Se(e){const t=e.children.filter(r=>!(r.type==="text"&&/^\s*$/.test(r.value))),n=[];for(const r of t){if(r.type!=="element"||r.name!=="cell"||!b(r.attributes??{}))return null;const o=Ce(r);if(!o)return null;n.push({type:"tableCell",children:o})}return{type:"tableRow",children:n}}function Ce(e){const t=[];for(const o of e.children){if(o.type==="comment")continue;const i=p(o);if(!i)return null;i.type==="text"&&(i.value=i.value.replace(/\s+/g," ")),t.push(i)}const n=t[0];n?.type==="text"&&(n.value=n.value.trimStart());const r=t[t.length-1];return r?.type==="text"&&(r.value=r.value.trimEnd()),t.filter(o=>o.type!=="text"||o.value!=="")}function b(e){return Object.values(e).every(t=>t==null)}function Ie(e){const t=[];for(const n of e.children)if(n.type==="element")t.push(n);else if(n.type==="text"&&!/^\s*$/.test(n.value))return[];return t}function Pe(e){const t=e.children;return t.length>0&&t[0].type==="text"?{type:"math",value:f.getPtxTextContent(e)}:{type:"math",value:t.filter(r=>r.type==="element"&&r.name==="mrow").map(r=>f.getPtxTextContent(r)).join(` \\\\
|
|
4
|
+
`)}}function Ae(e,t){const n=e.attributes??{},r=e.children,o={};n["xml:id"]&&(o.id=n["xml:id"]);for(const[l,a]of Object.entries(n))l==="xml:id"||a==null||(o[l]=a);const i=r.find(l=>l.type==="element"&&l.name==="title"),c=[];if(i){const l={type:"paragraph",data:{directiveLabel:!0},children:i.children.map(p).filter(h)};c.push(l)}if(ge.has(t.category))for(const l of r){if(l.type!=="element")continue;const a=l;if(a.name!=="title")if(a.name==="statement")for(const u of a.children){if(u.type!=="element")continue;const y=d(u);y&&c.push(y)}else{const u=d(a);u&&c.push(u)}}else for(const l of r){if(l.type!=="element")continue;const a=l;if(a.name==="title")continue;const u=d(a);u&&c.push(u)}return{type:"containerDirective",name:t.name,attributes:Object.keys(o).length>0?o:void 0,children:c}}function p(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(p).filter(h)};case"alert":return{type:"strong",children:t.children.map(p).filter(h)};case"c":return{type:"inlineCode",value:f.getPtxTextContent(t)};case"m":return{type:"inlineMath",value:f.getPtxTextContent(t)};case"xref":return $e(t);default:return null}}function $e(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(p).filter(h)}}function h(e){return e!==null}function De(e){return(t,n,r,o)=>{const i=D.defaultHandlers.heading(t,n,r,o),c=t.data;if(!c?.id||t===e)return i;const l=Object.entries(c.attributes??{}).map(([a,u])=>` ${a}="${u}"`);return`${i} {#${c.id}${l.join("")}}`}}function Me(e){const t=_(e),n=t.children.find(c=>c.type==="heading"),r=D.toMarkdown(t,{extensions:[Y.directiveToMarkdown(),H.mathToMarkdown(),B.gfmTableToMarkdown()],handlers:{heading:De(n)}}),o=L(e.children);if(!o)return r;const i=[];return o.name!=="chapter"&&i.push(`division: ${o.name}`),o.attributes.xmlid&&i.push(`xmlid: ${o.attributes.xmlid}`),o.attributes.label&&i.push(`label: ${o.attributes.label}`),o.attributes.component&&i.push(`component: ${o.attributes.component}`),i.length===0?r:`---
|
|
5
|
+
${i.join(`
|
|
4
6
|
`)}
|
|
5
7
|
---
|
|
6
8
|
|
|
7
|
-
${r}`}exports.ptxastToMarkdown=
|
|
9
|
+
${r}`}exports.ptxastToMarkdown=Me;exports.ptxastToMdast=_;
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,169 @@
|
|
|
1
|
-
import { DIVISION_HIERARCHY as
|
|
2
|
-
import { toMarkdown as
|
|
3
|
-
import { directiveToMarkdown as
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
]
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { DIVISION_HIERARCHY as q, EXTRA_DIVISION_TYPES as F, isTitlelessDivisionType as Y, TITLELESS_DIVISION_TYPES as B, getPtxTextContent as x } from "@pretextbook/ptxast";
|
|
2
|
+
import { toMarkdown as V, defaultHandlers as H } from "mdast-util-to-markdown";
|
|
3
|
+
import { directiveToMarkdown as X } from "mdast-util-directive";
|
|
4
|
+
import { gfmTableToMarkdown as W } from "mdast-util-gfm-table";
|
|
5
|
+
import { mathToMarkdown as U } from "mdast-util-math";
|
|
6
|
+
const G = /["&'<>`]/g, K = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, z = (
|
|
7
|
+
// eslint-disable-next-line no-control-regex, unicorn/no-hex-escape
|
|
8
|
+
/[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g
|
|
9
|
+
), J = /[|\\{}()[\]^$+*?.]/g, I = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
function Q(e, t) {
|
|
11
|
+
if (e = e.replace(
|
|
12
|
+
t.subset ? Z(t.subset) : G,
|
|
13
|
+
r
|
|
14
|
+
), t.subset || t.escapeOnly)
|
|
15
|
+
return e;
|
|
16
|
+
return e.replace(K, n).replace(z, r);
|
|
17
|
+
function n(o, i, c) {
|
|
18
|
+
return t.format(
|
|
19
|
+
(o.charCodeAt(0) - 55296) * 1024 + o.charCodeAt(1) - 56320 + 65536,
|
|
20
|
+
c.charCodeAt(i + 2),
|
|
21
|
+
t
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
function r(o, i, c) {
|
|
25
|
+
return t.format(
|
|
26
|
+
o.charCodeAt(0),
|
|
27
|
+
c.charCodeAt(i + 1),
|
|
28
|
+
t
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function Z(e) {
|
|
33
|
+
let t = I.get(e);
|
|
34
|
+
return t || (t = ee(e), I.set(e, t)), t;
|
|
35
|
+
}
|
|
36
|
+
function ee(e) {
|
|
37
|
+
const t = [];
|
|
38
|
+
let n = -1;
|
|
39
|
+
for (; ++n < e.length; )
|
|
40
|
+
t.push(e[n].replace(J, "\\$&"));
|
|
41
|
+
return new RegExp("(?:" + t.join("|") + ")", "g");
|
|
42
|
+
}
|
|
43
|
+
function te(e) {
|
|
44
|
+
return "&#x" + e.toString(16).toUpperCase() + ";";
|
|
45
|
+
}
|
|
46
|
+
function ne(e, t) {
|
|
47
|
+
return Q(e, Object.assign({ format: te }, t));
|
|
48
|
+
}
|
|
49
|
+
const re = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;
|
|
50
|
+
function d(e, t, n) {
|
|
51
|
+
const r = oe(e);
|
|
52
|
+
return n ? r.replace(n, o) : o(r);
|
|
53
|
+
function o(i) {
|
|
54
|
+
return ne(i, { subset: t });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function oe(e) {
|
|
58
|
+
return String(e || "").replace(re, "");
|
|
59
|
+
}
|
|
60
|
+
const ie = /]]>/g, le = [">"];
|
|
61
|
+
function ce(e) {
|
|
62
|
+
return "<![CDATA[" + d(e.value, le, ie) + "]]>";
|
|
63
|
+
}
|
|
64
|
+
function ae(e) {
|
|
65
|
+
return "<!--" + d(e.value, ["-"]) + "-->";
|
|
66
|
+
}
|
|
67
|
+
const se = [" ", `
|
|
68
|
+
`, " ", '"', "&", "'", "/", "<", "=", ">"];
|
|
69
|
+
function k(e) {
|
|
70
|
+
return d(e, se);
|
|
71
|
+
}
|
|
72
|
+
function C(e, t) {
|
|
73
|
+
const n = String(e);
|
|
74
|
+
if (typeof t != "string")
|
|
75
|
+
throw new TypeError("Expected character");
|
|
76
|
+
let r = 0, o = n.indexOf(t);
|
|
77
|
+
for (; o !== -1; )
|
|
78
|
+
r++, o = n.indexOf(t, o + t.length);
|
|
79
|
+
return r;
|
|
80
|
+
}
|
|
81
|
+
function E(e, t) {
|
|
82
|
+
const n = String(e);
|
|
83
|
+
let r = t.options.quote || '"';
|
|
84
|
+
if (t.options.quoteSmart) {
|
|
85
|
+
const o = r === '"' ? "'" : '"';
|
|
86
|
+
C(n, r) > C(n, o) && (r = o);
|
|
87
|
+
}
|
|
88
|
+
return r + d(n, ["<", "&", r]) + r;
|
|
89
|
+
}
|
|
90
|
+
function ue(e, t) {
|
|
91
|
+
const n = k(e.name), r = e.public, o = e.system;
|
|
92
|
+
let i = "<!DOCTYPE";
|
|
93
|
+
return n !== "" && (i += " " + n), r ? i += " PUBLIC " + E(r, t) : o && (i += " SYSTEM"), o && (i += " " + E(o, t)), i + ">";
|
|
94
|
+
}
|
|
95
|
+
const fe = {}.hasOwnProperty;
|
|
96
|
+
function me(e, t) {
|
|
97
|
+
const n = k(e.name), r = R(e, t), o = e.attributes || {}, i = r ? !1 : t.options.closeEmptyElements, c = [];
|
|
98
|
+
let l;
|
|
99
|
+
for (l in o)
|
|
100
|
+
if (fe.call(o, l)) {
|
|
101
|
+
const a = o[l];
|
|
102
|
+
a != null && c.push(k(l) + "=" + E(a, t));
|
|
103
|
+
}
|
|
104
|
+
return "<" + n + (c.length === 0 ? "" : " " + c.join(" ")) + (i ? (t.options.tightClose ? "" : " ") + "/" : "") + ">" + r + (i ? "" : "</" + n + ">");
|
|
105
|
+
}
|
|
106
|
+
const he = /\?>/g, pe = [">"];
|
|
107
|
+
function de(e) {
|
|
108
|
+
const t = k(e.name) || "x", n = d(e.value, pe, he);
|
|
109
|
+
return "<?" + t + (n ? " " + n : "") + "?>";
|
|
110
|
+
}
|
|
111
|
+
const ge = ["&", "<"];
|
|
112
|
+
function P(e) {
|
|
113
|
+
return d(e.value, ge);
|
|
114
|
+
}
|
|
115
|
+
function ye(e, t) {
|
|
116
|
+
return t.options.allowDangerousXml ? e.value : P(e);
|
|
117
|
+
}
|
|
118
|
+
const xe = {}.hasOwnProperty, $ = {
|
|
119
|
+
cdata: ce,
|
|
120
|
+
comment: ae,
|
|
121
|
+
doctype: ue,
|
|
122
|
+
element: me,
|
|
123
|
+
instruction: de,
|
|
124
|
+
raw: ye,
|
|
125
|
+
root: R,
|
|
126
|
+
text: P
|
|
127
|
+
};
|
|
128
|
+
function O(e, t) {
|
|
129
|
+
const n = e && e.type;
|
|
130
|
+
if (!n)
|
|
131
|
+
throw new Error("Expected node, not `" + e + "`");
|
|
132
|
+
if (!xe.call($, n))
|
|
133
|
+
throw new Error("Cannot compile unknown node `" + n + "`");
|
|
134
|
+
const r = $[n];
|
|
135
|
+
return r(e, t);
|
|
136
|
+
}
|
|
137
|
+
function R(e, t) {
|
|
138
|
+
const n = e && e.children || [];
|
|
139
|
+
let r = -1;
|
|
140
|
+
const o = [];
|
|
141
|
+
for (; ++r < n.length; )
|
|
142
|
+
o[r] = O(n[r], t);
|
|
143
|
+
return o.join("");
|
|
144
|
+
}
|
|
145
|
+
function v(e, t) {
|
|
146
|
+
const n = { options: t || {} };
|
|
147
|
+
if (typeof n.options.quote == "string" && n.options.quote !== '"' && n.options.quote !== "'")
|
|
148
|
+
throw new Error(
|
|
149
|
+
"Invalid quote `" + n.options.quote + "`, expected `'` or `\"`"
|
|
150
|
+
);
|
|
151
|
+
const r = Array.isArray(e) ? { type: "root", children: e } : e;
|
|
152
|
+
return O(r, n);
|
|
153
|
+
}
|
|
154
|
+
const _ = /* @__PURE__ */ new Set([
|
|
155
|
+
...q,
|
|
156
|
+
...F.filter((e) => !Y(e))
|
|
157
|
+
]), N = new Set(
|
|
158
|
+
B
|
|
159
|
+
), j = /* @__PURE__ */ new Set([
|
|
11
160
|
"pretext",
|
|
12
161
|
"book",
|
|
13
162
|
"article",
|
|
14
163
|
"slideshow",
|
|
15
164
|
"frontmatter",
|
|
16
165
|
"backmatter"
|
|
17
|
-
]),
|
|
166
|
+
]), be = /* @__PURE__ */ new Map([
|
|
18
167
|
// theorem-like
|
|
19
168
|
["theorem", { name: "theorem", category: "theorem-like" }],
|
|
20
169
|
["lemma", { name: "lemma", category: "theorem-like" }],
|
|
@@ -52,173 +201,266 @@ const k = /* @__PURE__ */ new Set([
|
|
|
52
201
|
// solution-like
|
|
53
202
|
["solution", { name: "solution", category: "solution-like" }],
|
|
54
203
|
["hint", { name: "hint", category: "solution-like" }],
|
|
55
|
-
["answer", { name: "answer", category: "solution-like" }]
|
|
56
|
-
|
|
57
|
-
|
|
204
|
+
["answer", { name: "answer", category: "solution-like" }],
|
|
205
|
+
// figure-like: a titled wrapper; `:::table[Title]` around a pipe table
|
|
206
|
+
["table", { name: "table", category: "figure-like" }]
|
|
207
|
+
]), ke = /* @__PURE__ */ new Set(["theorem-like", "definition-like"]);
|
|
208
|
+
function ve(e) {
|
|
58
209
|
return {
|
|
59
210
|
type: "root",
|
|
60
|
-
children:
|
|
211
|
+
children: T(e.children, 1)
|
|
61
212
|
};
|
|
62
213
|
}
|
|
63
|
-
function
|
|
214
|
+
function M(e) {
|
|
64
215
|
for (const t of e) {
|
|
65
216
|
if (t.type !== "element") continue;
|
|
66
217
|
const n = t;
|
|
67
|
-
if (
|
|
68
|
-
const r = n.attributes ?? {},
|
|
69
|
-
return r["xml:id"] && (
|
|
218
|
+
if (_.has(n.name) || N.has(n.name)) {
|
|
219
|
+
const r = n.attributes ?? {}, o = {};
|
|
220
|
+
return r["xml:id"] && (o.xmlid = r["xml:id"]), r.label && (o.label = r.label), r.component && (o.component = r.component), { name: n.name, attributes: o };
|
|
70
221
|
}
|
|
71
|
-
if (
|
|
72
|
-
const r =
|
|
222
|
+
if (j.has(n.name)) {
|
|
223
|
+
const r = M(n.children);
|
|
73
224
|
if (r) return r;
|
|
74
225
|
}
|
|
75
226
|
}
|
|
76
227
|
}
|
|
77
|
-
function
|
|
228
|
+
function T(e, t) {
|
|
78
229
|
const n = [];
|
|
79
230
|
for (const r of e) {
|
|
80
231
|
if (r.type !== "element") continue;
|
|
81
|
-
const
|
|
82
|
-
if (
|
|
83
|
-
n.push(...
|
|
84
|
-
else if (
|
|
232
|
+
const o = r;
|
|
233
|
+
if (j.has(o.name) || N.has(o.name))
|
|
234
|
+
n.push(...T(o.children, t));
|
|
235
|
+
else if (_.has(o.name))
|
|
85
236
|
n.push(
|
|
86
|
-
...
|
|
237
|
+
...we(o, Math.min(t, 6))
|
|
87
238
|
);
|
|
88
239
|
else {
|
|
89
|
-
const
|
|
90
|
-
|
|
240
|
+
const i = p(o);
|
|
241
|
+
i !== null && n.push(i);
|
|
91
242
|
}
|
|
92
243
|
}
|
|
93
244
|
return n;
|
|
94
245
|
}
|
|
95
|
-
function
|
|
96
|
-
const n = [], r = e.attributes ?? {},
|
|
97
|
-
(
|
|
98
|
-
), c =
|
|
99
|
-
(
|
|
100
|
-
), { "xml:id":
|
|
246
|
+
function we(e, t) {
|
|
247
|
+
const n = [], r = e.attributes ?? {}, o = e.children, i = o.find(
|
|
248
|
+
(s) => s.type === "element" && s.name === "title"
|
|
249
|
+
), c = o.filter(
|
|
250
|
+
(s) => !(s.type === "element" && s.name === "title")
|
|
251
|
+
), { "xml:id": l, ...a } = r, u = {
|
|
101
252
|
type: "heading",
|
|
102
253
|
depth: t,
|
|
103
|
-
children:
|
|
104
|
-
...
|
|
254
|
+
children: i ? i.children.map(h).filter(m) : [],
|
|
255
|
+
...l ? { data: { id: l, attributes: a, hProperties: { id: l } } } : {}
|
|
105
256
|
};
|
|
106
|
-
n.push(
|
|
107
|
-
const
|
|
108
|
-
return n.push(...
|
|
257
|
+
n.push(u);
|
|
258
|
+
const g = Math.min(t + 1, 6);
|
|
259
|
+
return n.push(...T(c, g)), n;
|
|
109
260
|
}
|
|
110
|
-
function
|
|
261
|
+
function p(e) {
|
|
111
262
|
switch (e.name) {
|
|
112
263
|
case "p":
|
|
113
|
-
return
|
|
264
|
+
return Ee(e);
|
|
114
265
|
case "blockquote":
|
|
115
|
-
return
|
|
266
|
+
return Te(e);
|
|
116
267
|
case "ol":
|
|
117
|
-
return
|
|
268
|
+
return A(e, !0);
|
|
118
269
|
case "ul":
|
|
119
|
-
return
|
|
270
|
+
return A(e, !1);
|
|
120
271
|
case "program":
|
|
121
|
-
return
|
|
272
|
+
return Ie(e);
|
|
273
|
+
case "tabular":
|
|
274
|
+
return Ce(e);
|
|
122
275
|
case "me":
|
|
123
276
|
case "men":
|
|
124
277
|
case "md":
|
|
125
278
|
case "mdn":
|
|
126
|
-
return
|
|
279
|
+
return Re(e);
|
|
127
280
|
default: {
|
|
128
|
-
const t =
|
|
129
|
-
return t ?
|
|
281
|
+
const t = be.get(e.name);
|
|
282
|
+
return t ? _e(e, t) : null;
|
|
130
283
|
}
|
|
131
284
|
}
|
|
132
285
|
}
|
|
133
|
-
function
|
|
286
|
+
function Ee(e) {
|
|
134
287
|
return {
|
|
135
288
|
type: "paragraph",
|
|
136
|
-
children: e.children.map(
|
|
289
|
+
children: e.children.map(h).filter(m)
|
|
137
290
|
};
|
|
138
291
|
}
|
|
139
|
-
function
|
|
292
|
+
function Te(e) {
|
|
140
293
|
return {
|
|
141
294
|
type: "blockquote",
|
|
142
|
-
children: e.children.filter((t) => t.type === "element").map((t) =>
|
|
295
|
+
children: e.children.filter((t) => t.type === "element").map((t) => p(t)).filter(m)
|
|
143
296
|
};
|
|
144
297
|
}
|
|
145
|
-
function
|
|
298
|
+
function A(e, t) {
|
|
146
299
|
return {
|
|
147
300
|
type: "list",
|
|
148
301
|
ordered: t,
|
|
149
302
|
spread: !1,
|
|
150
|
-
children: e.children.filter((n) => n.type === "element").map((n) =>
|
|
303
|
+
children: e.children.filter((n) => n.type === "element").map((n) => Se(n))
|
|
151
304
|
};
|
|
152
305
|
}
|
|
153
|
-
function
|
|
306
|
+
function Se(e) {
|
|
154
307
|
return {
|
|
155
308
|
type: "listItem",
|
|
156
309
|
spread: !1,
|
|
157
|
-
children: e.children.filter((t) => t.type === "element").map((t) =>
|
|
310
|
+
children: e.children.filter((t) => t.type === "element").map((t) => p(t)).filter(m)
|
|
158
311
|
};
|
|
159
312
|
}
|
|
160
|
-
function
|
|
313
|
+
function Ie(e) {
|
|
161
314
|
return {
|
|
162
315
|
type: "code",
|
|
163
316
|
lang: (e.attributes ?? {}).language ?? null,
|
|
164
|
-
value:
|
|
317
|
+
value: x(e)
|
|
165
318
|
};
|
|
166
319
|
}
|
|
167
|
-
|
|
320
|
+
const D = /* @__PURE__ */ new Set(["left", "center", "right"]);
|
|
321
|
+
function Ce(e) {
|
|
322
|
+
return Ae(e) ?? { type: "html", value: $e(e) };
|
|
323
|
+
}
|
|
324
|
+
const w = { closeEmptyElements: !0, tightClose: !0 };
|
|
325
|
+
function $e(e) {
|
|
326
|
+
const t = e.children.filter(
|
|
327
|
+
(i) => !(i.type === "text" && /^\s*$/.test(i.value))
|
|
328
|
+
);
|
|
329
|
+
if (t.length === 0 || t.some((i) => i.type === "text"))
|
|
330
|
+
return v(e, w);
|
|
331
|
+
const r = v({ ...e, children: [] }, w).replace(/\/>$/, ">"), o = t.map((i) => ` ${v(i, w)}`);
|
|
332
|
+
return [r, ...o, `</${e.name}>`].join(`
|
|
333
|
+
`);
|
|
334
|
+
}
|
|
335
|
+
function Ae(e) {
|
|
336
|
+
const { halign: t, ...n } = e.attributes ?? {};
|
|
337
|
+
if (!b(n) || t != null && !D.has(t)) return null;
|
|
338
|
+
const r = Oe(e), o = r.filter((s) => s.name === "col"), i = r.filter((s) => s.name === "row");
|
|
339
|
+
if (i.length === 0 || o.length + i.length !== r.length || r.findIndex((s) => s.name === "row") !== o.length) return null;
|
|
340
|
+
const c = [];
|
|
341
|
+
for (const s of o) {
|
|
342
|
+
const { halign: f, ...y } = s.attributes ?? {};
|
|
343
|
+
if (!b(y) || f != null && !D.has(f)) return null;
|
|
344
|
+
c.push(f ?? t ?? null);
|
|
345
|
+
}
|
|
346
|
+
const l = [];
|
|
347
|
+
let a = !1;
|
|
348
|
+
for (const [s, f] of i.entries()) {
|
|
349
|
+
const { header: y, ...L } = f.attributes ?? {};
|
|
350
|
+
if (!b(L)) return null;
|
|
351
|
+
if (y === "yes") {
|
|
352
|
+
if (s !== 0) return null;
|
|
353
|
+
a = !0;
|
|
354
|
+
} else if (y != null && y !== "no")
|
|
355
|
+
return null;
|
|
356
|
+
const S = De(f);
|
|
357
|
+
if (!S) return null;
|
|
358
|
+
l.push(S);
|
|
359
|
+
}
|
|
360
|
+
const u = Math.max(
|
|
361
|
+
o.length,
|
|
362
|
+
...l.map((s) => s.children.length)
|
|
363
|
+
);
|
|
364
|
+
if (u === 0) return null;
|
|
365
|
+
a || l.unshift({ type: "tableRow", children: [] });
|
|
366
|
+
for (const s of l)
|
|
367
|
+
for (; s.children.length < u; )
|
|
368
|
+
s.children.push({ type: "tableCell", children: [] });
|
|
369
|
+
return { type: "table", align: Array.from(
|
|
370
|
+
{ length: u },
|
|
371
|
+
(s, f) => c[f] ?? t ?? null
|
|
372
|
+
), children: l };
|
|
373
|
+
}
|
|
374
|
+
function De(e) {
|
|
375
|
+
const t = e.children.filter(
|
|
376
|
+
(r) => !(r.type === "text" && /^\s*$/.test(r.value))
|
|
377
|
+
), n = [];
|
|
378
|
+
for (const r of t) {
|
|
379
|
+
if (r.type !== "element" || r.name !== "cell" || !b(r.attributes ?? {})) return null;
|
|
380
|
+
const o = Pe(r);
|
|
381
|
+
if (!o) return null;
|
|
382
|
+
n.push({ type: "tableCell", children: o });
|
|
383
|
+
}
|
|
384
|
+
return { type: "tableRow", children: n };
|
|
385
|
+
}
|
|
386
|
+
function Pe(e) {
|
|
387
|
+
const t = [];
|
|
388
|
+
for (const o of e.children) {
|
|
389
|
+
if (o.type === "comment") continue;
|
|
390
|
+
const i = h(o);
|
|
391
|
+
if (!i) return null;
|
|
392
|
+
i.type === "text" && (i.value = i.value.replace(/\s+/g, " ")), t.push(i);
|
|
393
|
+
}
|
|
394
|
+
const n = t[0];
|
|
395
|
+
n?.type === "text" && (n.value = n.value.trimStart());
|
|
396
|
+
const r = t[t.length - 1];
|
|
397
|
+
return r?.type === "text" && (r.value = r.value.trimEnd()), t.filter((o) => o.type !== "text" || o.value !== "");
|
|
398
|
+
}
|
|
399
|
+
function b(e) {
|
|
400
|
+
return Object.values(e).every((t) => t == null);
|
|
401
|
+
}
|
|
402
|
+
function Oe(e) {
|
|
403
|
+
const t = [];
|
|
404
|
+
for (const n of e.children)
|
|
405
|
+
if (n.type === "element") t.push(n);
|
|
406
|
+
else if (n.type === "text" && !/^\s*$/.test(n.value)) return [];
|
|
407
|
+
return t;
|
|
408
|
+
}
|
|
409
|
+
function Re(e) {
|
|
168
410
|
const t = e.children;
|
|
169
|
-
return t.length > 0 && t[0].type === "text" ? { type: "math", value:
|
|
411
|
+
return t.length > 0 && t[0].type === "text" ? { type: "math", value: x(e) } : { type: "math", value: t.filter(
|
|
170
412
|
(r) => r.type === "element" && r.name === "mrow"
|
|
171
|
-
).map((r) =>
|
|
413
|
+
).map((r) => x(r)).join(` \\\\
|
|
172
414
|
`) };
|
|
173
415
|
}
|
|
174
|
-
function
|
|
175
|
-
const n = e.attributes ?? {}, r = e.children,
|
|
176
|
-
n["xml:id"] && (
|
|
177
|
-
for (const [
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
(
|
|
416
|
+
function _e(e, t) {
|
|
417
|
+
const n = e.attributes ?? {}, r = e.children, o = {};
|
|
418
|
+
n["xml:id"] && (o.id = n["xml:id"]);
|
|
419
|
+
for (const [l, a] of Object.entries(n))
|
|
420
|
+
l === "xml:id" || a == null || (o[l] = a);
|
|
421
|
+
const i = r.find(
|
|
422
|
+
(l) => l.type === "element" && l.name === "title"
|
|
181
423
|
), c = [];
|
|
182
|
-
if (
|
|
183
|
-
const
|
|
424
|
+
if (i) {
|
|
425
|
+
const l = {
|
|
184
426
|
type: "paragraph",
|
|
185
427
|
data: { directiveLabel: !0 },
|
|
186
|
-
children:
|
|
428
|
+
children: i.children.map(h).filter(m)
|
|
187
429
|
};
|
|
188
|
-
c.push(
|
|
430
|
+
c.push(l);
|
|
189
431
|
}
|
|
190
|
-
if (
|
|
191
|
-
for (const
|
|
192
|
-
if (
|
|
193
|
-
const
|
|
194
|
-
if (
|
|
195
|
-
if (
|
|
196
|
-
for (const
|
|
197
|
-
if (
|
|
198
|
-
const
|
|
199
|
-
|
|
432
|
+
if (ke.has(t.category))
|
|
433
|
+
for (const l of r) {
|
|
434
|
+
if (l.type !== "element") continue;
|
|
435
|
+
const a = l;
|
|
436
|
+
if (a.name !== "title")
|
|
437
|
+
if (a.name === "statement")
|
|
438
|
+
for (const u of a.children) {
|
|
439
|
+
if (u.type !== "element") continue;
|
|
440
|
+
const g = p(u);
|
|
441
|
+
g && c.push(g);
|
|
200
442
|
}
|
|
201
443
|
else {
|
|
202
|
-
const
|
|
203
|
-
|
|
444
|
+
const u = p(a);
|
|
445
|
+
u && c.push(u);
|
|
204
446
|
}
|
|
205
447
|
}
|
|
206
448
|
else
|
|
207
|
-
for (const
|
|
208
|
-
if (
|
|
209
|
-
const
|
|
210
|
-
if (
|
|
211
|
-
const
|
|
212
|
-
|
|
449
|
+
for (const l of r) {
|
|
450
|
+
if (l.type !== "element") continue;
|
|
451
|
+
const a = l;
|
|
452
|
+
if (a.name === "title") continue;
|
|
453
|
+
const u = p(a);
|
|
454
|
+
u && c.push(u);
|
|
213
455
|
}
|
|
214
456
|
return {
|
|
215
457
|
type: "containerDirective",
|
|
216
458
|
name: t.name,
|
|
217
|
-
attributes: Object.keys(
|
|
459
|
+
attributes: Object.keys(o).length > 0 ? o : void 0,
|
|
218
460
|
children: c
|
|
219
461
|
};
|
|
220
462
|
}
|
|
221
|
-
function
|
|
463
|
+
function h(e) {
|
|
222
464
|
if (e.type === "text")
|
|
223
465
|
return {
|
|
224
466
|
type: "text",
|
|
@@ -230,59 +472,59 @@ function p(e) {
|
|
|
230
472
|
case "em":
|
|
231
473
|
return {
|
|
232
474
|
type: "emphasis",
|
|
233
|
-
children: t.children.map(
|
|
475
|
+
children: t.children.map(h).filter(m)
|
|
234
476
|
};
|
|
235
477
|
case "alert":
|
|
236
478
|
return {
|
|
237
479
|
type: "strong",
|
|
238
|
-
children: t.children.map(
|
|
480
|
+
children: t.children.map(h).filter(m)
|
|
239
481
|
};
|
|
240
482
|
case "c":
|
|
241
|
-
return { type: "inlineCode", value:
|
|
483
|
+
return { type: "inlineCode", value: x(t) };
|
|
242
484
|
case "m":
|
|
243
|
-
return { type: "inlineMath", value:
|
|
485
|
+
return { type: "inlineMath", value: x(t) };
|
|
244
486
|
case "xref":
|
|
245
|
-
return
|
|
487
|
+
return Ne(t);
|
|
246
488
|
default:
|
|
247
489
|
return null;
|
|
248
490
|
}
|
|
249
491
|
}
|
|
250
|
-
function
|
|
492
|
+
function Ne(e) {
|
|
251
493
|
const t = e.attributes ?? {}, n = t.ref;
|
|
252
494
|
return !n || /[\s,]/.test(n) || Object.keys(t).length > 1 ? null : {
|
|
253
495
|
type: "link",
|
|
254
496
|
url: `#${n}`,
|
|
255
|
-
children: e.children.map(
|
|
497
|
+
children: e.children.map(h).filter(m)
|
|
256
498
|
};
|
|
257
499
|
}
|
|
258
500
|
function m(e) {
|
|
259
501
|
return e !== null;
|
|
260
502
|
}
|
|
261
|
-
function
|
|
262
|
-
return (t, n, r,
|
|
263
|
-
const
|
|
264
|
-
if (!c?.id || t === e) return
|
|
265
|
-
const
|
|
266
|
-
([
|
|
503
|
+
function je(e) {
|
|
504
|
+
return (t, n, r, o) => {
|
|
505
|
+
const i = H.heading(t, n, r, o), c = t.data;
|
|
506
|
+
if (!c?.id || t === e) return i;
|
|
507
|
+
const l = Object.entries(c.attributes ?? {}).map(
|
|
508
|
+
([a, u]) => ` ${a}="${u}"`
|
|
267
509
|
);
|
|
268
|
-
return `${
|
|
510
|
+
return `${i} {#${c.id}${l.join("")}}`;
|
|
269
511
|
};
|
|
270
512
|
}
|
|
271
|
-
function
|
|
272
|
-
const t =
|
|
273
|
-
extensions: [
|
|
274
|
-
handlers: { heading:
|
|
275
|
-
}),
|
|
276
|
-
if (!
|
|
277
|
-
const
|
|
278
|
-
return
|
|
279
|
-
${
|
|
513
|
+
function Be(e) {
|
|
514
|
+
const t = ve(e), n = t.children.find((c) => c.type === "heading"), r = V(t, {
|
|
515
|
+
extensions: [X(), U(), W()],
|
|
516
|
+
handlers: { heading: je(n) }
|
|
517
|
+
}), o = M(e.children);
|
|
518
|
+
if (!o) return r;
|
|
519
|
+
const i = [];
|
|
520
|
+
return o.name !== "chapter" && i.push(`division: ${o.name}`), o.attributes.xmlid && i.push(`xmlid: ${o.attributes.xmlid}`), o.attributes.label && i.push(`label: ${o.attributes.label}`), o.attributes.component && i.push(`component: ${o.attributes.component}`), i.length === 0 ? r : `---
|
|
521
|
+
${i.join(`
|
|
280
522
|
`)}
|
|
281
523
|
---
|
|
282
524
|
|
|
283
525
|
${r}`;
|
|
284
526
|
}
|
|
285
527
|
export {
|
|
286
|
-
|
|
287
|
-
|
|
528
|
+
Be as ptxastToMarkdown,
|
|
529
|
+
ve as ptxastToMdast
|
|
288
530
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pretextbook/ptxast-util-to-mdast",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"@pretextbook/ptxast": "^0.2.0",
|
|
43
43
|
"@types/mdast": "^4.0.0",
|
|
44
44
|
"mdast-util-directive": "^3.0.0",
|
|
45
|
+
"mdast-util-gfm-table": "^2.0.0",
|
|
45
46
|
"mdast-util-math": "^3.0.0",
|
|
46
47
|
"mdast-util-to-markdown": "^2.0.0",
|
|
47
48
|
"xast-util-from-xml": "^4.0.0",
|