jspdf-md-renderer 3.2.0 → 3.3.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/dist/index.umd.js CHANGED
@@ -1,4 +1,4 @@
1
- (function(S,H){typeof exports=="object"&&typeof module<"u"?H(exports,require("marked"),require("jspdf-autotable")):typeof define=="function"&&define.amd?define(["exports","marked","jspdf-autotable"],H):(S=typeof globalThis<"u"?globalThis:S||self,H(S.JspdfMdRenderer={},S.marked,S.jspdfAutoTable))})(this,(function(S,H,q){"use strict";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 E="__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?`#${E}${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(`#${E}`);if(e===-1)return{cleanHref:t,attrs:{}};const s=t.substring(0,e),c=t.substring(e+1+E.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 H.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):[]})},I=class I{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}};I.cursor={x:0,y:0},I.lastContentY_=0,I.inlineLock=!1;let n=I;const F=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=F(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 P{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=F(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=F(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=F(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+F(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 z{static renderText(e,s,c=n.X,g=n.Y,r,l=!1){const i=e.splitTextToSize(s,r),a=F(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&&(P.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 P.renderStyledParagraph(t,e.items,n.X+s,n.Y,g);else{const r=e.content??"",l=n.options.content?.textAlignment??"left";r.trim()&&z.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+F(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&&(P.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";z.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,C=m*x;n.Y+C>a.page.maxContentHeight?k(t):(n.updateY(C,"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"}),z.renderText(t,f,d+p+o,n.Y,m,i)}else{const o=a.page.maxContentWidth-p;z.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(F(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",C="#DDDDDD";let b=0;for(;b<o.length;){const L=n.options.page.maxContentHeight-n.Y,w=o.length-b,X=L-m*2;let Y=Math.floor(X/h);if(Y<=0){k(t);continue}Y>w&&(Y=w);const dt=o.slice(b,b+Y),v=b===0,N=b+Y>=o.length,$=Y*h;if(v&&n.updateY(m,"add"),t.setFillColor(x),t.setDrawColor(C),t.roundedRect(n.X,n.Y-m,n.options.page.maxContentWidth,$+(v?m:0)+(N?m:0),2,2,"FD"),v&&e.lang){const R=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(R),t.setTextColor("#000000")}let O=n.Y;for(const R of dt)t.text(R,n.X+4,O,{baseline:"top"}),O+=h;n.updateY($,"add"),n.recordContentY(n.Y+(N?m:0)),N&&n.updateY(m,"add"),b+=Y,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 C=t.getTextWidth(f[x])+y(t),b=F(t);t.setFillColor(m),t.roundedRect(n.X+s-o,n.Y-o,C+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(F(t),"add"),n.updateX(n.options.page.xpading,"set"))}else if(f.length>1){const x=f[0],C=f?.slice(1)?.join(" ");if(d){const w=t.getTextWidth(x)+y(t),X=F(t);t.setFillColor(m),t.roundedRect(n.X+(s>=2?s+2:0)-o,n.Y-o,w+o*2,X+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(F(t),"add");const b=n.options.page.maxContentWidth-s-n.options.page.xpading;t.splitTextToSize(C,b).forEach(w=>{if(d){const X=t.getTextWidth(w)+y(t),Y=F(t);t.setFillColor(m),t.roundedRect(n.X+y(t)-o,n.Y-o,X+o*2,Y+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),C=F(t);t.setFillColor(m),t.roundedRect(n.X+s-o,n.Y-o,x+o*2,C+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=F(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=F(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 C=n.options.page.maxContentWidth-s-n.options.page.xpading;t.splitTextToSize(o,C).forEach(L=>{const w=t.getTextDimensions(L).w;t.link(n.X+y(t),n.Y,w,x,{url:h}),t.text(L,n.X+y(t),n.Y,{baseline:"top",maxWidth:C})})}else{const d=t.getTextDimensions(p).w,o=F(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,C=m?p:g.page.topmargin,b=x?f:g.page.maxContentHeight;t.line(a,C,a,b)}n.recordContentY(),t.setPage(d)},A=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"},T=(t,e="mm")=>{switch(e){case"pt":return t*72/A;case"in":return t/A;case"px":return t;default:return t*25.4/A}},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=T(e.width,g),m=T(e.height,g)):e.width?(o=T(e.width,g),m=o/d):e.height?(m=T(e.height,g),o=m*d):(o=T(h,g),m=T(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 C=e.align||c.image?.defaultAlign||"left";let b;switch(C){case"right":b=i+l-o;break;case"center":b=i+(l-o)/2;break;default:b=i;break}const L=ct(e);t.addImage(e.data,L,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})}})},j=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 j(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 j(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}.
1
+ (function(S,I){typeof exports=="object"&&typeof module<"u"?I(exports,require("marked"),require("jspdf-autotable")):typeof define=="function"&&define.amd?define(["exports","marked","jspdf-autotable"],I):(S=typeof globalThis<"u"?globalThis:S||self,I(S.JspdfMdRenderer={},S.marked,S.jspdfAutoTable))})(this,(function(S,I,q){"use strict";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 P="__jmr_",U=/(!\[[^\]]*\]\()([^)]+)(\))\s*\{([^}]+)\}/g,Q=/(\w+)\s*=\s*(\w+)/g,R=["left","center","right"],V=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?`#${P}${e.join("&")}`:""},Z=t=>{const e={};let i;for(;(i=Q.exec(t))!==null;){const r=i[1].toLowerCase(),h=i[2];switch(r){case"width":case"w":{const o=parseInt(h,10);!isNaN(o)&&o>0&&(e.width=o);break}case"height":case"h":{const o=parseInt(h,10);!isNaN(o)&&o>0&&(e.height=o);break}case"align":{const o=h.toLowerCase();R.includes(o)&&(e.align=o);break}}}return e},M=t=>t.replace(U,(e,i,r,h,o)=>{const l=Z(o),a=V(l);return`${i}${r}${a}${h}`}),K=t=>{const e=t.indexOf(`#${P}`);if(e===-1)return{cleanHref:t,attrs:{}};const i=t.substring(0,e),r=t.substring(e+1+P.length),h={},o=r.split("&");for(const l of o){const[a,s]=l.split("=");switch(a){case"w":{const g=parseInt(s,10);!isNaN(g)&&g>0&&(h.width=g);break}case"h":{const g=parseInt(s,10);!isNaN(g)&&g>0&&(h.height=g);break}case"a":{R.includes(s)&&(h.align=s);break}}}return{cleanHref:i,attrs:h}},G=async t=>{const e=M(t),i=await I.marked.lexer(e,{async:!0,gfm:!0});return w(i)},w=t=>{const e=[];return t.forEach(i=>{try{const r=tt[i.type];r?e.push(r(i)):e.push({type:u.Raw,content:i.raw})}catch(r){console.error("Failed to handle token ==>",i,r)}}),e},tt={[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(i=>({type:u.TableCell,content:i.text})))}),[u.Image]:t=>{const{cleanHref:e,attrs:i}=K(t.href);return{type:u.Image,src:e,alt:t.text,width:i.width,height:i.height,align:i.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):[]})},T=class T{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,i="set"){i==="set"?this.cursor.x=e:i==="add"&&(this.cursor.x+=e)}static updateY(e,i="set"){i==="set"?this.cursor.y=e:i==="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}};T.cursor={x:0,y:0},T.lastContentY_=0,T.inlineLock=!1;let n=T;const C=t=>t.getTextDimensions("H").h*n.options.page.defaultLineHeightFactor,H=t=>t.getTextDimensions("H").w*n.options.page.defaultLineHeightFactor,et=(t,e,i,r)=>{const h=6-(e?.depth??0)>0?6-(e?.depth??0):1;if(t.setFontSize(n.options.page.defaultFontSize+h),e?.items&&e?.items.length>0)for(const o of e?.items??[])r(o,i,!1);else{const o=C(t);t.text(e?.content??"",n.X+i,n.Y,{align:"left",maxWidth:n.options.page.maxContentWidth-i,baseline:"top"}),n.recordContentY(n.Y+o),n.updateY(o,"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)},z=96,L=(t,e="mm")=>{switch(e){case"pt":return t*72/z;case"in":return t/z;case"px":return t;default:return t*25.4/z}},_=t=>{try{let e="";if(t.includes("base64,")){const a=t.split("base64,")[1];typeof window<"u"&&typeof window.atob=="function"?e=decodeURIComponent(escape(window.atob(a))):typeof Buffer<"u"?e=Buffer.from(a,"base64").toString("utf-8"):e=decodeURIComponent(escape(atob(a)))}else e=decodeURIComponent(t.split(",")[1]||"");const i=e.match(/<svg[^>]*\swidth=(?:'|")([0-9.]+)[a-zA-Z]*(?:'|")/i),r=e.match(/<svg[^>]*\sheight=(?:'|")([0-9.]+)[a-zA-Z]*(?:'|")/i),h=e.match(/<svg[^>]*\sviewBox=(?:'|")[^'"]*(?:'|")/i);let o=i?parseFloat(i[1]):0,l=r?parseFloat(r[1]):0;if((!o||!l)&&h){const a=h[0].match(/viewBox=(?:'|")([^'"]+)(?:'|")/i);if(a){const s=a[1].split(/[ ,]+/).filter(Boolean).map(parseFloat);s.length>=4&&(o=o||s[2],l=l||s[3])}}if(o>0&&l>0)return{width:o,height:l}}catch(e){console.warn("Failed to extract SVG dimensions:",e)}return null},j=(t,e,i,r,h="mm")=>{if(!e.data)return{finalWidth:0,finalHeight:0};let o=e.naturalWidth||0,l=e.naturalHeight||0;if(!o||!l)if(e.data.startsWith("data:image/svg")){const c=_(e.data);c&&(o=c.width,l=c.height)}else try{const c=t.getImageProperties(e.data);o=c.width,l=c.height}catch(c){console.warn("Failed to get image properties for intrinsic sizing:",c)}const a=l>0?o/l:1;let s,g;if(e.width&&e.height?(s=L(e.width,h),g=L(e.height,h)):e.width?(s=L(e.width,h),g=s/a):e.height?(g=L(e.height,h),s=g*a):(s=L(o,h),g=L(l,h)),s>i){const c=i/s;s=i,g=g*c}if(g>r){const c=r/g;g=r,s=s*c}return{finalWidth:s,finalHeight:g}},$=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 i=await fetch(e.src);if(!i.ok)throw new Error(`Failed to fetch image: ${i.statusText}`);const r=await i.blob(),h=await new Promise((o,l)=>{const a=new FileReader;a.onloadend=()=>{typeof a.result=="string"?o(a.result):l(new Error("Failed to convert image to base64 string"))},a.onerror=l,a.readAsDataURL(r)});e.data=h}e.data&&e.data.startsWith("data:image/svg")&&typeof window<"u"&&typeof document<"u"&&(e.data=await new Promise(i=>{const r=new Image;r.onload=()=>{const h=document.createElement("canvas"),o=_(e.data),l=o?o.width:r.width||300,a=o?o.height:r.height||150;e.naturalWidth=l,e.naturalHeight=a;const s=4;h.width=l*s,h.height=a*s;const g=h.getContext("2d");g?(g.scale(s,s),g.drawImage(r,0,0,l,a),i(h.toDataURL("image/png"))):i(e.data)},r.onerror=()=>i(e.data),r.src=e.data}))}catch(i){console.warn(`[jspdf-md-renderer] Warning: Failed to load image at ${e.src}. It will be skipped.`,i)}e.items&&e.items.length>0&&await $(e.items)}};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,i){const r=e.getFont().fontName,h=e.getFontSize(),o=()=>{const a=n.options.font.bold?.name;return a&&a!==""?a:r},l=()=>{const a=n.options.font.regular?.name;return a&&a!==""?a:r};switch(i){case"bold":e.setFont(o(),n.options.font.bold?.style||"bold");break;case"italic":e.setFont(l(),"italic");break;case"bolditalic":e.setFont(o(),"bolditalic");break;case"codespan":e.setFont("courier","normal"),e.setFontSize(h*this.getCodespanOptions().fontSizeScale);break;default:e.setFont(l(),e.getFont().fontStyle);break}}static measureWordWidth(e,i,r){const h=e.getFont(),o=e.getFontSize();this.applyStyle(e,r);const l=e.getTextWidth(i),a=e.getCharSpace?.()??0,s=l+i.length*a;return e.setFont(h.fontName,h.fontStyle),e.setFontSize(o),s}static getStyleFromType(e,i){switch(e){case"strong":return i==="italic"?"bolditalic":"bold";case"em":return i==="bold"?"bolditalic":"italic";case"codespan":return"codespan";default:return i||"normal"}}static flattenToWords(e,i,r="normal",h=!1,o){const l=[];for(const a of i){const s=this.getStyleFromType(a.type,r),g=a.type==="link"||h,c=a.href||o;if(a.items&&a.items.length>0){const f=this.flattenToWords(e,a.items,s,g,c);l.push(...f)}else if(a.type==="image"){const f=n.options.page.maxContentHeight-n.options.page.topmargin,d=n.options.page.maxContentWidth-n.options.page.indent*0,p=n.options.page.unit||"mm",{finalWidth:m,finalHeight:x}=j(e,a,d,f,p);l.push({text:"",width:m,style:s,isLink:g,href:c,linkColor:g?n.options.link?.linkColor||[0,0,255]:void 0,isImage:!0,imageElement:a,imageHeight:x})}else{const f=a.content||a.text||"";if(!f)continue;if(s==="codespan"){const p=f.trim();p&&l.push({text:p,width:this.measureWordWidth(e,p,s),style:s,isLink:g,href:c,linkColor:g?n.options.link?.linkColor||[0,0,255]:void 0});continue}const d=f.split(/\s+/).filter(p=>p.length>0);if(d.length===0)continue;for(let p=0;p<d.length;p++){const m=d[p];l.push({text:m,width:this.measureWordWidth(e,m,s),style:s,isLink:g,href:c,linkColor:g?n.options.link?.linkColor||[0,0,255]:void 0})}}}return l}static breakIntoLines(e,i,r){const h=[];let o=[],l=0,a=0,s=C(e)*n.options.page.defaultLineHeightFactor;const g=e.getTextWidth(" ");for(let c=0;c<i.length;c++){const f=i[c],d=o.length>0?g+f.width:f.width,p=f.isImage&&f.imageHeight?f.imageHeight:C(e)*n.options.page.defaultLineHeightFactor;a+d>r&&o.length>0?(h.push({words:o,totalTextWidth:l,isLastLine:!1,lineHeight:s}),o=[f],l=f.width,a=f.width,s=p):(o.push(f),l+=f.width,a+=d,s=Math.max(s,p))}return o.length>0&&h.push({words:o,totalTextWidth:l,isLastLine:!0,lineHeight:s}),h}static renderWord(e,i,r,h){const o=e.getFont(),l=e.getFontSize(),a=e.getTextColor();if(this.applyStyle(e,i.style),i.isLink&&i.linkColor&&e.setTextColor(...i.linkColor),i.isImage&&i.imageElement&&i.imageElement.data)try{let s="JPEG";if(i.imageElement.data.startsWith("data:image/png"))s="PNG";else if(i.imageElement.data.startsWith("data:image/webp"))s="WEBP";else if(i.imageElement.data.startsWith("data:image/gif"))s="GIF";else if(i.imageElement.src){const c=i.imageElement.src.split("?")[0].split("#")[0].split(".").pop()?.toUpperCase();c&&["PNG","JPEG","JPG","WEBP","GIF"].includes(c)&&(s=c==="JPG"?"JPEG":c)}if(i.width>0&&(i.imageHeight||0)>0){const g=i.imageHeight||0,c=h-g;e.addImage(i.imageElement.data,s,r,c,i.width,g)}}catch(s){console.warn("Failed to render inline image",s)}else{if(i.style==="codespan"){const s=this.getCodespanOptions();if(s.showBackground){const g=C(e),c=s.padding;e.setFillColor(s.backgroundColor),e.rect(r-c,h-c,i.width+c*2,g+c*2,"F"),e.setFillColor("#000000")}}e.text(i.text,r,h,{baseline:"top"})}if(i.isLink&&i.href){const s=i.isImage&&i.imageHeight?i.imageHeight:C(e);e.link(r,h,i.width,s,{url:i.href})}e.setFont(o.fontName,o.fontStyle),e.setFontSize(l),e.setTextColor(a)}static renderAlignedLine(e,i,r,h,o,l="left"){const{words:a,totalTextWidth:s,isLastLine:g}=i;if(a.length===0)return;const c=e.getTextWidth(" ");let f=r,d=c;const p=s+(a.length-1)*c;switch(l){case"right":f=r+o-p;break;case"center":f=r+(o-p)/2;break;case"justify":!g&&a.length>1&&(d=(o-s)/(a.length-1));break}let m=f;const x=C(e)*n.options.page.defaultLineHeightFactor;for(let F=0;F<a.length;F++){const b=a[F];let Y=h;const W=b.isImage&&b.imageHeight?b.imageHeight:x;W<i.lineHeight&&(Y=h+(i.lineHeight-W)),this.renderWord(e,b,m,Y),m+=b.width,F<a.length-1&&(m+=d)}}static renderStyledParagraph(e,i,r,h,o,l){const a=l??n.options.content?.textAlignment??"left",s=this.flattenToWords(e,i);if(s.length===0)return;const g=this.breakIntoLines(e,s,o);let c=h;for(const d of g)c+d.lineHeight>n.options.page.maxContentHeight&&(k(e),c=n.Y),this.renderAlignedLine(e,d,r,c,o,a),n.recordContentY(c+d.lineHeight),c+=d.lineHeight,n.updateY(d.lineHeight,"add");const f=g[g.length-1];if(f){const d=f.totalTextWidth+(f.words.length-1)*e.getTextWidth(" ");n.updateX(r+d,"set")}}static renderJustifiedParagraph(e,i,r,h,o){this.renderStyledParagraph(e,i,r,h,o)}}class X{static renderText(e,i,r=n.X,h=n.Y,o,l=!1){const a=e.splitTextToSize(i,o),s=C(e),g=s*n.options.page.defaultLineHeightFactor;let c=h;for(let f=0;f<a.length;f++){const d=a[f];c+g>n.options.page.maxContentHeight&&(k(e),c=n.Y),l?f===a.length-1?e.text(d,r,c,{baseline:"top"}):e.text(d,r,c,{maxWidth:o,align:"justify",baseline:"top"}):e.text(d,r,c,{baseline:"top"}),n.recordContentY(c+s),c+=g,n.updateY(g,"add")}return c}}const nt=(t,e,i,r)=>{n.activateInlineLock(),t.setFontSize(n.options.page.defaultFontSize);const h=n.options.page.maxContentWidth-i;if(e?.items&&e?.items.length>0){if(e.items.length===1&&e.items[0].type==="image"){r(e.items[0],i,!1),n.updateX(n.options.page.xpading),n.deactivateInlineLock();return}const o=["strong","em","text","codespan","link","image"];if(e.items.some(a=>!o.includes(a.type))){const a=[],s=()=>{a.length>0&&(v.renderStyledParagraph(t,a,n.X+i,n.Y,h),a.length=0)};for(const g of e.items)o.includes(g.type)?a.push(g):(s(),r(g,i,!1));s()}else v.renderStyledParagraph(t,e.items,n.X+i,n.Y,h)}else{const o=e.content??"",l=n.options.content?.textAlignment??"left";o.trim()&&X.renderText(t,o,n.X+i,n.Y,h,l==="justify")}n.updateX(n.options.page.xpading),n.deactivateInlineLock()},it=(t,e,i,r)=>{t.setFontSize(n.options.page.defaultFontSize);for(const[h,o]of e?.items?.entries()??[]){const l=e.ordered?(e.start??0)+h:e.start;r(o,i+1,!0,l,e.ordered)}},st=(t,e,i,r,h,o)=>{n.Y+C(t)>=n.options.page.maxContentHeight&&k(t);const l=n.options,a=i*l.page.indent,s=o?`${h}. `:"• ",g=l.page.xpading;n.updateX(g,"set"),t.setFont(l.font.regular.name,l.font.regular.style),t.text(s,g+a,n.Y,{baseline:"top"});const c=t.getTextWidth(s),f=g+a+c,d=l.page.maxContentWidth-a-c;if(e.items&&e.items.length>0){const p=[],m=()=>{p.length>0&&(v.renderStyledParagraph(t,p,f,n.Y,d),p.length=0,n.updateX(g,"set"))};for(const x of e.items)x.type===u.List?(m(),r(x,i,!0,h,x.ordered??!1)):x.type===u.ListItem?(m(),r(x,i,!0,h,o)):p.push(x);m()}else if(e.content){const p=l.content?.textAlignment??"left";X.renderText(t,e.content,f,n.Y,d,p==="justify")}},at=(t,e,i,r,h,o,l,a=!0)=>{if(e?.items&&e?.items.length>0)for(const s of e?.items??[])h(s,i,r,o,l,a);else{const s=n.options,g=i*s.page.indent,c=r?l?`${o}. `:"• ":"",f=e.content||"",d=s.page.xpading;if(!f&&!c)return;if(!f.trim()&&!c){const p=(f.match(/\n/g)||[]).length;if(p>1){const m=p-1,x=t.getTextDimensions("A").h*s.page.defaultLineHeightFactor,F=m*x;n.Y+F>s.page.maxContentHeight?k(t):(n.updateY(F,"add"),n.recordContentY(n.Y))}return}if(n.updateX(d,"set"),r&&c){const p=t.getTextWidth(c),m=s.page.maxContentWidth-g-p;t.setFont(s.font.regular.name,s.font.regular.style),t.text(c,d+g,n.Y,{baseline:"top"}),X.renderText(t,f,d+g+p,n.Y,m,a)}else{const p=s.page.maxContentWidth-g;X.renderText(t,f,d+g,n.Y,p,a)}n.updateX(d,"set")}},ot=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")},rt=(t,e,i,r)=>{const h=t.getFont(),o=t.getFontSize();t.setFont("courier","normal");const l=n.options.page.defaultFontSize*.9;t.setFontSize(l);const a=i*n.options.page.indent,s=n.options.page.maxContentWidth-a-8,g=t.getLineHeightFactor(),c=l/t.internal.scaleFactor*g,d=(e.code??"").replace(/[\r\n\s]+$/,"");if(!d){t.setFont(h.fontName,h.fontStyle),t.setFontSize(o);return}const p=t.splitTextToSize(d,s);for(;p.length>0&&p[p.length-1].trim()==="";)p.pop();if(p.length===0){t.setFont(h.fontName,h.fontStyle),t.setFontSize(o);return}const m=4,x="#EEEEEE",F="#DDDDDD";let b=0;for(;b<p.length;){const Y=n.options.page.maxContentHeight-n.Y,W=p.length-b,E=Y-m*2;let y=Math.floor(E/c);if(y<=0){k(t);continue}y>W&&(y=W);const mt=p.slice(b,b+y),A=b===0,B=b+y>=p.length,J=y*c;if(A&&n.updateY(m,"add"),t.setFillColor(x),t.setDrawColor(F),t.roundedRect(n.X,n.Y-m,n.options.page.maxContentWidth,J+(A?m:0)+(B?m:0),2,2,"FD"),A&&e.lang){const N=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(N),t.setTextColor("#000000")}let O=n.Y;for(const N of mt)t.text(N,n.X+4,O,{baseline:"top"}),O+=c;n.updateY(J,"add"),n.recordContentY(n.Y+(B?m:0)),B&&n.updateY(m,"add"),b+=y,b<p.length&&k(t)}t.setFont(h.fontName,h.fontStyle),t.setFontSize(o)},lt=(t,e,i)=>{const r=t.getFont().fontName,h=t.getFont().fontStyle,o=t.getFontSize(),l=s=>{switch(s){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}},a=(s,g)=>{g==="bold"?t.setFont(n.options.font.bold.name&&n.options.font.bold.name!==""?n.options.font.bold.name:r,n.options.font.bold.style||"bold"):g==="italic"?t.setFont(n.options.font.regular.name,"italic"):g==="bolditalic"?t.setFont(n.options.font.bold.name&&n.options.font.bold.name!==""?n.options.font.bold.name:r,"bolditalic"):g==="codespan"?(t.setFont("courier","normal"),t.setFontSize(o*.9)):t.setFont(n.options.font.regular.name,h);const c=n.options.page.maxContentWidth-i-n.X,f=t.splitTextToSize(s,c),d=g==="codespan",p=1,m="#EEEEEE";if(n.isInlineLockActive)for(let x=0;x<f.length;x++){if(d){const F=t.getTextWidth(f[x])+H(t),b=C(t);t.setFillColor(m),t.roundedRect(n.X+i-p,n.Y-p,F+p*2,b+p*2,2,2,"F"),t.setFillColor("#000000")}t.text(f[x],n.X+i,n.Y,{baseline:"top",maxWidth:c}),n.updateX(t.getTextDimensions(f[x]).w+(d?p*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)+H(t),E=C(t);t.setFillColor(m),t.roundedRect(n.X+(i>=2?i+2:0)-p,n.Y-p,W+p*2,E+p*2,2,2,"F"),t.setFillColor("#000000")}t.text(x,n.X+(i>=2?i+2*l(g):0),n.Y,{baseline:"top",maxWidth:c}),n.updateX(n.options.page.xpading+i),n.updateY(C(t),"add");const b=n.options.page.maxContentWidth-i-n.options.page.xpading;t.splitTextToSize(F,b).forEach(W=>{if(d){const E=t.getTextWidth(W)+H(t),y=C(t);t.setFillColor(m),t.roundedRect(n.X+H(t)-p,n.Y-p,E+p*2,y+p*2,2,2,"F"),t.setFillColor("#000000")}t.text(W,n.X+H(t),n.Y,{baseline:"top",maxWidth:b})})}else{if(d){const x=t.getTextWidth(s)+H(t),F=C(t);t.setFillColor(m),t.roundedRect(n.X+i-p,n.Y-p,x+p*2,F+p*2,2,2,"F"),t.setFillColor("#000000")}t.text(s,n.X+i,n.Y,{baseline:"top",maxWidth:c}),n.updateX(t.getTextDimensions(s).w+(i>=2?s.split(" ").length+2:2)*l(g)*.5+(d?p*2:0),"add")}};if(e.type==="text"&&e.items&&e.items.length>0)for(const s of e.items)if(s.type==="codespan")a(s.content||"","codespan");else if(s.type==="em"||s.type==="strong"){const g=s.type==="em"?"italic":"bold";if(s.items&&s.items.length>0)for(const c of s.items)c.type==="strong"&&g==="italic"||c.type==="em"&&g==="bold"?a(c.content||"","bolditalic"):a(c.content||"",g);else a(s.content||"",g)}else a(s.content||"","normal");else e.type==="em"?a(e.content||"","italic"):e.type==="strong"?a(e.content||"","bold"):e.type==="codespan"?a(e.content||"","codespan"):a(e.content||"","normal");t.setFont(r,h),t.setFontSize(o)},gt=(t,e,i)=>{const r=t.getFont().fontName,h=t.getFont().fontStyle,o=t.getFontSize(),l=t.getTextColor(),a=n.options.link?.linkColor||[0,0,255];t.setTextColor(...a);const s=n.options.page.maxContentWidth-i-n.X,g=e.text||e.content||"",c=e.href||"",f=t.splitTextToSize(g,s);if(n.isInlineLockActive)for(let d=0;d<f.length;d++){const p=t.getTextDimensions(f[d]).w,m=C(t)/2;t.link(n.X+i,n.Y,p,m,{url:c}),t.text(f[d],n.X+i,n.Y,{baseline:"top",maxWidth:s}),n.updateX(p+1,"add"),n.X+p>n.options.page.maxContentWidth-i&&(n.updateY(m,"add"),n.updateX(n.options.page.xpading+i,"set")),d<f.length-1&&(n.updateY(m,"add"),n.updateX(n.options.page.xpading+i,"set"))}else if(f.length>1){const d=f[0],p=f?.slice(1)?.join(" "),m=t.getTextDimensions(d).w,x=C(t)/2;t.link(n.X+i,n.Y,m,x,{url:c}),t.text(d,n.X+i,n.Y,{baseline:"top",maxWidth:s}),n.updateX(n.options.page.xpading+i),n.updateY(x,"add");const F=n.options.page.maxContentWidth-i-n.options.page.xpading;t.splitTextToSize(p,F).forEach(Y=>{const W=t.getTextDimensions(Y).w;t.link(n.X+H(t),n.Y,W,x,{url:c}),t.text(Y,n.X+H(t),n.Y,{baseline:"top",maxWidth:F})})}else{const d=t.getTextDimensions(g).w,p=C(t)/2;t.link(n.X+i,n.Y,d,p,{url:c}),t.text(g,n.X+i,n.Y,{baseline:"top",maxWidth:s}),n.updateX(d+2,"add")}t.setFont(r,h),t.setFontSize(o),t.setTextColor(l)},ct=(t,e,i,r)=>{const h=n.options,o=i+1,l=n.X+i*h.page.indent,a=n.Y,s=l+h.page.indent/2,g=a,c=t.internal.getCurrentPageInfo().pageNumber;e.items&&e.items.length>0&&e.items.forEach(p=>{r(p,o)});const f=n.Y,d=t.internal.getCurrentPageInfo().pageNumber;t.setDrawColor(100),t.setLineWidth(1);for(let p=c;p<=d;p++){t.setPage(p);const m=p===c,x=p===d,F=m?g:h.page.topmargin,b=x?f:h.page.maxContentHeight;t.line(s,F,s,b)}n.recordContentY(),t.setPage(d)},ht=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")||t.data.startsWith("data:image/webp"))return"WEBP";if(t.data.startsWith("data:image/gif"))return"GIF"}if(t.src){const i=t.src.split("?")[0].split("#")[0].split(".").pop()?.toUpperCase();if(i&&["PNG","JPEG","JPG","WEBP","GIF"].includes(i))return i==="JPG"?"JPEG":i}return"JPEG"},pt=(t,e,i)=>{if(!e.data)return;const r=n.options,h=r.page.unit||"mm",o=i*r.page.indent,l=r.page.maxContentWidth-o,a=n.X+o;let s=n.Y;try{const g=r.page.maxContentHeight-r.page.topmargin,{finalWidth:c,finalHeight:f}=j(t,e,l,g,h);s+f>r.page.maxContentHeight&&(k(t),s=n.Y);const d=e.align||r.image?.defaultAlign||"left";let p;switch(d){case"right":p=a+l-c;break;case"center":p=a+(l-c)/2;break;default:p=a;break}const m=ht(e);c>0&&f>0&&t.addImage(e.data,m,p,s,c,f),n.updateY(f,"add"),n.recordContentY()}catch(g){console.warn("Failed to render image",g)}},ft=(t,e,i)=>{if(!e.header||!e.rows)return;const r=n.options,h=r.page.xmargin+i*r.page.indent,o=[e.header.map(s=>s.content||"")],l=e.rows.map(s=>s.map(g=>g.content||"")),a=r.table||{};q(t,{head:o,body:l,startY:n.Y,margin:{left:h,right:r.page.xmargin},...a,didDrawPage:s=>{a.didDrawPage&&a.didDrawPage(s)},didDrawCell:s=>{a.didDrawCell&&a.didDrawCell(s),n.setCursor({x:n.X,y:s.cell.y+s.cell.height+2*r.page.lineSpace})}})},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"}},dt=t=>{if(!t)throw new Error("RenderOption is required");const e={...D.page,...t.page},i={...D.font,...t.font},r={...D.image,...t.image};return e.maxContentWidth||(e.maxContentWidth=190),e.maxContentHeight||(e.maxContentHeight=277),{...t,page:e,font:i,image:r}},ut=async(t,e,i)=>{const r=dt(i);n.initialize(r);const h=await G(e);await $(h);const o=(l,a=0,s=!1,g=0,c=!1,f=!0)=>{const d=a*r.page.indent;switch(l.type){case u.Heading:et(t,l,d,o);break;case u.Paragraph:nt(t,l,d,o);break;case u.List:it(t,l,a,o);break;case u.ListItem:st(t,l,a,o,g,c);break;case u.Hr:ot(t);break;case u.Code:rt(t,l,a);break;case u.Strong:case u.Em:case u.CodeSpan:lt(t,l,d);break;case u.Link:gt(t,l,d);break;case u.Blockquote:ct(t,l,a,o);break;case u.Image:pt(t,l,a);break;case u.Table:ft(t,l,a);break;case u.Raw:case u.Text:at(t,l,a,s,o,g,c,r.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 l of g)r(l);c.endCursorYHandler(n.Y)};S.MdTextParser=_,S.MdTextRender=ft,Object.defineProperty(S,Symbol.toStringTag,{value:"Module"})}));
4
+ with details of the element and expected behavior. Thanks for helping to improve this library!`);break}};for(const l of h)o(l);r.endCursorYHandler(n.Y)};S.MdTextParser=G,S.MdTextRender=ut,Object.defineProperty(S,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jspdf-md-renderer",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "A jsPDF utility to render Markdown directly into formatted PDFs with custom designs.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -43,7 +43,7 @@
43
43
  "bugs": {
44
44
  "url": "https://github.com/JeelGajera/jspdf-md-renderer/issues"
45
45
  },
46
- "homepage": "https://github.com/JeelGajera/jspdf-md-renderer#readme",
46
+ "homepage": "https://jeelgajera.github.io/jspdf-md-renderer/",
47
47
  "dependencies": {
48
48
  "jspdf": "^4.2.0",
49
49
  "jspdf-autotable": "^5.0.2",