@pretextbook/remark-pretext 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 ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("unified"),j=require("remark-parse"),z=require("remark-directive"),B=require("unist-util-visit"),q={theorem:{type:"theorem",category:"theorem-like",requiresStatement:!0},lemma:{type:"lemma",category:"theorem-like",requiresStatement:!0},corollary:{type:"corollary",category:"theorem-like",requiresStatement:!0},proposition:{type:"proposition",category:"theorem-like",requiresStatement:!0},claim:{type:"claim",category:"theorem-like",requiresStatement:!0},fact:{type:"fact",category:"theorem-like",requiresStatement:!0},conjecture:{type:"conjecture",category:"theorem-like",requiresStatement:!0},axiom:{type:"axiom",category:"theorem-like",requiresStatement:!0},principle:{type:"principle",category:"theorem-like",requiresStatement:!0},hypothesis:{type:"hypothesis",category:"theorem-like",requiresStatement:!0},algorithm:{type:"algorithm",category:"theorem-like",requiresStatement:!0},definition:{type:"definition",category:"definition-like",requiresStatement:!0},notation:{type:"notation",category:"definition-like",requiresStatement:!0},remark:{type:"remark",category:"remark-like",requiresStatement:!1},note:{type:"note",category:"remark-like",requiresStatement:!1},observation:{type:"observation",category:"remark-like",requiresStatement:!1},warning:{type:"warning",category:"remark-like",requiresStatement:!1},insight:{type:"insight",category:"remark-like",requiresStatement:!1},assemblage:{type:"assemblage",category:"remark-like",requiresStatement:!1},example:{type:"example",category:"example-like",requiresStatement:!1},question:{type:"question",category:"example-like",requiresStatement:!1},problem:{type:"problem",category:"example-like",requiresStatement:!1},exercise:{type:"exercise",category:"example-like",requiresStatement:!0,hasNestedTasks:!0},activity:{type:"activity",category:"example-like",requiresStatement:!1},exploration:{type:"exploration",category:"example-like",requiresStatement:!1},investigation:{type:"investigation",category:"example-like",requiresStatement:!1},project:{type:"project",category:"example-like",requiresStatement:!0,hasNestedTasks:!0},task:{type:"task",category:"example-like",requiresStatement:!0,hasNestedTasks:!0},proof:{type:"proof",category:"proof-like",requiresStatement:!1},case:{type:"case",category:"proof-like",requiresStatement:!1},solution:{type:"solution",category:"solution-like",requiresStatement:!1},hint:{type:"hint",category:"solution-like",requiresStatement:!1},answer:{type:"answer",category:"solution-like",requiresStatement:!1}},re=q;function oe(e){return q[e.toLowerCase()]}const F=new Set(Object.entries(q).filter(([e,t])=>t.category==="proof-like"||t.category==="solution-like").map(([e])=>e));function v(e,t,r){return{type:"element",name:e,attributes:r??{},children:t}}function C(e){return e.type==="containerDirective"&&e.name==="task"}function se(e){return e.type==="containerDirective"&&F.has(e.name)}function S(e){const t=[];for(const r of e)r.name==="ul"||r.name==="ol"?t.length>0&&t[t.length-1]?.name==="p"?t[t.length-1].children.push(r):t.push(v("p",[r])):t.push(r);return t}function ie(e,t,r,n,o,s,i){const l=[];if(r&&l.push(r),e.hasNestedTasks){const c=n.findIndex(C);if(c!==-1){const a=[];for(let p=0;p<n.length;p++)C(n[p])&&a.push(p);const u=a[a.length-1],d=n.slice(0,c),m=n.slice(c).filter(C),k=[],g=[],N=[];for(let p=c;p<n.length;p++){const f=n[p];if(!C(f)){if(p<u){k.push(f);continue}if(se(f)){N.push(f);continue}g.push(f)}}if(k.length>0&&(o.messages??(o.messages=[])).push({type:"warning",reason:`Dropped ${k.length} non-task node(s) between task directives in ${e.type}.`,category:"dropped-content-between-tasks",position:k[0].position?.start}),d.length>0){const p=S(d.map(f=>s(f,o)).filter(f=>f!==null));p.length>0&&l.push(v("introduction",p))}for(const p of m){const f=i(p,o);f&&l.push(f)}if(g.length>0){const p=S(g.map(f=>s(f,o)).filter(f=>f!==null));p.length>0&&l.push(v("conclusion",p))}for(const p of N){const f=i(p,o);f&&l.push(f)}}else $(l,e,n,o,s,i)}else $(l,e,n,o,s,i);return v(e.type,l,t)}function $(e,t,r,n,o,s){if(t.requiresStatement){const i=[],l=[];for(const c of r)if(c.type==="containerDirective"&&F.has(c.name)){const a=s(c,n);a&&l.push(a)}else i.push(c);if(i.length>0){const c=S(i.map(a=>o(a,n)).filter(a=>a!==null));c.length>0&&e.push(v("statement",c))}e.push(...l)}else{const i=S(r.map(l=>o(l,n)).filter(l=>l!==null));e.push(...i)}}function W(e){return{type:"text",value:e}}function h(e,t,r){return{type:"element",name:e,attributes:r??{},children:t}}function x(e,t,r){return h(e,[W(t)],r)}function U(e){const t=[];for(const r of e)r.name==="ul"||r.name==="ol"?t.length>0&&t[t.length-1]?.name==="p"?t[t.length-1].children.push(r):t.push(h("p",[r])):t.push(r);return t}function T(e,t){return le(e,t).tree}function le(e,t){const r=[],n={ancestors:[],depth:0,messages:r,source:t},o=e.children;return{tree:{type:"root",children:H(o,n)},messages:r}}const O=["chapter","section","subsection","subsubsection","paragraphs"];function ce(e){return O[Math.min(e-1,O.length-1)]}function H(e,t,r=!1){let n=7;for(const a of e)a.type==="heading"&&(n=Math.min(n,a.depth));if(n===7)return w(e,t,r);const o=[];let s=null,i=[],l=[];const c=()=>{s&&(o.push(ae(s,l,t)),s=null,l=[])};for(const a of e)a.type==="heading"&&a.depth===n?(!s&&i.length>0&&(o.push(...w(i,t,r)),i=[]),c(),s=a,l=[]):s?l.push(a):i.push(a);return!s&&i.length>0&&o.push(...w(i,t,r)),c(),o}function w(e,t,r=!1){const n=[];for(let o=0;o<e.length;o+=1){const s=e[o],i=E(s,t);if(i!==null){if(i.name==="p"&&i.children.length===1&&i.children[0].name==="md"){const l=i.children[0],c=n[n.length-1],a=o+1<e.length?E(e[o+1],t):null,u=a?.name==="p"?a.children:null;if(c?.name==="p")c.children.push(l),u&&(c.children.push(...u),o+=1);else{const d=[l];u&&(d.push(...u),o+=1),n.push(h("p",d))}continue}if((i.name==="ul"||i.name==="ol")&&n.length>0){const l=n[n.length-1];if(l?.name==="p"){l.children.push(i);continue}}n.push(i)}}return r?U(n):n}function ae(e,t,r){const n=ce(e.depth),o=h("title",b(e.children,r)),s=ue(e),i=H(t,{...r,depth:r.depth+1},!0),l=U(i);return h(n,[o,...l],s)}function ue(e){const t=e.data;return t?.id?{"xml:id":t.id}:void 0}const pe={paragraph:(e,t)=>Z(e,t),blockquote:(e,t)=>fe(e,t),list:(e,t)=>he(e,t),code:(e,t)=>me(e),containerDirective:(e,t)=>V(e,t)};function E(e,t){const r=pe[e.type];return r?r(e,t):(t.messages&&t.messages.push({type:"warning",reason:`Unknown block type: ${e.type}`,category:"unknown-block-type"}),null)}function Z(e,t){return h("p",b(e.children,t))}function fe(e,t){const r=e.children.filter(n=>n.type==="paragraph").map(n=>{const o={...t,ancestors:[...t.ancestors,e],depth:t.depth+1};return Z(n,o)});return h("blockquote",r)}function he(e,t){const r=e.children.map(n=>{const o={...t,ancestors:[...t.ancestors,e],depth:t.depth+1};return de(n,o)});return h(e.ordered?"ol":"ul",r)}function de(e,t){const r=w(e.children,t);return h("li",r)}function me(e,t){return x("program",e.value,e.lang?{language:e.lang}:void 0)}function ge(e,t){const r=e.value.split(/\\\\|[\r\n]+/).map(n=>n.trim()).filter(n=>n.length>0);return r.length>1?h("md",r.map(n=>x("mrow",n))):x("md",r[0]??"")}function ye(e,t){return e.meta==="inline"?x("m",e.value):ge(e)}function V(e,t){const r=oe(e.name);if(!r)return t.messages&&t.messages.push({type:"warning",reason:`Unknown directive: ${e.name}`,category:"unknown-directive"}),null;const n=ve(e),o=e.children,{title:s,body:i}=ke(o,t),l={...t,ancestors:[...t.ancestors,e],depth:t.depth+1};return ie(r,n,s,i,l,(c,a)=>E(c,a),(c,a)=>V(c,a))}function ke(e,t){const r=e[0];return r?.type==="paragraph"&&r.data?.directiveLabel===!0?{title:h("title",b(r.children,t)),body:e.slice(1)}:{title:null,body:e}}function ve(e){const t=e.attributes??{},r={};for(const[n,o]of Object.entries(t))o!=null&&(r[n==="id"?"xml:id":n]=o);return Object.keys(r).length>0?r:void 0}function xe(e,t){return!t||e.position?.start?.offset===null||e.position?.start?.offset===void 0?"asterisk":t.charAt(e.position.start.offset)==="_"?"underscore":"asterisk"}const be={text:(e,t)=>W(e.value),emphasis:(e,t)=>{const n=xe(e,t.source)==="underscore"?"term":"em";return h(n,b(e.children,t))},strong:(e,t)=>h("alert",b(e.children,t)),inlineCode:(e,t)=>x("c",e.value),math:(e,t)=>ye(e)};function b(e,t){return e.flatMap(r=>Ce(r,t)).filter(r=>r!==null)}function Ce(e,t){const r=be[e.type];if(!r)return t.messages&&t.messages.push({type:"warning",reason:`Unknown inline type: ${e.type}`,category:"unknown-inline-type"}),null;const n={...t,ancestors:[...t.ancestors,e]};return r(e,n)}const we=[{open:"$$",close:"$$",isDisplay:!0},{open:"\\[",close:"\\]",isDisplay:!0},{open:"$",close:"$",isDisplay:!1},{open:"\\(",close:"\\)",isDisplay:!1}],X="PTX_MATH_TOKEN_",I=new RegExp(`${X}(\\d+)`,"g");function K(e,t){let r=null;for(const n of we){const o=e.indexOf(n.open,t);if(o===-1)continue;const s=e.indexOf(n.close,o+n.open.length);if(s!==-1&&!(n.open==="$"&&(o>0&&e[o-1]==="$"||o+1<e.length&&e[o+1]==="$"))&&(!r||o<r.index)){const i=e.substring(o+n.open.length,s);r={index:o,pattern:n,content:i}}}return r}function Se(e,t){let r=t;for(;r<e.length;){const n=e.indexOf("`",r);if(n===-1)return null;let o=0,s=n;for(;s<e.length&&e[s]==="`";)o++,s++;const i="`".repeat(o);let l=s;for(;l<e.length;){const c=e.indexOf(i,l);if(c===-1)break;const a=c+o;if(a>=e.length||e[a]!=="`")return{start:n,end:a};l=c+1}r=n+o}return null}function De(e){const t=new Map;let r="",n=0,o=0;for(;n<e.length;){const s=Se(e,n),i=K(e,n);if(s&&(!i||s.start<i.index)){r+=e.substring(n,s.end),n=s.end;continue}if(!i){r+=e.substring(n);break}r+=e.substring(n,i.index);const l=`${X}${o}`;r+=l,t.set(l,{type:"math",value:i.content,meta:i.pattern.isDisplay?"display":"inline"}),n=i.index+i.pattern.open.length+i.content.length+i.pattern.close.length,o+=1}return{markdown:r,tokens:t}}function qe(e){const t=[];let r=0;for(;r<e.length;){const n=K(e,r);if(!n){r<e.length&&t.push({type:"text",value:e.substring(r)});break}n.index>r&&t.push({type:"text",value:e.substring(r,n.index)}),t.push({type:"math",value:n.content,meta:n.pattern.isDisplay?"display":"inline"}),r=n.index+n.pattern.open.length+n.content.length+n.pattern.close.length}return t.length===0?[{type:"text",value:e}]:t}function Ie(e,t){const r=[];let n=0;I.lastIndex=0;let o=I.exec(e);for(;o;){const s=o.index,i=o[0];s>n&&r.push({type:"text",value:e.substring(n,s)});const l=t.get(i);l?r.push(l):r.push({type:"text",value:i}),n=s+i.length,o=I.exec(e)}return n<e.length&&r.push({type:"text",value:e.substring(n)}),r.length===0?[{type:"text",value:e}]:r}function Y(e){B.visit(e,"paragraph",t=>{const r=t.children||[],n=[];for(const o of r)if(o.type==="text"){const s=qe(o.value);n.push(...s)}else n.push(o);t.children=n})}function Te(e,t){B.visit(e,"paragraph",r=>{const n=r.children||[],o=[];for(const s of n)if(s.type==="text"){const i=Ie(s.value,t);o.push(...i)}else o.push(s);r.children=o})}const Ee=()=>e=>{Y(e)};function Me(e){const t=e.trim();if(!t.startsWith(":"))return null;let r=0;for(let s=0;s<t.length&&t[s]===":";s++)r++;if(r<3)return null;const n=t.slice(r).trim(),o=n.match(/^([a-zA-Z0-9_-]+)(\[[^\]]*\])?(\{[^}]*\})?$/);return n===""?{colons:r,label:null,lineIndex:-1,isOpen:!1}:o?{colons:r,label:o[0],lineIndex:-1,isOpen:!0}:null}function Le(e){const t=[],r=[];for(const n of e)if(n.isOpen){const o={colons:n.colons,finalColons:n.colons,label:n.label,openLineIndex:n.lineIndex,closeLineIndex:null,children:[],parent:null};if(t.length>0){const s=t[t.length-1];o.parent=s,s.children.push(o)}else r.push(o);t.push(o)}else{const o=t[t.length-1];o&&o.colons===n.colons&&(o.closeLineIndex=n.lineIndex,t.pop())}return r}function G(e){if(e.children.length===0)e.finalColons=e.colons;else{const t=Math.max(...e.children.map(G));e.finalColons=Math.max(e.colons,t+1)}return e.finalColons}function Fe(e,t,r){const n=e.slice(),o=[],s=t.slice();for(;s.length>0;){const i=s.shift();o.push(i),s.push(...i.children)}for(const i of o){if(i.openLineIndex>=0){const l=n[i.openLineIndex],c=l.match(/^:+/)?.[0].length||3,a=l.slice(c);n[i.openLineIndex]=":".repeat(i.finalColons)+a}i.closeLineIndex!==null&&i.closeLineIndex>=0&&(n[i.closeLineIndex]=":".repeat(i.finalColons))}return n}function Ne(e){const t=e.split(`
2
+ `),r=[];let n=!1,o=null;for(let l=0;l<t.length;l++){const c=t[l],u=c.trim().match(/^(`{3,}|~{3,})/);if(u){const m=u[1];n?o&&m[0]===o[0]&&m.length>=o.length&&(n=!1,o=null):(n=!0,o=m);continue}if(n)continue;const d=Me(c);d&&r.push({...d,lineIndex:l})}if(r.length===0)return e;const s=Le(r);for(const l of s)G(l);return Fe(t,s).join(`
3
+ `)}function $e(e){const t=e.trim();if(!t.endsWith(":"))return null;const r=t.slice(0,-1);if(!/^[a-zA-Z]/.test(r))return null;const n=r.match(/^([a-zA-Z][a-zA-Z0-9_-]*)(.*)$/);if(!n)return null;const[,o,s]=n;return s&&!/^(\[[^\]]*\]|\{[^}]*\})*$/.test(s)||!q[o.toLowerCase()]?null:r}function M(e){let t=0;for(let r=0;r<e.length;r++)if(e[r]===" ")t+=1;else if(e[r]===" ")t+=4;else break;return t}function Oe(e,t){const r=M(e[t]);for(let n=t+1;n<e.length;n++)if(!J(e[n]))return M(e[n])>r;return!1}function J(e){return e.trim()===""}function Pe(e,t){let r=0,n=0;for(;n<e.length&&r<t;)if(e[n]===" ")r++,n++;else if(e[n]===" ")r+=4,n++;else break;return e.slice(n)}function Ae(e){const t=e.split(`
4
+ `),r=[],n={level:0,openDirectives:[],inCodeFence:!1,codeFenceMarker:null};for(let o=0;o<t.length;o++){const s=t[o],i=s.trim(),l=M(s),c=i.match(/^(`{3,}|~{3,})/);if(c){const u=c[1];n.inCodeFence?n.codeFenceMarker&&i.startsWith(n.codeFenceMarker)&&(n.inCodeFence=!1,n.codeFenceMarker=null):(n.inCodeFence=!0,n.codeFenceMarker=u),r.push(s);continue}if(n.inCodeFence){r.push(s);continue}if(J(s)){r.push("");continue}const a=$e(i);if(a&&Oe(t,o)){for(;n.openDirectives.length>0&&!(n.openDirectives[n.openDirectives.length-1].indentLevel<l);){const g=n.openDirectives.pop();g&&r.push(":".repeat(g.colonCount))}const u=a.match(/^([a-zA-Z][a-zA-Z0-9_-]*)(.*)/),d=u?u[1].toLowerCase()+(u[2]||""):a,m=n.openDirectives.length*1+3;r.push(":".repeat(m)+d),n.openDirectives.push({label:d,indentLevel:l,colonCount:m,bodyBaseIndent:-1}),n.level=l}else{for(;n.openDirectives.length>0&&l<=n.openDirectives[n.openDirectives.length-1].indentLevel;){const u=n.openDirectives.pop();u&&r.push(":".repeat(u.colonCount))}if(n.openDirectives.length>0){const u=n.openDirectives[n.openDirectives.length-1];u.bodyBaseIndent<0&&(u.bodyBaseIndent=l),r.push(Pe(s,u.bodyBaseIndent))}else r.push(s);n.level=l}}for(;n.openDirectives.length>0;){const o=n.openDirectives.pop();o&&r.push(":".repeat(o.colonCount))}return r.join(`
5
+ `)}const Q=function(){return function(t,r){if(typeof r?.value=="string"){const n=Ae(r.value),o=Ne(n),s=De(o),l=R.unified().data("micromarkExtensions",[{disable:{null:["codeIndented"]}}]).use(j).use(z).parse(s.markdown);return Te(l,s.tokens),T(l,s.markdown)}return Y(t),T(t)}},_e=/["&'<>`]/g,Re=/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,je=/[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g,ze=/[|\\{}()[\]^$+*?.]/g,P=new WeakMap;function Be(e,t){if(e=e.replace(t.subset?We(t.subset):_e,n),t.subset||t.escapeOnly)return e;return e.replace(Re,r).replace(je,n);function r(o,s,i){return t.format((o.charCodeAt(0)-55296)*1024+o.charCodeAt(1)-56320+65536,i.charCodeAt(s+2),t)}function n(o,s,i){return t.format(o.charCodeAt(0),i.charCodeAt(s+1),t)}}function We(e){let t=P.get(e);return t||(t=Ue(e),P.set(e,t)),t}function Ue(e){const t=[];let r=-1;for(;++r<e.length;)t.push(e[r].replace(ze,"\\$&"));return new RegExp("(?:"+t.join("|")+")","g")}function He(e){return"&#x"+e.toString(16).toUpperCase()+";"}function Ze(e,t){return Be(e,Object.assign({format:He},t))}const Ve=/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;function y(e,t,r){const n=Xe(e);return r?n.replace(r,o):o(n);function o(s){return Ze(s,{subset:t})}}function Xe(e){return String(e||"").replace(Ve,"")}const Ke=/]]>/g,Ye=[">"];function Ge(e){return"<![CDATA["+y(e.value,Ye,Ke)+"]]>"}function Je(e){return"<!--"+y(e.value,["-"])+"-->"}const Qe=[" ",`
6
+ `," ",'"',"&","'","/","<","=",">"];function D(e){return y(e,Qe)}function A(e,t){const r=String(e);if(typeof t!="string")throw new TypeError("Expected character");let n=0,o=r.indexOf(t);for(;o!==-1;)n++,o=r.indexOf(t,o+t.length);return n}function L(e,t){const r=String(e);let n=t.options.quote||'"';if(t.options.quoteSmart){const o=n==='"'?"'":'"';A(r,n)>A(r,o)&&(n=o)}return n+y(r,["<","&",n])+n}function et(e,t){const r=D(e.name),n=e.public,o=e.system;let s="<!DOCTYPE";return r!==""&&(s+=" "+r),n?s+=" PUBLIC "+L(n,t):o&&(s+=" SYSTEM"),o&&(s+=" "+L(o,t)),s+">"}const tt={}.hasOwnProperty;function nt(e,t){const r=D(e.name),n=ne(e,t),o=e.attributes||{},s=n?!1:t.options.closeEmptyElements,i=[];let l;for(l in o)if(tt.call(o,l)){const c=o[l];c!=null&&i.push(D(l)+"="+L(c,t))}return"<"+r+(i.length===0?"":" "+i.join(" "))+(s?(t.options.tightClose?"":" ")+"/":"")+">"+n+(s?"":"</"+r+">")}const rt=/\?>/g,ot=[">"];function st(e){const t=D(e.name)||"x",r=y(e.value,ot,rt);return"<?"+t+(r?" "+r:"")+"?>"}const it=["&","<"];function ee(e){return y(e.value,it)}function lt(e,t){return t.options.allowDangerousXml?e.value:ee(e)}const ct={}.hasOwnProperty,_={cdata:Ge,comment:Je,doctype:et,element:nt,instruction:st,raw:lt,root:ne,text:ee};function te(e,t){const r=e&&e.type;if(!r)throw new Error("Expected node, not `"+e+"`");if(!ct.call(_,r))throw new Error("Cannot compile unknown node `"+r+"`");const n=_[r];return n(e,t)}function ne(e,t){const r=e&&e.children||[];let n=-1;const o=[];for(;++n<r.length;)o[n]=te(r[n],t);return o.join("")}function at(e,t){const r={options:{}};if(typeof r.options.quote=="string"&&r.options.quote!=='"'&&r.options.quote!=="'")throw new Error("Invalid quote `"+r.options.quote+"`, expected `'` or `\"`");const n=Array.isArray(e)?{type:"root",children:e}:e;return te(n,r)}function ut(e){const t=R.unified().use(j).use(z).use(Q),r=t.parse(e),n=t.runSync(r,{value:e});return at(n.children)}exports.DIRECTIVE_MAP=re;exports.PROOF_SOLUTION_NAMES=F;exports.markdownToPretext=ut;exports.mdastToPtxast=T;exports.remarkMath=Ee;exports.remarkPretext=Q;
@@ -0,0 +1,8 @@
1
+ export { default as remarkPretext } from './lib/remark-pretext.js';
2
+ export { markdownToPretext } from './lib/markdown-to-pretext.js';
3
+ export { mdastToPtxast } from './lib/remark-pretext.js';
4
+ export { DIRECTIVE_MAP, PROOF_SOLUTION_NAMES } from './lib/directive-map.js';
5
+ export type { DirectiveInfo, DirectiveCategory } from './lib/directive-map.js';
6
+ export type { RemarkPretextOptions } from './lib/remark-pretext.js';
7
+ export { default as remarkMath } from './lib/math-parser.js';
8
+ export type { Math } from './lib/math-parser.js';
package/dist/index.js ADDED
@@ -0,0 +1,813 @@
1
+ import { unified as _ } from "unified";
2
+ import R from "remark-parse";
3
+ import j from "remark-directive";
4
+ import { visit as z } from "unist-util-visit";
5
+ const q = {
6
+ // theorem-like (requires statement wrapping)
7
+ theorem: { type: "theorem", category: "theorem-like", requiresStatement: !0 },
8
+ lemma: { type: "lemma", category: "theorem-like", requiresStatement: !0 },
9
+ corollary: { type: "corollary", category: "theorem-like", requiresStatement: !0 },
10
+ proposition: { type: "proposition", category: "theorem-like", requiresStatement: !0 },
11
+ claim: { type: "claim", category: "theorem-like", requiresStatement: !0 },
12
+ fact: { type: "fact", category: "theorem-like", requiresStatement: !0 },
13
+ conjecture: { type: "conjecture", category: "theorem-like", requiresStatement: !0 },
14
+ axiom: { type: "axiom", category: "theorem-like", requiresStatement: !0 },
15
+ principle: { type: "principle", category: "theorem-like", requiresStatement: !0 },
16
+ hypothesis: { type: "hypothesis", category: "theorem-like", requiresStatement: !0 },
17
+ algorithm: { type: "algorithm", category: "theorem-like", requiresStatement: !0 },
18
+ // definition-like (requires statement wrapping)
19
+ definition: { type: "definition", category: "definition-like", requiresStatement: !0 },
20
+ notation: { type: "notation", category: "definition-like", requiresStatement: !0 },
21
+ // remark-like (no statement wrapping)
22
+ remark: { type: "remark", category: "remark-like", requiresStatement: !1 },
23
+ note: { type: "note", category: "remark-like", requiresStatement: !1 },
24
+ observation: { type: "observation", category: "remark-like", requiresStatement: !1 },
25
+ warning: { type: "warning", category: "remark-like", requiresStatement: !1 },
26
+ insight: { type: "insight", category: "remark-like", requiresStatement: !1 },
27
+ assemblage: { type: "assemblage", category: "remark-like", requiresStatement: !1 },
28
+ // example-like (exercise/project/task support nested tasks; require statement if no nested tasks)
29
+ example: { type: "example", category: "example-like", requiresStatement: !1 },
30
+ question: { type: "question", category: "example-like", requiresStatement: !1 },
31
+ problem: { type: "problem", category: "example-like", requiresStatement: !1 },
32
+ exercise: { type: "exercise", category: "example-like", requiresStatement: !0, hasNestedTasks: !0 },
33
+ activity: { type: "activity", category: "example-like", requiresStatement: !1 },
34
+ exploration: { type: "exploration", category: "example-like", requiresStatement: !1 },
35
+ investigation: { type: "investigation", category: "example-like", requiresStatement: !1 },
36
+ project: { type: "project", category: "example-like", requiresStatement: !0, hasNestedTasks: !0 },
37
+ // task (can have nested tasks, requires statement if no nested tasks)
38
+ task: { type: "task", category: "example-like", requiresStatement: !0, hasNestedTasks: !0 },
39
+ // proof-like (no statement wrapping)
40
+ proof: { type: "proof", category: "proof-like", requiresStatement: !1 },
41
+ case: { type: "case", category: "proof-like", requiresStatement: !1 },
42
+ // solution-like (no statement wrapping)
43
+ solution: { type: "solution", category: "solution-like", requiresStatement: !1 },
44
+ hint: { type: "hint", category: "solution-like", requiresStatement: !1 },
45
+ answer: { type: "answer", category: "solution-like", requiresStatement: !1 }
46
+ }, ft = q;
47
+ function ne(e) {
48
+ return q[e.toLowerCase()];
49
+ }
50
+ const B = new Set(
51
+ Object.entries(q).filter(([e, t]) => t.category === "proof-like" || t.category === "solution-like").map(([e]) => e)
52
+ );
53
+ function v(e, t, o) {
54
+ return {
55
+ type: "element",
56
+ name: e,
57
+ attributes: o ?? {},
58
+ children: t
59
+ };
60
+ }
61
+ function C(e) {
62
+ return e.type === "containerDirective" && e.name === "task";
63
+ }
64
+ function oe(e) {
65
+ return e.type === "containerDirective" && B.has(e.name);
66
+ }
67
+ function D(e) {
68
+ const t = [];
69
+ for (const o of e)
70
+ o.name === "ul" || o.name === "ol" ? t.length > 0 && t[t.length - 1]?.name === "p" ? t[t.length - 1].children.push(o) : t.push(v("p", [o])) : t.push(o);
71
+ return t;
72
+ }
73
+ function re(e, t, o, n, r, s, i) {
74
+ const l = [];
75
+ if (o && l.push(o), e.hasNestedTasks) {
76
+ const c = n.findIndex(C);
77
+ if (c !== -1) {
78
+ const a = [];
79
+ for (let p = 0; p < n.length; p++)
80
+ C(n[p]) && a.push(p);
81
+ const u = a[a.length - 1], d = n.slice(0, c), m = n.slice(c).filter(C), k = [], g = [], L = [];
82
+ for (let p = c; p < n.length; p++) {
83
+ const f = n[p];
84
+ if (!C(f)) {
85
+ if (p < u) {
86
+ k.push(f);
87
+ continue;
88
+ }
89
+ if (oe(f)) {
90
+ L.push(f);
91
+ continue;
92
+ }
93
+ g.push(f);
94
+ }
95
+ }
96
+ if (k.length > 0 && (r.messages ?? (r.messages = [])).push({
97
+ type: "warning",
98
+ reason: `Dropped ${k.length} non-task node(s) between task directives in ${e.type}.`,
99
+ category: "dropped-content-between-tasks",
100
+ position: k[0].position?.start
101
+ }), d.length > 0) {
102
+ const p = D(
103
+ d.map((f) => s(f, r)).filter((f) => f !== null)
104
+ );
105
+ p.length > 0 && l.push(v("introduction", p));
106
+ }
107
+ for (const p of m) {
108
+ const f = i(p, r);
109
+ f && l.push(f);
110
+ }
111
+ if (g.length > 0) {
112
+ const p = D(
113
+ g.map((f) => s(f, r)).filter((f) => f !== null)
114
+ );
115
+ p.length > 0 && l.push(v("conclusion", p));
116
+ }
117
+ for (const p of L) {
118
+ const f = i(p, r);
119
+ f && l.push(f);
120
+ }
121
+ } else
122
+ F(l, e, n, r, s, i);
123
+ } else
124
+ F(l, e, n, r, s, i);
125
+ return v(e.type, l, t);
126
+ }
127
+ function F(e, t, o, n, r, s) {
128
+ if (t.requiresStatement) {
129
+ const i = [], l = [];
130
+ for (const c of o)
131
+ if (c.type === "containerDirective" && B.has(c.name)) {
132
+ const a = s(c, n);
133
+ a && l.push(a);
134
+ } else
135
+ i.push(c);
136
+ if (i.length > 0) {
137
+ const c = D(
138
+ i.map((a) => r(a, n)).filter((a) => a !== null)
139
+ );
140
+ c.length > 0 && e.push(v("statement", c));
141
+ }
142
+ e.push(...l);
143
+ } else {
144
+ const i = D(
145
+ o.map((l) => r(l, n)).filter((l) => l !== null)
146
+ );
147
+ e.push(...i);
148
+ }
149
+ }
150
+ function W(e) {
151
+ return { type: "text", value: e };
152
+ }
153
+ function h(e, t, o) {
154
+ return {
155
+ type: "element",
156
+ name: e,
157
+ attributes: o ?? {},
158
+ children: t
159
+ };
160
+ }
161
+ function x(e, t, o) {
162
+ return h(e, [W(t)], o);
163
+ }
164
+ function H(e) {
165
+ const t = [];
166
+ for (const o of e)
167
+ o.name === "ul" || o.name === "ol" ? t.length > 0 && t[t.length - 1]?.name === "p" ? t[t.length - 1].children.push(o) : t.push(h("p", [o])) : t.push(o);
168
+ return t;
169
+ }
170
+ function $(e, t) {
171
+ return se(e, t).tree;
172
+ }
173
+ function se(e, t) {
174
+ const o = [], n = { ancestors: [], depth: 0, messages: o, source: t }, r = e.children;
175
+ return {
176
+ tree: { type: "root", children: U(r, n) },
177
+ messages: o
178
+ };
179
+ }
180
+ const N = ["chapter", "section", "subsection", "subsubsection", "paragraphs"];
181
+ function ie(e) {
182
+ return N[Math.min(e - 1, N.length - 1)];
183
+ }
184
+ function U(e, t, o = !1) {
185
+ let n = 7;
186
+ for (const a of e)
187
+ a.type === "heading" && (n = Math.min(n, a.depth));
188
+ if (n === 7)
189
+ return w(e, t, o);
190
+ const r = [];
191
+ let s = null, i = [], l = [];
192
+ const c = () => {
193
+ s && (r.push(le(s, l, t)), s = null, l = []);
194
+ };
195
+ for (const a of e)
196
+ a.type === "heading" && a.depth === n ? (!s && i.length > 0 && (r.push(...w(i, t, o)), i = []), c(), s = a, l = []) : s ? l.push(a) : i.push(a);
197
+ return !s && i.length > 0 && r.push(...w(i, t, o)), c(), r;
198
+ }
199
+ function w(e, t, o = !1) {
200
+ const n = [];
201
+ for (let r = 0; r < e.length; r += 1) {
202
+ const s = e[r], i = T(s, t);
203
+ if (i !== null) {
204
+ if (i.name === "p" && i.children.length === 1 && i.children[0].name === "md") {
205
+ const l = i.children[0], c = n[n.length - 1], a = r + 1 < e.length ? T(e[r + 1], t) : null, u = a?.name === "p" ? a.children : null;
206
+ if (c?.name === "p")
207
+ c.children.push(l), u && (c.children.push(...u), r += 1);
208
+ else {
209
+ const d = [l];
210
+ u && (d.push(...u), r += 1), n.push(h("p", d));
211
+ }
212
+ continue;
213
+ }
214
+ if ((i.name === "ul" || i.name === "ol") && n.length > 0) {
215
+ const l = n[n.length - 1];
216
+ if (l?.name === "p") {
217
+ l.children.push(i);
218
+ continue;
219
+ }
220
+ }
221
+ n.push(i);
222
+ }
223
+ }
224
+ return o ? H(n) : n;
225
+ }
226
+ function le(e, t, o) {
227
+ const n = ie(e.depth), r = h("title", b(e.children, o)), s = ce(e), i = U(t, { ...o, depth: o.depth + 1 }, !0), l = H(i);
228
+ return h(n, [r, ...l], s);
229
+ }
230
+ function ce(e) {
231
+ const t = e.data;
232
+ return t?.id ? { "xml:id": t.id } : void 0;
233
+ }
234
+ const ae = {
235
+ paragraph: (e, t) => Z(e, t),
236
+ blockquote: (e, t) => ue(e, t),
237
+ list: (e, t) => pe(e, t),
238
+ code: (e, t) => he(e),
239
+ containerDirective: (e, t) => X(e, t)
240
+ };
241
+ function T(e, t) {
242
+ const o = ae[e.type];
243
+ return o ? o(e, t) : (t.messages && t.messages.push({
244
+ type: "warning",
245
+ reason: `Unknown block type: ${e.type}`,
246
+ category: "unknown-block-type"
247
+ }), null);
248
+ }
249
+ function Z(e, t) {
250
+ return h("p", b(e.children, t));
251
+ }
252
+ function ue(e, t) {
253
+ const o = e.children.filter((n) => n.type === "paragraph").map((n) => {
254
+ const r = { ...t, ancestors: [...t.ancestors, e], depth: t.depth + 1 };
255
+ return Z(n, r);
256
+ });
257
+ return h("blockquote", o);
258
+ }
259
+ function pe(e, t) {
260
+ const o = e.children.map((n) => {
261
+ const r = { ...t, ancestors: [...t.ancestors, e], depth: t.depth + 1 };
262
+ return fe(n, r);
263
+ });
264
+ return h(e.ordered ? "ol" : "ul", o);
265
+ }
266
+ function fe(e, t) {
267
+ const o = w(e.children, t);
268
+ return h("li", o);
269
+ }
270
+ function he(e, t) {
271
+ return x("program", e.value, e.lang ? { language: e.lang } : void 0);
272
+ }
273
+ function de(e, t) {
274
+ const o = e.value.split(/\\\\|[\r\n]+/).map((n) => n.trim()).filter((n) => n.length > 0);
275
+ return o.length > 1 ? h("md", o.map((n) => x("mrow", n))) : x("md", o[0] ?? "");
276
+ }
277
+ function me(e, t) {
278
+ return e.meta === "inline" ? x("m", e.value) : de(e);
279
+ }
280
+ function X(e, t) {
281
+ const o = ne(e.name);
282
+ if (!o)
283
+ return t.messages && t.messages.push({
284
+ type: "warning",
285
+ reason: `Unknown directive: ${e.name}`,
286
+ category: "unknown-directive"
287
+ }), null;
288
+ const n = ye(e), r = e.children, { title: s, body: i } = ge(r, t), l = { ...t, ancestors: [...t.ancestors, e], depth: t.depth + 1 };
289
+ return re(
290
+ o,
291
+ n,
292
+ s,
293
+ i,
294
+ l,
295
+ (c, a) => T(c, a),
296
+ (c, a) => X(c, a)
297
+ );
298
+ }
299
+ function ge(e, t) {
300
+ const o = e[0];
301
+ return o?.type === "paragraph" && o.data?.directiveLabel === !0 ? {
302
+ title: h("title", b(o.children, t)),
303
+ body: e.slice(1)
304
+ } : { title: null, body: e };
305
+ }
306
+ function ye(e) {
307
+ const t = e.attributes ?? {}, o = {};
308
+ for (const [n, r] of Object.entries(t))
309
+ r != null && (o[n === "id" ? "xml:id" : n] = r);
310
+ return Object.keys(o).length > 0 ? o : void 0;
311
+ }
312
+ function ke(e, t) {
313
+ return !t || e.position?.start?.offset === null || e.position?.start?.offset === void 0 ? "asterisk" : t.charAt(e.position.start.offset) === "_" ? "underscore" : "asterisk";
314
+ }
315
+ const ve = {
316
+ text: (e, t) => W(e.value),
317
+ emphasis: (e, t) => {
318
+ const n = ke(e, t.source) === "underscore" ? "term" : "em";
319
+ return h(n, b(e.children, t));
320
+ },
321
+ strong: (e, t) => h("alert", b(e.children, t)),
322
+ inlineCode: (e, t) => x("c", e.value),
323
+ math: (e, t) => me(e)
324
+ };
325
+ function b(e, t) {
326
+ return e.flatMap((o) => xe(o, t)).filter((o) => o !== null);
327
+ }
328
+ function xe(e, t) {
329
+ const o = ve[e.type];
330
+ if (!o)
331
+ return t.messages && t.messages.push({
332
+ type: "warning",
333
+ reason: `Unknown inline type: ${e.type}`,
334
+ category: "unknown-inline-type"
335
+ }), null;
336
+ const n = { ...t, ancestors: [...t.ancestors, e] };
337
+ return o(e, n);
338
+ }
339
+ const be = [
340
+ // Display math (check these first, before single $)
341
+ { open: "$$", close: "$$", isDisplay: !0 },
342
+ { open: "\\[", close: "\\]", isDisplay: !0 },
343
+ // Inline math
344
+ { open: "$", close: "$", isDisplay: !1 },
345
+ { open: "\\(", close: "\\)", isDisplay: !1 }
346
+ ], K = "PTX_MATH_TOKEN_", I = new RegExp(`${K}(\\d+)`, "g");
347
+ function Y(e, t) {
348
+ let o = null;
349
+ for (const n of be) {
350
+ const r = e.indexOf(n.open, t);
351
+ if (r === -1) continue;
352
+ const s = e.indexOf(n.close, r + n.open.length);
353
+ if (s !== -1 && !(n.open === "$" && (r > 0 && e[r - 1] === "$" || r + 1 < e.length && e[r + 1] === "$")) && (!o || r < o.index)) {
354
+ const i = e.substring(r + n.open.length, s);
355
+ o = { index: r, pattern: n, content: i };
356
+ }
357
+ }
358
+ return o;
359
+ }
360
+ function Ce(e, t) {
361
+ let o = t;
362
+ for (; o < e.length; ) {
363
+ const n = e.indexOf("`", o);
364
+ if (n === -1) return null;
365
+ let r = 0, s = n;
366
+ for (; s < e.length && e[s] === "`"; )
367
+ r++, s++;
368
+ const i = "`".repeat(r);
369
+ let l = s;
370
+ for (; l < e.length; ) {
371
+ const c = e.indexOf(i, l);
372
+ if (c === -1) break;
373
+ const a = c + r;
374
+ if (a >= e.length || e[a] !== "`")
375
+ return { start: n, end: a };
376
+ l = c + 1;
377
+ }
378
+ o = n + r;
379
+ }
380
+ return null;
381
+ }
382
+ function we(e) {
383
+ const t = /* @__PURE__ */ new Map();
384
+ let o = "", n = 0, r = 0;
385
+ for (; n < e.length; ) {
386
+ const s = Ce(e, n), i = Y(e, n);
387
+ if (s && (!i || s.start < i.index)) {
388
+ o += e.substring(n, s.end), n = s.end;
389
+ continue;
390
+ }
391
+ if (!i) {
392
+ o += e.substring(n);
393
+ break;
394
+ }
395
+ o += e.substring(n, i.index);
396
+ const l = `${K}${r}`;
397
+ o += l, t.set(l, {
398
+ type: "math",
399
+ value: i.content,
400
+ meta: i.pattern.isDisplay ? "display" : "inline"
401
+ }), n = i.index + i.pattern.open.length + i.content.length + i.pattern.close.length, r += 1;
402
+ }
403
+ return { markdown: o, tokens: t };
404
+ }
405
+ function De(e) {
406
+ const t = [];
407
+ let o = 0;
408
+ for (; o < e.length; ) {
409
+ const n = Y(e, o);
410
+ if (!n) {
411
+ o < e.length && t.push({ type: "text", value: e.substring(o) });
412
+ break;
413
+ }
414
+ n.index > o && t.push({
415
+ type: "text",
416
+ value: e.substring(o, n.index)
417
+ }), t.push({
418
+ type: "math",
419
+ value: n.content,
420
+ meta: n.pattern.isDisplay ? "display" : "inline"
421
+ }), o = n.index + n.pattern.open.length + n.content.length + n.pattern.close.length;
422
+ }
423
+ return t.length === 0 ? [{ type: "text", value: e }] : t;
424
+ }
425
+ function Se(e, t) {
426
+ const o = [];
427
+ let n = 0;
428
+ I.lastIndex = 0;
429
+ let r = I.exec(e);
430
+ for (; r; ) {
431
+ const s = r.index, i = r[0];
432
+ s > n && o.push({ type: "text", value: e.substring(n, s) });
433
+ const l = t.get(i);
434
+ l ? o.push(l) : o.push({ type: "text", value: i }), n = s + i.length, r = I.exec(e);
435
+ }
436
+ return n < e.length && o.push({ type: "text", value: e.substring(n) }), o.length === 0 ? [{ type: "text", value: e }] : o;
437
+ }
438
+ function V(e) {
439
+ z(e, "paragraph", (t) => {
440
+ const o = t.children || [], n = [];
441
+ for (const r of o)
442
+ if (r.type === "text") {
443
+ const s = De(r.value);
444
+ n.push(...s);
445
+ } else
446
+ n.push(r);
447
+ t.children = n;
448
+ });
449
+ }
450
+ function qe(e, t) {
451
+ z(e, "paragraph", (o) => {
452
+ const n = o.children || [], r = [];
453
+ for (const s of n)
454
+ if (s.type === "text") {
455
+ const i = Se(s.value, t);
456
+ r.push(...i);
457
+ } else
458
+ r.push(s);
459
+ o.children = r;
460
+ });
461
+ }
462
+ const ht = () => (e) => {
463
+ V(e);
464
+ };
465
+ function Ie(e) {
466
+ const t = e.trim();
467
+ if (!t.startsWith(":")) return null;
468
+ let o = 0;
469
+ for (let s = 0; s < t.length && t[s] === ":"; s++)
470
+ o++;
471
+ if (o < 3) return null;
472
+ const n = t.slice(o).trim(), r = n.match(/^([a-zA-Z0-9_-]+)(\[[^\]]*\])?(\{[^}]*\})?$/);
473
+ return n === "" ? { colons: o, label: null, lineIndex: -1, isOpen: !1 } : r ? { colons: o, label: r[0], lineIndex: -1, isOpen: !0 } : null;
474
+ }
475
+ function Te(e) {
476
+ const t = [], o = [];
477
+ for (const n of e)
478
+ if (n.isOpen) {
479
+ const r = {
480
+ colons: n.colons,
481
+ finalColons: n.colons,
482
+ label: n.label,
483
+ openLineIndex: n.lineIndex,
484
+ closeLineIndex: null,
485
+ children: [],
486
+ parent: null
487
+ };
488
+ if (t.length > 0) {
489
+ const s = t[t.length - 1];
490
+ r.parent = s, s.children.push(r);
491
+ } else
492
+ o.push(r);
493
+ t.push(r);
494
+ } else {
495
+ const r = t[t.length - 1];
496
+ r && r.colons === n.colons && (r.closeLineIndex = n.lineIndex, t.pop());
497
+ }
498
+ return o;
499
+ }
500
+ function G(e) {
501
+ if (e.children.length === 0)
502
+ e.finalColons = e.colons;
503
+ else {
504
+ const t = Math.max(...e.children.map(G));
505
+ e.finalColons = Math.max(e.colons, t + 1);
506
+ }
507
+ return e.finalColons;
508
+ }
509
+ function Ee(e, t, o) {
510
+ const n = e.slice(), r = [], s = t.slice();
511
+ for (; s.length > 0; ) {
512
+ const i = s.shift();
513
+ r.push(i), s.push(...i.children);
514
+ }
515
+ for (const i of r) {
516
+ if (i.openLineIndex >= 0) {
517
+ const l = n[i.openLineIndex], c = l.match(/^:+/)?.[0].length || 3, a = l.slice(c);
518
+ n[i.openLineIndex] = ":".repeat(i.finalColons) + a;
519
+ }
520
+ i.closeLineIndex !== null && i.closeLineIndex >= 0 && (n[i.closeLineIndex] = ":".repeat(i.finalColons));
521
+ }
522
+ return n;
523
+ }
524
+ function Me(e) {
525
+ const t = e.split(`
526
+ `), o = [];
527
+ let n = !1, r = null;
528
+ for (let l = 0; l < t.length; l++) {
529
+ const c = t[l], u = c.trim().match(/^(`{3,}|~{3,})/);
530
+ if (u) {
531
+ const m = u[1];
532
+ n ? r && m[0] === r[0] && m.length >= r.length && (n = !1, r = null) : (n = !0, r = m);
533
+ continue;
534
+ }
535
+ if (n)
536
+ continue;
537
+ const d = Ie(c);
538
+ d && o.push({ ...d, lineIndex: l });
539
+ }
540
+ if (o.length === 0)
541
+ return e;
542
+ const s = Te(o);
543
+ for (const l of s)
544
+ G(l);
545
+ return Ee(t, s).join(`
546
+ `);
547
+ }
548
+ function Le(e) {
549
+ const t = e.trim();
550
+ if (!t.endsWith(":")) return null;
551
+ const o = t.slice(0, -1);
552
+ if (!/^[a-zA-Z]/.test(o)) return null;
553
+ const n = o.match(/^([a-zA-Z][a-zA-Z0-9_-]*)(.*)$/);
554
+ if (!n) return null;
555
+ const [, r, s] = n;
556
+ return s && !/^(\[[^\]]*\]|\{[^}]*\})*$/.test(s) || !q[r.toLowerCase()] ? null : o;
557
+ }
558
+ function E(e) {
559
+ let t = 0;
560
+ for (let o = 0; o < e.length; o++)
561
+ if (e[o] === " ")
562
+ t += 1;
563
+ else if (e[o] === " ")
564
+ t += 4;
565
+ else
566
+ break;
567
+ return t;
568
+ }
569
+ function Fe(e, t) {
570
+ const o = E(e[t]);
571
+ for (let n = t + 1; n < e.length; n++)
572
+ if (!J(e[n]))
573
+ return E(e[n]) > o;
574
+ return !1;
575
+ }
576
+ function J(e) {
577
+ return e.trim() === "";
578
+ }
579
+ function $e(e, t) {
580
+ let o = 0, n = 0;
581
+ for (; n < e.length && o < t; )
582
+ if (e[n] === " ")
583
+ o++, n++;
584
+ else if (e[n] === " ")
585
+ o += 4, n++;
586
+ else
587
+ break;
588
+ return e.slice(n);
589
+ }
590
+ function Ne(e) {
591
+ const t = e.split(`
592
+ `), o = [], n = {
593
+ level: 0,
594
+ openDirectives: [],
595
+ inCodeFence: !1,
596
+ codeFenceMarker: null
597
+ };
598
+ for (let r = 0; r < t.length; r++) {
599
+ const s = t[r], i = s.trim(), l = E(s), c = i.match(/^(`{3,}|~{3,})/);
600
+ if (c) {
601
+ const u = c[1];
602
+ n.inCodeFence ? n.codeFenceMarker && i.startsWith(n.codeFenceMarker) && (n.inCodeFence = !1, n.codeFenceMarker = null) : (n.inCodeFence = !0, n.codeFenceMarker = u), o.push(s);
603
+ continue;
604
+ }
605
+ if (n.inCodeFence) {
606
+ o.push(s);
607
+ continue;
608
+ }
609
+ if (J(s)) {
610
+ o.push("");
611
+ continue;
612
+ }
613
+ const a = Le(i);
614
+ if (a && Fe(t, r)) {
615
+ for (; n.openDirectives.length > 0 && !(n.openDirectives[n.openDirectives.length - 1].indentLevel < l); ) {
616
+ const g = n.openDirectives.pop();
617
+ g && o.push(":".repeat(g.colonCount));
618
+ }
619
+ const u = a.match(/^([a-zA-Z][a-zA-Z0-9_-]*)(.*)/), d = u ? u[1].toLowerCase() + (u[2] || "") : a, m = n.openDirectives.length * 1 + 3;
620
+ o.push(":".repeat(m) + d), n.openDirectives.push({
621
+ label: d,
622
+ indentLevel: l,
623
+ colonCount: m,
624
+ bodyBaseIndent: -1
625
+ }), n.level = l;
626
+ } else {
627
+ for (; n.openDirectives.length > 0 && l <= n.openDirectives[n.openDirectives.length - 1].indentLevel; ) {
628
+ const u = n.openDirectives.pop();
629
+ u && o.push(":".repeat(u.colonCount));
630
+ }
631
+ if (n.openDirectives.length > 0) {
632
+ const u = n.openDirectives[n.openDirectives.length - 1];
633
+ u.bodyBaseIndent < 0 && (u.bodyBaseIndent = l), o.push($e(s, u.bodyBaseIndent));
634
+ } else
635
+ o.push(s);
636
+ n.level = l;
637
+ }
638
+ }
639
+ for (; n.openDirectives.length > 0; ) {
640
+ const r = n.openDirectives.pop();
641
+ r && o.push(":".repeat(r.colonCount));
642
+ }
643
+ return o.join(`
644
+ `);
645
+ }
646
+ const Ae = function() {
647
+ return function(t, o) {
648
+ if (typeof o?.value == "string") {
649
+ const n = Ne(o.value), r = Me(n), s = we(r), l = _().data("micromarkExtensions", [{ disable: { null: ["codeIndented"] } }]).use(R).use(j).parse(s.markdown);
650
+ return qe(l, s.tokens), $(l, s.markdown);
651
+ }
652
+ return V(t), $(t);
653
+ };
654
+ }, Oe = /["&'<>`]/g, Pe = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, _e = (
655
+ // eslint-disable-next-line no-control-regex, unicorn/no-hex-escape
656
+ /[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g
657
+ ), Re = /[|\\{}()[\]^$+*?.]/g, A = /* @__PURE__ */ new WeakMap();
658
+ function je(e, t) {
659
+ if (e = e.replace(
660
+ t.subset ? ze(t.subset) : Oe,
661
+ n
662
+ ), t.subset || t.escapeOnly)
663
+ return e;
664
+ return e.replace(Pe, o).replace(_e, n);
665
+ function o(r, s, i) {
666
+ return t.format(
667
+ (r.charCodeAt(0) - 55296) * 1024 + r.charCodeAt(1) - 56320 + 65536,
668
+ i.charCodeAt(s + 2),
669
+ t
670
+ );
671
+ }
672
+ function n(r, s, i) {
673
+ return t.format(
674
+ r.charCodeAt(0),
675
+ i.charCodeAt(s + 1),
676
+ t
677
+ );
678
+ }
679
+ }
680
+ function ze(e) {
681
+ let t = A.get(e);
682
+ return t || (t = Be(e), A.set(e, t)), t;
683
+ }
684
+ function Be(e) {
685
+ const t = [];
686
+ let o = -1;
687
+ for (; ++o < e.length; )
688
+ t.push(e[o].replace(Re, "\\$&"));
689
+ return new RegExp("(?:" + t.join("|") + ")", "g");
690
+ }
691
+ function We(e) {
692
+ return "&#x" + e.toString(16).toUpperCase() + ";";
693
+ }
694
+ function He(e, t) {
695
+ return je(e, Object.assign({ format: We }, t));
696
+ }
697
+ const Ue = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;
698
+ function y(e, t, o) {
699
+ const n = Ze(e);
700
+ return o ? n.replace(o, r) : r(n);
701
+ function r(s) {
702
+ return He(s, { subset: t });
703
+ }
704
+ }
705
+ function Ze(e) {
706
+ return String(e || "").replace(Ue, "");
707
+ }
708
+ const Xe = /]]>/g, Ke = [">"];
709
+ function Ye(e) {
710
+ return "<![CDATA[" + y(e.value, Ke, Xe) + "]]>";
711
+ }
712
+ function Ve(e) {
713
+ return "<!--" + y(e.value, ["-"]) + "-->";
714
+ }
715
+ const Ge = [" ", `
716
+ `, " ", '"', "&", "'", "/", "<", "=", ">"];
717
+ function S(e) {
718
+ return y(e, Ge);
719
+ }
720
+ function O(e, t) {
721
+ const o = String(e);
722
+ if (typeof t != "string")
723
+ throw new TypeError("Expected character");
724
+ let n = 0, r = o.indexOf(t);
725
+ for (; r !== -1; )
726
+ n++, r = o.indexOf(t, r + t.length);
727
+ return n;
728
+ }
729
+ function M(e, t) {
730
+ const o = String(e);
731
+ let n = t.options.quote || '"';
732
+ if (t.options.quoteSmart) {
733
+ const r = n === '"' ? "'" : '"';
734
+ O(o, n) > O(o, r) && (n = r);
735
+ }
736
+ return n + y(o, ["<", "&", n]) + n;
737
+ }
738
+ function Je(e, t) {
739
+ const o = S(e.name), n = e.public, r = e.system;
740
+ let s = "<!DOCTYPE";
741
+ return o !== "" && (s += " " + o), n ? s += " PUBLIC " + M(n, t) : r && (s += " SYSTEM"), r && (s += " " + M(r, t)), s + ">";
742
+ }
743
+ const Qe = {}.hasOwnProperty;
744
+ function et(e, t) {
745
+ const o = S(e.name), n = te(e, t), r = e.attributes || {}, s = n ? !1 : t.options.closeEmptyElements, i = [];
746
+ let l;
747
+ for (l in r)
748
+ if (Qe.call(r, l)) {
749
+ const c = r[l];
750
+ c != null && i.push(S(l) + "=" + M(c, t));
751
+ }
752
+ return "<" + o + (i.length === 0 ? "" : " " + i.join(" ")) + (s ? (t.options.tightClose ? "" : " ") + "/" : "") + ">" + n + (s ? "" : "</" + o + ">");
753
+ }
754
+ const tt = /\?>/g, nt = [">"];
755
+ function ot(e) {
756
+ const t = S(e.name) || "x", o = y(e.value, nt, tt);
757
+ return "<?" + t + (o ? " " + o : "") + "?>";
758
+ }
759
+ const rt = ["&", "<"];
760
+ function Q(e) {
761
+ return y(e.value, rt);
762
+ }
763
+ function st(e, t) {
764
+ return t.options.allowDangerousXml ? e.value : Q(e);
765
+ }
766
+ const it = {}.hasOwnProperty, P = {
767
+ cdata: Ye,
768
+ comment: Ve,
769
+ doctype: Je,
770
+ element: et,
771
+ instruction: ot,
772
+ raw: st,
773
+ root: te,
774
+ text: Q
775
+ };
776
+ function ee(e, t) {
777
+ const o = e && e.type;
778
+ if (!o)
779
+ throw new Error("Expected node, not `" + e + "`");
780
+ if (!it.call(P, o))
781
+ throw new Error("Cannot compile unknown node `" + o + "`");
782
+ const n = P[o];
783
+ return n(e, t);
784
+ }
785
+ function te(e, t) {
786
+ const o = e && e.children || [];
787
+ let n = -1;
788
+ const r = [];
789
+ for (; ++n < o.length; )
790
+ r[n] = ee(o[n], t);
791
+ return r.join("");
792
+ }
793
+ function lt(e, t) {
794
+ const o = { options: {} };
795
+ if (typeof o.options.quote == "string" && o.options.quote !== '"' && o.options.quote !== "'")
796
+ throw new Error(
797
+ "Invalid quote `" + o.options.quote + "`, expected `'` or `\"`"
798
+ );
799
+ const n = Array.isArray(e) ? { type: "root", children: e } : e;
800
+ return ee(n, o);
801
+ }
802
+ function dt(e) {
803
+ const t = _().use(R).use(j).use(Ae), o = t.parse(e), n = t.runSync(o, { value: e });
804
+ return lt(n.children);
805
+ }
806
+ export {
807
+ ft as DIRECTIVE_MAP,
808
+ B as PROOF_SOLUTION_NAMES,
809
+ dt as markdownToPretext,
810
+ $ as mdastToPtxast,
811
+ ht as remarkMath,
812
+ Ae as remarkPretext
813
+ };
@@ -0,0 +1,22 @@
1
+ import { BlockContent, DefinitionContent, PhrasingContent } from 'mdast';
2
+ export interface VisitContext {
3
+ /** Parent node, if any. */
4
+ parent?: BlockContent | DefinitionContent | PhrasingContent;
5
+ /** All ancestor nodes in traversal order. */
6
+ ancestors: (BlockContent | DefinitionContent | PhrasingContent)[];
7
+ /** Nesting depth. */
8
+ depth: number;
9
+ /** Accumulated error/warning messages. */
10
+ messages?: ConversionMessage[];
11
+ /** Raw source markdown (optional, used for delimiter detection). */
12
+ source?: string;
13
+ }
14
+ export interface ConversionMessage {
15
+ type: 'warning' | 'error';
16
+ reason: string;
17
+ category: string;
18
+ position?: {
19
+ line?: number;
20
+ column?: number;
21
+ };
22
+ }
@@ -0,0 +1,24 @@
1
+ import { BlockContent, DefinitionContent } from 'mdast';
2
+ import { ContainerDirective } from 'mdast-util-directive';
3
+ import { Element } from 'xast';
4
+ import { DirectiveSpec } from './directive-map.js';
5
+ import { VisitContext } from './context.js';
6
+ /**
7
+ * Generic factory function: applies semantic rules from spec.
8
+ *
9
+ * Handles:
10
+ * - Statement wrapping for theorem-like elements
11
+ * - Task nesting with introduction wrapping (exercise/project/task)
12
+ * - Proof/solution sibling extraction
13
+ *
14
+ * @param spec Semantic specification for this directive type
15
+ * @param attrs HTML/XML attributes for the element
16
+ * @param titleEl Optional <title> element (extracted from first paragraph if present)
17
+ * @param children Body content of the directive
18
+ * @param ctx Visitor context
19
+ * @param convertBlock Callback to recursively convert block children
20
+ * @param convertDirective Callback to recursively convert nested directives
21
+ *
22
+ * @returns XAST element for this directive
23
+ */
24
+ export declare function buildDirectiveWithSpec(spec: DirectiveSpec, attrs: Record<string, string> | undefined, titleEl: Element | null, children: Array<BlockContent | DefinitionContent>, ctx: VisitContext, convertBlock: (child: BlockContent | DefinitionContent, ctx: VisitContext) => Element | null, convertDirective: (node: ContainerDirective, ctx: VisitContext) => Element | null): Element;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Maps directive names (from remark-directive's `:::name`) to their
3
+ * corresponding xast element names and semantic specifications.
4
+ *
5
+ * Architecture (Phase 2 refactor):
6
+ * - Separated semantic specs (requiresStatement) from routing logic
7
+ * - Factories apply spec-driven semantic rules uniformly
8
+ * - Adding new directives requires only configuration change, not new functions
9
+ */
10
+ export type DirectiveCategory = 'theorem-like' | 'proof-like' | 'definition-like' | 'axiom-like' | 'remark-like' | 'example-like' | 'project-like' | 'exercise-like' | 'solution-like';
11
+ /**
12
+ * Semantic specification for how a directive type should be converted.
13
+ * This pattern mirrors unified-latex-to-pretext's factory specifications.
14
+ */
15
+ export interface DirectiveSpec {
16
+ /** The PreTeXt XML element name (= xast `name` field). */
17
+ type: string;
18
+ /** Routing category for handler selection. */
19
+ category: DirectiveCategory;
20
+ /**
21
+ * If true, the directive's body content is wrapped in a <statement> element,
22
+ * and proof/solution siblings are extracted as siblings of the statement
23
+ * (not children of it). This matches PreTeXt semantics where theorems have:
24
+ * <theorem><title/><statement>...</statement><proof/></theorem>
25
+ *
26
+ * If false, content is added directly without statement wrapping.
27
+ */
28
+ requiresStatement: boolean;
29
+ /**
30
+ * If true, this directive can have nested task children.
31
+ * Content before the first task becomes an <introduction> wrapper.
32
+ * Example:
33
+ * <exercise><introduction>...</introduction><task>...</task><task>...</task></exercise>
34
+ *
35
+ * Only applies if nested tasks are actually present in the body.
36
+ * If no tasks are found, requiresStatement still applies.
37
+ */
38
+ hasNestedTasks?: boolean;
39
+ }
40
+ /**
41
+ * Backwards-compatible type for code that only needs type+category.
42
+ * @deprecated Use DirectiveSpec instead for new code.
43
+ */
44
+ export interface DirectiveInfo {
45
+ type: string;
46
+ category: DirectiveCategory;
47
+ }
48
+ /**
49
+ * Semantic specification table: directive name → spec.
50
+ *
51
+ * Pattern mirrors unified-latex-to-pretext's environment specification approach:
52
+ * Declarative table + factory functions apply rules uniformly.
53
+ * To add a new directive: just add an entry here.
54
+ */
55
+ export declare const DIRECTIVE_SPEC_TABLE: Readonly<Record<string, DirectiveSpec>>;
56
+ /**
57
+ * @deprecated Use DIRECTIVE_SPEC_TABLE directly or via getDirectiveSpec().
58
+ * Kept for backwards compatibility. Returns spec as old DirectiveInfo type.
59
+ */
60
+ export declare const DIRECTIVE_MAP: Readonly<Record<string, DirectiveInfo>>;
61
+ /** Get directive spec, or undefined if not found. */
62
+ export declare function getDirectiveSpec(name: string): DirectiveSpec | undefined;
63
+ /** Set of directive names that are proof/solution-like (used when separating statement siblings). */
64
+ export declare const PROOF_SOLUTION_NAMES: Set<string>;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Directive Normalizer (Two-Pass Algorithm)
3
+ *
4
+ * Transforms user-friendly flexible colon syntax into standard remark-directive format.
5
+ *
6
+ * INPUT (flexible):
7
+ * :::exercise
8
+ * Intro
9
+ * :::task
10
+ * Content
11
+ * :::
12
+ * :::task
13
+ * More
14
+ * :::
15
+ * :::
16
+ *
17
+ * OUTPUT (normalized for remark-directive):
18
+ * ::::exercise
19
+ * Intro
20
+ * :::task
21
+ * Content
22
+ * :::
23
+ * :::task
24
+ * More
25
+ * :::
26
+ * ::::
27
+ *
28
+ * Algorithm (Two-Pass):
29
+ * - Pass 1: Parse markers, build nesting tree with parent references
30
+ * - Pass 2: Traverse tree bottom-up, compute final colon counts
31
+ * (each parent = max(children) + 1, ensuring proper nesting)
32
+ * - Pass 3: Re-emit with adjusted markers
33
+ */
34
+ /**
35
+ * Main normalization function
36
+ */
37
+ export declare function normalizeDirectiveColons(markdown: string): string;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Indentation-Based Directive Normalizer
3
+ *
4
+ * Transforms Python-like indentation-based directive syntax into colon syntax.
5
+ *
6
+ * INPUT (indentation-based):
7
+ * Theorem[Pythagorean]{#thm}:
8
+ * Statement text here.
9
+ *
10
+ * Proof:
11
+ * Proof content.
12
+ *
13
+ * Plain text (outdented).
14
+ *
15
+ * OUTPUT (colon-based):
16
+ * :::theorem[Pythagorean]{#thm}
17
+ * Statement text here.
18
+ *
19
+ * ::::proof
20
+ * Proof content.
21
+ * ::::
22
+ * :::
23
+ *
24
+ * Plain text (outdented).
25
+ *
26
+ * Algorithm:
27
+ * 1. Track fenced code blocks (``` and ~~~) and skip directive detection inside them
28
+ * 2. Parse each line to detect directive markers (Keyword[...]{...}:)
29
+ * 3. Track indentation stack: when indent increases, open a block; when decreases, close block(s)
30
+ * 4. Convert to colon syntax with proper nesting depth (validated against DIRECTIVE_SPEC_TABLE)
31
+ * 5. Preserve all non-directive content and indentation outside directives and code fences
32
+ */
33
+ /**
34
+ * Normalize indentation-based syntax to colon-based syntax
35
+ */
36
+ export declare function normalizeIndentationDirectives(markdown: string): string;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Convert a markdown string (with PreTeXt directive extensions) to a PreTeXt
3
+ * XML string.
4
+ *
5
+ * The output contains the converted XML fragment — the children of the root
6
+ * node serialized as a string — suitable for embedding in a PreTeXt document.
7
+ *
8
+ * @param markdown - The markdown source to convert.
9
+ * @returns A PreTeXt XML string.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { markdownToPretext } from '@pretextbook/remark-pretext';
14
+ *
15
+ * const xml = markdownToPretext('# Hello\n\nThis is a paragraph.');
16
+ * // → '<section>\n <title>Hello</title>\n <p>This is a paragraph.</p>\n</section>'
17
+ * ```
18
+ */
19
+ export declare function markdownToPretext(markdown: string): string;
@@ -0,0 +1,31 @@
1
+ import { Plugin } from 'unified';
2
+ import { Root, Text } from 'mdast';
3
+ export interface Math {
4
+ type: 'math';
5
+ value: string;
6
+ meta?: string;
7
+ }
8
+ declare module 'mdast' {
9
+ interface PhrasingContentMap {
10
+ math: Math;
11
+ }
12
+ }
13
+ export interface MathTokenizationResult {
14
+ markdown: string;
15
+ tokens: Map<string, Math>;
16
+ }
17
+ export declare function tokenizeMathInMarkdown(markdown: string): MathTokenizationResult;
18
+ /**
19
+ * Find math in a string and split into text and math nodes.
20
+ * Returns array of alternating Text and Math nodes.
21
+ *
22
+ * Order matters: we check longer/more specific delimiters first.
23
+ */
24
+ export declare function splitTextWithMath(text: string): (Text | Math)[];
25
+ /**
26
+ * Remark plugin that detects and parses math delimiters in text nodes.
27
+ */
28
+ export declare function applyMathDelimiters(tree: Root): void;
29
+ export declare function applyMathTokens(tree: Root, tokens: Map<string, Math>): void;
30
+ declare const remarkMath: Plugin<[], Root>;
31
+ export default remarkMath;
@@ -0,0 +1,13 @@
1
+ import { Root as MdastRoot } from 'mdast';
2
+ import { Root } from 'xast';
3
+ import { ConversionMessage } from './context.js';
4
+ /** Result type for conversion: tree + diagnostic messages. */
5
+ export interface ConversionResult {
6
+ tree: Root;
7
+ messages: ConversionMessage[];
8
+ }
9
+ /** Transform an mdast Root node into an xast Root node (backwards compatible). */
10
+ export declare function mdastToPtxast(tree: MdastRoot, source?: string): Root;
11
+ export { mdastToPtxast as mdastToXast };
12
+ /** Transform an mdast Root node, returning tree + diagnostic messages. */
13
+ export declare function mdastToPtxastWithDiagnostics(tree: MdastRoot, source?: string): ConversionResult;
@@ -0,0 +1,11 @@
1
+ import { Plugin } from 'unified';
2
+ import { Root as MdastRoot } from 'mdast';
3
+ import { Root } from 'xast';
4
+ /** Options for the remark-pretext plugin (currently none, reserved for future use). */
5
+ export interface RemarkPretextOptions {
6
+ }
7
+ declare const remarkPretext: Plugin<[RemarkPretextOptions?], MdastRoot, Root>;
8
+ export default remarkPretext;
9
+ export { mdastToPtxast } from './mdast-to-ptxast.js';
10
+ export { DIRECTIVE_MAP, PROOF_SOLUTION_NAMES } from './directive-map.js';
11
+ export type { DirectiveInfo, DirectiveCategory } from './directive-map.js';
package/index.cjs CHANGED
@@ -1 +1 @@
1
- module.exports = require('../../dist/packages/remark-pretext/index.cjs');
1
+ module.exports = require('./dist/index.cjs');
package/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from '../../dist/packages/remark-pretext/index.d.ts';
1
+ export * from "./dist/index.d.ts";
package/index.js CHANGED
@@ -1 +1 @@
1
- export * from '../../dist/packages/remark-pretext/index.js';
1
+ export * from './dist/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretextbook/remark-pretext",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "remark plugin to transform markdown-style PreTeXt directives into ptxast",
5
5
  "author": "Oscar Levin",
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  "*.cjs",
32
32
  "*.d.ts",
33
33
  "*.d.cts",
34
- "lib",
34
+ "dist",
35
35
  "types"
36
36
  ],
37
37
  "scripts": {
@@ -39,7 +39,7 @@
39
39
  "test": "vitest run"
40
40
  },
41
41
  "dependencies": {
42
- "@pretextbook/ptxast": "^0.0.2",
42
+ "@pretextbook/ptxast": "^0.0.4",
43
43
  "@types/mdast": "^4.0.0",
44
44
  "mdast-util-directive": "^3.0.0",
45
45
  "remark-directive": "^4.0.0",