@reviseio/engine 3.0.1 → 3.1.1
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/CHANGELOG.md +29 -0
- package/assets/{historyMaterializeWorker-Kl0syEbc.js → historyMaterializeWorker-BMHTnStL.js} +131 -130
- package/chunks/{AgentHtmlConverter-CnnB8-WI.js → AgentHtmlConverter-B5qstcmM.js} +129 -128
- package/chunks/{AgentPageRenderer-DLgUGrGy.js → AgentPageRenderer-JMhCw-hi.js} +4 -4
- package/chunks/BlockDiff-C8eiZ-sf.js +45 -0
- package/chunks/{CanvasRenderer-C1Vegx9Y.js → CanvasRenderer-C2vAzLWM.js} +12 -12
- package/chunks/DocumentCompare-DX4RWeSV.js +10 -0
- package/chunks/{DocumentLayoutEngine-C62pyGVf.js → DocumentLayoutEngine-BhcWXkeu.js} +45 -45
- package/chunks/{DocumentMutator-Bat_oLBQ.js → DocumentMutator-FEFU1YM0.js} +6 -6
- package/chunks/DocumentOutline-BOZoTohH.js +4 -0
- package/chunks/{DocumentScrubber-BZQn_z92.js → DocumentScrubber-C4N3Qagm.js} +1 -1
- package/chunks/{DocumentSession-BDcazSEv.js → DocumentSession-gZHWP7By.js} +1 -1
- package/chunks/{DocxConverter-BC31uYIL.js → DocxConverter-BOyLn5eZ.js} +709 -703
- package/chunks/{DocxGraft-L10HXM3U.js → DocxGraft-BbE-AaM5.js} +2 -2
- package/chunks/{HtmlConverter-C5ihhZ6G.js → HtmlConverter-BlzwsPEn.js} +3 -3
- package/chunks/MarkdownConverter-BHVskVXI.js +59 -0
- package/chunks/{OdtConverter-c1fTWoAt.js → OdtConverter-BdmBFOD1.js} +1 -1
- package/chunks/{OfficeImportShared-CrOpHpJ-.js → OfficeImportShared-B2XJgnDO.js} +4 -4
- package/chunks/{PdfConverter-DX7fsRH5.js → PdfConverter-DZMCHDAo.js} +6 -6
- package/chunks/{RtfConverter-hbJ2529G.js → RtfConverter-RqE-xZcW.js} +2 -2
- package/chunks/{TableBorders-DVhdvo6f.js → TableBorders-ARyTi49V.js} +1 -1
- package/chunks/{TxtConverter-YHv5FXWb.js → TxtConverter-EPQtb6A6.js} +1 -1
- package/chunks/{Typography-B4gndEdB.js → Typography-XR6scRNV.js} +9 -9
- package/chunks/{XmlConverter-BRRluy9k.js → XmlConverter-9sFRFaza.js} +1 -1
- package/chunks/{capture-i_dPkQIS.js → capture-DhGn0CWA.js} +1 -1
- package/chunks/{carrier-pYM9xf6x.js → carrier-D58g-gw4.js} +1 -1
- package/chunks/{chromeImageLayout-BiQ3Iz1-.js → chromeImageLayout-DeYYS8YP.js} +1 -1
- package/chunks/{engine-LMUnxdm2.js → engine-C5fxuZJo.js} +139 -139
- package/chunks/{footnoteFlatten-EE85Hjre.js → footnoteFlatten-coCVFd3c.js} +1 -1
- package/chunks/{graftExport-G-dG7Vcc.js → graftExport-FBGE2qvt.js} +1 -1
- package/chunks/{index-BLb10wQt.js → index-Cezk1s_y.js} +4 -4
- package/chunks/{serverDocumentSession-DGDU5Lh3.js → serverDocumentSession-LMu3kocH.js} +559 -560
- package/chunks/{signatures-DTNDS2PH.js → signatures-DsJnnIqV.js} +1 -1
- package/chunks/textExtraction-Bqaqt1Kg.js +2 -0
- package/engine.js +2 -2
- package/package.json +2 -2
- package/chunks/BlockDiff-B6-aJxHo.js +0 -42
- package/chunks/DocumentCompare-C8295s35.js +0 -6
- package/chunks/MarkdownConverter-DofaSeC4.js +0 -57
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{r as d,ad as A,a7 as k,
|
|
1
|
+
import{r as d,ad as A,a7 as k,bi as x,bj as S}from"./engine-C5fxuZJo.js";const c=2;function s(e){return e===null||typeof e!="object"?JSON.stringify(e)??"null":Array.isArray(e)?`[${e.map(s).join(",")}]`:
|
|
2
2
|
`{${Object.entries(e).filter(([,r])=>r!==void 0).sort(([r],[t])=>r<t?-1:r>t?1:0).map(([r,t])=>`${JSON.stringify(r)}:${s(t)}`).join(",")}}`}function i(e,r=c){if(e===null||typeof e!="object")return e;if(Array.
|
|
3
3
|
isArray(e))return e.map(o=>i(o,r));const t=e;if(t.type==="text")return{...t,marks:(r===1?x:S)(t.marks)};const n={};for(const[o,a]of Object.entries(t))o==="indentLevel"&&t.type==="list-item"&&!a||o==="\
|
|
4
4
|
hasHeader"&&t.type==="table"&&!a||o==="footnoteNumber"&&t.type==="footnote"||(n[o]=i(a,r));if(Array.isArray(n.children)){const o=[];for(const a of n.children){const f=o[o.length-1];if(f&&f.type==="tex\
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function r(e){if(!e||typeof e!="object")return"";const t=e;if(t.type==="text"&&typeof t.text=="string")return t.text;if((t.type==="code"||t.type==="math")&&typeof t.content=="string")return t.content;
|
|
2
|
+
if(Array.isArray(t.children)){const n=t.type==="list"?" ":"";return t.children.map(o=>r(o)).join(n)}return""}export{r as e};
|
package/engine.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{
|
|
2
|
-
e as decodeYDoc,o as documentToDocx,c as encodeYDoc,t as engineModules,i as exportDocument,D as fileToYDoc,m as installDomShims,r as parseDocument,
|
|
1
|
+
import{bU as s,bV as a,bW as e,bX as o,bY as c,bT as t,bZ as i,b_ as D,j as m,b$ as r,c3 as d,c0 as n,c1 as b,c2 as l}from"./chunks/engine-C5fxuZJo.js";export{s as createReviseYDoc,a as createServerDocumentSession,
|
|
2
|
+
e as decodeYDoc,o as documentToDocx,c as encodeYDoc,t as engineModules,i as exportDocument,D as fileToYDoc,m as installDomShims,r as parseDocument,d as seedDocRootWithClaim,n as seedYDocFromFile,b as ydocSourceFidelity,
|
|
3
3
|
l as ydocToDocument};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reviseio/engine",
|
|
3
|
-
"version": "3.
|
|
4
|
-
"reviseBuild": "
|
|
3
|
+
"version": "3.1.1",
|
|
4
|
+
"reviseBuild": "2953a6472",
|
|
5
5
|
"description": "Revise's headless document engine: Word-faithful import and export, tracked changes, comments, layout, and agent tools. Free to use; the `revise` package is the API.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"docx",
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import{M as ke}from"./engine-LMUnxdm2.js";import{a as le}from"./ParagraphDiffAnalysis-DrmStvmz.js";const ce=new WeakMap,E=r=>{let s=ce.get(r);return s===void 0&&(s=r.children.map(d=>d.text).join(""),ce.
|
|
2
|
-
set(r,s)),s},z=r=>r.trim()?r.trim().split(/\s+/).length:0,ae=new WeakMap,h=r=>{let s=ae.get(r);if(s===void 0){const{id:d,children:m,...J}=r;s=JSON.stringify(J),ae.set(r,s)}return s},U=(r={})=>Object.fromEntries(
|
|
3
|
-
ke.filter(s=>r[s]!==void 0).map(s=>[s,r[s]])),be=["comment","footnoteRef","footnotePendingBody","footnotePendingKind","oxmlRange","bookmark"],de=r=>{const s={...r};for(const d of be)delete s[d];return s};
|
|
4
|
-
function xe(r){return{...r,children:r.children.map(s=>({...s,marks:U(s.marks)}))}}function he(r){return r.type==="paragraph"?`p${h(r)}${E(r)}`:r.type==="list"?`l${h(r)}${r.children.map(s=>h(s)+E(s)).join(
|
|
5
|
-
"\0")}`:JSON.stringify(r,(s,d)=>s==="id"?void 0:d)}function we(r){return r.type==="paragraph"||r.type==="list"||r.type==="table"}function ee(r,s){const d=JSON.parse(JSON.stringify(r)),m=new Map,J=[],C=O=>{
|
|
6
|
-
if(!O||typeof O!="object")return;const y=O;if(typeof y.type=="string"&&typeof y.id=="string"){const W=s();m.set(y.id,W),y.id=W}typeof y.docxTablePropertyOverrides=="string"&&J.push(y),Array.isArray(y.
|
|
7
|
-
children)&&y.children.forEach(C)};C(d);for(const O of J)try{const y=JSON.parse(O.docxTablePropertyOverrides);for(const W of["rows","rowExceptions","cells"]){const x=y[W];!x||typeof x!="object"||Array.
|
|
8
|
-
isArray(x)||(y[W]=Object.fromEntries(Object.entries(x).map(([u,$])=>[m.get(u)??u,$])))}O.docxTablePropertyOverrides=JSON.stringify(y)}catch{}return d}function te(r){const s=d=>{if(!d||typeof d!="objec\
|
|
9
|
-
t")return;const m=d;m.type==="text"&&m.marks&&(m.marks=de(m.marks)),m.children?.forEach(s)};return s(r),r}const Y=new Map,H=r=>{let s=Y.get(r);return s||(s=new Set(r.toLowerCase().match(/[\p{L}\p{N}]+/gu)??
|
|
10
|
-
[]),Y.size>5e4&&Y.clear(),Y.set(r,s)),s};function re(r,s){let d=0;for(const m of r)s.has(m)&&d++;return 2*d/(r.size+s.size||1)}function ne(r,s,d,m,J,C,O,y,W=()=>!1){const x=new Map,u=g=>{const f=d(g);
|
|
11
|
-
return x.has(f)||x.set(f,`t${x.size}`),x.get(f)+" "},$=le(r.map(u).join(""),s.map(u).join("")).diffs;let K=0,D=0,L=0,Q=0;const I=()=>{const g=r.slice(L,K),f=s.slice(Q,D);if(g.length===1&&f.length===1&&
|
|
12
|
-
W(g[0],f[0])){C(g[0],f[0]);return}const M=g.map(v=>f.map(j=>m(v,j))),w=Array.from({length:g.length+1},()=>new Float64Array(f.length+1));for(let v=g.length-1;v>=0;v--)for(let j=f.length-1;j>=0;j--)w[v][j]=
|
|
13
|
-
Math.max(w[v+1][j],w[v][j+1],M[v][j]+w[v+1][j+1]);let k=0,S=0;for(;k<g.length||S<f.length;)k<g.length&&S<f.length&&M[k][S]>0&&w[k][S]===M[k][S]+w[k+1][S+1]?C(g[k++],f[S++]):k<g.length&&(S===f.length||
|
|
14
|
-
w[k+1][S]>=w[k][S+1])?O(g[k++]):y(f[S++])};for(const[g,f]of $){const M=f.trim().split(/\s+/).filter(Boolean).length;if(g===0){I();for(let w=0;w<M;w++)J(r[K++],s[D++]);L=K,Q=D}else g===-1?K+=M:D+=M}I()}
|
|
15
|
-
const Z=8e6,pe=1e6;function Se(r,s,d){if(r.length*s.length>Z)return null;const{nextId:m,nextBlockId:J,label:C,authorName:O,authorOf:y,pairLoneChanges:W}=d,x=t=>W?t:()=>!1,u={inlineChanges:0,insertedBlocks:0,
|
|
16
|
-
deletedBlocks:0,unmarkedBlocks:0,insertedWords:0,deletedWords:0},$=t=>y?.(t)??O,K=(t,e,n)=>{const c=$(n);return{[t]:!0,suggestionId:e,suggestionSource:"manual",suggestionAuthorType:"human",suggestionLabel:C,
|
|
17
|
-
...c?{suggestionAuthorName:c}:{}}},D=(t,e)=>{if(!we(t))return null;const n=t.type==="paragraph"?t.children[0]?.marks:void 0,c=t.type==="paragraph"?xe(t):t,i=e==="delete"?te(ee(c,J)):c,o=m(),p=$(n),l={
|
|
18
|
-
id:o,blockId:i.id,source:"manual",authorType:"human",label:e==="insert"?"Insert":"Delete",...p?{agentName:p}:{}},a=e==="insert"?{...l,type:"block-insert"}:{...l,type:"block-delete",beforeBlock:i},b="c\
|
|
19
|
-
hildren"in i&&i.type!=="table"?P(i):"";return e==="insert"?(u.insertedBlocks++,u.insertedWords+=z(b)):(u.deletedBlocks++,u.deletedWords+=z(b)),{...i,structuralSuggestionId:o,structuralSuggestionKind:e,
|
|
20
|
-
structuralSuggestionRecord:JSON.stringify(a)}},L=(t,e,n)=>{const c=[];let i=0;for(const o of t.children){const p=Math.max(e-i,0),l=Math.min(e+n-i,o.text.length);if(l>p&&c.push({...o,text:o.text.slice(
|
|
21
|
-
p,l)}),i+=o.text.length,i>=e+n)break}return c},Q=(t,e,n)=>{const c=E(t),i=E(e);if(h(t)!==h(e)||z(c)*z(i)>pe){I(t,"delete",n),I(e,"insert",n);return}const o=[];let p=0,l=0,a="";const b=(A,N)=>{a||(a=m(),
|
|
22
|
-
u.inlineChanges++);const R=z(A.map(B=>B.text).join(""));N==="insertion"?u.insertedWords+=R:u.deletedWords+=R,o.push(...A.map(B=>{const T=U(B.marks);return{...B,marks:{...N==="deletion"?de(T):T,...K(N,
|
|
23
|
-
a,B.marks)}}}))};for(const[A,N]of le(c,i).diffs){if(A===-1)b(L(t,p,N.length),"deletion");else if(A===1)b(L(e,l,N.length),"insertion");else{const R=L(t,p,N.length),B=L(e,l,N.length);let T=0,F=0,q=0,G=0;
|
|
24
|
-
for(;T<R.length&&F<B.length;){const V=Math.min(R[T].text.length-q,B[F].text.length-G),oe={...R[T],text:R[T].text.slice(q,q+V)},X={...B[F],text:B[F].text.slice(G,G+V)},{bookmark:Ne,...me}=U(oe.marks),{
|
|
25
|
-
bookmark:Ie,...ye}=U(X.marks);JSON.stringify(me)===JSON.stringify(ye)?(a="",o.push({...X,marks:U(X.marks)})):(b([oe],"deletion"),b([X],"insertion")),q+=V,G+=V,q===R[T].text.length&&(T++,q=0),G===B[F].
|
|
26
|
-
text.length&&(F++,G=0)}}A!==1&&(p+=N.length),A!==-1&&(l+=N.length)}n.push({...e,children:o})},I=(t,e,n)=>{if(t.type==="table"){n.push(S(t,e));return}const c=D(t,e);c?n.push(c):(u.unmarkedBlocks++,e===
|
|
27
|
-
"insert"&&n.push(t))},g=(t,e)=>{if(h(t)!==h(e))return 0;const n=re(H(E(t)),H(E(e)));return n>=.5?n:0},f=(t,e,n)=>{const c=()=>{I(t,"delete",n),I(e,"insert",n)};if(t.children.length*e.children.length>Z)
|
|
28
|
-
return c();const i=[];let o=!1;if(ne(t.children,e.children,l=>h(l)+E(l),g,(l,a)=>i.push([l,a]),(l,a)=>i.push([l,a]),()=>o=!0,()=>o=!0,x((l,a)=>h(l)===h(a))),o||i.some(([l,a])=>h(l)!==h(a)||z(E(l))*z(E(
|
|
29
|
-
a))>pe))return c();const p=[];for(const[l,a]of i)Q(l,a,p);n.push({...e,children:p})},M=(t,e)=>{const n=z(P(t));e==="insert"?(u.insertedBlocks++,u.insertedWords+=n):(u.deletedBlocks++,u.deletedWords+=n)},
|
|
30
|
-
w=(t,e)=>e==="delete"?te(ee(t,J)):t,k=(t,e,n,c)=>{const i=m();return{id:i,record:{id:i,type:"table-change",tableId:t,source:"manual",authorType:"human",label:c[n],...O?{agentName:O}:{},tableRevision:{
|
|
31
|
-
version:1,suggestionKind:n,markers:e.map(([o,p])=>({kind:n==="insert"?"row-insert":"row-delete",rowId:o.id,rowIndex:p})),operations:e.map(([o,p])=>({kind:"row",action:n,rowId:o.id,rowIndex:p,row:JSON.
|
|
32
|
-
parse(JSON.stringify(o))}))}}}},S=(t,e)=>{const n=e==="delete"?te(ee(t,J)):t,{id:c,record:i}=k(n.id,n.children.map((o,p)=>[o,p]),e,{insert:"Insert table",delete:"Delete table"});return M(n,e),{...n,structuralSuggestionRecords:JSON.
|
|
33
|
-
stringify([i]),children:n.children.map(o=>({...o,diffState:e,suggestionId:c}))}},v=(t,e)=>h(t)===h(e)&&t.children.length===e.children.length&&t.children.every((n,c)=>h(n)===h(e.children[c])),j=(t,e)=>t.
|
|
34
|
-
children.every((n,c)=>n.children.length*e.children[c].children.length<=Z),_=(t,e)=>v(t,e)&&j(t,e),se=(t,e)=>({...e,children:e.children.map((n,c)=>({...n,children:ie(t.children[c].children,n.children)}))}),
|
|
35
|
-
ge=(t,e,n)=>{if(h(t)!==h(e)||t.children.length*e.children.length>Z){I(t,"delete",n),I(e,"insert",n);return}const c=[],i=[],o=(l,a)=>{const b=w(l,a),{id:A,record:N}=k(e.id,[[b,c.length]],a,{insert:"Ins\
|
|
36
|
-
ert table row",delete:"Delete table row"});i.push(N),M(b,a),c.push({...b,diffState:a,suggestionId:A})},p=l=>he(l);ne(t.children,e.children,p,(l,a)=>{if(!_(l,a))return 0;const b=re(H(P(l)),H(P(a)));return b>=
|
|
37
|
-
.5?b:0},(l,a)=>{_(l,a)?c.push(se(l,a)):(o(l,"delete"),o(a,"insert"))},(l,a)=>c.push(se(l,a)),l=>o(l,"delete"),l=>o(l,"insert"),x(_)),n.push({...e,children:c,...i.length>0?{structuralSuggestionRecords:JSON.
|
|
38
|
-
stringify(i)}:{}})},fe=(t,e)=>{if(t.type==="paragraph"&&e.type==="paragraph")return g(t,e);if(t.type==="list"&&e.type==="list"&&h(t)===h(e)||t.type==="table"&&e.type==="table"&&h(t)===h(e)){const n=re(
|
|
39
|
-
H(P(t)),H(P(e)));return n>=.3?n:0}return 0};function ie(t,e){const n=[],c=(i,o)=>{i.type==="list"&&o.type==="list"?f(i,o,n):i.type==="table"&&o.type==="table"?ge(i,o,n):i.type==="paragraph"&&o.type===
|
|
40
|
-
"paragraph"?Q(i,o,n):n.push(o)};return ne(t,e,he,fe,c,c,i=>I(i,"delete",n),i=>I(i,"insert",n),x((i,o)=>i.type===o.type&&(i.type==="paragraph"||i.type==="list"||i.type==="table")&&h(i)===h(o))),n}return{
|
|
41
|
-
blocks:ie(r,s),stats:u}}const ue=new WeakMap;function P(r){const s=ue.get(r);if(s!==void 0)return s;const d=Oe(r);return ue.set(r,d),d}function Oe(r){return"children"in r&&Array.isArray(r.children)?r.
|
|
42
|
-
children.map(s=>{const d=s;return d.type==="text"?d.text??"":P(s)}).join(" "):""}export{Se as d};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import{DocumentSession as m}from"./DocumentSession-BDcazSEv.js";import{g as i,a as g}from"./engine-LMUnxdm2.js";import{d}from"./BlockDiff-B6-aJxHo.js";const p=[{label:"headers and footers",keys:["chro\
|
|
2
|
-
me","headers","footers"]},{label:"footnotes and endnotes",keys:["notes"]},{label:"page layout",keys:["pageLayout","pageless"]}];function l(s,t){return JSON.stringify(t.map(e=>s[e]??null),(e,o)=>e==="i\
|
|
3
|
-
d"||e.startsWith("suggestion")?void 0:o)}function c(s){const t=new m(`compare-accept-${i()}`,s);try{const e=t.getDocumentMutator(),o=e.getLiveSuggestionIds();return o.length>0&&e.acceptLiveSuggestions(
|
|
4
|
-
o),t.getWorkingDocument()}finally{t.dispose()}}function f(s,t,e={}){const o=c(s),a=c(t),r=d(o.children,a.children,{nextId:()=>`compare-${i()}`,nextBlockId:g,label:e.label??"Compare",authorName:e.authorName,
|
|
5
|
-
pairLoneChanges:!0});if(!r)return null;const u=p.filter(({keys:n})=>l(o,n)!==l(a,n)).map(({label:n})=>n);return{doc:{...a,children:r.blocks},stats:r.stats,untrackedRegions:u}}export{c as acceptAllRevisions,
|
|
6
|
-
f as compareDocuments};
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import{a_ as k,b0 as y,aR as b,aK as $,b1 as w,b2 as M,b3 as T,b4 as I,a as u,b5 as _}from"./engine-LMUnxdm2.js";import{b as j}from"./TableGrid-DbroRucl.js";import{a as C,e as v,i as N}from"./ListNumbering-Qlu6AuRZ.js";
|
|
2
|
-
class L{static toMarkdown(e){if(!e||e.type!=="doc"||!e.children)return"";this.exportListNumbering=C(e);const n=[];for(const t of e.children){if(t.type==="header-footer"||t.type==="footnote")continue;if(t.
|
|
3
|
-
type==="toc"){const a=this.tocToMarkdown(t,e);a!==null&&n.push(a);continue}const s=this.blockToMarkdown(t);s!==null&&n.push(s)}const i=[...e.notes??[],...e.children.filter(t=>t.type==="footnote")];if(i.
|
|
4
|
-
length>0){const t=new Map,s=c=>{if(!c||typeof c!="object")return;if(Array.isArray(c)){c.forEach(s);return}const o=c;o.type!=="footnote"&&(o.marks?.footnoteRef&&!t.has(o.marks.footnoteRef)&&t.set(o.marks.
|
|
5
|
-
footnoteRef,o.text??""),s(o.children))};s(e.children.filter(c=>c.type!=="footnote"));let a=t.size;for(const c of i){const o=t.get(c.id)||String(a+=1),r=[];c.children.forEach((h,l)=>{const d=this.blockToMarkdown(
|
|
6
|
-
h)??"";l===0?r.push(`[^${o}]: ${d}`):r.push(d.split(`
|
|
7
|
-
`).map(p=>` ${p}`).join(`
|
|
8
|
-
`))}),n.push(r.join(`
|
|
9
|
-
|
|
10
|
-
`))}}return n.join(`
|
|
11
|
-
|
|
12
|
-
`)}static fromInlineMarkdown(e){return k(this.parseInlineContent(e))}static tocToMarkdown(e,n){const i=y(n,b(e));return i.length===0?null:i.map(t=>`${" ".repeat(t.level-1)}${t.text}`).join(`
|
|
13
|
-
`)}static blockToMarkdown(e){switch(e.type){case"paragraph":return $(e)?"---":this.paragraphToMarkdown(e);case"list":return this.listToMarkdown(e);case"table":return this.tableToMarkdown(e);case"image":
|
|
14
|
-
return this.imageToMarkdown(e);case"code":return this.codeBlockToMarkdown(e);case"math":return this.mathBlockToMarkdown(e);case"placeholder":return null;default:return null}}static paragraphToMarkdown(e){
|
|
15
|
-
const n=this.inlineNodesToMarkdown(e.children);return e.heading?`${"#".repeat(e.heading)} ${n}`:n}static inlineNodesToMarkdown(e){return this.coalesceMarkdownRuns(e).map(n=>this.textNodeToMarkdown(n)).
|
|
16
|
-
join("")}static markdownMarksKey(e){const n=e.marks||{};return JSON.stringify([!!n.code,!!n.latex,!!n.bold,!!n.italic,!!n.strikethrough,n.link||null])}static coalesceMarkdownRuns(e){const n=[];let i=null;
|
|
17
|
-
for(const t of e){const s=t.type==="text"&&!t.marks?.footnoteRef?this.markdownMarksKey(t):null,a=n[n.length-1];s!==null&&s===i&&a?n[n.length-1]={...a,text:a.text+t.text}:n.push(t),i=s}return n}static textNodeToMarkdown(e){
|
|
18
|
-
if(e.marks?.footnoteRef)return`[^${e.text}]`;let n=e.text;return n=n.replace(/\\/g,"\\\\"),e.marks&&(e.marks.code?n=`\`${n.replace(/`/g,"\\`")}\``:e.marks.latex?n=`$${n}$`:(e.marks.bold&&(n=`**${n}**`),
|
|
19
|
-
e.marks.italic&&(n=`*${n}*`),e.marks.strikethrough&&(n=`~~${n}~~`)),e.marks.link&&(n=`[${n}](${e.marks.link})`)),n}static listToMarkdown(e){const n=[],i=this.exportListNumbering?.get(e.id);let t=i?v(i):
|
|
20
|
-
e.startNumber??1;for(const s of e.children){if(s.type!=="list-item")continue;const a=" ".repeat(s.indentLevel??0),c=this.inlineNodesToMarkdown(s.children);let o;e.variant==="ordered"?(o=`${t}.`,t++):
|
|
21
|
-
e.variant==="todo"?o=s.checked?"- [x]":"- [ ]":o="-",n.push(`${a}${o} ${c}`)}return n.join(`
|
|
22
|
-
`)}static tableToMarkdown(e){if(!e.children||e.children.length===0)return"";const n=j(e),i=[];for(let t=0;t<n.rows;t++){const s=[];for(let a=0;a<n.cols;a++){const c=n.occupancy[t][a];if(!c||c.row!==t||
|
|
23
|
-
c.col!==a){s.push("");continue}const o=c.cell.children.map(r=>r.type==="paragraph"?this.inlineNodesToMarkdown(r.children):r.type==="code"?`\`${r.content||""}\``:"").join(" ");s.push(o)}if(i.push(`| ${s.
|
|
24
|
-
join(" | ")} |`),t===0){const a=s.map(()=>"---").join(" | ");i.push(`| ${a} |`)}}return i.join(`
|
|
25
|
-
`)}static imageToMarkdown(e){return``}static codeBlockToMarkdown(e){const n=e.language||"",i=e.content||"";return`\`\`\`${n}
|
|
26
|
-
${i}
|
|
27
|
-
\`\`\``}static mathBlockToMarkdown(e){return`$$
|
|
28
|
-
${e.content||""}
|
|
29
|
-
$$`}static fromMarkdown(e){const n=e.split(`
|
|
30
|
-
`),i=[];let t=0;for(;t<n.length;){const s=n[t];if(s.trim()===""){t++;continue}if(s.trim().startsWith("```")){const l=this.parseCodeBlock(n,t);i.push(l.block),t=l.nextIndex;continue}if(s.trim().startsWith(
|
|
31
|
-
"$$")){const l=this.parseMathBlock(n,t);i.push(l.block),t=l.nextIndex;continue}if(s.trim().startsWith("|")&&s.trim().endsWith("|")){const l=this.parseTable(n,t);l.block&&i.push(l.block),t=l.nextIndex;
|
|
32
|
-
continue}const a=s.match(/^\[\^([^\]]+)\]:\s?(.*)$/);if(a){const l=a[1],d=[k(this.parseInlineContent(a[2]??""))];for(t++;t<n.length;){const p=n[t];if(p.trim()===""){const f=n[t+1];if(f!==void 0&&/^ {4}/.
|
|
33
|
-
test(f)){t++;continue}break}if(!/^ {4}/.test(p))break;d.push(k(this.parseInlineContent(p.slice(4)))),t++}w(d),i.push({type:"footnote",id:`mdfn-${l}`,children:d,...M(l)==="endnote"?{noteKind:"endnote"}:
|
|
34
|
-
{}});continue}const c=T(s,"markdown");if(c){i.push(I(_[c])),t++;continue}if(this.isListItem(s)){const l=this.parseList(n,t);i.push(l.block),t=l.nextIndex;continue}const o=s.trim().match(/^!\[([^\]]*)\]\(([^)]+)\)$/);
|
|
35
|
-
if(o){i.push({type:"image",id:u(),src:o[2],alt:o[1],width:400,height:300}),t++;continue}const r=s.match(/^(#{1,6})\s+(.*)$/);if(r){const l=r[1].length,d=r[2],p=k(this.parseInlineContent(d));p.heading=
|
|
36
|
-
l,i.push(p),t++;continue}const h=k(this.parseInlineContent(s));i.push(h),t++}return N(u,i),{type:"doc",children:i.length>0?i:[k()]}}static isListItem(e){const n=e.trimStart();return!!(/^[-*+]\s/.test(
|
|
37
|
-
n)||/^\d+\.\s/.test(n)||/^[-*+]\s\[[x ]\]\s/i.test(n))}static parseList(e,n){const i=[];let t=n,s="unordered",a=1,c=!0;for(;t<e.length;){const o=e[t];if(o.trim()===""||!this.isListItem(o))break;const r=o.
|
|
38
|
-
match(/^(\s*)/),h=r?r[1].length:0,l=h===0?0:Math.max(1,Math.floor(h/2)),d=o.trimStart();let p,f;const m=d.match(/^[-*+]\s\[([x ])\]\s(.*)$/i);if(m)c&&(s="todo"),f=m[1].toLowerCase()==="x",p=m[2];else if(/^\d+\.\s/.
|
|
39
|
-
test(d)){const g=d.match(/^(\d+)\.\s(.*)$/);g?(c&&(s="ordered",a=parseInt(g[1],10)),p=g[2]):p=d}else{const g=d.match(/^[-*+]\s(.*)$/);p=g?g[1]:d}const x={type:"list-item",id:u(),children:this.parseInlineContent(
|
|
40
|
-
p),indentLevel:l};f!==void 0&&(x.checked=f),i.push(x),c=!1,t++}return{block:{type:"list",id:u(),variant:s,children:i,startNumber:s==="ordered"?a:void 0},nextIndex:t}}static parseCodeBlock(e,n){const i=e[n].
|
|
41
|
-
trim().slice(3).trim(),t=[];let s=n+1;for(;s<e.length;){const a=e[s];if(a.trim()==="```"){s++;break}t.push(a),s++}return{block:{type:"code",id:u(),content:t.join(`
|
|
42
|
-
`),language:i||void 0},nextIndex:s}}static parseMathBlock(e,n){const i=[];let t=n+1;for(;t<e.length;){const a=e[t];if(a.trim()==="$$"){t++;break}i.push(a),t++}const s=i.join(`
|
|
43
|
-
`).replace(/\\\\/g,"\\");return{block:{type:"math",id:u(),content:s},nextIndex:t}}static parseTable(e,n){const i=[];let t=n;for(;t<e.length;){const h=e[t].trim();if(!h.startsWith("|")||!h.endsWith("|"))
|
|
44
|
-
break;i.push(h),t++}if(i.length<2)return{block:null,nextIndex:t};const s=i[1];if(!/^\|[\s\-:|]+\|$/.test(s))return{block:null,nextIndex:t};const a=[],c=!0;for(let h=0;h<i.length;h++){if(h===1)continue;
|
|
45
|
-
const l=i[h].slice(1,-1).split("|").map(d=>d.trim()).map(d=>({type:"table-cell",id:u(),children:[k(this.parseInlineContent(d))]}));a.push({type:"table-row",id:u(),children:l})}const o=a[0]?.children.length||
|
|
46
|
-
1,r=Array(o).fill(100/o);return{block:{type:"table",id:u(),children:a,columnWidths:r,hasHeader:c},nextIndex:t}}static parseInlineContent(e){const n=[],i=[{regex:/\[\^([^\]]+)\]/g,type:"footnoteref"},{
|
|
47
|
-
regex:/!\[([^\]]*)\]\(([^)]+)\)/g,type:"image"},{regex:/\[([^\]]+)\]\(([^)]+)\)/g,type:"link"},{regex:/\*\*\*([^*]+)\*\*\*/g,type:"bolditalic"},{regex:/\*\*([^*]+)\*\*/g,type:"bold"},{regex:/\*([^*]+)\*/g,
|
|
48
|
-
type:"italic"},{regex:/(^|[^A-Za-z0-9_])___([^_]+)___(?![A-Za-z0-9_])/g,type:"bolditalic",underscore:!0},{regex:/(^|[^A-Za-z0-9_])__([^_]+)__(?![A-Za-z0-9_])/g,type:"bold",underscore:!0},{regex:/(^|[^A-Za-z0-9_])_([^_]+)_(?![A-Za-z0-9_])/g,
|
|
49
|
-
type:"italic",underscore:!0},{regex:/~~([^~]+)~~/g,type:"strikethrough"},{regex:/`([^`]+)`/g,type:"code"},{regex:/\$([^$]+)\$/g,type:"latex"}],t=[];for(const o of i){o.regex.lastIndex=0;let r;for(;(r=
|
|
50
|
-
o.regex.exec(e))!==null;)if(o.type==="link")t.push({index:r.index,length:r[0].length,type:o.type,content:r[1],url:r[2]});else if(o.type==="image")t.push({index:r.index,length:r[0].length,type:o.type,content:r[1]||
|
|
51
|
-
"[image]",url:r[2]});else if(o.type==="bolditalic"||o.type==="bold"||o.type==="italic")if("underscore"in o&&o.underscore){const h=r[1];t.push({index:r.index+h.length,length:r[0].length-h.length,type:o.
|
|
52
|
-
type,content:r[2]})}else t.push({index:r.index,length:r[0].length,type:o.type,content:r[1]});else t.push({index:r.index,length:r[0].length,type:o.type,content:r[1]})}t.sort((o,r)=>o.index-r.index);const s=[];
|
|
53
|
-
let a=0;for(const o of t)o.index>=a&&(s.push(o),a=o.index+o.length);let c=0;for(const o of s){if(o.index>c){const h=e.slice(c,o.index);h&&n.push({type:"text",text:h,marks:{}})}const r={type:"text",text:o.
|
|
54
|
-
content,marks:{}};switch(o.type){case"bold":r.marks={bold:!0};break;case"italic":r.marks={italic:!0};break;case"bolditalic":r.marks={bold:!0,italic:!0};break;case"strikethrough":r.marks={strikethrough:!0};
|
|
55
|
-
break;case"code":r.marks={code:!0};break;case"latex":r.text=o.content.replace(/\\\\/g,"\\"),r.marks={latex:!0};break;case"link":r.marks={link:o.url};break;case"image":r.text=o.content||"[image]",r.marks=
|
|
56
|
-
{link:o.url};break;case"footnoteref":r.text=o.content,r.marks={footnoteRef:`mdfn-${o.content}`,superscript:!0};break}n.push(r),c=o.index+o.length}if(c<e.length){const o=e.slice(c);o&&n.push({type:"tex\
|
|
57
|
-
t",text:o,marks:{}})}return n.length===0&&n.push({type:"text",text:e,marks:{}}),n}}export{L as MarkdownConverter};
|