@mdgate/rtf 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/rtf.d.ts +2 -0
- package/dist/to-markdown.d.ts +6 -0
- package/package.json +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @mdgate/rtf
|
|
2
|
+
|
|
3
|
+
Convert RTF documents to Markdown. Outputs GitHub-Flavored Markdown. Works in Node, Edge, and
|
|
4
|
+
browsers. No native addons.
|
|
5
|
+
|
|
6
|
+
Handles: `.rtf` (also RTF content wearing a `.doc` extension)
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { toMarkdown } from '@mdgate/rtf';
|
|
12
|
+
|
|
13
|
+
const markdown = await toMarkdown(bytes);
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Compose with other converters:
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { create } from '@mdgate/core';
|
|
20
|
+
import { rtf } from '@mdgate/rtf';
|
|
21
|
+
|
|
22
|
+
const convert = create([rtf()]);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Part of [mdgate converters](https://github.com/mdgate/converters); install
|
|
26
|
+
`@mdgate/converters` for every format at once.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{documentToMarkdown as n8}from"@mdgate/document";import{asciiStartsWith as t8,fileExtension as e8}from"@mdgate/utils";import{ConvertError as E8}from"@mdgate/core";import{inlinesAreEmpty as L,markerIsOrdered as v,markerLabel as S8,PLAIN as P}from"@mdgate/document";import{AssetSink as g8,applyDelta as v8,compositeLabel as L8,fieldResult as k8,flushList as e,rebaseEmphasis as x8,StyledRun as m8}from"@mdgate/office-common";import{cleanText as I,debug as q8,trim as k,warn as u8}from"@mdgate/utils";var K8=-9223372036854775808n,y=9223372036854775807n;function h(q,z,J){let Q=q;if(typeof Q.latin1Slice==="function")return Q.latin1Slice(z,J);let $=J-z;if($<=0)return"";let Z=Array($);for(let H=0;H<$;H+=1)Z[H]=q[z+H];return String.fromCharCode(...Z)}var w=[],E=[];function U8(q,z,J){let Q=0;for(let H=z;H<J;H+=1){let K=q[H],U=w[Q];if(U===void 0)U=w[Q]=[];let j=U[K];if(j===void 0)j=w.length,U[K]=j,w[j]=void 0,E[j]=void 0;Q=j}let $=E[Q];if($!==void 0)return $;let Z=h(q,z,J);return E[Q]=Z,Z}function Y8(q,z,J,Q){let $=J-z;if($===0)return;if($<=10){let H=0;for(let K=z;K<J;K+=1)H=H*10+(q[K]-48);if(Q)H=-H;if(H<-2147483648)return-2147483648;if(H>2147483647)return 2147483647;return H}let Z=h(q,z,J);try{let H=BigInt(Z);if(H>y)return;if(Q)H=-H;if(H<K8||H>y)return;if(H<BigInt(-2147483648))return-2147483648;if(H>BigInt(2147483647))return 2147483647;return Number(H)}catch{return}}class C{bytes;pos=0;openTok={type:"open"};closeTok={type:"close"};wordTok={type:"word",name:"",param:void 0};symbolTok={type:"symbol",byte:0};hexTok={type:"hex",byte:0};byteTok={type:"byte",byte:0};binTok={type:"bin",payload:new Uint8Array(0)};constructor(q){this.bytes=q}nextToken(){let q=this.bytes,z=q.length;for(;;){let J=this.pos;if(J>=z)return;let Q=q[J];if(this.pos=J+1,Q===123)return this.openTok;if(Q===125)return this.closeTok;if(Q===92){let Z=this.control();if(Z===void 0)return;return Z}if(Q===13||Q===10||Q===0)continue;let $=this.byteTok;return $.byte=Q,$}}control(){let q=this.bytes,z=q.length,J=q[this.pos];if(J===void 0)return;if(!(J>=65&&J<=90||J>=97&&J<=122)){if(this.pos+=1,J===13||J===10){let Y=this.wordTok;return Y.name="par",Y.param=void 0,Y}if(J===39){let Y=q[this.pos],O=q[this.pos+1];if(Y===void 0||O===void 0){let M=this.byteTok;return M.byte=39,M}let F=b(Y),B=b(O);if(F===void 0||B===void 0){let M=this.byteTok;return M.byte=39,M}this.pos+=2;let V=this.hexTok;return V.byte=F*16+B,V}let X=this.symbolTok;return X.byte=J,X}let Q=this.pos,$=this.pos;while($<z){let X=q[$];if(X>=65&&X<=90||X>=97&&X<=122)$+=1;else break}this.pos=$;let Z=U8(q,Q,$),H,K=!1;if(q[this.pos]===45)K=!0,this.pos+=1;let U=this.pos;while(this.pos<z&&q[this.pos]>=48&&q[this.pos]<=57)this.pos+=1;if(this.pos>U)H=Y8(q,U,this.pos,K);else if(K)this.pos-=1;if(q[this.pos]===32)this.pos+=1;if(Z==="bin"){let X=Math.max(H??0,0),Y=Math.min(this.pos+X,z),O=q.subarray(this.pos,Y);this.pos=Y;let F=this.binTok;return F.payload=O,F}let j=this.wordTok;return j.name=Z,j.param=H,j}}function c(q,z){let J=[],Q=[];for(let U=0;U<z.length;U+=1)J.push([]),Q.push(void 0);let $=new C(q),Z=0,H,K;for(;;){let U=$.pos,j=$.nextToken();if(j===void 0)break;switch(j.type){case"open":Z+=1,H=Z;break;case"close":for(let X=0;X<Q.length;X+=1){let Y=Q[X];if(Y!==void 0&&Z===Y.depth)J[X].push(q.subarray(Y.start,U)),Q[X]=void 0}Z=Z>0?Z-1:0,H=void 0;break;case"symbol":if(j.byte===42&&H===Z)break;H=void 0;break;case"word":if(K===void 0&&j.name==="ansicpg"&&j.param!==void 0)K=j.param;if(H===Z){let X=j.name;for(let Y=0;Y<z.length;Y+=1)if(z[Y]===X&&Q[Y]===void 0){Q[Y]={depth:Z,start:$.pos};break}}H=void 0;break;default:H=void 0;break}}return{groups:J,ansicpg:K}}function b(q){if(q>=48&&q<=57)return q-48;if(q>=97&&q<=102)return q-97+10;if(q>=65&&q<=70)return q-65+10;return}import{inlinesAreEmpty as j8}from"@mdgate/document";import{buildEdgeTable as G8,emptyCellProp as _,StyledRun as p}from"@mdgate/office-common";function o(){return{cells:[],header:!1}}function i(){return{rows:[],rowProps:[],propCursor:0,pendingProp:_(),row:o(),rowHeader:!1}}function M8(q){return{mergeFirst:q.mergeFirst,mergeCont:q.mergeCont,vmergeFirst:q.vmergeFirst,vmergeCont:q.vmergeCont,right:q.right}}class S{tables=[];cellBlocks=[[]];cellRuns=[new p];depth(){return this.tables.length}tableAt(q){while(this.tables.length<q)this.tables.push(i());return this.ensureCellDepth(q),this.tables[q-1]}ensureCellDepth(q){while(this.cellBlocks.length<q)this.cellBlocks.push([]);while(this.cellRuns.length<q)this.cellRuns.push(new p)}cellBlockAt(q){return this.ensureCellDepth(q),this.cellBlocks[q-1]}flushCellRun(q){this.ensureCellDepth(q),this.cellRuns[q-1].flush(this.cellBlocks[q-1])}beginRow(q){let z=this.tableAt(q);z.rowProps=[],z.propCursor=0,z.pendingProp=_(),z.rowHeader=!1}markHeaderRow(q){this.tableAt(q).rowHeader=!0}pendingProp(q){return this.tableAt(q).pendingProp}declareCell(q,z){let J=this.tableAt(q),Q=J.pendingProp;J.pendingProp=_(),Q.right=z,J.rowProps.push(Q)}pushCellParagraph(q,z,J){this.ensureCellDepth(q);let Q=this.cellBlocks[q-1],$=this.cellRuns[q-1];if(z!==void 0)$.push(z,J,Q);else if($.flush(Q),!j8(J))Q.push({type:"paragraph",inlines:J})}hasPendingCell(q){return this.flushCellRun(q),this.cellBlockAt(q).length>0}hasPartialRow(q){let z=this.tables[q-1];return this.hasPendingCell(q)||z!==void 0&&z.row.cells.length>0}endCell(q,z,J){this.pushCellParagraph(q,z,J),this.flushCellRun(q),this.flushIntoCell(q+1,q);let Q=this.cellBlockAt(q);this.cellBlocks[q-1]=[];let $=this.tableAt(q),Z=$.rowProps[$.propCursor],H=Z!==void 0?M8(Z):_();$.propCursor+=1,$.row.cells.push([Q,H])}endRow(q){let z=this.tableAt(q),J=z.row;if(z.row=o(),J.header=z.rowHeader,z.propCursor=0,J.cells.length>0)z.rows.push(J)}takeTable(q){if(this.tables.length<q||this.tables[q-1].rows.length===0)return;let z=this.tables[q-1];this.tables[q-1]=i();let J=z.rows.map((Q)=>({cells:Q.cells,header:Q.header}));return G8(J)}flushIntoCell(q,z){let J=this.takeTable(q);if(J!==void 0)this.flushCellRun(z),this.cellBlockAt(z).push(J)}collapseNested(){for(let q=this.tables.length;q>=2;q-=1)this.flushIntoCell(q,q-1)}}import{emptyDelta as B8,emptyNumberPattern as d,fromStyleName as F8,mergeDelta as O8}from"@mdgate/office-common";import{decode as R8,trimEndMatches as C8,warn as W8}from"@mdgate/utils";var G=9,r="windows-1252";function V8(){return{marker:"bullet",start:1,pattern:d()}}function D(){return Array.from({length:G},V8)}function N8(q){return{marker:q.marker,start:q.start,pattern:{text:q.pattern.text.slice(),legal:q.pattern.legal}}}function s(q){return q.map(N8)}function a(){return{outline:void 0,delta:B8(),block:void 0}}function T(q,z){if(q.length===0)return"";return R8(q,z)}function l(q,z,J){if(q.length===0)return[];let Q=q[0],$=q.subarray(1),Z=$.subarray(0,Math.min($.length,Q)),H=[],K=[],U=(X)=>{for(let Y=0;Y<z.length;Y+=1)if(z[Y]===X)return!0;return!1},j=()=>{if(K.length===0)return;H.push({type:"literal",text:T(Uint8Array.from(K),J)}),K.length=0};for(let X=0;X<Z.length;X+=1){let Y=Z[X];if(Y<=8&&U(X+1))j(),H.push({type:"level",level:Y});else K.push(Y)}return j(),H}var P8=["fonttbl","stylesheet","listtable","listoverridetable"];function n(q){let z={fonts:new Map,styles:new Map,lists:new Map},{groups:J,ansicpg:Q}=c(q,P8),$=Q!==void 0?f8(Math.max(Q,0)):r,[Z,H,K,U]=J;for(let X of Z)A8(X,z.fonts,$);for(let X of H)_8(X,z.styles,$);let j=new Map;for(let X of K)D8(X,j,$);for(let X of U)T8(X,j,z.lists,$);return{prelude:z,encoding:$}}class g{active;text=[];numbers=[];legal=!1;byte(q){if(this.active===!0)this.text.push(q);else if(this.active===!1)this.numbers.push(q)}finish(q,z){let J=q!==void 0&&q!=="bullet"?{text:l(Uint8Array.from(this.text),Uint8Array.from(this.numbers),z),legal:this.legal}:d();return this.active=void 0,this.text=[],this.numbers=[],this.legal=!1,J}reset(){this.active=void 0,this.text=[],this.numbers=[],this.legal=!1}}function A8(q,z,J){let Q=new C(q),$;for(;;){let Z=Q.nextToken();if(Z===void 0)break;if(Z.type==="word"){if(Z.name==="f")$=Z.param;else if(Z.name==="fcharset"&&$!==void 0&&Z.param!==void 0)z.set($,I8(Z.param,J))}}}var w8=222;function _8(q,z,J){let Q=new C(q),$=0,Z,H=new Map,K=[];for(;;){let U=Q.nextToken();if(U===void 0)break;switch(U.type){case"open":$+=1;break;case"close":if($===1&&Z!==void 0){let j=T(Uint8Array.from(K),J);Z.def.block=F8(C8(j,";")),H.set(Z.id,{def:Z.def,base:Z.base}),Z=void 0}K.length=0,$=$>0?$-1:0;break;case"hex":case"byte":if($===1&&Z!==void 0)K.push(U.byte);break;case"word":switch(U.name){case"s":Z={id:U.param??0,def:a(),base:void 0},K.length=0;break;case"sbasedon":if(Z!==void 0)Z.base=U.param!==void 0&&U.param!==w8?U.param:void 0;break;case"outlinelevel":if(Z!==void 0&&U.param!==void 0&&U.param>=0&&U.param<9)Z.def.outline=U.param+1;break;case"b":if(Z!==void 0)Z.def.delta.bold=U.param!==0;break;case"i":if(Z!==void 0)Z.def.delta.italic=U.param!==0;break;default:break}break;default:break}}for(let U of H.keys()){let j=[],X=new Set,Y=U;while(Y!==void 0){if(X.has(Y)){W8(`style inheritance cycle at rtf style ${Y}`);break}X.add(Y);let F=H.get(Y);if(F===void 0)break;j.push(F.def),Y=F.base}let O=a();for(let F=j.length-1;F>=0;F-=1){let B=j[F];O.delta=O8(O.delta,B.delta),O.outline=B.outline??O.outline,O.block=B.block??O.block}z.set(U,O)}}function D8(q,z,J){let Q=new C(q),$=0,Z,H=D(),K=0,U=!1,j,X=new g;for(;;){let Y=Q.nextToken();if(Y===void 0)break;switch(Y.type){case"open":$+=1;break;case"close":if(X.active=void 0,U&&Z!==void 0&&$===Z+1){if(U=!1,K<G)H[K].pattern=X.finish(H[K].marker,J);K+=1}if(Z===$){if(j!==void 0)z.set(j,{levels:s(H)}),j=void 0;H=D(),K=0,Z=void 0}$=$>0?$-1:0;break;case"word":switch(Y.name){case"list":Z=$,j=void 0,H=D(),K=0;break;case"listid":if(Z!==void 0)j=Y.param;break;case"listlevel":U=!0,X.reset();break;case"levelnfc":case"levelnfcn":if(U&&K<G)H[K].marker=t(Y.param??0);break;case"levelstartat":if(U&&K<G&&Y.param!==void 0)H[K].start=Math.max(Y.param,0);break;case"leveltext":if(U)X.active=!0;break;case"levelnumbers":if(U)X.active=!1;break;case"levellegal":if(U)X.legal=Y.param!==0;break;default:break}break;case"hex":case"byte":X.byte(Y.byte);break;default:break}}}function T8(q,z,J,Q){let $=new C(q),Z=0,H,K,U,j,X=0,Y=Array.from({length:G},()=>{return}),O=Array.from({length:G},()=>null),F=Array.from({length:G},()=>{return}),B=new g,V=()=>{if(j!==void 0&&U!==void 0){let M=z.get(U),N={levels:M!==void 0?s(M.levels):D()};for(let R=0;R<G;R+=1){let m=O[R];if(m!==null)N.levels[R].marker=m;let u=Y[R];if(u!==void 0)N.levels[R].start=u;let A=F[R];if(A!==void 0&&N.levels[R].marker!==void 0&&N.levels[R].marker!=="bullet")N.levels[R].pattern={text:l(Uint8Array.from(A.text),Uint8Array.from(A.numbers),Q),legal:A.legal}}J.set(j,N)}j=void 0,U=void 0,Y=Array.from({length:G},()=>{return}),O=Array.from({length:G},()=>null),F=Array.from({length:G},()=>{return})};for(;;){let M=$.nextToken();if(M===void 0)break;switch(M.type){case"open":Z+=1;break;case"close":if(B.active=void 0,K===Z){if(K=void 0,X<G&&B.text.length>0)F[X]={text:B.text,numbers:B.numbers,legal:B.legal};B.reset(),X+=1}if(H===Z)V(),X=0,H=void 0;Z=Z>0?Z-1:0;break;case"word":switch(M.name){case"listoverride":V(),H=Z,X=0;break;case"listid":if(H!==void 0)U=M.param;break;case"ls":if(H!==void 0)j=M.param;break;case"lfolevel":K=Z,B.reset();break;case"levelstartat":if(K!==void 0&&X<G&&M.param!==void 0)Y[X]=Math.max(M.param,0);break;case"levelnfc":case"levelnfcn":if(K!==void 0&&X<G)O[X]=t(M.param??0);break;case"leveltext":if(K!==void 0)B.active=!0;break;case"levelnumbers":if(K!==void 0)B.active=!1;break;case"levellegal":if(K!==void 0)B.legal=M.param!==0;break;default:break}break;case"hex":case"byte":B.byte(M.byte);break;default:break}}V()}function t(q){switch(q){case 0:return"decimal";case 1:return"upperRoman";case 2:return"lowerRoman";case 3:return"upperAlpha";case 4:return"lowerAlpha";case 23:return"bullet";case 255:return;default:return"decimal"}}function I8(q,z){switch(q){case 0:case 1:return z;case 128:return"shiftjis";case 129:return"euc-kr";case 134:return"gbk";case 136:return"big5";case 161:return"windows-1253";case 162:return"windows-1254";case 163:return"windows-1258";case 177:return"windows-1255";case 178:case 179:case 180:return"windows-1256";case 186:return"windows-1257";case 204:return"windows-1251";case 222:return"windows-874";case 238:return"windows-1250";default:return z}}function f8(q){switch(q){case 932:return"shiftjis";case 936:return"gbk";case 949:return"euc-kr";case 950:return"big5";case 1250:return"windows-1250";case 1251:return"windows-1251";case 1253:return"windows-1253";case 1254:return"windows-1254";case 1255:return"windows-1255";case 1256:return"windows-1256";case 1257:return"windows-1257";case 1258:return"windows-1258";case 874:return"windows-874";default:return r}}function Z8(q){if(!y8(q))throw E8.malformed("not an RTF file");let{prelude:z,encoding:J}=n(q),Q=new f(q,z,J);return Q.run(),Q.finish()}function y8(q){if(q.length<5)return!1;return q[0]===123&&q[1]===92&&q[2]===114&&q[3]===116&&q[4]===102}function W(q){return{bold:q.bold,italic:q.italic,strike:q.strike,code:q.code}}function b8(){return{style:W(P),font:void 0,ucSkip:1,inTable:!1,itap:1,ilvl:0,ls:void 0,legacyList:void 0,outline:void 0,block:void 0,styleBase:W(P),suppress:!1,capture:"none",note:void 0}}function h8(q){return{style:q.style,font:q.font,ucSkip:q.ucSkip,inTable:q.inTable,itap:q.itap,ilvl:q.ilvl,ls:q.ls,legacyList:q.legacyList,outline:q.outline,block:q.block,styleBase:q.styleBase,suppress:q.suppress,capture:q.capture,note:q.note}}var z8=2147483647,c8=Number.MAX_SAFE_INTEGER,p8=Number.MAX_SAFE_INTEGER-1;class $8{state=new Map;slot(q){let z=this.state.get(q);if(z===void 0)z={values:Array.from({length:G},()=>0),started:Array.from({length:G},()=>!1)},this.state.set(q,z);return z}next(q,z,J){let Q=Math.min(z,G-1),$=this.slot(q),Z=$.started[Q]?$.values[Q]+1:J;$.values[Q]=Z,$.started[Q]=!0;for(let H=Q+1;H<$.started.length;H+=1)$.started[H]=!1;return Z}nextLabeled(q,z,J){let Q=Math.min(z,G-1),$=J[Q],Z=this.next(q,Q,$.start),H=this.slot(q),K=$.marker,U=K===void 0?void 0:L8($.pattern,K,Z,(j)=>J[Math.min(j,G-1)].marker??"decimal",(j)=>{let X=Math.min(j,G-1);return H.started[X]?H.values[X]:J[X].start});return{value:Z,label:U}}seed(q,z,J){let Q=Math.min(z,G-1),$=this.slot(q);$.values[Q]=J,$.started[Q]=!0;for(let Z=Q+1;Z<$.started.length;Z+=1)$.started[Z]=!1}}class H8{defaultEncoding;pending=[];skip=0;surrogate;constructor(q){this.defaultEncoding=q}get hasPending(){return this.pending.length>0}byte(q){if(this.skip>0)this.skip-=1;else this.pending.push(q)}skipChar(){if(this.skip>0)return this.skip-=1,!0;return!1}takePending(q){let z=this.pending,J=z.length;if(J===0)return;let Q=!0;for(let Z=0;Z<J;Z+=1)if(z[Z]>127){Q=!1;break}let $;if(Q){let Z="";for(let H=0;H<J;H+=1)Z+=String.fromCharCode(z[H]);$=Z}else $=T(Uint8Array.from(z),q??this.defaultEncoding);return z.length=0,$}unicode(q,z){if(this.skip=0,q===void 0)return;let J=q<0?q+65536:q,Q=J&65535,$=this.surrogate;this.surrogate=void 0;let Z;if($===void 0&&Q>=55296&&Q<=56319)this.surrogate=Q,Z=void 0;else if($!==void 0&&Q>=56320&&Q<=57343){let H=65536+($-55296<<10)+(Q-56320);Z=String.fromCodePoint(H)}else Z=i8(J);return this.skip=z,Z}}function i8(q){if(q>1114111)return;if(q>=55296&&q<=57343)return;if(q<0)return;return String.fromCodePoint(q)}function o8(q){return{depth:q,hex:[],binary:void 0,format:void 0}}function a8(q){if(q.binary!==void 0)return q.binary;let z=[],J;for(let Q of q.hex){let $=d8(Q);if($===void 0)continue;if(J!==void 0)z.push(J<<4|$),J=void 0;else J=$}return Uint8Array.from(z)}function d8(q){if(q>=48&&q<=57)return q-48;if(q>=97&&q<=102)return q-97+10;if(q>=65&&q<=70)return q-65+10;return}class X8{fields=[];noteFrames=[];notes=[];bookmark="";listtext;pict;closeFields(q,z){while(this.fields.length>0&&this.fields[this.fields.length-1].depth>q){let J=this.fields.pop(),Q=Math.min(J.start,z.length),$=z.splice(Q);z.push(...k8(J.instr,$))}}closeNotes(q,z){while(this.noteFrames.length>0&&this.noteFrames[this.noteFrames.length-1].depth>q){let J=this.noteFrames.pop(),Q=Math.min(J.start,z.length),$=z.splice(Q);if(!L($)){let Z=`rtf${this.notes.length}`;this.notes.push({id:Z,kind:J.kind,blocks:[{type:"paragraph",inlines:$}]}),z.push({type:"noteRef",id:Z})}}}closeBookmark(q,z){if(!q&&this.bookmark.length>0){let J=k(this.bookmark);if(this.bookmark="",J.length>0)z.push({type:"anchor",id:J})}}}var r8=new Set(["u","uc","f","b","i","strike","striked","plain","s","par","sect","pard","line","lbr","page","column","tab","emdash","endash","lquote","rquote","ldblquote","rdblquote","bullet","enspace","emspace","qmspace","intbl","itap","trowd","trhdr","clmgf","clmrg","clvmgf","clvmrg","cellx","cell","nestcell","row","nestrow","nesttableprops","outlinelevel","ilvl","ls","listtext","pntext","pnlvlblt","pnlvlbody","pndec","field","fldinst","fldrslt","footnote","ftnalt","chftn","bkmkstart","shppict","shptxt","result","pict","pngblip","jpegblip","emfblip","wmetafile","macpict","dibitmap","wbitmap"]),J8=new Set(["fonttbl","colortbl","stylesheet","info","object","header","footer","headerl","headerr","headerf","footerl","footerr","footerf","ftnsep","ftnsepc","aftnsep","aftnsepc","xmlnstbl","themedata","colorschememapping","datastore","latentstyles","listtable","listoverridetable","rsidtbl","generator","filetbl","revtbl","datafield","bkmkend","annotation","atnid","atnauthor","template","defchp","defpap","panose","falt","objdata","blipuid","nonshppict","wgrffmtfilter","pgdsctbl","docvar","sp","sn","sv","shpinst","background","userprops","operator","author","title","subject","keywords","doccomm","creatim","revtim","printim"]);class f{lexer;stack=[];state=b8();prelude;decoder;recovered=!1;inlines=[];blocks=[];listRun=[];styled=new m8;counters=new $8;table=new S;dest=new X8;assets=new g8;constructor(q,z,J){this.lexer=new C(q),this.prelude=z,this.decoder=new H8(J)}run(){for(;;){let q=this.lexer.nextToken();if(q===void 0)break;this.dispatch(q)}if(this.stack.length>0)this.recovered=!0;if(this.recovered)u8("recovered unbalanced rtf groups");this.flushPending(),this.endParagraph()}dispatch(q){switch(q.type){case"open":this.flushPending(),this.stack.push(h8(this.state));break;case"close":this.flushPending();{let z=this.stack.pop();if(z!==void 0)this.state=z;else this.recovered=!0}if(this.dest.closeFields(this.stack.length,this.inlines),this.dest.closeNotes(this.stack.length,this.inlines),this.dest.closeBookmark(this.state.capture==="bookmark",this.inlines),this.dest.pict!==void 0&&this.stack.length<this.dest.pict.depth)this.finishPict();break;case"word":this.controlWord(q.name,q.param);break;case"symbol":this.controlSymbol(q.byte);break;case"hex":case"byte":if(this.state.capture==="pict"){if(this.dest.pict!==void 0&&this.stack.length===this.dest.pict.depth)this.dest.pict.hex.push(q.byte)}else if(this.acceptsText())this.decoder.byte(q.byte);break;case"bin":if(this.state.capture==="pict"&&this.dest.pict!==void 0&&this.stack.length===this.dest.pict.depth)this.dest.pict.binary=q.payload;else q8(`skipping ${q.payload.length} bytes of embedded binary data`);break}}acceptsText(){return this.state.capture!=="none"||!this.state.suppress}controlSymbol(q){switch(q){case 126:this.pushChar(" ");break;case 45:break;case 95:this.pushChar("-");break;case 42:this.state.suppress=!0;break;case 92:case 123:case 125:this.pushChar(String.fromCharCode(q));break;default:break}}controlWord(q,z){if(!r8.has(q)&&!J8.has(q))return;if(this.textControl(q,z))return;if(this.tableControl(q,z))return;if(this.listControl(q,z))return;if(this.objectControl(q))return;if(J8.has(q))this.flushPending(),this.state.suppress=!0,this.state.capture="none"}textControl(q,z){let J=z!==0;switch(q){case"u":if(this.acceptsText()){this.flushPending();let Q=this.decoder.unicode(z,this.state.ucSkip);if(Q!==void 0)this.pushText(Q)}return!0;case"uc":return this.state.ucSkip=Math.max(z??1,0),!0;case"f":return this.flushPending(),this.state.font=z,!0;case"b":return this.setStyle((Q)=>{Q.bold=J}),!0;case"i":return this.setStyle((Q)=>{Q.italic=J}),!0;case"strike":case"striked":return this.setStyle((Q)=>{Q.strike=J}),!0;case"plain":{this.flushPending();let Q=this.state.font;return this.state.style=W(P),this.state.font=Q,!0}case"s":{let Q=z!==void 0?this.prelude.styles.get(z):void 0;if(Q!==void 0)this.flushPending(),this.state.outline=Q.outline,this.state.block=Q.block,this.state.style=v8(Q.delta,this.state.style),this.state.styleBase=W(this.state.style);return!0}case"par":case"sect":if(this.flushPending(),this.state.note!==void 0)this.inlines.push({type:"lineBreak"});else if(!this.state.suppress)this.endParagraph();return!0;case"pard":return this.flushPending(),this.state.inTable=!1,this.state.itap=1,this.state.ilvl=0,this.state.ls=void 0,this.state.legacyList=void 0,this.state.outline=void 0,this.state.block=void 0,this.state.styleBase=W(P),!0;case"line":case"lbr":case"page":case"column":if(this.flushPending(),!this.state.suppress)this.inlines.push({type:"lineBreak"});return!0;case"tab":return this.pushChar(" "),!0;case"emdash":return this.pushChar("—"),!0;case"endash":return this.pushChar("–"),!0;case"lquote":return this.pushChar("‘"),!0;case"rquote":return this.pushChar("’"),!0;case"ldblquote":return this.pushChar("“"),!0;case"rdblquote":return this.pushChar("”"),!0;case"bullet":return this.pushChar("•"),!0;case"enspace":case"emspace":case"qmspace":return this.pushChar(" "),!0;default:return!1}}tableControl(q,z){switch(q){case"intbl":return this.state.inTable=!0,!0;case"itap":if(this.state.itap=Q8(z??1,0,8),this.state.itap>1)this.state.inTable=!0;return!0;case"trowd":if(this.tableActive())this.table.beginRow(Math.max(this.state.itap,1));return!0;case"trhdr":if(this.tableActive())this.table.markHeaderRow(Math.max(this.state.itap,1));return!0;case"clmgf":return this.pendingCellProp((J)=>{J.mergeFirst=!0}),!0;case"clmrg":return this.pendingCellProp((J)=>{J.mergeCont=!0}),!0;case"clvmgf":return this.pendingCellProp((J)=>{J.vmergeFirst=!0}),!0;case"clvmrg":return this.pendingCellProp((J)=>{J.vmergeCont=!0}),!0;case"cellx":if(this.tableActive())this.table.declareCell(Math.max(this.state.itap,1),z??0);return!0;case"cell":if(this.flushPending(),this.tableActive())this.endCell(1);return!0;case"nestcell":if(this.flushPending(),this.tableActive())this.endCell(Math.max(this.state.itap,2));return!0;case"row":if(this.flushPending(),this.tableActive())this.endRow(1);return!0;case"nestrow":if(this.flushPending(),this.tableActive())this.endRow(Math.max(this.state.itap,2));return!0;case"nesttableprops":return this.state.suppress=!1,!0;default:return!1}}listControl(q,z){switch(q){case"outlinelevel":if(z!==void 0&&z>=0&&z<9)this.state.outline=z+1;return!0;case"ilvl":return this.state.ilvl=Q8(z??0,0,8),!0;case"ls":return this.state.ls=z,!0;case"listtext":case"pntext":if(this.flushPending(),this.state.capture="listText",this.dest.listtext===void 0)this.dest.listtext="";return!0;case"pnlvlblt":return this.state.legacyList="bullet",!0;case"pnlvlbody":case"pndec":return this.state.legacyList="decimal",!0;default:return!1}}objectControl(q){switch(q){case"field":if(this.flushPending(),!this.state.suppress)this.dest.fields.push({depth:this.stack.length,instr:"",start:this.inlines.length});return!0;case"fldinst":if(this.flushPending(),this.dest.fields.length>0)this.state.capture="fieldInstr";return this.state.suppress=!0,!0;case"fldrslt":return this.flushPending(),this.state.capture="none",!0;case"footnote":return this.flushPending(),this.state.note="footnote",this.state.suppress=!1,this.state.capture="none",this.dest.noteFrames.push({depth:this.stack.length,start:this.inlines.length,kind:"footnote"}),!0;case"ftnalt":if(this.dest.noteFrames.length>0)this.dest.noteFrames[this.dest.noteFrames.length-1].kind="endnote";return!0;case"chftn":return!0;case"bkmkstart":return this.flushPending(),this.state.capture="bookmark",this.state.suppress=!1,!0;case"shppict":return this.state.suppress=!1,!0;case"shptxt":return this.state.suppress=!1,!0;case"result":return this.state.suppress=!1,!0;case"pict":if(!this.state.suppress)this.flushPending(),this.state.capture="pict",this.dest.pict=o8(this.stack.length);return!0;case"pngblip":return this.setPictFormat({mediaType:"image/png",extension:"png"}),!0;case"jpegblip":return this.setPictFormat({mediaType:"image/jpeg",extension:"jpg"}),!0;case"emfblip":return this.setPictFormat({mediaType:"image/emf",extension:"emf"}),!0;case"wmetafile":return this.setPictFormat({mediaType:"image/wmf",extension:"wmf"}),!0;case"macpict":case"dibitmap":case"wbitmap":return this.setPictFormat(void 0),!0;default:return!1}}setPictFormat(q){if(this.state.capture==="pict"&&this.dest.pict!==void 0)this.dest.pict.format=q}finishPict(){let q=this.dest.pict;if(this.dest.pict=void 0,q===void 0)return;if(q.format===void 0){q8("skipping picture in an unsupported format");return}let z=a8(q);if(z.length===0)return;let J=`pict/${this.assets.assets.length}.${q.format.extension}`,Q=this.assets.add(q.format.mediaType,J,z);this.inlines.push({type:"image",alt:"",source:{type:"asset",id:Q}})}tableActive(){return!this.state.suppress&&this.state.note===void 0}pendingCellProp(q){if(this.tableActive())q(this.table.pendingProp(Math.max(this.state.itap,1)))}setStyle(q){this.flushPending(),this.state.style=W(this.state.style),q(this.state.style)}pushChar(q){if(!this.acceptsText())return;if(this.decoder.skipChar())return;this.flushPending(),this.pushText(q)}flushPending(){if(!this.decoder.hasPending)return;let q=this.state.font!==void 0?this.prelude.fonts.get(this.state.font):void 0,z=this.decoder.takePending(q);if(z!==void 0)this.pushText(z)}pushText(q){let z=s8(q);if(z.length===0)return;switch(this.state.capture){case"listText":if(this.dest.listtext!==void 0)this.dest.listtext+=z;break;case"fieldInstr":if(this.dest.fields.length>0)this.dest.fields[this.dest.fields.length-1].instr+=z;break;case"bookmark":this.dest.bookmark+=z;break;case"pict":break;case"none":if(!this.state.suppress)this.inlines.push({type:"text",text:z,style:W(this.state.style)});break}}flushTopTable(){let q=this.table.takeTable(1);if(q!==void 0)this.flushRuns(),this.blocks.push(q)}endParagraph(){let q=this.inlines;this.inlines=[];let z=this.dest.listtext;if(this.dest.listtext=void 0,this.state.inTable){this.table.pushCellParagraph(Math.max(this.state.itap,1),this.state.block,q);return}if(this.flushTopTable(),this.state.block!==void 0){e(this.blocks,this.listRun),this.styled.push(this.state.block,q,this.blocks);return}if(L(q)){this.flushRuns();return}let J=this.listEntry(z);if(this.state.outline!==void 0){this.flushRuns();let Q=q;if(x8(Q,this.state.styleBase),J!==void 0&&v(J.key.marker)){let $=`${J.label??S8(J.key.marker,J.number)} `;Q.unshift({type:"text",text:$,style:W(P)})}this.blocks.push({type:"heading",level:this.state.outline,anchor:void 0,content:Q});return}if(J!==void 0){this.listRun.push({level:J.level,key:J.key,number:J.number,label:J.label,blocks:[{type:"paragraph",inlines:q}]});return}this.flushRuns(),this.blocks.push({type:"paragraph",inlines:q})}static trimmedLabel(q){if(q===void 0)return;let z=k(q);return z.length>0?z:void 0}listEntry(q){if(this.state.ls!==void 0){let z=this.state.ilvl,J=this.prelude.lists.get(this.state.ls);if(J!==void 0){let $=J.levels[Math.min(z,G-1)].marker;if($===void 0)return;let Z=0,H;if(v($)){let K=this.counters.nextLabeled(this.state.ls,z,J.levels);Z=K.value,H=K.label}return{key:{instance:this.state.ls,marker:$},level:z,number:Z,label:H}}return{key:{instance:this.state.ls,marker:"bullet"},level:z,number:0,label:f.trimmedLabel(q)}}if(this.state.legacyList!==void 0){let z=this.state.legacyList,J=0;if(v(z)){let Q=l8(q);if(Q!==void 0)this.counters.seed(z8,this.state.ilvl,Q),J=Q;else J=this.counters.next(z8,this.state.ilvl,1)}return{key:{instance:c8,marker:z},level:this.state.ilvl,number:J,label:void 0}}if(q!==void 0&&k(q).length>0)return{key:{instance:p8,marker:"bullet"},level:this.state.ilvl,number:0,label:f.trimmedLabel(q)};return}endCell(q){let z=this.inlines;this.inlines=[],this.dest.listtext=void 0,this.table.endCell(q,this.state.block,z)}endRow(q){if(this.table.hasPendingCell(q)||!L(this.inlines))this.endCell(q);this.table.endRow(q)}flushRuns(){this.styled.flush(this.blocks),e(this.blocks,this.listRun)}finish(){for(let q=this.table.depth();q>=1;q-=1)if(this.table.hasPartialRow(q))this.endRow(q);return this.table.collapseNested(),this.flushTopTable(),this.flushRuns(),{blocks:this.blocks,notes:this.dest.notes,assets:this.assets.assets}}}function s8(q){let z=q.length;for(let J=0;J<z;J+=1){let Q=q.charCodeAt(J);if(Q<32&&Q!==9)return I(q);if(Q===127||Q===160||Q===173)return I(q);if(Q>=128&&Q<=159)return I(q);if(Q===8203||Q===65279)return I(q)}return q}function Q8(q,z,J){if(q<z)return z;if(q>J)return J;return q}function l8(q){if(q===void 0)return;let z="";for(let Q of q){if(Q<"0"||Q>"9")break;z+=Q}if(z.length===0)return;let J=Number(z);if(!Number.isFinite(J))return;return Math.min(J,4294967295)}function x(){return{id:"rtf",sniff(q,z){if(t8(q,"{\\rtf"))return 2;if(z?.path!==void 0&&e8(z.path)==="rtf")return 1;return 0},convert(q){return{markdown:n8(Z8(q))}}}}import{create as q0}from"@mdgate/core";var z0=q0([x()]);function J0(q,z){return z0(q,z)}export{J0 as toMarkdown,x as rtf};
|
package/dist/rtf.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ConvertHint } from '@mdgate/core';
|
|
2
|
+
/**
|
|
3
|
+
* Convert a single file's bytes to Markdown with the rtf converter.
|
|
4
|
+
* `hint.path` is only a sniff hint; it is never read from disk.
|
|
5
|
+
*/
|
|
6
|
+
export declare function toMarkdown(bytes: Uint8Array, hint?: ConvertHint): Promise<string>;
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mdgate/rtf",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "mdgate RTF converter",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/**/*.js",
|
|
18
|
+
"dist/**/*.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "bun ../../scripts/build-package.ts",
|
|
22
|
+
"prepublishOnly": "bun run build"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@mdgate/core": "0.1.3",
|
|
26
|
+
"@mdgate/document": "0.1.0",
|
|
27
|
+
"@mdgate/office-common": "0.1.0",
|
|
28
|
+
"@mdgate/utils": "0.1.2"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=20"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"mdgate",
|
|
38
|
+
"markdown",
|
|
39
|
+
"rtf"
|
|
40
|
+
]
|
|
41
|
+
}
|