jspdf-md-renderer 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +2 -2
- package/dist/index.mjs +587 -470
- package/dist/index.umd.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -216,6 +216,40 @@ The following Markdown elements are currently supported by `jspdf-md-renderer`:
|
|
|
216
216
|
```markdown
|
|
217
217
|

|
|
218
218
|
```
|
|
219
|
+
Images render at their **intrinsic (original) size** by default and scale down automatically if they exceed the available page width. You can control image dimensions and alignment using an optional attribute block `{...}` after the image syntax:
|
|
220
|
+
|
|
221
|
+
**Custom Attributes:**
|
|
222
|
+
| Attribute | Description | Example |
|
|
223
|
+
|-----------|-------------|---------|
|
|
224
|
+
| `width` or `w` | Image width in px | `{width=200}` or `{w=200}` |
|
|
225
|
+
| `height` or `h` | Image height in px | `{height=150}` or `{h=150}` |
|
|
226
|
+
| `align` | Alignment: `left`, `center`, `right` | `{align=center}` |
|
|
227
|
+
|
|
228
|
+
**Sizing Rules:**
|
|
229
|
+
- If only `width` is given, height is auto-calculated from aspect ratio
|
|
230
|
+
- If only `height` is given, width is auto-calculated from aspect ratio
|
|
231
|
+
- If both are given, the image uses exact dimensions (may distort if ratio differs)
|
|
232
|
+
- Images that exceed page bounds are always scaled down proportionally
|
|
233
|
+
|
|
234
|
+
**Examples:**
|
|
235
|
+
```markdown
|
|
236
|
+

|
|
237
|
+
{width=200}
|
|
238
|
+
{h=150 align=center}
|
|
239
|
+
{width=200 height=150 align=right}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
**Global Default Alignment:**
|
|
243
|
+
You can set a default alignment for all images via the `image` option:
|
|
244
|
+
```ts
|
|
245
|
+
const options = {
|
|
246
|
+
// ...other options
|
|
247
|
+
image: {
|
|
248
|
+
defaultAlign: 'center', // 'left' (default) | 'center' | 'right'
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
```
|
|
252
|
+
|
|
219
253
|
- **Inline Code**:
|
|
220
254
|
```markdown
|
|
221
255
|
This is an `inline code` example.
|
package/dist/index.d.ts
CHANGED
|
@@ -40,6 +40,11 @@ declare type ParsedElement = {
|
|
|
40
40
|
header?: ParsedElement[];
|
|
41
41
|
rows?: ParsedElement[][];
|
|
42
42
|
data?: string;
|
|
43
|
+
width?: number;
|
|
44
|
+
height?: number;
|
|
45
|
+
align?: 'left' | 'center' | 'right';
|
|
46
|
+
naturalWidth?: number;
|
|
47
|
+
naturalHeight?: number;
|
|
43
48
|
};
|
|
44
49
|
|
|
45
50
|
export declare type RenderOption = {
|
|
@@ -84,6 +89,10 @@ export declare type RenderOption = {
|
|
|
84
89
|
linkColor: [number, number, number];
|
|
85
90
|
};
|
|
86
91
|
table?: UserOptions;
|
|
92
|
+
image?: {
|
|
93
|
+
/** Default alignment for images: 'left' | 'center' | 'right'. Default: 'left' */
|
|
94
|
+
defaultAlign?: 'left' | 'center' | 'right';
|
|
95
|
+
};
|
|
87
96
|
pageBreakHandler?: (doc: default_2) => void;
|
|
88
97
|
endCursorYHandler: (y: number) => void;
|
|
89
98
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A=require("marked"),j=require("jspdf-autotable");var u=(t=>(t.Heading="heading",t.Paragraph="paragraph",t.List="list",t.ListItem="list_item",t.Blockquote="blockquote",t.Code="code",t.CodeSpan="codespan",t.Table="table",t.Html="html",t.Hr="hr",t.Image="image",t.Link="link",t.Strong="strong",t.Em="em",t.TableHeader="table_header",t.TableCell="table_cell",t.Raw="raw",t.Text="text",t))(u||{});const B=async t=>{const n=await A.marked.lexer(t,{async:!0,gfm:!0});return W(n)},W=t=>{const n=[];return t.forEach(s=>{try{const g=q[s.type];g?n.push(g(s)):n.push({type:u.Raw,content:s.raw})}catch(g){console.error("Failed to handle token ==>",s,g)}}),n},q={[u.Heading]:t=>({type:u.Heading,depth:t.depth,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Paragraph]:t=>({type:u.Paragraph,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.List]:t=>({type:u.List,ordered:t.ordered,start:t.start,items:t.items?W(t.items):[]}),[u.ListItem]:t=>({type:u.ListItem,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Code]:t=>({type:u.Code,lang:t.lang,code:t.text}),[u.Table]:t=>({type:u.Table,header:t.header.map(n=>({type:u.TableHeader,content:n.text})),rows:t.rows.map(n=>n.map(s=>({type:u.TableCell,content:s.text})))}),[u.Image]:t=>({type:u.Image,src:t.href,alt:t.text}),[u.Link]:t=>({type:u.Link,href:t.href,text:t.text,items:t.tokens?W(t.tokens):[]}),[u.Strong]:t=>({type:u.Strong,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Em]:t=>({type:u.Em,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Text]:t=>({type:u.Text,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Hr]:t=>({type:u.Hr,content:t.raw,items:t.tokens?W(t.tokens):[]}),[u.CodeSpan]:t=>({type:u.CodeSpan,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Blockquote]:t=>({type:u.Blockquote,content:t.text,items:t.tokens?W(t.tokens):[]})},T=class T{static initialize(n){this.options_=n,this.cursor={x:n.cursor.x,y:n.cursor.y},this.lastContentY_=n.cursor.y}static getCursor(){return this.cursor}static setCursor(n){this.cursor=n}static get options(){return this.options_}static get isInlineLockActive(){return this.inlineLock}static activateInlineLock(){this.inlineLock=!0}static deactivateInlineLock(){this.inlineLock=!1}static updateX(n,s="set"){s==="set"?this.cursor.x=n:s==="add"&&(this.cursor.x+=n)}static updateY(n,s="set"){s==="set"?this.cursor.y=n:s==="add"&&(this.cursor.y+=n)}static recordContentY(n){this.lastContentY_=n!==void 0?n:this.cursor.y}static get lastContentY(){return this.lastContentY_}static get X(){return this.cursor.x}static get Y(){return this.cursor.y}};T.cursor={x:0,y:0},T.lastContentY_=0,T.inlineLock=!1;let e=T;const b=t=>t.getTextDimensions("H").h*e.options.page.defaultLineHeightFactor,y=t=>t.getTextDimensions("H").w*e.options.page.defaultLineHeightFactor,O=(t,n,s,g)=>{const c=6-(n?.depth??0)>0?6-(n?.depth??0):1;if(t.setFontSize(e.options.page.defaultFontSize+c),n?.items&&n?.items.length>0)for(const l of n?.items??[])g(l,s,!1);else{const l=b(t);t.text(n?.content??"",e.X+s,e.Y,{align:"left",maxWidth:e.options.page.maxContentWidth-s,baseline:"top"}),e.recordContentY(e.Y+l),e.updateY(l,"add")}t.setFontSize(e.options.page.defaultFontSize),e.updateX(e.options.page.xpading,"set")},w=t=>{typeof e.options.pageBreakHandler=="function"?e.options.pageBreakHandler(t):t.addPage(e.options.page?.format,e.options.page?.orientation),e.updateY(e.options.page.topmargin),e.updateX(e.options.page.xpading)};class P{static getCodespanOptions(){const n=e.options.codespan??{};return{backgroundColor:n.backgroundColor??"#EEEEEE",padding:n.padding??.5,showBackground:n.showBackground!==!1,fontSizeScale:n.fontSizeScale??.9}}static applyStyle(n,s){const g=n.getFont().fontName,c=n.getFontSize(),l=()=>{const i=e.options.font.bold?.name;return i&&i!==""?i:g},r=()=>{const i=e.options.font.regular?.name;return i&&i!==""?i:g};switch(s){case"bold":n.setFont(l(),e.options.font.bold?.style||"bold");break;case"italic":n.setFont(r(),"italic");break;case"bolditalic":n.setFont(l(),"bolditalic");break;case"codespan":n.setFont("courier","normal"),n.setFontSize(c*this.getCodespanOptions().fontSizeScale);break;default:n.setFont(r(),n.getFont().fontStyle);break}}static measureWordWidth(n,s,g){const c=n.getFont(),l=n.getFontSize();this.applyStyle(n,g);const r=n.getTextWidth(s),i=n.getCharSpace?.()??0,a=r+s.length*i;return n.setFont(c.fontName,c.fontStyle),n.setFontSize(l),a}static getStyleFromType(n,s){switch(n){case"strong":return s==="italic"?"bolditalic":"bold";case"em":return s==="bold"?"bolditalic":"italic";case"codespan":return"codespan";default:return s||"normal"}}static flattenToWords(n,s,g="normal",c=!1,l){const r=[];for(const i of s){const a=this.getStyleFromType(i.type,g),h=i.type==="link"||c,p=i.href||l;if(i.items&&i.items.length>0){const f=this.flattenToWords(n,i.items,a,h,p);r.push(...f)}else{const f=i.content||i.text||"";if(!f)continue;if(a==="codespan"){const o=f.trim();o&&r.push({text:o,width:this.measureWordWidth(n,o,a),style:a,isLink:h,href:p,linkColor:h?e.options.link?.linkColor||[0,0,255]:void 0});continue}const d=f.split(/\s+/).filter(o=>o.length>0);if(d.length===0)continue;for(let o=0;o<d.length;o++){const x=d[o];r.push({text:x,width:this.measureWordWidth(n,x,a),style:a,isLink:h,href:p,linkColor:h?e.options.link?.linkColor||[0,0,255]:void 0})}}}return r}static breakIntoLines(n,s,g){const c=[];let l=[],r=0,i=0;const a=n.getTextWidth(" ");for(let h=0;h<s.length;h++){const p=s[h],f=l.length>0?a+p.width:p.width;i+f>g&&l.length>0?(c.push({words:l,totalTextWidth:r,isLastLine:!1}),l=[p],r=p.width,i=p.width):(l.push(p),r+=p.width,i+=f)}return l.length>0&&c.push({words:l,totalTextWidth:r,isLastLine:!0}),c}static renderWord(n,s,g,c){const l=n.getFont(),r=n.getFontSize(),i=n.getTextColor();if(this.applyStyle(n,s.style),s.isLink&&s.linkColor&&n.setTextColor(...s.linkColor),s.style==="codespan"){const a=this.getCodespanOptions();if(a.showBackground){const h=b(n),p=a.padding;n.setFillColor(a.backgroundColor),n.rect(g-p,c-p,s.width+p*2,h+p*2,"F"),n.setFillColor("#000000")}}if(n.text(s.text,g,c,{baseline:"top"}),s.isLink&&s.href){const a=b(n)/2;n.link(g,c,s.width,a,{url:s.href})}n.setFont(l.fontName,l.fontStyle),n.setFontSize(r),n.setTextColor(i)}static renderAlignedLine(n,s,g,c,l,r="left"){const{words:i,totalTextWidth:a,isLastLine:h}=s;if(i.length===0)return;const p=n.getTextWidth(" ");let f=g,d=p;const o=a+(i.length-1)*p;switch(r){case"right":f=g+l-o;break;case"center":f=g+(l-o)/2;break;case"justify":!h&&i.length>1&&(d=(l-a)/(i.length-1));break}let x=f;for(let m=0;m<i.length;m++)this.renderWord(n,i[m],x,c),x+=i[m].width,m<i.length-1&&(x+=d)}static renderStyledParagraph(n,s,g,c,l,r){const i=r??e.options.content?.textAlignment??"left",a=this.flattenToWords(n,s);if(a.length===0)return;const h=this.breakIntoLines(n,a,l),p=b(n)*e.options.page.defaultLineHeightFactor;let f=c;for(const o of h)f+p>e.options.page.maxContentHeight&&(w(n),f=e.Y),this.renderAlignedLine(n,o,g,f,l,i),e.recordContentY(f+b(n)),f+=p,e.updateY(p,"add");const d=h[h.length-1];if(d){const o=d.totalTextWidth+(d.words.length-1)*n.getTextWidth(" ");e.updateX(g+o,"set")}}static renderJustifiedParagraph(n,s,g,c,l){this.renderStyledParagraph(n,s,g,c,l)}}class H{static renderText(n,s,g=e.X,c=e.Y,l,r=!1){const i=n.splitTextToSize(s,l),a=b(n),h=a*e.options.page.defaultLineHeightFactor;let p=c;for(let f=0;f<i.length;f++){const d=i[f];p+h>e.options.page.maxContentHeight&&(w(n),p=e.Y),r?f===i.length-1?n.text(d,g,p,{baseline:"top"}):n.text(d,g,p,{maxWidth:l,align:"justify",baseline:"top"}):n.text(d,g,p,{baseline:"top"}),e.recordContentY(p+a),p+=h,e.updateY(h,"add")}return p}}const _=(t,n,s,g)=>{e.activateInlineLock(),t.setFontSize(e.options.page.defaultFontSize);const c=e.options.page.maxContentWidth-s;if(n?.items&&n?.items.length>0)if(n.items.some(r=>!["strong","em","text","codespan","link"].includes(r.type))){const r=[],i=()=>{r.length>0&&(P.renderStyledParagraph(t,r,e.X+s,e.Y,c),r.length=0)};for(const a of n.items)["strong","em","text","codespan","link"].includes(a.type)?r.push(a):(i(),g(a,s,!1));i()}else P.renderStyledParagraph(t,n.items,e.X+s,e.Y,c);else{const l=n.content??"",r=e.options.content?.textAlignment??"left";l.trim()&&H.renderText(t,l,e.X+s,e.Y,c,r==="justify")}e.updateX(e.options.page.xpading),e.deactivateInlineLock()},$=(t,n,s,g)=>{t.setFontSize(e.options.page.defaultFontSize);for(const[c,l]of n?.items?.entries()??[]){const r=n.ordered?(n.start??0)+c:n.start;g(l,s+1,!0,r,n.ordered)}},J=(t,n,s,g,c,l)=>{e.Y+b(t)>=e.options.page.maxContentHeight&&w(t);const r=e.options,i=s*r.page.indent,a=l?`${c}. `:"• ",h=r.page.xpading;e.updateX(h,"set"),t.setFont(r.font.regular.name,r.font.regular.style),t.text(a,h+i,e.Y,{baseline:"top"});const p=t.getTextWidth(a),f=h+i+p,d=r.page.maxContentWidth-i-p;if(n.items&&n.items.length>0){const o=[],x=()=>{o.length>0&&(P.renderStyledParagraph(t,o,f,e.Y,d),o.length=0,e.updateX(h,"set"))};for(const m of n.items)m.type===u.List?(x(),g(m,s,!0,c,m.ordered??!1)):m.type===u.ListItem?(x(),g(m,s,!0,c,l)):o.push(m);x()}else if(n.content){const o=r.content?.textAlignment??"left";H.renderText(t,n.content,f,e.Y,d,o==="justify")}},G=(t,n,s,g,c,l,r,i=!0)=>{if(n?.items&&n?.items.length>0)for(const a of n?.items??[])c(a,s,g,l,r,i);else{const a=e.options,h=s*a.page.indent,p=g?r?`${l}. `:"• ":"",f=n.content||"",d=a.page.xpading;if(!f&&!p)return;if(!f.trim()&&!p){const o=(f.match(/\n/g)||[]).length;if(o>1){const x=o-1,m=t.getTextDimensions("A").h*a.page.defaultLineHeightFactor,C=x*m;e.Y+C>a.page.maxContentHeight?w(t):(e.updateY(C,"add"),e.recordContentY(e.Y))}return}if(e.updateX(d,"set"),g&&p){const o=t.getTextWidth(p),x=a.page.maxContentWidth-h-o;t.setFont(a.font.regular.name,a.font.regular.style),t.text(p,d+h,e.Y,{baseline:"top"}),H.renderText(t,f,d+h+o,e.Y,x,i)}else{const o=a.page.maxContentWidth-h;H.renderText(t,f,d+h,e.Y,o,i)}e.updateX(d,"set")}},U=t=>{const n=t.internal.pageSize.getWidth();t.setLineDashPattern([1,1],0),t.setLineWidth(.1),t.line(e.options.page.xpading,e.Y,n-e.options.page.xpading,e.Y),t.setLineWidth(.1),t.setLineDashPattern([],0),e.updateY(b(t),"add")},K=(t,n,s,g)=>{const c=t.getFont(),l=t.getFontSize();t.setFont("courier","normal");const r=e.options.page.defaultFontSize*.9;t.setFontSize(r);const i=s*e.options.page.indent,a=e.options.page.maxContentWidth-i-8,h=t.getLineHeightFactor(),p=r/t.internal.scaleFactor*h,d=(n.code??"").replace(/[\r\n\s]+$/,"");if(!d){t.setFont(c.fontName,c.fontStyle),t.setFontSize(l);return}const o=t.splitTextToSize(d,a);for(;o.length>0&&o[o.length-1].trim()==="";)o.pop();if(o.length===0){t.setFont(c.fontName,c.fontStyle),t.setFontSize(l);return}const x=4,m="#EEEEEE",C="#DDDDDD";let F=0;for(;F<o.length;){const k=e.options.page.maxContentHeight-e.Y,S=o.length-F,L=k-x*2;let Y=Math.floor(L/p);if(Y<=0){w(t);continue}Y>S&&(Y=S);const N=o.slice(F,F+Y),X=F===0,z=F+Y>=o.length,E=Y*p;if(X&&e.updateY(x,"add"),t.setFillColor(m),t.setDrawColor(C),t.roundedRect(e.X,e.Y-x,e.options.page.maxContentWidth,E+(X?x:0)+(z?x:0),2,2,"FD"),X&&n.lang){const I=t.getFontSize();t.setFontSize(10),t.setTextColor("#666666"),t.text(n.lang,e.X+e.options.page.maxContentWidth-t.getTextWidth(n.lang)-4,e.Y,{baseline:"top"}),t.setFontSize(I),t.setTextColor("#000000")}let D=e.Y;for(const I of N)t.text(I,e.X+4,D,{baseline:"top"}),D+=p;e.updateY(E,"add"),e.recordContentY(e.Y+(z?x:0)),z&&e.updateY(x,"add"),F+=Y,F<o.length&&w(t)}t.setFont(c.fontName,c.fontStyle),t.setFontSize(l)},Q=(t,n,s)=>{const g=t.getFont().fontName,c=t.getFont().fontStyle,l=t.getFontSize(),r=a=>{switch(a){case"normal":return 0;case"bold":return 1;case"italic":return 1.5;case"bolditalic":return 1.5;case"codespan":return .5;default:return 0}},i=(a,h)=>{h==="bold"?t.setFont(e.options.font.bold.name&&e.options.font.bold.name!==""?e.options.font.bold.name:g,e.options.font.bold.style||"bold"):h==="italic"?t.setFont(e.options.font.regular.name,"italic"):h==="bolditalic"?t.setFont(e.options.font.bold.name&&e.options.font.bold.name!==""?e.options.font.bold.name:g,"bolditalic"):h==="codespan"?(t.setFont("courier","normal"),t.setFontSize(l*.9)):t.setFont(e.options.font.regular.name,c);const p=e.options.page.maxContentWidth-s-e.X,f=t.splitTextToSize(a,p),d=h==="codespan",o=1,x="#EEEEEE";if(e.isInlineLockActive)for(let m=0;m<f.length;m++){if(d){const C=t.getTextWidth(f[m])+y(t),F=b(t);t.setFillColor(x),t.roundedRect(e.X+s-o,e.Y-o,C+o*2,F+o*2,2,2,"F"),t.setFillColor("#000000")}t.text(f[m],e.X+s,e.Y,{baseline:"top",maxWidth:p}),e.updateX(t.getTextDimensions(f[m]).w+(d?o*2:1),"add"),m<f.length-1&&(e.updateY(b(t),"add"),e.updateX(e.options.page.xpading,"set"))}else if(f.length>1){const m=f[0],C=f?.slice(1)?.join(" ");if(d){const S=t.getTextWidth(m)+y(t),L=b(t);t.setFillColor(x),t.roundedRect(e.X+(s>=2?s+2:0)-o,e.Y-o,S+o*2,L+o*2,2,2,"F"),t.setFillColor("#000000")}t.text(m,e.X+(s>=2?s+2*r(h):0),e.Y,{baseline:"top",maxWidth:p}),e.updateX(e.options.page.xpading+s),e.updateY(b(t),"add");const F=e.options.page.maxContentWidth-s-e.options.page.xpading;t.splitTextToSize(C,F).forEach(S=>{if(d){const L=t.getTextWidth(S)+y(t),Y=b(t);t.setFillColor(x),t.roundedRect(e.X+y(t)-o,e.Y-o,L+o*2,Y+o*2,2,2,"F"),t.setFillColor("#000000")}t.text(S,e.X+y(t),e.Y,{baseline:"top",maxWidth:F})})}else{if(d){const m=t.getTextWidth(a)+y(t),C=b(t);t.setFillColor(x),t.roundedRect(e.X+s-o,e.Y-o,m+o*2,C+o*2,2,2,"F"),t.setFillColor("#000000")}t.text(a,e.X+s,e.Y,{baseline:"top",maxWidth:p}),e.updateX(t.getTextDimensions(a).w+(s>=2?a.split(" ").length+2:2)*r(h)*.5+(d?o*2:0),"add")}};if(n.type==="text"&&n.items&&n.items.length>0)for(const a of n.items)if(a.type==="codespan")i(a.content||"","codespan");else if(a.type==="em"||a.type==="strong"){const h=a.type==="em"?"italic":"bold";if(a.items&&a.items.length>0)for(const p of a.items)p.type==="strong"&&h==="italic"||p.type==="em"&&h==="bold"?i(p.content||"","bolditalic"):i(p.content||"",h);else i(a.content||"",h)}else i(a.content||"","normal");else n.type==="em"?i(n.content||"","italic"):n.type==="strong"?i(n.content||"","bold"):n.type==="codespan"?i(n.content||"","codespan"):i(n.content||"","normal");t.setFont(g,c),t.setFontSize(l)},V=(t,n,s)=>{const g=t.getFont().fontName,c=t.getFont().fontStyle,l=t.getFontSize(),r=t.getTextColor(),i=e.options.link?.linkColor||[0,0,255];t.setTextColor(...i);const a=e.options.page.maxContentWidth-s-e.X,h=n.text||n.content||"",p=n.href||"",f=t.splitTextToSize(h,a);if(e.isInlineLockActive)for(let d=0;d<f.length;d++){const o=t.getTextDimensions(f[d]).w,x=b(t)/2;t.link(e.X+s,e.Y,o,x,{url:p}),t.text(f[d],e.X+s,e.Y,{baseline:"top",maxWidth:a}),e.updateX(o+1,"add"),e.X+o>e.options.page.maxContentWidth-s&&(e.updateY(x,"add"),e.updateX(e.options.page.xpading+s,"set")),d<f.length-1&&(e.updateY(x,"add"),e.updateX(e.options.page.xpading+s,"set"))}else if(f.length>1){const d=f[0],o=f?.slice(1)?.join(" "),x=t.getTextDimensions(d).w,m=b(t)/2;t.link(e.X+s,e.Y,x,m,{url:p}),t.text(d,e.X+s,e.Y,{baseline:"top",maxWidth:a}),e.updateX(e.options.page.xpading+s),e.updateY(m,"add");const C=e.options.page.maxContentWidth-s-e.options.page.xpading;t.splitTextToSize(o,C).forEach(k=>{const S=t.getTextDimensions(k).w;t.link(e.X+y(t),e.Y,S,m,{url:p}),t.text(k,e.X+y(t),e.Y,{baseline:"top",maxWidth:C})})}else{const d=t.getTextDimensions(h).w,o=b(t)/2;t.link(e.X+s,e.Y,d,o,{url:p}),t.text(h,e.X+s,e.Y,{baseline:"top",maxWidth:a}),e.updateX(d+2,"add")}t.setFont(g,c),t.setFontSize(l),t.setTextColor(r)},Z=(t,n,s,g)=>{const c=e.options,l=s+1,r=e.X+s*c.page.indent,i=e.Y,a=r+c.page.indent/2,h=i,p=t.internal.getCurrentPageInfo().pageNumber;n.items&&n.items.length>0&&n.items.forEach(o=>{g(o,l)});const f=e.Y,d=t.internal.getCurrentPageInfo().pageNumber;t.setDrawColor(100),t.setLineWidth(1);for(let o=p;o<=d;o++){t.setPage(o);const x=o===p,m=o===d,C=x?h:c.page.topmargin,F=m?f:c.page.maxContentHeight;t.line(a,C,a,F)}e.recordContentY(),t.setPage(d)},M=(t,n,s)=>{if(!n.data)return;const g=e.options,c=e.X+s*g.page.indent;let l=e.Y;const r=g.page.maxContentWidth-s*g.page.indent;try{const i=t.getImageProperties(n.data),a=i.width,h=i.height;let p=a,f=h;const d=a/h;p>0&&(p=r,f=p/d),l+f>g.page.maxContentHeight&&(t.addPage(),l=g.page.topmargin,e.updateY(l));let o=n.src?.split(".").pop()?.toUpperCase()||"JPEG";n.data.startsWith("data:image/png")?o="PNG":n.data.startsWith("data:image/jpeg")||n.data.startsWith("data:image/jpg")?o="JPEG":n.data.startsWith("data:image/webp")&&(o="WEBP"),t.addImage(n.data,o,c,l,p,f),e.updateY(f,"add"),e.recordContentY()}catch(i){console.warn("Failed to render image",i)}},tt=(t,n,s)=>{if(!n.header||!n.rows)return;const g=e.options,c=g.page.xmargin+s*g.page.indent,l=[n.header.map(a=>a.content||"")],r=n.rows.map(a=>a.map(h=>h.content||"")),i=g.table||{};j(t,{head:l,body:r,startY:e.Y,margin:{left:c,right:g.page.xmargin},...i,didDrawPage:a=>{i.didDrawPage&&i.didDrawPage(a)},didDrawCell:a=>{i.didDrawCell&&i.didDrawCell(a),e.setCursor({x:e.X,y:a.cell.y+2*g.page.lineSpace})}})},R=async t=>{for(const n of t){if(n.type===u.Image&&n.src)try{if(n.src.startsWith("data:"))n.data=n.src;else{const s=await fetch(n.src);if(!s.ok)throw new Error(`Failed to fetch image: ${s.statusText}`);const g=await s.blob(),c=await new Promise((l,r)=>{const i=new FileReader;i.onloadend=()=>{typeof i.result=="string"?l(i.result):r(new Error("Failed to convert image to base64 string"))},i.onerror=r,i.readAsDataURL(g)});n.data=c}}catch(s){console.warn(`[jspdf-md-renderer] Warning: Failed to load image at ${n.src}. It will be skipped.`,s)}n.items&&n.items.length>0&&await R(n.items)}},v={page:{indent:10,maxContentWidth:190,maxContentHeight:277,lineSpace:1.5,defaultLineHeightFactor:1.2,defaultFontSize:12,defaultTitleFontSize:14,topmargin:10,xpading:10,xmargin:10,format:"a4",orientation:"p"},font:{bold:{name:"helvetica",style:"bold"},regular:{name:"helvetica",style:"normal"},light:{name:"helvetica",style:"light"}}},et=t=>{if(!t)throw new Error("RenderOption is required");const n={...v.page,...t.page},s={...v.font,...t.font};return n.maxContentWidth||(n.maxContentWidth=190),n.maxContentHeight||(n.maxContentHeight=277),{...t,page:n,font:s}},nt=async(t,n,s)=>{const g=et(s);e.initialize(g);const c=await B(n);await R(c);const l=(r,i=0,a=!1,h=0,p=!1,f=!0)=>{const d=i*g.page.indent;switch(r.type){case u.Heading:O(t,r,d,l);break;case u.Paragraph:_(t,r,d,l);break;case u.List:$(t,r,i,l);break;case u.ListItem:J(t,r,i,l,h,p);break;case u.Hr:U(t);break;case u.Code:K(t,r,i);break;case u.Strong:case u.Em:case u.CodeSpan:Q(t,r,d);break;case u.Link:V(t,r,d);break;case u.Blockquote:Z(t,r,i,l);break;case u.Image:M(t,r,i);break;case u.Table:tt(t,r,i);break;case u.Raw:case u.Text:G(t,r,i,a,l,h,p,g.page.defaultFontSize>0);break;default:console.warn(`Warning: Unsupported element type encountered: ${r.type}.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("marked"),q=require("jspdf-autotable");var u=(t=>(t.Heading="heading",t.Paragraph="paragraph",t.List="list",t.ListItem="list_item",t.Blockquote="blockquote",t.Code="code",t.CodeSpan="codespan",t.Table="table",t.Html="html",t.Hr="hr",t.Image="image",t.Link="link",t.Strong="strong",t.Em="em",t.TableHeader="table_header",t.TableCell="table_cell",t.Raw="raw",t.Text="text",t))(u||{});const A="__jmr_",G=/(!\[[^\]]*\]\()([^)]+)(\))\s*\{([^}]+)\}/g,U=/(\w+)\s*=\s*(\w+)/g,B=["left","center","right"],J=t=>{const e=[];return t.width!==void 0&&e.push(`w=${t.width}`),t.height!==void 0&&e.push(`h=${t.height}`),t.align&&e.push(`a=${t.align}`),e.length>0?`#${A}${e.join("&")}`:""},V=t=>{const e={};let s;for(;(s=U.exec(t))!==null;){const c=s[1].toLowerCase(),g=s[2];switch(c){case"width":case"w":{const r=parseInt(g,10);!isNaN(r)&&r>0&&(e.width=r);break}case"height":case"h":{const r=parseInt(g,10);!isNaN(r)&&r>0&&(e.height=r);break}case"align":{const r=g.toLowerCase();B.includes(r)&&(e.align=r);break}}}return e},K=t=>t.replace(G,(e,s,c,g,r)=>{const l=V(r),i=J(l);return`${s}${c}${i}${g}`}),Q=t=>{const e=t.indexOf(`#${A}`);if(e===-1)return{cleanHref:t,attrs:{}};const s=t.substring(0,e),c=t.substring(e+1+A.length),g={},r=c.split("&");for(const l of r){const[i,a]=l.split("=");switch(i){case"w":{const p=parseInt(a,10);!isNaN(p)&&p>0&&(g.width=p);break}case"h":{const p=parseInt(a,10);!isNaN(p)&&p>0&&(g.height=p);break}case"a":{B.includes(a)&&(g.align=a);break}}}return{cleanHref:s,attrs:g}},_=async t=>{const e=K(t),s=await O.marked.lexer(e,{async:!0,gfm:!0});return W(s)},W=t=>{const e=[];return t.forEach(s=>{try{const c=Z[s.type];c?e.push(c(s)):e.push({type:u.Raw,content:s.raw})}catch(c){console.error("Failed to handle token ==>",s,c)}}),e},Z={[u.Heading]:t=>({type:u.Heading,depth:t.depth,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Paragraph]:t=>({type:u.Paragraph,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.List]:t=>({type:u.List,ordered:t.ordered,start:t.start,items:t.items?W(t.items):[]}),[u.ListItem]:t=>({type:u.ListItem,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Code]:t=>({type:u.Code,lang:t.lang,code:t.text}),[u.Table]:t=>({type:u.Table,header:t.header.map(e=>({type:u.TableHeader,content:e.text})),rows:t.rows.map(e=>e.map(s=>({type:u.TableCell,content:s.text})))}),[u.Image]:t=>{const{cleanHref:e,attrs:s}=Q(t.href);return{type:u.Image,src:e,alt:t.text,width:s.width,height:s.height,align:s.align}},[u.Link]:t=>({type:u.Link,href:t.href,text:t.text,items:t.tokens?W(t.tokens):[]}),[u.Strong]:t=>({type:u.Strong,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Em]:t=>({type:u.Em,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Text]:t=>({type:u.Text,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Hr]:t=>({type:u.Hr,content:t.raw,items:t.tokens?W(t.tokens):[]}),[u.CodeSpan]:t=>({type:u.CodeSpan,content:t.text,items:t.tokens?W(t.tokens):[]}),[u.Blockquote]:t=>({type:u.Blockquote,content:t.text,items:t.tokens?W(t.tokens):[]})},H=class H{static initialize(e){this.options_=e,this.cursor={x:e.cursor.x,y:e.cursor.y},this.lastContentY_=e.cursor.y}static getCursor(){return this.cursor}static setCursor(e){this.cursor=e}static get options(){return this.options_}static get isInlineLockActive(){return this.inlineLock}static activateInlineLock(){this.inlineLock=!0}static deactivateInlineLock(){this.inlineLock=!1}static updateX(e,s="set"){s==="set"?this.cursor.x=e:s==="add"&&(this.cursor.x+=e)}static updateY(e,s="set"){s==="set"?this.cursor.y=e:s==="add"&&(this.cursor.y+=e)}static recordContentY(e){this.lastContentY_=e!==void 0?e:this.cursor.y}static get lastContentY(){return this.lastContentY_}static get X(){return this.cursor.x}static get Y(){return this.cursor.y}};H.cursor={x:0,y:0},H.lastContentY_=0,H.inlineLock=!1;let n=H;const C=t=>t.getTextDimensions("H").h*n.options.page.defaultLineHeightFactor,Y=t=>t.getTextDimensions("H").w*n.options.page.defaultLineHeightFactor,M=(t,e,s,c)=>{const g=6-(e?.depth??0)>0?6-(e?.depth??0):1;if(t.setFontSize(n.options.page.defaultFontSize+g),e?.items&&e?.items.length>0)for(const r of e?.items??[])c(r,s,!1);else{const r=C(t);t.text(e?.content??"",n.X+s,n.Y,{align:"left",maxWidth:n.options.page.maxContentWidth-s,baseline:"top"}),n.recordContentY(n.Y+r),n.updateY(r,"add")}t.setFontSize(n.options.page.defaultFontSize),n.updateX(n.options.page.xpading,"set")},k=t=>{typeof n.options.pageBreakHandler=="function"?n.options.pageBreakHandler(t):t.addPage(n.options.page?.format,n.options.page?.orientation),n.updateY(n.options.page.topmargin),n.updateX(n.options.page.xpading)};class v{static getCodespanOptions(){const e=n.options.codespan??{};return{backgroundColor:e.backgroundColor??"#EEEEEE",padding:e.padding??.5,showBackground:e.showBackground!==!1,fontSizeScale:e.fontSizeScale??.9}}static applyStyle(e,s){const c=e.getFont().fontName,g=e.getFontSize(),r=()=>{const i=n.options.font.bold?.name;return i&&i!==""?i:c},l=()=>{const i=n.options.font.regular?.name;return i&&i!==""?i:c};switch(s){case"bold":e.setFont(r(),n.options.font.bold?.style||"bold");break;case"italic":e.setFont(l(),"italic");break;case"bolditalic":e.setFont(r(),"bolditalic");break;case"codespan":e.setFont("courier","normal"),e.setFontSize(g*this.getCodespanOptions().fontSizeScale);break;default:e.setFont(l(),e.getFont().fontStyle);break}}static measureWordWidth(e,s,c){const g=e.getFont(),r=e.getFontSize();this.applyStyle(e,c);const l=e.getTextWidth(s),i=e.getCharSpace?.()??0,a=l+s.length*i;return e.setFont(g.fontName,g.fontStyle),e.setFontSize(r),a}static getStyleFromType(e,s){switch(e){case"strong":return s==="italic"?"bolditalic":"bold";case"em":return s==="bold"?"bolditalic":"italic";case"codespan":return"codespan";default:return s||"normal"}}static flattenToWords(e,s,c="normal",g=!1,r){const l=[];for(const i of s){const a=this.getStyleFromType(i.type,c),p=i.type==="link"||g,h=i.href||r;if(i.items&&i.items.length>0){const f=this.flattenToWords(e,i.items,a,p,h);l.push(...f)}else{const f=i.content||i.text||"";if(!f)continue;if(a==="codespan"){const o=f.trim();o&&l.push({text:o,width:this.measureWordWidth(e,o,a),style:a,isLink:p,href:h,linkColor:p?n.options.link?.linkColor||[0,0,255]:void 0});continue}const d=f.split(/\s+/).filter(o=>o.length>0);if(d.length===0)continue;for(let o=0;o<d.length;o++){const m=d[o];l.push({text:m,width:this.measureWordWidth(e,m,a),style:a,isLink:p,href:h,linkColor:p?n.options.link?.linkColor||[0,0,255]:void 0})}}}return l}static breakIntoLines(e,s,c){const g=[];let r=[],l=0,i=0;const a=e.getTextWidth(" ");for(let p=0;p<s.length;p++){const h=s[p],f=r.length>0?a+h.width:h.width;i+f>c&&r.length>0?(g.push({words:r,totalTextWidth:l,isLastLine:!1}),r=[h],l=h.width,i=h.width):(r.push(h),l+=h.width,i+=f)}return r.length>0&&g.push({words:r,totalTextWidth:l,isLastLine:!0}),g}static renderWord(e,s,c,g){const r=e.getFont(),l=e.getFontSize(),i=e.getTextColor();if(this.applyStyle(e,s.style),s.isLink&&s.linkColor&&e.setTextColor(...s.linkColor),s.style==="codespan"){const a=this.getCodespanOptions();if(a.showBackground){const p=C(e),h=a.padding;e.setFillColor(a.backgroundColor),e.rect(c-h,g-h,s.width+h*2,p+h*2,"F"),e.setFillColor("#000000")}}if(e.text(s.text,c,g,{baseline:"top"}),s.isLink&&s.href){const a=C(e)/2;e.link(c,g,s.width,a,{url:s.href})}e.setFont(r.fontName,r.fontStyle),e.setFontSize(l),e.setTextColor(i)}static renderAlignedLine(e,s,c,g,r,l="left"){const{words:i,totalTextWidth:a,isLastLine:p}=s;if(i.length===0)return;const h=e.getTextWidth(" ");let f=c,d=h;const o=a+(i.length-1)*h;switch(l){case"right":f=c+r-o;break;case"center":f=c+(r-o)/2;break;case"justify":!p&&i.length>1&&(d=(r-a)/(i.length-1));break}let m=f;for(let x=0;x<i.length;x++)this.renderWord(e,i[x],m,g),m+=i[x].width,x<i.length-1&&(m+=d)}static renderStyledParagraph(e,s,c,g,r,l){const i=l??n.options.content?.textAlignment??"left",a=this.flattenToWords(e,s);if(a.length===0)return;const p=this.breakIntoLines(e,a,r),h=C(e)*n.options.page.defaultLineHeightFactor;let f=g;for(const o of p)f+h>n.options.page.maxContentHeight&&(k(e),f=n.Y),this.renderAlignedLine(e,o,c,f,r,i),n.recordContentY(f+C(e)),f+=h,n.updateY(h,"add");const d=p[p.length-1];if(d){const o=d.totalTextWidth+(d.words.length-1)*e.getTextWidth(" ");n.updateX(c+o,"set")}}static renderJustifiedParagraph(e,s,c,g,r){this.renderStyledParagraph(e,s,c,g,r)}}class I{static renderText(e,s,c=n.X,g=n.Y,r,l=!1){const i=e.splitTextToSize(s,r),a=C(e),p=a*n.options.page.defaultLineHeightFactor;let h=g;for(let f=0;f<i.length;f++){const d=i[f];h+p>n.options.page.maxContentHeight&&(k(e),h=n.Y),l?f===i.length-1?e.text(d,c,h,{baseline:"top"}):e.text(d,c,h,{maxWidth:r,align:"justify",baseline:"top"}):e.text(d,c,h,{baseline:"top"}),n.recordContentY(h+a),h+=p,n.updateY(p,"add")}return h}}const tt=(t,e,s,c)=>{n.activateInlineLock(),t.setFontSize(n.options.page.defaultFontSize);const g=n.options.page.maxContentWidth-s;if(e?.items&&e?.items.length>0)if(e.items.some(l=>!["strong","em","text","codespan","link"].includes(l.type))){const l=[],i=()=>{l.length>0&&(v.renderStyledParagraph(t,l,n.X+s,n.Y,g),l.length=0)};for(const a of e.items)["strong","em","text","codespan","link"].includes(a.type)?l.push(a):(i(),c(a,s,!1));i()}else v.renderStyledParagraph(t,e.items,n.X+s,n.Y,g);else{const r=e.content??"",l=n.options.content?.textAlignment??"left";r.trim()&&I.renderText(t,r,n.X+s,n.Y,g,l==="justify")}n.updateX(n.options.page.xpading),n.deactivateInlineLock()},et=(t,e,s,c)=>{t.setFontSize(n.options.page.defaultFontSize);for(const[g,r]of e?.items?.entries()??[]){const l=e.ordered?(e.start??0)+g:e.start;c(r,s+1,!0,l,e.ordered)}},nt=(t,e,s,c,g,r)=>{n.Y+C(t)>=n.options.page.maxContentHeight&&k(t);const l=n.options,i=s*l.page.indent,a=r?`${g}. `:"• ",p=l.page.xpading;n.updateX(p,"set"),t.setFont(l.font.regular.name,l.font.regular.style),t.text(a,p+i,n.Y,{baseline:"top"});const h=t.getTextWidth(a),f=p+i+h,d=l.page.maxContentWidth-i-h;if(e.items&&e.items.length>0){const o=[],m=()=>{o.length>0&&(v.renderStyledParagraph(t,o,f,n.Y,d),o.length=0,n.updateX(p,"set"))};for(const x of e.items)x.type===u.List?(m(),c(x,s,!0,g,x.ordered??!1)):x.type===u.ListItem?(m(),c(x,s,!0,g,r)):o.push(x);m()}else if(e.content){const o=l.content?.textAlignment??"left";I.renderText(t,e.content,f,n.Y,d,o==="justify")}},st=(t,e,s,c,g,r,l,i=!0)=>{if(e?.items&&e?.items.length>0)for(const a of e?.items??[])g(a,s,c,r,l,i);else{const a=n.options,p=s*a.page.indent,h=c?l?`${r}. `:"• ":"",f=e.content||"",d=a.page.xpading;if(!f&&!h)return;if(!f.trim()&&!h){const o=(f.match(/\n/g)||[]).length;if(o>1){const m=o-1,x=t.getTextDimensions("A").h*a.page.defaultLineHeightFactor,F=m*x;n.Y+F>a.page.maxContentHeight?k(t):(n.updateY(F,"add"),n.recordContentY(n.Y))}return}if(n.updateX(d,"set"),c&&h){const o=t.getTextWidth(h),m=a.page.maxContentWidth-p-o;t.setFont(a.font.regular.name,a.font.regular.style),t.text(h,d+p,n.Y,{baseline:"top"}),I.renderText(t,f,d+p+o,n.Y,m,i)}else{const o=a.page.maxContentWidth-p;I.renderText(t,f,d+p,n.Y,o,i)}n.updateX(d,"set")}},it=t=>{const e=t.internal.pageSize.getWidth();t.setLineDashPattern([1,1],0),t.setLineWidth(.1),t.line(n.options.page.xpading,n.Y,e-n.options.page.xpading,n.Y),t.setLineWidth(.1),t.setLineDashPattern([],0),n.updateY(C(t),"add")},at=(t,e,s,c)=>{const g=t.getFont(),r=t.getFontSize();t.setFont("courier","normal");const l=n.options.page.defaultFontSize*.9;t.setFontSize(l);const i=s*n.options.page.indent,a=n.options.page.maxContentWidth-i-8,p=t.getLineHeightFactor(),h=l/t.internal.scaleFactor*p,d=(e.code??"").replace(/[\r\n\s]+$/,"");if(!d){t.setFont(g.fontName,g.fontStyle),t.setFontSize(r);return}const o=t.splitTextToSize(d,a);for(;o.length>0&&o[o.length-1].trim()==="";)o.pop();if(o.length===0){t.setFont(g.fontName,g.fontStyle),t.setFontSize(r);return}const m=4,x="#EEEEEE",F="#DDDDDD";let b=0;for(;b<o.length;){const y=n.options.page.maxContentHeight-n.Y,w=o.length-b,T=y-m*2;let S=Math.floor(T/h);if(S<=0){k(t);continue}S>w&&(S=w);const j=o.slice(b,b+S),X=b===0,z=b+S>=o.length,N=S*h;if(X&&n.updateY(m,"add"),t.setFillColor(x),t.setDrawColor(F),t.roundedRect(n.X,n.Y-m,n.options.page.maxContentWidth,N+(X?m:0)+(z?m:0),2,2,"FD"),X&&e.lang){const E=t.getFontSize();t.setFontSize(10),t.setTextColor("#666666"),t.text(e.lang,n.X+n.options.page.maxContentWidth-t.getTextWidth(e.lang)-4,n.Y,{baseline:"top"}),t.setFontSize(E),t.setTextColor("#000000")}let R=n.Y;for(const E of j)t.text(E,n.X+4,R,{baseline:"top"}),R+=h;n.updateY(N,"add"),n.recordContentY(n.Y+(z?m:0)),z&&n.updateY(m,"add"),b+=S,b<o.length&&k(t)}t.setFont(g.fontName,g.fontStyle),t.setFontSize(r)},ot=(t,e,s)=>{const c=t.getFont().fontName,g=t.getFont().fontStyle,r=t.getFontSize(),l=a=>{switch(a){case"normal":return 0;case"bold":return 1;case"italic":return 1.5;case"bolditalic":return 1.5;case"codespan":return .5;default:return 0}},i=(a,p)=>{p==="bold"?t.setFont(n.options.font.bold.name&&n.options.font.bold.name!==""?n.options.font.bold.name:c,n.options.font.bold.style||"bold"):p==="italic"?t.setFont(n.options.font.regular.name,"italic"):p==="bolditalic"?t.setFont(n.options.font.bold.name&&n.options.font.bold.name!==""?n.options.font.bold.name:c,"bolditalic"):p==="codespan"?(t.setFont("courier","normal"),t.setFontSize(r*.9)):t.setFont(n.options.font.regular.name,g);const h=n.options.page.maxContentWidth-s-n.X,f=t.splitTextToSize(a,h),d=p==="codespan",o=1,m="#EEEEEE";if(n.isInlineLockActive)for(let x=0;x<f.length;x++){if(d){const F=t.getTextWidth(f[x])+Y(t),b=C(t);t.setFillColor(m),t.roundedRect(n.X+s-o,n.Y-o,F+o*2,b+o*2,2,2,"F"),t.setFillColor("#000000")}t.text(f[x],n.X+s,n.Y,{baseline:"top",maxWidth:h}),n.updateX(t.getTextDimensions(f[x]).w+(d?o*2:1),"add"),x<f.length-1&&(n.updateY(C(t),"add"),n.updateX(n.options.page.xpading,"set"))}else if(f.length>1){const x=f[0],F=f?.slice(1)?.join(" ");if(d){const w=t.getTextWidth(x)+Y(t),T=C(t);t.setFillColor(m),t.roundedRect(n.X+(s>=2?s+2:0)-o,n.Y-o,w+o*2,T+o*2,2,2,"F"),t.setFillColor("#000000")}t.text(x,n.X+(s>=2?s+2*l(p):0),n.Y,{baseline:"top",maxWidth:h}),n.updateX(n.options.page.xpading+s),n.updateY(C(t),"add");const b=n.options.page.maxContentWidth-s-n.options.page.xpading;t.splitTextToSize(F,b).forEach(w=>{if(d){const T=t.getTextWidth(w)+Y(t),S=C(t);t.setFillColor(m),t.roundedRect(n.X+Y(t)-o,n.Y-o,T+o*2,S+o*2,2,2,"F"),t.setFillColor("#000000")}t.text(w,n.X+Y(t),n.Y,{baseline:"top",maxWidth:b})})}else{if(d){const x=t.getTextWidth(a)+Y(t),F=C(t);t.setFillColor(m),t.roundedRect(n.X+s-o,n.Y-o,x+o*2,F+o*2,2,2,"F"),t.setFillColor("#000000")}t.text(a,n.X+s,n.Y,{baseline:"top",maxWidth:h}),n.updateX(t.getTextDimensions(a).w+(s>=2?a.split(" ").length+2:2)*l(p)*.5+(d?o*2:0),"add")}};if(e.type==="text"&&e.items&&e.items.length>0)for(const a of e.items)if(a.type==="codespan")i(a.content||"","codespan");else if(a.type==="em"||a.type==="strong"){const p=a.type==="em"?"italic":"bold";if(a.items&&a.items.length>0)for(const h of a.items)h.type==="strong"&&p==="italic"||h.type==="em"&&p==="bold"?i(h.content||"","bolditalic"):i(h.content||"",p);else i(a.content||"",p)}else i(a.content||"","normal");else e.type==="em"?i(e.content||"","italic"):e.type==="strong"?i(e.content||"","bold"):e.type==="codespan"?i(e.content||"","codespan"):i(e.content||"","normal");t.setFont(c,g),t.setFontSize(r)},rt=(t,e,s)=>{const c=t.getFont().fontName,g=t.getFont().fontStyle,r=t.getFontSize(),l=t.getTextColor(),i=n.options.link?.linkColor||[0,0,255];t.setTextColor(...i);const a=n.options.page.maxContentWidth-s-n.X,p=e.text||e.content||"",h=e.href||"",f=t.splitTextToSize(p,a);if(n.isInlineLockActive)for(let d=0;d<f.length;d++){const o=t.getTextDimensions(f[d]).w,m=C(t)/2;t.link(n.X+s,n.Y,o,m,{url:h}),t.text(f[d],n.X+s,n.Y,{baseline:"top",maxWidth:a}),n.updateX(o+1,"add"),n.X+o>n.options.page.maxContentWidth-s&&(n.updateY(m,"add"),n.updateX(n.options.page.xpading+s,"set")),d<f.length-1&&(n.updateY(m,"add"),n.updateX(n.options.page.xpading+s,"set"))}else if(f.length>1){const d=f[0],o=f?.slice(1)?.join(" "),m=t.getTextDimensions(d).w,x=C(t)/2;t.link(n.X+s,n.Y,m,x,{url:h}),t.text(d,n.X+s,n.Y,{baseline:"top",maxWidth:a}),n.updateX(n.options.page.xpading+s),n.updateY(x,"add");const F=n.options.page.maxContentWidth-s-n.options.page.xpading;t.splitTextToSize(o,F).forEach(y=>{const w=t.getTextDimensions(y).w;t.link(n.X+Y(t),n.Y,w,x,{url:h}),t.text(y,n.X+Y(t),n.Y,{baseline:"top",maxWidth:F})})}else{const d=t.getTextDimensions(p).w,o=C(t)/2;t.link(n.X+s,n.Y,d,o,{url:h}),t.text(p,n.X+s,n.Y,{baseline:"top",maxWidth:a}),n.updateX(d+2,"add")}t.setFont(c,g),t.setFontSize(r),t.setTextColor(l)},lt=(t,e,s,c)=>{const g=n.options,r=s+1,l=n.X+s*g.page.indent,i=n.Y,a=l+g.page.indent/2,p=i,h=t.internal.getCurrentPageInfo().pageNumber;e.items&&e.items.length>0&&e.items.forEach(o=>{c(o,r)});const f=n.Y,d=t.internal.getCurrentPageInfo().pageNumber;t.setDrawColor(100),t.setLineWidth(1);for(let o=h;o<=d;o++){t.setPage(o);const m=o===h,x=o===d,F=m?p:g.page.topmargin,b=x?f:g.page.maxContentHeight;t.line(a,F,a,b)}n.recordContentY(),t.setPage(d)},P=96,ct=t=>{if(t.data){if(t.data.startsWith("data:image/png"))return"PNG";if(t.data.startsWith("data:image/jpeg")||t.data.startsWith("data:image/jpg"))return"JPEG";if(t.data.startsWith("data:image/webp"))return"WEBP";if(t.data.startsWith("data:image/gif"))return"GIF"}return t.src?.split(".").pop()?.toUpperCase()||"JPEG"},L=(t,e="mm")=>{switch(e){case"pt":return t*72/P;case"in":return t/P;case"px":return t;default:return t*25.4/P}},gt=(t,e,s)=>{if(!e.data)return;const c=n.options,g=c.page.unit||"mm",r=s*c.page.indent,l=c.page.maxContentWidth-r,i=n.X+r;let a=n.Y;try{const p=t.getImageProperties(e.data),h=p.width,f=p.height,d=f>0?h/f:1;let o,m;if(e.width&&e.height?(o=L(e.width,g),m=L(e.height,g)):e.width?(o=L(e.width,g),m=o/d):e.height?(m=L(e.height,g),o=m*d):(o=L(h,g),m=L(f,g)),o>l){const w=l/o;o=l,m=m*w}const x=c.page.maxContentHeight-c.page.topmargin;if(m>x){const w=x/m;m=x,o=o*w}a+m>c.page.maxContentHeight&&(k(t),a=n.Y);const F=e.align||c.image?.defaultAlign||"left";let b;switch(F){case"right":b=i+l-o;break;case"center":b=i+(l-o)/2;break;default:b=i;break}const y=ct(e);t.addImage(e.data,y,b,a,o,m),n.updateY(m,"add"),n.recordContentY()}catch(p){console.warn("Failed to render image",p)}},pt=(t,e,s)=>{if(!e.header||!e.rows)return;const c=n.options,g=c.page.xmargin+s*c.page.indent,r=[e.header.map(a=>a.content||"")],l=e.rows.map(a=>a.map(p=>p.content||"")),i=c.table||{};q(t,{head:r,body:l,startY:n.Y,margin:{left:g,right:c.page.xmargin},...i,didDrawPage:a=>{i.didDrawPage&&i.didDrawPage(a)},didDrawCell:a=>{i.didDrawCell&&i.didDrawCell(a),n.setCursor({x:n.X,y:a.cell.y+2*c.page.lineSpace})}})},$=async t=>{for(const e of t){if(e.type===u.Image&&e.src)try{if(e.src.startsWith("data:"))e.data=e.src;else{const s=await fetch(e.src);if(!s.ok)throw new Error(`Failed to fetch image: ${s.statusText}`);const c=await s.blob(),g=await new Promise((r,l)=>{const i=new FileReader;i.onloadend=()=>{typeof i.result=="string"?r(i.result):l(new Error("Failed to convert image to base64 string"))},i.onerror=l,i.readAsDataURL(c)});e.data=g}}catch(s){console.warn(`[jspdf-md-renderer] Warning: Failed to load image at ${e.src}. It will be skipped.`,s)}e.items&&e.items.length>0&&await $(e.items)}},D={page:{indent:10,maxContentWidth:190,maxContentHeight:277,lineSpace:1.5,defaultLineHeightFactor:1.2,defaultFontSize:12,defaultTitleFontSize:14,topmargin:10,xpading:10,xmargin:10,format:"a4",orientation:"p"},font:{bold:{name:"helvetica",style:"bold"},regular:{name:"helvetica",style:"normal"},light:{name:"helvetica",style:"light"}},image:{defaultAlign:"left"}},ht=t=>{if(!t)throw new Error("RenderOption is required");const e={...D.page,...t.page},s={...D.font,...t.font},c={...D.image,...t.image};return e.maxContentWidth||(e.maxContentWidth=190),e.maxContentHeight||(e.maxContentHeight=277),{...t,page:e,font:s,image:c}},ft=async(t,e,s)=>{const c=ht(s);n.initialize(c);const g=await _(e);await $(g);const r=(l,i=0,a=!1,p=0,h=!1,f=!0)=>{const d=i*c.page.indent;switch(l.type){case u.Heading:M(t,l,d,r);break;case u.Paragraph:tt(t,l,d,r);break;case u.List:et(t,l,i,r);break;case u.ListItem:nt(t,l,i,r,p,h);break;case u.Hr:it(t);break;case u.Code:at(t,l,i);break;case u.Strong:case u.Em:case u.CodeSpan:ot(t,l,d);break;case u.Link:rt(t,l,d);break;case u.Blockquote:lt(t,l,i,r);break;case u.Image:gt(t,l,i);break;case u.Table:pt(t,l,i);break;case u.Raw:case u.Text:st(t,l,i,a,r,p,h,c.page.defaultFontSize>0);break;default:console.warn(`Warning: Unsupported element type encountered: ${l.type}.
|
|
2
2
|
If you believe this element type should be supported, please create an issue at:
|
|
3
3
|
https://github.com/JeelGajera/jspdf-md-renderer/issues
|
|
4
|
-
with details of the element and expected behavior. Thanks for helping to improve this library!`);break}};for(const
|
|
4
|
+
with details of the element and expected behavior. Thanks for helping to improve this library!`);break}};for(const l of g)r(l);c.endCursorYHandler(n.Y)};exports.MdTextParser=_;exports.MdTextRender=ft;
|