@pretextbook/ptxast-util-to-mdast 0.0.10 → 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 CHANGED
@@ -1,7 +1,9 @@
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 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
- ${i}`}exports.ptxastToMarkdown=q;exports.ptxastToMdast=v;
9
+ ${r}`}exports.ptxastToMarkdown=Me;exports.ptxastToMdast=_;
package/dist/index.js CHANGED
@@ -1,20 +1,169 @@
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 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
- ]), P = /* @__PURE__ */ new Map([
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 g = /* @__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
- ]), D = /* @__PURE__ */ new Set(["theorem-like", "definition-like"]);
57
- function N(e) {
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: h(e.children, 1)
211
+ children: T(e.children, 1)
61
212
  };
62
213
  }
63
- function x(e) {
214
+ function M(e) {
64
215
  for (const t of e) {
65
216
  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 };
217
+ const n = t;
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 (v.has(i.name)) {
72
- const n = x(i.children);
73
- if (n) return n;
222
+ if (j.has(n.name)) {
223
+ const r = M(n.children);
224
+ if (r) return r;
74
225
  }
75
226
  }
76
227
  }
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))
228
+ function T(e, t) {
229
+ const n = [];
230
+ for (const r of e) {
231
+ if (r.type !== "element") continue;
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))
236
+ n.push(
237
+ ...we(o, Math.min(t, 6))
87
238
  );
88
239
  else {
89
- const c = u(r);
90
- c !== null && i.push(c);
240
+ const i = p(o);
241
+ i !== null && n.push(i);
91
242
  }
92
243
  }
93
- return i;
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 = {
244
+ return n;
245
+ }
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: c ? c.children.map(f).filter(m) : [],
104
- ...n["xml:id"] ? { data: { id: n["xml:id"], hProperties: { id: n["xml:id"] } } } : {}
254
+ children: i ? i.children.map(h).filter(m) : [],
255
+ ...l ? { data: { id: l, attributes: a, hProperties: { id: l } } } : {}
105
256
  };
106
- i.push(o);
107
- const l = Math.min(t + 1, 6);
108
- return i.push(...h(s, l)), i;
257
+ n.push(u);
258
+ const g = Math.min(t + 1, 6);
259
+ return n.push(...T(c, g)), n;
109
260
  }
110
- function u(e) {
261
+ function p(e) {
111
262
  switch (e.name) {
112
263
  case "p":
113
- return L(e);
264
+ return Ee(e);
114
265
  case "blockquote":
115
- return A(e);
266
+ return Te(e);
116
267
  case "ol":
117
- return y(e, !0);
268
+ return A(e, !0);
118
269
  case "ul":
119
- return y(e, !1);
270
+ return A(e, !1);
120
271
  case "program":
121
- return j(e);
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 R(e);
279
+ return Re(e);
127
280
  default: {
128
- const t = P.get(e.name);
129
- return t ? Y(e, t) : null;
281
+ const t = be.get(e.name);
282
+ return t ? _e(e, t) : null;
130
283
  }
131
284
  }
132
285
  }
133
- function L(e) {
286
+ function Ee(e) {
134
287
  return {
135
288
  type: "paragraph",
136
- children: e.children.map(f).filter(m)
289
+ children: e.children.map(h).filter(m)
137
290
  };
138
291
  }
139
- function A(e) {
292
+ function Te(e) {
140
293
  return {
141
294
  type: "blockquote",
142
- children: e.children.filter((t) => t.type === "element").map((t) => u(t)).filter(m)
295
+ children: e.children.filter((t) => t.type === "element").map((t) => p(t)).filter(m)
143
296
  };
144
297
  }
145
- function y(e, t) {
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((i) => i.type === "element").map((i) => O(i))
303
+ children: e.children.filter((n) => n.type === "element").map((n) => Se(n))
151
304
  };
152
305
  }
153
- function O(e) {
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) => u(t)).filter(m)
310
+ children: e.children.filter((t) => t.type === "element").map((t) => p(t)).filter(m)
158
311
  };
