jspdf-md-renderer 3.3.1 → 3.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +5 -2
- package/dist/index.js +2 -2
- package/dist/index.mjs +943 -1337
- package/dist/index.umd.js +2 -2
- package/package.json +7 -4
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -264,8 +264,11 @@ The following Markdown elements are currently supported by `jspdf-md-renderer`:
|
|
|
264
264
|
|
|
265
265
|
## Examples
|
|
266
266
|
|
|
267
|
-
|
|
268
|
-
|
|
267
|
+
You can explore complete rendered examples in the documentation site:
|
|
268
|
+
- [Resume example](https://jeelgajera.github.io/jspdf-md-renderer/examples/resume)
|
|
269
|
+
- [Invoice example](https://jeelgajera.github.io/jspdf-md-renderer/examples/invoice)
|
|
270
|
+
- [Technical report example](https://jeelgajera.github.io/jspdf-md-renderer/examples/report)
|
|
271
|
+
- [Custom fonts guide](https://jeelgajera.github.io/jspdf-md-renderer/examples/custom-fonts)
|
|
269
272
|
|
|
270
273
|
## Contributing
|
|
271
274
|
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const q=require("marked"),z=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_",U=/(!\[[^\]]*\]\()([^)]+)(\))\s*\{([^}]+)\}/g,Q=/(\w+)\s*=\s*(\w+)/g,G=["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?`#${A}${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();G.includes(o)&&(e.align=o);break}}}return e},K=t=>t.replace(U,(e,i,r,h,o)=>{const l=Z(o),a=V(l);return`${i}${r}${a}${h}`}),M=t=>{const e=t.indexOf(`#${A}`);if(e===-1)return{cleanHref:t,attrs:{}};const i=t.substring(0,e),r=t.substring(e+1+A.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":{G.includes(s)&&(h.align=s);break}}}return{cleanHref:i,attrs:h}},_=async t=>{const e=K(t),i=await q.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}=M(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):[]})},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,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}};I.cursor={x:0,y:0},I.lastContentY_=0,I.inlineLock=!1;let n=I;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")},Y=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)},v=96,k=(t,e="mm")=>{switch(e){case"pt":return t*72/v;case"in":return t/v;case"px":return t;default:return t*25.4/v}},$=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=k(e.width,h),g=k(e.height,h)):e.width?(s=k(e.width,h),g=s/a):e.height?(g=k(e.height,h),s=g*a):(s=k(o,h),g=k(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}},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 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 j(e.items)}};class B{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&&(Y(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 T{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&&(Y(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&&(B.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 B.renderStyledParagraph(t,e.items,n.X+i,n.Y,h)}else{const o=e.content??"",l=n.options.content?.textAlignment??"left";o.trim()&&T.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&&Y(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&&(B.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";T.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?Y(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"}),T.renderText(t,f,d+g+p,n.Y,m,a)}else{const p=s.page.maxContentWidth-g;T.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,L=y-m*2;let S=Math.floor(L/c);if(S<=0){Y(t);continue}S>w&&(S=w);const O=p.slice(b,b+S),E=b===0,X=b+S>=p.length,N=S*c;if(E&&n.updateY(m,"add"),t.setFillColor(x),t.setDrawColor(F),t.roundedRect(n.X,n.Y-m,n.options.page.maxContentWidth,N+(E?m:0)+(X?m:0),2,2,"FD"),E&&e.lang){const P=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(P),t.setTextColor("#000000")}let R=n.Y;for(const P of O)t.text(P,n.X+4,R,{baseline:"top"}),R+=c;n.updateY(N,"add"),n.recordContentY(n.Y+(X?m:0)),X&&n.updateY(m,"add"),b+=S,b<p.length&&Y(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),L=C(t);t.setFillColor(m),t.roundedRect(n.X+(i>=2?i+2:0)-p,n.Y-p,w+p*2,L+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 L=t.getTextWidth(w)+H(t),S=C(t);t.setFillColor(m),t.roundedRect(n.X+H(t)-p,n.Y-p,L+p*2,S+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&&(Y(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=()=>{const t=z;if(typeof z=="function")return z;if(typeof t.default=="function")return t.default;if(typeof t.autoTable=="function")return t.autoTable;throw new Error("Could not resolve jspdf-autotable export. Expected a callable export.")},dt=(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||{};ft()(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"}},ut=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}},mt=async(t,e,i)=>{const r=ut(i);n.initialize(r);const h=await _(e);await j(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:dt(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}.
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`marked`),l=require(`jspdf-autotable`);l=s(l);var u=function(e){return e.Heading=`heading`,e.Paragraph=`paragraph`,e.List=`list`,e.ListItem=`list_item`,e.Blockquote=`blockquote`,e.Code=`code`,e.CodeSpan=`codespan`,e.Table=`table`,e.Html=`html`,e.Hr=`hr`,e.Image=`image`,e.Link=`link`,e.Strong=`strong`,e.Em=`em`,e.TableHeader=`table_header`,e.TableCell=`table_cell`,e.Raw=`raw`,e.Text=`text`,e}({}),d=`__jmr_`,f=/(!\[[^\]]*\]\()([^)]+)(\))\s*\{([^}]+)\}/g,p=/(\w+)\s*=\s*(\w+)/g,m=[`left`,`center`,`right`],h=e=>{let t=[];return e.width!==void 0&&t.push(`w=${e.width}`),e.height!==void 0&&t.push(`h=${e.height}`),e.align&&t.push(`a=${e.align}`),t.length>0?`#${d}${t.join(`&`)}`:``},g=e=>{let t={},n;for(;(n=p.exec(e))!==null;){let e=n[1].toLowerCase(),r=n[2];switch(e){case`width`:case`w`:{let e=parseInt(r,10);!isNaN(e)&&e>0&&(t.width=e);break}case`height`:case`h`:{let e=parseInt(r,10);!isNaN(e)&&e>0&&(t.height=e);break}case`align`:{let e=r.toLowerCase();m.includes(e)&&(t.align=e);break}}}return t},_=e=>e.replace(f,(e,t,n,r,i)=>`${t}${n}${h(g(i))}${r}`),v=e=>{let t=e.indexOf(`#${d}`);if(t===-1)return{cleanHref:e,attrs:{}};let n=e.substring(0,t),r=e.substring(t+1+6),i={},a=r.split(`&`);for(let e of a){let[t,n]=e.split(`=`);switch(t){case`w`:{let e=parseInt(n,10);!isNaN(e)&&e>0&&(i.width=e);break}case`h`:{let e=parseInt(n,10);!isNaN(e)&&e>0&&(i.height=e);break}case`a`:m.includes(n)&&(i.align=n);break}}return{cleanHref:n,attrs:i}},y=async e=>{let t=_(e);return b(await c.marked.lexer(t,{async:!0,gfm:!0}))},b=e=>{let t=[];return e.forEach(e=>{try{let n=x[e.type];n?t.push(n(e)):t.push({type:u.Raw,content:e.raw})}catch(t){console.error(`Failed to handle token ==>`,e,t)}}),t},x={[u.Heading]:e=>({type:u.Heading,depth:e.depth,content:e.text,items:e.tokens?b(e.tokens):[]}),[u.Paragraph]:e=>({type:u.Paragraph,content:e.text,items:e.tokens?b(e.tokens):[]}),[u.List]:e=>({type:u.List,ordered:e.ordered,start:e.start,items:e.items?b(e.items):[]}),[u.ListItem]:e=>({type:u.ListItem,content:e.text,items:e.tokens?b(e.tokens):[]}),[u.Code]:e=>({type:u.Code,lang:e.lang,code:e.text}),[u.Table]:e=>({type:u.Table,header:e.header.map(e=>({type:u.TableHeader,content:e.text})),rows:e.rows.map(e=>e.map(e=>({type:u.TableCell,content:e.text})))}),[u.Image]:e=>{let{cleanHref:t,attrs:n}=v(e.href);return{type:u.Image,src:t,alt:e.text,width:n.width,height:n.height,align:n.align}},[u.Link]:e=>({type:u.Link,href:e.href,text:e.text,items:e.tokens?b(e.tokens):[]}),[u.Strong]:e=>({type:u.Strong,content:e.text,items:e.tokens?b(e.tokens):[]}),[u.Em]:e=>({type:u.Em,content:e.text,items:e.tokens?b(e.tokens):[]}),[u.Text]:e=>({type:u.Text,content:e.text,items:e.tokens?b(e.tokens):[]}),[u.Hr]:e=>({type:u.Hr,content:e.raw,items:e.tokens?b(e.tokens):[]}),[u.CodeSpan]:e=>({type:u.CodeSpan,content:e.text,items:e.tokens?b(e.tokens):[]}),[u.Blockquote]:e=>({type:u.Blockquote,content:e.text,items:e.tokens?b(e.tokens):[]})},S=class{static{this.cursor={x:0,y:0}}static{this.lastContentY_=0}static{this.inlineLock=!1}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,t=`set`){t===`set`?this.cursor.x=e:t===`add`&&(this.cursor.x+=e)}static updateY(e,t=`set`){t===`set`?this.cursor.y=e:t===`add`&&(this.cursor.y+=e)}static recordContentY(e){this.lastContentY_=e===void 0?this.cursor.y:e}static get lastContentY(){return this.lastContentY_}static get X(){return this.cursor.x}static get Y(){return this.cursor.y}},C=e=>e.getTextDimensions(`H`).h*S.options.page.defaultLineHeightFactor,w=e=>e.getTextDimensions(`H`).w*S.options.page.defaultLineHeightFactor,T=(e,t,n,r)=>{let i=6-(t?.depth??0)>0?6-(t?.depth??0):1;if(e.setFontSize(S.options.page.defaultFontSize+i),t?.items&&t?.items.length>0)for(let e of t?.items??[])r(e,n,!1);else{let r=C(e);e.text(t?.content??``,S.X+n,S.Y,{align:`left`,maxWidth:S.options.page.maxContentWidth-n,baseline:`top`}),S.recordContentY(S.Y+r),S.updateY(r,`add`)}e.setFontSize(S.options.page.defaultFontSize),S.updateX(S.options.page.xpading,`set`)},E=e=>{typeof S.options.pageBreakHandler==`function`?S.options.pageBreakHandler(e):e.addPage(S.options.page?.format,S.options.page?.orientation),S.updateY(S.options.page.topmargin),S.updateX(S.options.page.xpading)},D=96,O=(e,t=`mm`)=>{switch(t){case`pt`:return e*72/D;case`in`:return e/D;case`px`:return e;default:return e*25.4/D}},k=e=>{try{let t=``;if(e.includes(`base64,`)){let n=e.split(`base64,`)[1];t=typeof window<`u`&&typeof window.atob==`function`?decodeURIComponent(escape(window.atob(n))):typeof Buffer<`u`?Buffer.from(n,`base64`).toString(`utf-8`):decodeURIComponent(escape(atob(n)))}else t=decodeURIComponent(e.split(`,`)[1]||``);let n=t.match(/<svg[^>]*\swidth=(?:'|")([0-9.]+)[a-zA-Z]*(?:'|")/i),r=t.match(/<svg[^>]*\sheight=(?:'|")([0-9.]+)[a-zA-Z]*(?:'|")/i),i=t.match(/<svg[^>]*\sviewBox=(?:'|")[^'"]*(?:'|")/i),a=n?parseFloat(n[1]):0,o=r?parseFloat(r[1]):0;if((!a||!o)&&i){let e=i[0].match(/viewBox=(?:'|")([^'"]+)(?:'|")/i);if(e){let t=e[1].split(/[ ,]+/).filter(Boolean).map(parseFloat);t.length>=4&&(a||=t[2],o||=t[3])}}if(a>0&&o>0)return{width:a,height:o}}catch(e){console.warn(`Failed to extract SVG dimensions:`,e)}return null},A=(e,t,n,r,i=`mm`)=>{if(!t.data)return{finalWidth:0,finalHeight:0};let a=t.naturalWidth||0,o=t.naturalHeight||0;if(!a||!o)if(t.data.startsWith(`data:image/svg`)){let e=k(t.data);e&&(a=e.width,o=e.height)}else try{let n=e.getImageProperties(t.data);a=n.width,o=n.height}catch(e){console.warn(`Failed to get image properties for intrinsic sizing:`,e)}let s=o>0?a/o:1,c,l;if(t.width&&t.height?(c=O(t.width,i),l=O(t.height,i)):t.width?(c=O(t.width,i),l=c/s):t.height?(l=O(t.height,i),c=l*s):(c=O(a,i),l=O(o,i)),c>n){let e=n/c;c=n,l*=e}if(l>r){let e=r/l;l=r,c*=e}return{finalWidth:c,finalHeight:l}},j=async e=>{for(let t of e){if(t.type===u.Image&&t.src)try{if(t.src.startsWith(`data:`))t.data=t.src;else{let e=await fetch(t.src);if(!e.ok)throw Error(`Failed to fetch image: ${e.statusText}`);let n=await e.blob();t.data=await new Promise((e,t)=>{let r=new FileReader;r.onloadend=()=>{typeof r.result==`string`?e(r.result):t(Error(`Failed to convert image to base64 string`))},r.onerror=t,r.readAsDataURL(n)})}t.data&&t.data.startsWith(`data:image/svg`)&&typeof window<`u`&&typeof document<`u`&&(t.data=await new Promise(e=>{let n=new Image;n.onload=()=>{let r=document.createElement(`canvas`),i=k(t.data),a=i?i.width:n.width||300,o=i?i.height:n.height||150;t.naturalWidth=a,t.naturalHeight=o,r.width=a*4,r.height=o*4;let s=r.getContext(`2d`);s?(s.scale(4,4),s.drawImage(n,0,0,a,o),e(r.toDataURL(`image/png`))):e(t.data)},n.onerror=()=>e(t.data),n.src=t.data}))}catch(e){console.warn(`[jspdf-md-renderer] Warning: Failed to load image at ${t.src}. It will be skipped.`,e)}t.items&&t.items.length>0&&await j(t.items)}},M=class{static getCodespanOptions(){let e=S.options.codespan??{};return{backgroundColor:e.backgroundColor??`#EEEEEE`,padding:e.padding??.5,showBackground:e.showBackground!==!1,fontSizeScale:e.fontSizeScale??.9}}static applyStyle(e,t){let n=e.getFont().fontName,r=e.getFontSize(),i=()=>{let e=S.options.font.bold?.name;return e&&e!==``?e:n},a=()=>{let e=S.options.font.regular?.name;return e&&e!==``?e:n};switch(t){case`bold`:e.setFont(i(),S.options.font.bold?.style||`bold`);break;case`italic`:e.setFont(a(),`italic`);break;case`bolditalic`:e.setFont(i(),`bolditalic`);break;case`codespan`:e.setFont(`courier`,`normal`),e.setFontSize(r*this.getCodespanOptions().fontSizeScale);break;default:e.setFont(a(),e.getFont().fontStyle);break}}static measureWordWidth(e,t,n){let r=e.getFont(),i=e.getFontSize();this.applyStyle(e,n);let a=e.getTextWidth(t),o=e.getCharSpace?.()??0,s=a+t.length*o;return e.setFont(r.fontName,r.fontStyle),e.setFontSize(i),s}static getStyleFromType(e,t){switch(e){case`strong`:return t===`italic`?`bolditalic`:`bold`;case`em`:return t===`bold`?`bolditalic`:`italic`;case`codespan`:return`codespan`;default:return t||`normal`}}static flattenToWords(e,t,n=`normal`,r=!1,i){let a=[];for(let o of t){let t=this.getStyleFromType(o.type,n),s=o.type===`link`||r,c=o.href||i;if(o.items&&o.items.length>0){let n=this.flattenToWords(e,o.items,t,s,c);a.push(...n)}else if(o.type===`image`){let n=S.options.page.maxContentHeight-S.options.page.topmargin,{finalWidth:r,finalHeight:i}=A(e,o,S.options.page.maxContentWidth-S.options.page.indent*0,n,S.options.page.unit||`mm`);a.push({text:``,width:r,style:t,isLink:s,href:c,linkColor:s?S.options.link?.linkColor||[0,0,255]:void 0,isImage:!0,imageElement:o,imageHeight:i})}else{let n=o.content||o.text||``;if(!n)continue;if(t===`codespan`){let r=n.trim();r&&a.push({text:r,width:this.measureWordWidth(e,r,t),style:t,isLink:s,href:c,linkColor:s?S.options.link?.linkColor||[0,0,255]:void 0});continue}let r=n.split(/\s+/).filter(e=>e.length>0);if(r.length===0)continue;for(let n=0;n<r.length;n++){let i=r[n];a.push({text:i,width:this.measureWordWidth(e,i,t),style:t,isLink:s,href:c,linkColor:s?S.options.link?.linkColor||[0,0,255]:void 0})}}}return a}static breakIntoLines(e,t,n){let r=[],i=[],a=0,o=0,s=C(e)*S.options.page.defaultLineHeightFactor,c=e.getTextWidth(` `);for(let l=0;l<t.length;l++){let u=t[l],d=i.length>0?c+u.width:u.width,f=u.isImage&&u.imageHeight?u.imageHeight:C(e)*S.options.page.defaultLineHeightFactor;o+d>n&&i.length>0?(r.push({words:i,totalTextWidth:a,isLastLine:!1,lineHeight:s}),i=[u],a=u.width,o=u.width,s=f):(i.push(u),a+=u.width,o+=d,s=Math.max(s,f))}return i.length>0&&r.push({words:i,totalTextWidth:a,isLastLine:!0,lineHeight:s}),r}static renderWord(e,t,n,r){let i=e.getFont(),a=e.getFontSize(),o=e.getTextColor();if(this.applyStyle(e,t.style),t.isLink&&t.linkColor&&e.setTextColor(...t.linkColor),t.isImage&&t.imageElement&&t.imageElement.data)try{let i=`JPEG`;if(t.imageElement.data.startsWith(`data:image/png`))i=`PNG`;else if(t.imageElement.data.startsWith(`data:image/webp`))i=`WEBP`;else if(t.imageElement.data.startsWith(`data:image/gif`))i=`GIF`;else if(t.imageElement.src){let e=t.imageElement.src.split(`?`)[0].split(`#`)[0].split(`.`).pop()?.toUpperCase();e&&[`PNG`,`JPEG`,`JPG`,`WEBP`,`GIF`].includes(e)&&(i=e===`JPG`?`JPEG`:e)}if(t.width>0&&(t.imageHeight||0)>0){let a=t.imageHeight||0,o=r;e.addImage(t.imageElement.data,i,n,o,t.width,a)}}catch(e){console.warn(`Failed to render inline image`,e)}else{if(t.style===`codespan`){let i=this.getCodespanOptions();if(i.showBackground){let a=C(e),o=i.padding;e.setFillColor(i.backgroundColor),e.rect(n-o,r-o,t.width+o*2,a+o*2,`F`),e.setFillColor(`#000000`)}}e.text(t.text,n,r,{baseline:`top`})}if(t.isLink&&t.href){let i=t.isImage&&t.imageHeight?t.imageHeight:C(e);e.link(n,r,t.width,i,{url:t.href})}e.setFont(i.fontName,i.fontStyle),e.setFontSize(a),e.setTextColor(o)}static renderAlignedLine(e,t,n,r,i,a=`left`){let{words:o,totalTextWidth:s,isLastLine:c}=t;if(o.length===0)return;let l=e.getTextWidth(` `),u=n,d=l,f=s+(o.length-1)*l;switch(a){case`right`:u=n+i-f;break;case`center`:u=n+(i-f)/2;break;case`justify`:!c&&o.length>1&&(d=(i-s)/(o.length-1));break;default:break}let p=u,m=C(e)*S.options.page.defaultLineHeightFactor;for(let n=0;n<o.length;n++){let i=o[n],a=r,s=i.isImage&&i.imageHeight?i.imageHeight:m;i.isImage?a=r:s<t.lineHeight&&(a=r+(t.lineHeight-s)),this.renderWord(e,i,p,a),p+=i.width,n<o.length-1&&(p+=d)}}static renderStyledParagraph(e,t,n,r,i,a){let o=a??S.options.content?.textAlignment??`left`,s=this.flattenToWords(e,t);if(s.length===0)return;let c=this.breakIntoLines(e,s,i),l=r;for(let t of c)l+t.lineHeight>S.options.page.maxContentHeight&&(E(e),l=S.Y),this.renderAlignedLine(e,t,n,l,i,o),S.recordContentY(l+t.lineHeight),l+=t.lineHeight,S.updateY(t.lineHeight,`add`);let u=c[c.length-1];if(u){let t=u.totalTextWidth+(u.words.length-1)*e.getTextWidth(` `);S.updateX(n+t,`set`)}}static renderJustifiedParagraph(e,t,n,r,i){this.renderStyledParagraph(e,t,n,r,i)}},N=class{static renderText(e,t,n=S.X,r=S.Y,i,a=!1){let o=e.splitTextToSize(t,i),s=C(e),c=s*S.options.page.defaultLineHeightFactor,l=r;for(let t=0;t<o.length;t++){let r=o[t];l+c>S.options.page.maxContentHeight&&(E(e),l=S.Y),a?t===o.length-1?e.text(r,n,l,{baseline:`top`}):e.text(r,n,l,{maxWidth:i,align:`justify`,baseline:`top`}):e.text(r,n,l,{baseline:`top`}),S.recordContentY(l+s),l+=c,S.updateY(c,`add`)}return l}},P=(e,t,n,r)=>{S.activateInlineLock(),e.setFontSize(S.options.page.defaultFontSize);let i=S.options.page.maxContentWidth-n;if(t?.items&&t?.items.length>0){if(t.items.length===1&&t.items[0].type===`image`){r(t.items[0],n,!1),S.updateX(S.options.page.xpading),S.deactivateInlineLock();return}let a=[`strong`,`em`,`text`,`codespan`,`link`,`image`];if(t.items.some(e=>!a.includes(e.type))){let o=[],s=()=>{o.length>0&&(M.renderStyledParagraph(e,o,S.X+n,S.Y,i),o.length=0)};for(let e of t.items)a.includes(e.type)?o.push(e):(s(),r(e,n,!1));s()}else M.renderStyledParagraph(e,t.items,S.X+n,S.Y,i)}else{let r=t.content??``,a=S.options.content?.textAlignment??`left`;r.trim()&&N.renderText(e,r,S.X+n,S.Y,i,a===`justify`)}S.updateX(S.options.page.xpading),S.deactivateInlineLock()},F=(e,t,n,r)=>{e.setFontSize(S.options.page.defaultFontSize);for(let[e,i]of t?.items?.entries()??[]){let a=t.ordered?(t.start??0)+e:t.start;r(i,n+1,!0,a,t.ordered)}},I=(e,t,n,r,i,a)=>{S.Y+C(e)>=S.options.page.maxContentHeight&&E(e);let o=S.options,s=n*o.page.indent,c=a?`${i}. `:`• `,l=o.page.xpading;S.updateX(l,`set`),e.setFont(o.font.regular.name,o.font.regular.style),e.text(c,l+s,S.Y,{baseline:`top`});let d=e.getTextWidth(c),f=l+s+d,p=o.page.maxContentWidth-s-d;if(t.items&&t.items.length>0){let o=[],s=()=>{o.length>0&&(M.renderStyledParagraph(e,o,f,S.Y,p),o.length=0,S.updateX(l,`set`))};for(let e of t.items)e.type===u.List?(s(),r(e,n,!0,i,e.ordered??!1)):e.type===u.ListItem?(s(),r(e,n,!0,i,a)):o.push(e);s()}else if(t.content){let n=o.content?.textAlignment??`left`;N.renderText(e,t.content,f,S.Y,p,n===`justify`)}},L=(e,t,n,r,i,a,o,s=!0)=>{if(t?.items&&t?.items.length>0)for(let e of t?.items??[])i(e,n,r,a,o,s);else{let i=S.options,c=n*i.page.indent,l=r?o?`${a}. `:`• `:``,u=t.content||``,d=i.page.xpading;if(!u&&!l)return;if(!u.trim()&&!l){let t=(u.match(/\n/g)||[]).length;if(t>1){let n=(t-1)*(e.getTextDimensions(`A`).h*i.page.defaultLineHeightFactor);S.Y+n>i.page.maxContentHeight?E(e):(S.updateY(n,`add`),S.recordContentY(S.Y))}return}if(S.updateX(d,`set`),r&&l){let t=e.getTextWidth(l),n=i.page.maxContentWidth-c-t;e.setFont(i.font.regular.name,i.font.regular.style),e.text(l,d+c,S.Y,{baseline:`top`}),N.renderText(e,u,d+c+t,S.Y,n,s)}else{let t=i.page.maxContentWidth-c;N.renderText(e,u,d+c,S.Y,t,s)}S.updateX(d,`set`)}},R=e=>{let t=e.internal.pageSize.getWidth();e.setLineDashPattern([1,1],0),e.setLineWidth(.1),e.line(S.options.page.xpading,S.Y,t-S.options.page.xpading,S.Y),e.setLineWidth(.1),e.setLineDashPattern([],0),S.updateY(C(e),`add`)},z=(e,t,n)=>{let r=e.getFont(),i=e.getFontSize();e.setFont(`courier`,`normal`);let a=S.options.page.defaultFontSize*.9;e.setFontSize(a);let o=n*S.options.page.indent,s=S.options.page.maxContentWidth-o-8,c=e.getLineHeightFactor(),l=a/e.internal.scaleFactor*c,u=(t.code??``).replace(/[\r\n\s]+$/,``);if(!u){e.setFont(r.fontName,r.fontStyle),e.setFontSize(i);return}let d=e.splitTextToSize(u,s);for(;d.length>0&&d[d.length-1].trim()===``;)d.pop();if(d.length===0){e.setFont(r.fontName,r.fontStyle),e.setFontSize(i);return}let f=0;for(;f<d.length;){let n=S.options.page.maxContentHeight-S.Y,r=d.length-f,i=n-8,a=Math.floor(i/l);if(a<=0){E(e);continue}a>r&&(a=r);let o=d.slice(f,f+a),s=f===0,c=f+a>=d.length,u=a*l;if(s&&S.updateY(4,`add`),e.setFillColor(`#EEEEEE`),e.setDrawColor(`#DDDDDD`),e.roundedRect(S.X,S.Y-4,S.options.page.maxContentWidth,u+(s?4:0)+(c?4:0),2,2,`FD`),s&&t.lang){let n=e.getFontSize();e.setFontSize(10),e.setTextColor(`#666666`),e.text(t.lang,S.X+S.options.page.maxContentWidth-e.getTextWidth(t.lang)-4,S.Y,{baseline:`top`}),e.setFontSize(n),e.setTextColor(`#000000`)}let p=S.Y;for(let t of o)e.text(t,S.X+4,p,{baseline:`top`}),p+=l;S.updateY(u,`add`),S.recordContentY(S.Y+(c?4:0)),c&&S.updateY(4,`add`),f+=a,f<d.length&&E(e)}e.setFont(r.fontName,r.fontStyle),e.setFontSize(i)},B=(e,t,n)=>{let r=e.getFont().fontName,i=e.getFont().fontStyle,a=e.getFontSize(),o=e=>{switch(e){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}},s=(t,s)=>{s===`bold`?e.setFont(S.options.font.bold.name&&S.options.font.bold.name!==``?S.options.font.bold.name:r,S.options.font.bold.style||`bold`):s===`italic`?e.setFont(S.options.font.regular.name,`italic`):s===`bolditalic`?e.setFont(S.options.font.bold.name&&S.options.font.bold.name!==``?S.options.font.bold.name:r,`bolditalic`):s===`codespan`?(e.setFont(`courier`,`normal`),e.setFontSize(a*.9)):e.setFont(S.options.font.regular.name,i);let c=S.options.page.maxContentWidth-n-S.X,l=e.splitTextToSize(t,c),u=s===`codespan`,d=`#EEEEEE`;if(S.isInlineLockActive)for(let t=0;t<l.length;t++){if(u){let r=e.getTextWidth(l[t])+w(e),i=C(e);e.setFillColor(d),e.roundedRect(S.X+n-1,S.Y-1,r+2,i+2,2,2,`F`),e.setFillColor(`#000000`)}e.text(l[t],S.X+n,S.Y,{baseline:`top`,maxWidth:c}),S.updateX(e.getTextDimensions(l[t]).w+(u?2:1),`add`),t<l.length-1&&(S.updateY(C(e),`add`),S.updateX(S.options.page.xpading,`set`))}else if(l.length>1){let t=l[0],r=l?.slice(1)?.join(` `);if(u){let r=e.getTextWidth(t)+w(e),i=C(e);e.setFillColor(d),e.roundedRect(S.X+(n>=2?n+2:0)-1,S.Y-1,r+2,i+2,2,2,`F`),e.setFillColor(`#000000`)}e.text(t,S.X+(n>=2?n+2*o(s):0),S.Y,{baseline:`top`,maxWidth:c}),S.updateX(S.options.page.xpading+n),S.updateY(C(e),`add`);let i=S.options.page.maxContentWidth-n-S.options.page.xpading;e.splitTextToSize(r,i).forEach(t=>{if(u){let n=e.getTextWidth(t)+w(e),r=C(e);e.setFillColor(d),e.roundedRect(S.X+w(e)-1,S.Y-1,n+2,r+2,2,2,`F`),e.setFillColor(`#000000`)}e.text(t,S.X+w(e),S.Y,{baseline:`top`,maxWidth:i})})}else{if(u){let r=e.getTextWidth(t)+w(e),i=C(e);e.setFillColor(d),e.roundedRect(S.X+n-1,S.Y-1,r+2,i+2,2,2,`F`),e.setFillColor(`#000000`)}e.text(t,S.X+n,S.Y,{baseline:`top`,maxWidth:c}),S.updateX(e.getTextDimensions(t).w+(n>=2?t.split(` `).length+2:2)*o(s)*.5+(u?2:0),`add`)}};if(t.type===`text`&&t.items&&t.items.length>0)for(let e of t.items)if(e.type===`codespan`)s(e.content||``,`codespan`);else if(e.type===`em`||e.type===`strong`){let t=e.type===`em`?`italic`:`bold`;if(e.items&&e.items.length>0)for(let n of e.items)n.type===`strong`&&t===`italic`||n.type===`em`&&t===`bold`?s(n.content||``,`bolditalic`):s(n.content||``,t);else s(e.content||``,t)}else s(e.content||``,`normal`);else t.type===`em`?s(t.content||``,`italic`):t.type===`strong`?s(t.content||``,`bold`):t.type===`codespan`?s(t.content||``,`codespan`):s(t.content||``,`normal`);e.setFont(r,i),e.setFontSize(a)},V=(e,t,n)=>{let r=e.getFont().fontName,i=e.getFont().fontStyle,a=e.getFontSize(),o=e.getTextColor(),s=S.options.link?.linkColor||[0,0,255];e.setTextColor(...s);let c=S.options.page.maxContentWidth-n-S.X,l=t.text||t.content||``,u=t.href||``,d=e.splitTextToSize(l,c);if(S.isInlineLockActive)for(let t=0;t<d.length;t++){let r=e.getTextDimensions(d[t]).w,i=C(e)/2;e.link(S.X+n,S.Y,r,i,{url:u}),e.text(d[t],S.X+n,S.Y,{baseline:`top`,maxWidth:c}),S.updateX(r+1,`add`),S.X+r>S.options.page.maxContentWidth-n&&(S.updateY(i,`add`),S.updateX(S.options.page.xpading+n,`set`)),t<d.length-1&&(S.updateY(i,`add`),S.updateX(S.options.page.xpading+n,`set`))}else if(d.length>1){let t=d[0],r=d?.slice(1)?.join(` `),i=e.getTextDimensions(t).w,a=C(e)/2;e.link(S.X+n,S.Y,i,a,{url:u}),e.text(t,S.X+n,S.Y,{baseline:`top`,maxWidth:c}),S.updateX(S.options.page.xpading+n),S.updateY(a,`add`);let o=S.options.page.maxContentWidth-n-S.options.page.xpading;e.splitTextToSize(r,o).forEach(t=>{let n=e.getTextDimensions(t).w;e.link(S.X+w(e),S.Y,n,a,{url:u}),e.text(t,S.X+w(e),S.Y,{baseline:`top`,maxWidth:o})})}else{let t=e.getTextDimensions(l).w,r=C(e)/2;e.link(S.X+n,S.Y,t,r,{url:u}),e.text(l,S.X+n,S.Y,{baseline:`top`,maxWidth:c}),S.updateX(t+2,`add`)}e.setFont(r,i),e.setFontSize(a),e.setTextColor(o)},H=(e,t,n,r)=>{let i=S.options,a=n+1,o=S.X+n*i.page.indent,s=S.Y,c=o+i.page.indent/2,l=s,u=e.internal.getCurrentPageInfo().pageNumber;t.items&&t.items.length>0&&t.items.forEach(e=>{r(e,a)});let d=S.Y,f=e.internal.getCurrentPageInfo().pageNumber;e.setDrawColor(100),e.setLineWidth(1);for(let t=u;t<=f;t++){e.setPage(t);let n=t===u,r=t===f,a=n?l:i.page.topmargin,o=r?d:i.page.maxContentHeight;e.line(c,a,c,o)}S.recordContentY(),e.setPage(f)},U=e=>{if(e.data){if(e.data.startsWith(`data:image/png`))return`PNG`;if(e.data.startsWith(`data:image/jpeg`)||e.data.startsWith(`data:image/jpg`))return`JPEG`;if(e.data.startsWith(`data:image/webp`)||e.data.startsWith(`data:image/webp`))return`WEBP`;if(e.data.startsWith(`data:image/gif`))return`GIF`}if(e.src){let t=e.src.split(`?`)[0].split(`#`)[0].split(`.`).pop()?.toUpperCase();if(t&&[`PNG`,`JPEG`,`JPG`,`WEBP`,`GIF`].includes(t))return t===`JPG`?`JPEG`:t}return`JPEG`},W=(e,t,n)=>{if(!t.data)return;let r=S.options,i=r.page.unit||`mm`,a=n*r.page.indent,o=r.page.maxContentWidth-a,s=S.X+a,c=S.Y;try{let{finalWidth:n,finalHeight:a}=A(e,t,o,r.page.maxContentHeight-r.page.topmargin,i);c+a>r.page.maxContentHeight&&(E(e),c=S.Y);let l=t.align||r.image?.defaultAlign||`left`,u;switch(l){case`right`:u=s+o-n;break;case`center`:u=s+(o-n)/2;break;default:u=s;break}let d=U(t);n>0&&a>0&&e.addImage(t.data,d,u,c,n,a),S.updateY(a,`add`),S.recordContentY()}catch(e){console.warn(`Failed to render image`,e)}},G=()=>{let e=l.default;if(typeof l.default==`function`)return l.default;if(typeof e.default==`function`)return e.default;if(typeof e.autoTable==`function`)return e.autoTable;throw Error(`Could not resolve jspdf-autotable export. Expected a callable export.`)},K=(e,t,n)=>{if(!t.header||!t.rows)return;let r=S.options,i=r.page.xmargin+n*r.page.indent,a=[t.header.map(e=>e.content||``)],o=t.rows.map(e=>e.map(e=>e.content||``)),s=r.table||{};G()(e,{head:a,body:o,startY:S.Y,margin:{left:i,right:r.page.xmargin},...s,didDrawPage:e=>{s.didDrawPage&&s.didDrawPage(e)},didDrawCell:e=>{s.didDrawCell&&s.didDrawCell(e),S.setCursor({x:S.X,y:e.cell.y+e.cell.height+2*r.page.lineSpace})}})},q={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`}},J=e=>{if(!e)throw Error(`RenderOption is required`);let t={...q.page,...e.page},n={...q.font,...e.font},r={...q.image,...e.image};return t.maxContentWidth||=190,t.maxContentHeight||=277,{...e,page:t,font:n,image:r}},Y=async(e,t,n)=>{let r=J(n);S.initialize(r);let i=await y(t);await j(i);let a=(t,n=0,i=!1,o=0,s=!1)=>{let c=n*r.page.indent;switch(t.type){case u.Heading:T(e,t,c,a);break;case u.Paragraph:P(e,t,c,a);break;case u.List:F(e,t,n,a);break;case u.ListItem:I(e,t,n,a,o,s);break;case u.Hr:R(e);break;case u.Code:z(e,t,n);break;case u.Strong:case u.Em:case u.CodeSpan:B(e,t,c);break;case u.Link:V(e,t,c);break;case u.Blockquote:H(e,t,n,a);break;case u.Image:W(e,t,n);break;case u.Table:K(e,t,n);break;case u.Raw:case u.Text:L(e,t,n,i,a,o,s,r.content?.textAlignment===`justify`);break;default:console.warn(`Warning: Unsupported element type encountered: ${t.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(
|
|
4
|
+
with details of the element and expected behavior. Thanks for helping to improve this library!`);break}};for(let e of i)a(e);r.endCursorYHandler(S.Y)};exports.MdTextParser=y,exports.MdTextRender=Y;
|