@pretextbook/remark-pretext 0.0.1 → 0.0.3
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/README.md +76 -76
- package/dist/index.cjs +6 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +796 -0
- package/dist/lib/context.d.ts +22 -0
- package/dist/lib/directive-factory.d.ts +24 -0
- package/dist/lib/directive-map.d.ts +64 -0
- package/dist/lib/directive-normalizer.d.ts +37 -0
- package/dist/lib/indentation-normalizer.d.ts +36 -0
- package/dist/lib/markdown-to-pretext.d.ts +19 -0
- package/dist/lib/math-parser.d.ts +31 -0
- package/dist/lib/mdast-to-ptxast.d.ts +13 -0
- package/dist/lib/remark-pretext.d.ts +11 -0
- package/index.cjs +1 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +54 -53
package/README.md
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
# @pretextbook/remark-pretext
|
|
2
|
-
|
|
3
|
-
A [remark](https://github.com/remarkjs/remark) plugin that transforms **markdown-style PreTeXt** into a [`@pretextbook/ptxast`](../ptxast) tree.
|
|
4
|
-
|
|
5
|
-
## What is markdown-style PreTeXt?
|
|
6
|
-
|
|
7
|
-
Standard Markdown extended with [generic directives](https://github.com/remarkjs/remark-directive) to express PreTeXt-specific environments:
|
|
8
|
-
|
|
9
|
-
```markdown
|
|
10
|
-
## Pythagorean Theorem
|
|
11
|
-
|
|
12
|
-
A paragraph with inline $math$ and **important** terms.
|
|
13
|
-
|
|
14
|
-
::::theorem[Pythagorean Theorem]{#thm-pythagoras}
|
|
15
|
-
For a right triangle with legs $a$, $b$ and hypotenuse $c$:
|
|
16
|
-
|
|
17
|
-
$$
|
|
18
|
-
a^2 + b^2 = c^2
|
|
19
|
-
$$
|
|
20
|
-
|
|
21
|
-
:::proof
|
|
22
|
-
Left as an exercise.
|
|
23
|
-
:::
|
|
24
|
-
::::
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## Usage
|
|
28
|
-
|
|
29
|
-
```ts
|
|
30
|
-
import { unified } from 'unified'
|
|
31
|
-
import remarkParse from 'remark-parse'
|
|
32
|
-
import remarkDirective from 'remark-directive'
|
|
33
|
-
import { remarkPretext } from '@pretextbook/remark-pretext'
|
|
34
|
-
|
|
35
|
-
const processor = unified()
|
|
36
|
-
.use(remarkParse)
|
|
37
|
-
.use(remarkDirective) // enables ::: directives
|
|
38
|
-
.use(remarkPretext)
|
|
39
|
-
|
|
40
|
-
const mdast = processor.parse(markdownString)
|
|
41
|
-
const ptxast = processor.runSync(mdast, { value: markdownString }) // PtxRoot
|
|
42
|
-
|
|
43
|
-
// Serialize to XML (use @pretextbook/ptxast-util-to-xml)
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
> **Note:** Use `.runSync()` (or `.run()`) rather than `.parse()` — `.parse()` only runs the parser phase; transformers (including `remarkPretext`) run during the transform phase.
|
|
47
|
-
|
|
48
|
-
## Syntax Rules
|
|
49
|
-
|
|
50
|
-
- `:::name[optional title]{#id attr=val}` opens a block directive
|
|
51
|
-
- `:::` alone closes it
|
|
52
|
-
- **Nesting**: use one more colon for the outer container when nesting directives:
|
|
53
|
-
- `::::theorem` containing `:::proof` — use `::::` for theorem, `:::` for proof
|
|
54
|
-
- All standard PreTeXt block names are recognised (see `DIRECTIVE_MAP`)
|
|
55
|
-
- Standard Markdown headings map to divisions:
|
|
56
|
-
- `#` → `<chapter>`
|
|
57
|
-
- `##` → `<section>`
|
|
58
|
-
- `###` → `<subsection>`
|
|
59
|
-
- `####` → `<subsubsection>`
|
|
60
|
-
- Inline elements:
|
|
61
|
-
- `*text*` → `<em>`
|
|
62
|
-
- `**text**` → `<alert>` (semantic emphasis in PreTeXt)
|
|
63
|
-
- `` `code` `` → `<c>`
|
|
64
|
-
- `$...$` and `\(...\)` → `<m>`
|
|
65
|
-
- `$$...$$` and `\[...\]` → display math (`<md>`)
|
|
66
|
-
|
|
67
|
-
## Supported Directives
|
|
68
|
-
|
|
69
|
-
| Category | Names |
|
|
70
|
-
|----------|-------|
|
|
71
|
-
| theorem-like | `theorem`, `lemma`, `corollary`, `proposition`, `claim`, `fact`, `conjecture`, `axiom`, `principle`, `hypothesis`, `algorithm` |
|
|
72
|
-
| definition-like | `definition`, `notation` |
|
|
73
|
-
| remark-like | `remark`, `note`, `observation`, `warning`, `insight`, `assemblage` |
|
|
74
|
-
| example-like | `example`, `question`, `problem`, `exercise`, `activity`, `exploration`, `investigation`, `project` |
|
|
75
|
-
| proof-like | `proof`, `case` |
|
|
76
|
-
| solution-like | `solution`, `hint`, `answer` |
|
|
1
|
+
# @pretextbook/remark-pretext
|
|
2
|
+
|
|
3
|
+
A [remark](https://github.com/remarkjs/remark) plugin that transforms **markdown-style PreTeXt** into a [`@pretextbook/ptxast`](../ptxast) tree.
|
|
4
|
+
|
|
5
|
+
## What is markdown-style PreTeXt?
|
|
6
|
+
|
|
7
|
+
Standard Markdown extended with [generic directives](https://github.com/remarkjs/remark-directive) to express PreTeXt-specific environments:
|
|
8
|
+
|
|
9
|
+
```markdown
|
|
10
|
+
## Pythagorean Theorem
|
|
11
|
+
|
|
12
|
+
A paragraph with inline $math$ and **important** terms.
|
|
13
|
+
|
|
14
|
+
::::theorem[Pythagorean Theorem]{#thm-pythagoras}
|
|
15
|
+
For a right triangle with legs $a$, $b$ and hypotenuse $c$:
|
|
16
|
+
|
|
17
|
+
$$
|
|
18
|
+
a^2 + b^2 = c^2
|
|
19
|
+
$$
|
|
20
|
+
|
|
21
|
+
:::proof
|
|
22
|
+
Left as an exercise.
|
|
23
|
+
:::
|
|
24
|
+
::::
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { unified } from 'unified'
|
|
31
|
+
import remarkParse from 'remark-parse'
|
|
32
|
+
import remarkDirective from 'remark-directive'
|
|
33
|
+
import { remarkPretext } from '@pretextbook/remark-pretext'
|
|
34
|
+
|
|
35
|
+
const processor = unified()
|
|
36
|
+
.use(remarkParse)
|
|
37
|
+
.use(remarkDirective) // enables ::: directives
|
|
38
|
+
.use(remarkPretext)
|
|
39
|
+
|
|
40
|
+
const mdast = processor.parse(markdownString)
|
|
41
|
+
const ptxast = processor.runSync(mdast, { value: markdownString }) // PtxRoot
|
|
42
|
+
|
|
43
|
+
// Serialize to XML (use @pretextbook/ptxast-util-to-xml)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> **Note:** Use `.runSync()` (or `.run()`) rather than `.parse()` — `.parse()` only runs the parser phase; transformers (including `remarkPretext`) run during the transform phase.
|
|
47
|
+
|
|
48
|
+
## Syntax Rules
|
|
49
|
+
|
|
50
|
+
- `:::name[optional title]{#id attr=val}` opens a block directive
|
|
51
|
+
- `:::` alone closes it
|
|
52
|
+
- **Nesting**: use one more colon for the outer container when nesting directives:
|
|
53
|
+
- `::::theorem` containing `:::proof` — use `::::` for theorem, `:::` for proof
|
|
54
|
+
- All standard PreTeXt block names are recognised (see `DIRECTIVE_MAP`)
|
|
55
|
+
- Standard Markdown headings map to divisions:
|
|
56
|
+
- `#` → `<chapter>`
|
|
57
|
+
- `##` → `<section>`
|
|
58
|
+
- `###` → `<subsection>`
|
|
59
|
+
- `####` → `<subsubsection>`
|
|
60
|
+
- Inline elements:
|
|
61
|
+
- `*text*` → `<em>`
|
|
62
|
+
- `**text**` → `<alert>` (semantic emphasis in PreTeXt)
|
|
63
|
+
- `` `code` `` → `<c>`
|
|
64
|
+
- `$...$` and `\(...\)` → `<m>`
|
|
65
|
+
- `$$...$$` and `\[...\]` → display math (`<md>`)
|
|
66
|
+
|
|
67
|
+
## Supported Directives
|
|
68
|
+
|
|
69
|
+
| Category | Names |
|
|
70
|
+
|----------|-------|
|
|
71
|
+
| theorem-like | `theorem`, `lemma`, `corollary`, `proposition`, `claim`, `fact`, `conjecture`, `axiom`, `principle`, `hypothesis`, `algorithm` |
|
|
72
|
+
| definition-like | `definition`, `notation` |
|
|
73
|
+
| remark-like | `remark`, `note`, `observation`, `warning`, `insight`, `assemblage` |
|
|
74
|
+
| example-like | `example`, `question`, `problem`, `exercise`, `activity`, `exploration`, `investigation`, `project` |
|
|
75
|
+
| proof-like | `proof`, `case` |
|
|
76
|
+
| solution-like | `solution`, `hint`, `answer` |
|
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"),W=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,n){return{type:"element",name:e,attributes:n??{},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 n of e)n.name==="ul"||n.name==="ol"?t.length>0&&t[t.length-1]?.name==="p"?t[t.length-1].children.push(n):t.push(v("p",[n])):t.push(n);return t}function ie(e,t,n,r,o,s,i){const l=[];if(n&&l.push(n),e.hasNestedTasks){const c=r.findIndex(C);if(c!==-1){const a=[];for(let u=0;u<r.length;u++)C(r[u])&&a.push(u);const f=a[a.length-1],d=r.slice(0,c),m=r.slice(c).filter(C),k=[],g=[],N=[];for(let u=c;u<r.length;u++){const p=r[u];if(!C(p)){if(u<f){k.push(p);continue}if(se(p)){N.push(p);continue}g.push(p)}}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 u=S(d.map(p=>s(p,o)).filter(p=>p!==null));u.length>0&&l.push(v("introduction",u))}for(const u of m){const p=i(u,o);p&&l.push(p)}if(g.length>0){const u=S(g.map(p=>s(p,o)).filter(p=>p!==null));u.length>0&&l.push(v("conclusion",u))}for(const u of N){const p=i(u,o);p&&l.push(p)}}else $(l,e,r,o,s,i)}else $(l,e,r,o,s,i);return v(e.type,l,t)}function $(e,t,n,r,o,s){if(t.requiresStatement){const i=[],l=[];for(const c of n)if(c.type==="containerDirective"&&F.has(c.name)){const a=s(c,r);a&&l.push(a)}else i.push(c);if(i.length>0){const c=S(i.map(a=>o(a,r)).filter(a=>a!==null));c.length>0&&e.push(v("statement",c))}e.push(...l)}else{const i=S(n.map(l=>o(l,r)).filter(l=>l!==null));e.push(...i)}}function B(e){return{type:"text",value:e}}function h(e,t,n){return{type:"element",name:e,attributes:n??{},children:t}}function x(e,t,n){return h(e,[B(t)],n)}function U(e){const t=[];for(const n of e)n.name==="ul"||n.name==="ol"?t.length>0&&t[t.length-1]?.name==="p"?t[t.length-1].children.push(n):t.push(h("p",[n])):t.push(n);return t}function T(e,t){return le(e,t).tree}function le(e,t){const n=[],r={ancestors:[],depth:0,messages:n,source:t},o=e.children;return{tree:{type:"root",children:H(o,r)},messages:n}}const O=["chapter","section","subsection","subsubsection","paragraphs"];function ce(e){return O[Math.min(e-1,O.length-1)]}function H(e,t,n=!1){let r=7;for(const a of e)a.type==="heading"&&(r=Math.min(r,a.depth));if(r===7)return w(e,t,n);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===r?(!s&&i.length>0&&(o.push(...w(i,t,n)),i=[]),c(),s=a,l=[]):s?l.push(a):i.push(a);return!s&&i.length>0&&o.push(...w(i,t,n)),c(),o}function w(e,t,n=!1){const r=[];for(let o=0;o<e.length;o+=1){const s=e[o],i=M(s,t);if(i!==null){if(i.name==="p"&&i.children.length===1&&i.children[0].name==="md"){const l=i.children[0],c=r[r.length-1],a=o+1<e.length?M(e[o+1],t):null,f=a?.name==="p"?a.children:null;if(c?.name==="p")c.children.push(l),f&&(c.children.push(...f),o+=1);else{const d=[l];f&&(d.push(...f),o+=1),r.push(h("p",d))}continue}if((i.name==="ul"||i.name==="ol")&&r.length>0){const l=r[r.length-1];if(l?.name==="p"){l.children.push(i);continue}}r.push(i)}}return n?U(r):r}function ae(e,t,n){const r=ce(e.depth),o=h("title",b(e.children,n)),s=ue(e),i=H(t,{...n,depth:n.depth+1},!0),l=U(i);return h(r,[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 M(e,t){const n=pe[e.type];return n?n(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 n=e.children.filter(r=>r.type==="paragraph").map(r=>{const o={...t,ancestors:[...t.ancestors,e],depth:t.depth+1};return Z(r,o)});return h("blockquote",n)}function he(e,t){const n=e.children.map(r=>{const o={...t,ancestors:[...t.ancestors,e],depth:t.depth+1};return de(r,o)});return h(e.ordered?"ol":"ul",n)}function de(e,t){const n=w(e.children,t);return h("li",n)}function me(e,t){return x("program",e.value,e.lang?{language:e.lang}:void 0)}function ge(e,t){const n=e.value.split(/\\\\|[\r\n]+/).map(r=>r.trim()).filter(r=>r.length>0);return n.length>1?h("md",n.map(r=>x("mrow",r))):x("md",n[0]??"")}function ye(e,t){return e.meta==="inline"?x("m",e.value):ge(e)}function V(e,t){const n=oe(e.name);if(!n)return t.messages&&t.messages.push({type:"warning",reason:`Unknown directive: ${e.name}`,category:"unknown-directive"}),null;const r=ve(e),o=e.children,{title:s,body:i}=ke(o,t),l={...t,ancestors:[...t.ancestors,e],depth:t.depth+1};return ie(n,r,s,i,l,(c,a)=>M(c,a),(c,a)=>V(c,a))}function ke(e,t){const n=e[0];return n?.type==="paragraph"&&n.data?.directiveLabel===!0?{title:h("title",b(n.children,t)),body:e.slice(1)}:{title:null,body:e}}function ve(e){const t=e.attributes??{},n={};for(const[r,o]of Object.entries(t))o!=null&&(n[r==="id"?"xml:id":r]=o);return Object.keys(n).length>0?n: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)=>B(e.value),emphasis:(e,t)=>{const r=xe(e,t.source)==="underscore"?"term":"em";return h(r,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(n=>Ce(n,t)).filter(n=>n!==null)}function Ce(e,t){const n=be[e.type];if(!n)return t.messages&&t.messages.push({type:"warning",reason:`Unknown inline type: ${e.type}`,category:"unknown-inline-type"}),null;const r={...t,ancestors:[...t.ancestors,e]};return n(e,r)}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 n=null;for(const r of we){const o=e.indexOf(r.open,t);if(o===-1)continue;const s=e.indexOf(r.close,o+r.open.length);if(s!==-1&&!(r.open==="$"&&(o>0&&e[o-1]==="$"||o+1<e.length&&e[o+1]==="$"))&&(!n||o<n.index)){const i=e.substring(o+r.open.length,s);n={index:o,pattern:r,content:i}}}return n}function Se(e,t){let n=t;for(;n<e.length;){const r=e.indexOf("`",n);if(r===-1)return null;let o=0,s=r;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:r,end:a};l=c+1}n=r+o}return null}function De(e){const t=new Map;let n="",r=0,o=0;for(;r<e.length;){const s=Se(e,r),i=K(e,r);if(s&&(!i||s.start<i.index)){n+=e.substring(r,s.end),r=s.end;continue}if(!i){n+=e.substring(r);break}n+=e.substring(r,i.index);const l=`${X}${o}`;n+=l,t.set(l,{type:"math",value:i.content,meta:i.pattern.isDisplay?"display":"inline"}),r=i.index+i.pattern.open.length+i.content.length+i.pattern.close.length,o+=1}return{markdown:n,tokens:t}}function qe(e){const t=[];let n=0;for(;n<e.length;){const r=K(e,n);if(!r){n<e.length&&t.push({type:"text",value:e.substring(n)});break}r.index>n&&t.push({type:"text",value:e.substring(n,r.index)}),t.push({type:"math",value:r.content,meta:r.pattern.isDisplay?"display":"inline"}),n=r.index+r.pattern.open.length+r.content.length+r.pattern.close.length}return t.length===0?[{type:"text",value:e}]:t}function Ie(e,t){const n=[];let r=0;I.lastIndex=0;let o=I.exec(e);for(;o;){const s=o.index,i=o[0];s>r&&n.push({type:"text",value:e.substring(r,s)});const l=t.get(i);l?n.push(l):n.push({type:"text",value:i}),r=s+i.length,o=I.exec(e)}return r<e.length&&n.push({type:"text",value:e.substring(r)}),n.length===0?[{type:"text",value:e}]:n}function Y(e){W.visit(e,"paragraph",t=>{const n=t.children||[],r=[];for(const o of n)if(o.type==="text"){const s=qe(o.value);r.push(...s)}else r.push(o);t.children=r})}function Te(e,t){W.visit(e,"paragraph",n=>{const r=n.children||[],o=[];for(const s of r)if(s.type==="text"){const i=Ie(s.value,t);o.push(...i)}else o.push(s);n.children=o})}const Me=()=>e=>{Y(e)};function Ee(e){const t=e.trim();if(!t.startsWith(":"))return null;let n=0;for(let s=0;s<t.length&&t[s]===":";s++)n++;if(n<3)return null;const r=t.slice(n).trim(),o=r.match(/^([a-zA-Z0-9_-]+)(\[[^\]]*\])?(\{[^}]*\})?$/);return r===""?{colons:n,label:null,lineIndex:-1,isOpen:!1}:o?{colons:n,label:o[0],lineIndex:-1,isOpen:!0}:null}function Le(e){const t=[],n=[];for(const r of e)if(r.isOpen){const o={colons:r.colons,finalColons:r.colons,label:r.label,openLineIndex:r.lineIndex,closeLineIndex:null,children:[],parent:null};if(t.length>0){const s=t[t.length-1];o.parent=s,s.children.push(o)}else n.push(o);t.push(o)}else{const o=t[t.length-1];o&&o.colons===r.colons&&(o.closeLineIndex=r.lineIndex,t.pop())}return n}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,n){const r=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=r[i.openLineIndex],c=l.match(/^:+/)?.[0].length||3,a=l.slice(c);r[i.openLineIndex]=":".repeat(i.finalColons)+a}i.closeLineIndex!==null&&i.closeLineIndex>=0&&(r[i.closeLineIndex]=":".repeat(i.finalColons))}return r}function Ne(e){const t=e.split(`
|
|
2
|
+
`),n=[];let r=!1,o=null;for(let l=0;l<t.length;l++){const c=t[l],f=c.trim().match(/^(`{3,}|~{3,})/);if(f){const m=f[1];r?o&&m[0]===o[0]&&m.length>=o.length&&(r=!1,o=null):(r=!0,o=m);continue}if(r)continue;const d=Ee(c);d&&n.push({...d,lineIndex:l})}if(n.length===0)return e;const s=Le(n);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 n=t.slice(0,-1);if(!/^[a-zA-Z]/.test(n))return null;const r=n.match(/^([a-zA-Z][a-zA-Z0-9_-]*)(.*)$/);if(!r)return null;const[,o,s]=r;return s&&!/^(\[[^\]]*\]|\{[^}]*\})*$/.test(s)||!q[o.toLowerCase()]?null:n}function E(e){let t=0;for(let n=0;n<e.length;n++)if(e[n]===" ")t+=1;else if(e[n]===" ")t+=4;else break;return t}function Oe(e,t){const n=E(e[t]);for(let r=t+1;r<e.length;r++)if(!J(e[r]))return E(e[r])>n;return!1}function J(e){return e.trim()===""}function Pe(e){const t=e.split(`
|
|
4
|
+
`),n=[],r={level:0,openDirectives:[],inCodeFence:!1,codeFenceMarker:null};for(let o=0;o<t.length;o++){const s=t[o],i=s.trim(),l=E(s),c=i.match(/^(`{3,}|~{3,})/);if(c){const f=c[1];r.inCodeFence?r.codeFenceMarker&&i.startsWith(r.codeFenceMarker)&&(r.inCodeFence=!1,r.codeFenceMarker=null):(r.inCodeFence=!0,r.codeFenceMarker=f),n.push(s);continue}if(r.inCodeFence){n.push(s);continue}if(J(s)){n.push("");continue}const a=$e(i);if(a&&Oe(t,o)){for(;r.openDirectives.length>0&&!(r.openDirectives[r.openDirectives.length-1].indentLevel<l);){const g=r.openDirectives.pop();g&&n.push(":".repeat(g.colonCount))}const f=a.match(/^([a-zA-Z][a-zA-Z0-9_-]*)(.*)/),d=f?f[1].toLowerCase()+(f[2]||""):a,m=r.openDirectives.length*1+3;n.push(":".repeat(m)+d),r.openDirectives.push({label:d,indentLevel:l,colonCount:m}),r.level=l}else{for(;r.openDirectives.length>0&&l<=r.openDirectives[r.openDirectives.length-1].indentLevel;){const f=r.openDirectives.pop();f&&n.push(":".repeat(f.colonCount))}r.openDirectives.length>0,n.push(s),r.level=l}}for(;r.openDirectives.length>0;){const o=r.openDirectives.pop();o&&n.push(":".repeat(o.colonCount))}return n.join(`
|
|
5
|
+
`)}const Q=function(){return function(t,n){if(typeof n?.value=="string"){const r=Pe(n.value),o=Ne(r),s=De(o),l=R.unified().use(j).use(z).parse(s.markdown);return Te(l,s.tokens),T(l,s.markdown)}return Y(t),T(t)}},Ae=/["&'<>`]/g,_e=/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,Re=/[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g,je=/[|\\{}()[\]^$+*?.]/g,P=new WeakMap;function ze(e,t){if(e=e.replace(t.subset?We(t.subset):Ae,r),t.subset||t.escapeOnly)return e;return e.replace(_e,n).replace(Re,r);function n(o,s,i){return t.format((o.charCodeAt(0)-55296)*1024+o.charCodeAt(1)-56320+65536,i.charCodeAt(s+2),t)}function r(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=Be(e),P.set(e,t)),t}function Be(e){const t=[];let n=-1;for(;++n<e.length;)t.push(e[n].replace(je,"\\$&"));return new RegExp("(?:"+t.join("|")+")","g")}function Ue(e){return"&#x"+e.toString(16).toUpperCase()+";"}function He(e,t){return ze(e,Object.assign({format:Ue},t))}const Ze=/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;function y(e,t,n){const r=Ve(e);return n?r.replace(n,o):o(r);function o(s){return He(s,{subset:t})}}function Ve(e){return String(e||"").replace(Ze,"")}const Xe=/]]>/g,Ke=[">"];function Ye(e){return"<![CDATA["+y(e.value,Ke,Xe)+"]]>"}function Ge(e){return"<!--"+y(e.value,["-"])+"-->"}const Je=[" ",`
|
|
6
|
+
`," ",'"',"&","'","/","<","=",">"];function D(e){return y(e,Je)}function A(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 L(e,t){const n=String(e);let r=t.options.quote||'"';if(t.options.quoteSmart){const o=r==='"'?"'":'"';A(n,r)>A(n,o)&&(r=o)}return r+y(n,["<","&",r])+r}function Qe(e,t){const n=D(e.name),r=e.public,o=e.system;let s="<!DOCTYPE";return n!==""&&(s+=" "+n),r?s+=" PUBLIC "+L(r,t):o&&(s+=" SYSTEM"),o&&(s+=" "+L(o,t)),s+">"}const et={}.hasOwnProperty;function tt(e,t){const n=D(e.name),r=ne(e,t),o=e.attributes||{},s=r?!1:t.options.closeEmptyElements,i=[];let l;for(l in o)if(et.call(o,l)){const c=o[l];c!=null&&i.push(D(l)+"="+L(c,t))}return"<"+n+(i.length===0?"":" "+i.join(" "))+(s?(t.options.tightClose?"":" ")+"/":"")+">"+r+(s?"":"</"+n+">")}const nt=/\?>/g,rt=[">"];function ot(e){const t=D(e.name)||"x",n=y(e.value,rt,nt);return"<?"+t+(n?" "+n:"")+"?>"}const st=["&","<"];function ee(e){return y(e.value,st)}function it(e,t){return t.options.allowDangerousXml?e.value:ee(e)}const lt={}.hasOwnProperty,_={cdata:Ye,comment:Ge,doctype:Qe,element:tt,instruction:ot,raw:it,root:ne,text:ee};function te(e,t){const n=e&&e.type;if(!n)throw new Error("Expected node, not `"+e+"`");if(!lt.call(_,n))throw new Error("Cannot compile unknown node `"+n+"`");const r=_[n];return r(e,t)}function ne(e,t){const n=e&&e.children||[];let r=-1;const o=[];for(;++r<n.length;)o[r]=te(n[r],t);return o.join("")}function ct(e,t){const n={options:{}};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 te(r,n)}function at(e){const t=R.unified().use(j).use(z).use(Q),n=t.parse(e),r=t.runSync(n,{value:e});return ct(r.children)}exports.DIRECTIVE_MAP=re;exports.PROOF_SOLUTION_NAMES=F;exports.markdownToPretext=at;exports.mdastToPtxast=T;exports.remarkMath=Me;exports.remarkPretext=Q;
|
package/dist/index.d.ts
ADDED
|
@@ -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';
|