159
312
  }
160
- function j(e) {
313
+ function Ie(e) {
161
314
  return {
162
315
  type: "code",
163
316
  lang: (e.attributes ?? {}).language ?? null,
164
- value: p(e)
317
+ value: x(e)
165
318
  };
166
319
  }
167
- function R(e) {
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: p(e) } : { type: "math", value: t.filter(
170
- (n) => n.type === "element" && n.name === "mrow"
171
- ).map((n) => p(n)).join(` \\\\
411
+ return t.length > 0 && t[0].type === "text" ? { type: "math", value: x(e) } : { type: "math", value: t.filter(
412
+ (r) => r.type === "element" && r.name === "mrow"
413
+ ).map((r) => x(r)).join(` \\\\
172
414
  `) };
173
415
  }
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(
180
- (o) => o.type === "element" && o.name === "title"
181
- ), s = [];
182
- if (c) {
183
- const o = {
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"
423
+ ), c = [];
424
+ if (i) {
425
+ const l = {
184
426
  type: "paragraph",
185
427
  data: { directiveLabel: !0 },
186
- children: c.children.map(f).filter(m)
428
+ children: i.children.map(h).filter(m)
187
429
  };
188
- s.push(o);
430
+ c.push(l);
189
431
  }
190
- if (D.has(t.category))
191
- for (const o of n) {
192
- if (o.type !== "element") continue;
193
- const l = o;
194
- if (l.name !== "title")
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);
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 a = u(l);
203
- a && s.push(a);
444
+ const u = p(a);
445
+ u && c.push(u);
204
446
  }
205
447
  }
206
448
  else
207
- for (const o of n) {
208
- if (o.type !== "element") continue;
209
- const l = o;
210
- if (l.name === "title") continue;
211
- const a = u(l);
212
- a && s.push(a);
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(r).length > 0 ? r : void 0,
218
- children: s
459
+ attributes: Object.keys(o).length > 0 ? o : void 0,
460
+ children: c
219
461
  };
220
462
  }
221
- function f(e) {
463
+ function h(e) {
222
464
  if (e.type === "text")
223
465
  return {
224
466
  type: "text",
@@ -230,38 +472,59 @@ function f(e) {
230
472
  case "em":
231
473
  return {
232
474
  type: "emphasis",
233
- children: t.children.map(f).filter(m)
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(f).filter(m)
480
+ children: t.children.map(h).filter(m)
239
481
  };
240
482
  case "c":
241
- return { type: "inlineCode", value: p(t) };
483
+ return { type: "inlineCode", value: x(t) };
242
484
  case "m":
243
- return { type: "inlineMath", value: p(t) };
485
+ return { type: "inlineMath", value: x(t) };
486
+ case "xref":
487
+ return Ne(t);
244
488
  default:
245
489
  return null;
246
490
  }
247
491
  }
492
+ function Ne(e) {
493
+ const t = e.attributes ?? {}, n = t.ref;
494
+ return !n || /[\s,]/.test(n) || Object.keys(t).length > 1 ? null : {
495
+ type: "link",
496
+ url: `#${n}`,
497
+ children: e.children.map(h).filter(m)
498
+ };
499
+ }
248
500
  function m(e) {
249
501
  return e !== null;
250
502
  }
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(`
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}"`
509
+ );
510
+ return `${i} {#${c.id}${l.join("")}}`;
511
+ };
512
+ }
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(`
259
522
  `)}
260
523
  ---
261
524
 
262
- ${i}`;
525
+ ${r}`;
263
526
  }
264
527
  export {
265
- H as ptxastToMarkdown,
266
- N as ptxastToMdast
528
+ Be as ptxastToMarkdown,
529
+ ve as ptxastToMdast
267
530
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretextbook/ptxast-util-to-mdast",
3
- "version": "0.0.10",
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",