jspdf-md-renderer 1.6.0 → 1.6.2
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/package.json +65 -66
- package/dist/index.d.ts +0 -81
- package/dist/index.js +0 -4
- package/dist/index.mjs +0 -464
package/package.json
CHANGED
|
@@ -1,69 +1,68 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
"name": "jspdf-md-renderer",
|
|
3
|
+
"version": "1.6.2",
|
|
4
|
+
"description": "A jsPDF utility to render Markdown directly into formatted PDFs with custom designs.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"./types": "./dist/types/index.d.ts"
|
|
13
15
|
},
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"vite": "^6.2.4",
|
|
67
|
-
"vite-plugin-dts": "^4.5.3"
|
|
68
|
-
}
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"types",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"dev": "npm run format && npm run lint:fix && npm run build",
|
|
24
|
+
"build": "rimraf dist && vite build",
|
|
25
|
+
"lint": "eslint src/**",
|
|
26
|
+
"lint:fix": "eslint src/** --fix",
|
|
27
|
+
"format": "prettier --write .",
|
|
28
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
29
|
+
"watch": "vite build --watch"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/JeelGajera/jspdf-md-renderer.git"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"jspdf",
|
|
37
|
+
"markdown",
|
|
38
|
+
"pdf",
|
|
39
|
+
"renderer"
|
|
40
|
+
],
|
|
41
|
+
"author": "Jeel Gajera <jeelgajera200@gmail.com>",
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/JeelGajera/jspdf-md-renderer/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/JeelGajera/jspdf-md-renderer#readme",
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"jspdf": "^3.0.1",
|
|
49
|
+
"jspdf-md-renderer": "file:",
|
|
50
|
+
"marked": "^15.0.3"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@eslint/js": "^9.16.0",
|
|
54
|
+
"@types/node": "^22.10.2",
|
|
55
|
+
"@typescript-eslint/eslint-plugin": "^8.18.0",
|
|
56
|
+
"@typescript-eslint/parser": "^8.18.0",
|
|
57
|
+
"eslint": "^9.16.0",
|
|
58
|
+
"eslint-config-prettier": "^10.1.2",
|
|
59
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
60
|
+
"globals": "^16.0.0",
|
|
61
|
+
"prettier": "^3.4.2",
|
|
62
|
+
"rimraf": "^6.0.1",
|
|
63
|
+
"typescript": "^5.7.2",
|
|
64
|
+
"typescript-eslint": "^8.18.0",
|
|
65
|
+
"vite": "^6.2.4",
|
|
66
|
+
"vite-plugin-dts": "^4.5.3"
|
|
67
|
+
}
|
|
69
68
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { default as default_2 } from 'jspdf';
|
|
2
|
-
import { jsPDFOptions } from 'jspdf';
|
|
3
|
-
|
|
4
|
-
declare type FontItem = {
|
|
5
|
-
name: string;
|
|
6
|
-
style: string;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Parses markdown into tokens and converts to a custom parsed structure.
|
|
11
|
-
*
|
|
12
|
-
* @param text - The markdown content to parse.
|
|
13
|
-
* @returns Parsed markdown elements.
|
|
14
|
-
*/
|
|
15
|
-
export declare const MdTextParser: (text: string) => Promise<ParsedElement[]>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Renders parsed markdown text into jsPDF document.
|
|
19
|
-
*
|
|
20
|
-
* @param doc - The jsPDF document.
|
|
21
|
-
* @param text - The markdown content to render.
|
|
22
|
-
* @param options - The render options (fonts, page margins, etc.).
|
|
23
|
-
*/
|
|
24
|
-
export declare const MdTextRender: (doc: default_2, text: string, options: RenderOption) => Promise<void>;
|
|
25
|
-
|
|
26
|
-
declare type ParsedElement = {
|
|
27
|
-
type: string;
|
|
28
|
-
content?: string;
|
|
29
|
-
depth?: number;
|
|
30
|
-
items?: ParsedElement[];
|
|
31
|
-
ordered?: boolean;
|
|
32
|
-
start?: number;
|
|
33
|
-
lang?: string;
|
|
34
|
-
code?: string;
|
|
35
|
-
src?: string;
|
|
36
|
-
alt?: string;
|
|
37
|
-
href?: string;
|
|
38
|
-
text?: string;
|
|
39
|
-
header?: {
|
|
40
|
-
type?: string;
|
|
41
|
-
content?: any;
|
|
42
|
-
};
|
|
43
|
-
rows?: {
|
|
44
|
-
type?: string;
|
|
45
|
-
content?: any;
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export declare type RenderOption = {
|
|
50
|
-
cursor: {
|
|
51
|
-
x: number;
|
|
52
|
-
y: number;
|
|
53
|
-
};
|
|
54
|
-
page: {
|
|
55
|
-
format?: string | number[];
|
|
56
|
-
unit?: jsPDFOptions['unit'];
|
|
57
|
-
orientation?: jsPDFOptions['orientation'];
|
|
58
|
-
maxContentWidth: number;
|
|
59
|
-
maxContentHeight: number;
|
|
60
|
-
lineSpace: number;
|
|
61
|
-
defaultLineHeightFactor: number;
|
|
62
|
-
defaultFontSize: number;
|
|
63
|
-
defaultTitleFontSize: number;
|
|
64
|
-
topmargin: number;
|
|
65
|
-
xpading: number;
|
|
66
|
-
xmargin: number;
|
|
67
|
-
indent: number;
|
|
68
|
-
};
|
|
69
|
-
font: {
|
|
70
|
-
bold: FontItem;
|
|
71
|
-
regular: FontItem;
|
|
72
|
-
light: FontItem;
|
|
73
|
-
};
|
|
74
|
-
content?: {
|
|
75
|
-
textAlignment: 'left' | 'right' | 'center' | 'justify';
|
|
76
|
-
};
|
|
77
|
-
pageBreakHandler?: () => void;
|
|
78
|
-
endCursorYHandler: (y: number) => void;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
export { }
|
package/dist/index.js
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const W=require("marked");var s=(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))(s||{});const S=async t=>{const a=await W.marked.lexer(t,{async:!0});return d(a)},d=t=>{const a=[];return t.forEach(e=>{try{const g=m[e.type];g?a.push(g(e)):a.push({type:s.Raw,content:e.raw})}catch(g){console.error("Failed to handle token ==>",e,g)}}),a},m={[s.Heading]:t=>({type:s.Heading,depth:t.depth,content:t.text,items:t.tokens?d(t.tokens):[]}),[s.Paragraph]:t=>({type:s.Paragraph,content:t.text,items:t.tokens?d(t.tokens):[]}),[s.List]:t=>({type:s.List,ordered:t.ordered,start:t.start,items:t.items?d(t.items):[]}),[s.ListItem]:t=>({type:s.ListItem,content:t.text,items:t.tokens?d(t.tokens):[]}),[s.Code]:t=>({type:s.Code,lang:t.lang,code:t.text}),[s.Table]:t=>({type:s.Table,header:t.header.map(a=>({type:s.TableHeader,content:a})),rows:t.rows.map(a=>a.map(e=>({type:s.TableCell,content:e})))}),[s.Image]:t=>({type:s.Image,src:t.href,alt:t.text}),[s.Link]:t=>({type:s.Link,href:t.href,text:t.text,items:t.tokens?d(t.tokens):[]}),[s.Strong]:t=>({type:s.Strong,content:t.text,items:t.tokens?d(t.tokens):[]}),[s.Em]:t=>({type:s.Em,content:t.text,items:t.tokens?d(t.tokens):[]}),[s.Text]:t=>({type:s.Text,content:t.text,items:t.tokens?d(t.tokens):[]}),[s.Hr]:t=>({type:s.Hr,content:t.raw,items:t.tokens?d(t.tokens):[]}),[s.CodeSpan]:t=>({type:s.CodeSpan,content:t.text,items:t.tokens?d(t.tokens):[]})},c=(t,a)=>{var e,g;typeof a.pageBreakHandler=="function"?a.pageBreakHandler():t.addPage((e=a.page)==null?void 0:e.format,(g=a.page)==null?void 0:g.orientation)},p=(t,a)=>t.getTextDimensions("H").h*a.page.defaultLineHeightFactor,w=(t,a,e,g,i,n)=>{const h=6-((a==null?void 0:a.depth)??0)>0?6-((a==null?void 0:a.depth)??0):0;if(t.setFontSize(i.page.defaultFontSize+h),a!=null&&a.items&&(a==null?void 0:a.items.length)>0)for(const f of(a==null?void 0:a.items)??[])e=n(f,g,!1);else t.text((a==null?void 0:a.content)??"",e.x+g,e.y,{align:"left",maxWidth:i.page.maxContentWidth-g}),e.y+=1.5*p(t,i);return t.setFontSize(i.page.defaultFontSize),e},L=(t,a,e,g,i,n,h,f,y)=>{const l=(f-e)/(a.length-1);let r=i;const x=n+g*h;for(const b of a)t.text(b.text,r,x,{align:"justify",lineHeightFactor:y,maxWidth:f}),r+=b.wordLength+l},z=(t,a,e,g,i,n,h,f)=>{const y=t.map(l=>l.text).join(" ");a.text(y,e,g+i*n,{align:"justify",lineHeightFactor:f,maxWidth:h})},H=(t,a,e,g,i,n)=>{const h={x:e,y:g},f=t.getTextDimensions("A").h*n,y=a.split(" ");let l=0,r=[],x=0;for(const b of y){const C=t.getTextWidth(b+"a");C+x>=i&&(L(t,r,x,l++,e,g,f,i,n),r=[],x=0),r.push({text:b,wordLength:C}),x+=C}return r.length>0&&z(r,t,e,g,l,f,i,n),h.y=g+l*f,h.x=t.getTextWidth(a)+e,h},I=(t,a,e,g,i,n)=>{t.setFontSize(i.page.defaultFontSize);let h=a.content;const f=t.getTextDimensions("A").h*i.page.defaultLineHeightFactor;if(a!=null&&a.items&&(a==null?void 0:a.items.length)>0)for(const y of(a==null?void 0:a.items)??[])e=n(y,g,!1);else{if(e.y+t.splitTextToSize(h??"",i.page.maxContentWidth-g).length*f-3*f>=i.page.maxContentHeight){const y=t.splitTextToSize(h??"",i.page.maxContentWidth-g),l=[],r=e.y;for(let x=0;x<y.length;x++)if(e.y-2*f<i.page.maxContentHeight)l.push(y[x]),e.y+=i.page.lineSpace;else{x<=y.length-1&&(h=y.slice(x).join(""));break}l.length>0&&(e=H(t,l.join(" "),e.x+g,r,i.page.maxContentWidth-g,i.page.defaultLineHeightFactor)),c(t,i),e.y=i.page.topmargin}e.y=H(t,h??"",e.x+g,e.y,i.page.maxContentWidth-g,i.page.defaultLineHeightFactor).y+p(t,i),e.x=i.page.xpading}return e},u=(t,a,e,g,i,n)=>{var h;t.setFontSize(i.page.defaultFontSize);for(const[f,y]of((h=a==null?void 0:a.items)==null?void 0:h.entries())??[]){const l=a.ordered?(a.start??0)+f:a.start;e=n(y,g+1,!0,l,a.ordered),e.y+=p(t,i)*.2}return e},F=(t,a,e,g,i)=>{const n=t.getFont().fontName,h=t.getFont().fontStyle,f=t.getFontSize(),y=r=>{switch(r){case"normal":return 0;case"bold":return 1;case"italic":return 1.5;case"bolditalic":return 2.5;default:return 0}},l=(r,x)=>{x==="bold"?t.setFont(i.font.bold.name&&i.font.bold.name!==""?i.font.bold.name:n,i.font.bold.style||"bold"):x==="italic"?t.setFont(i.font.regular.name,"italic"):x==="bolditalic"?t.setFont(i.font.bold.name&&i.font.bold.name!==""?i.font.bold.name:n,"bolditalic"):t.setFont(i.font.regular.name,h);const b=t.getTextWidth(r);e.x+b>i.page.xpading+i.page.maxContentWidth&&(e.x=i.page.xpading,e.y+=p(t,i));const C=t.getTextWidth(" ");t.text(r,e.x+g,e.y,{baseline:"top"}),e.x+=b+C*y(x)+(x==="normal"?g*.7:0)};if(a.type==="text"&&a.items&&a.items.length>0)for(const r of a.items)if(r.type==="em"||r.type==="strong"){const x=r.type==="em"?"italic":"bold";if(r.items&&r.items.length>0)for(const b of r.items)b.type==="strong"&&x==="italic"||b.type==="em"&&x==="bold"?l(b.content||"","bolditalic"):l(b.content||"",x);else l(r.content||"",x)}else l(r.content||"","normal");else a.type==="em"?l(a.content||"","italic"):a.type==="strong"?l(a.content||"","bold"):l(a.content||"","normal");return t.setFont(n,h),t.setFontSize(f),e},E=(t,a,e,g,i,n,h,f)=>{const y=g*i.page.indent,l=f?`${h}. `:"• ";e.y+p(t,i)>=i.page.maxContentHeight&&(c(t,i),e.y=i.page.topmargin),t.setFont(i.font.regular.name,i.font.regular.style),t.text(l,e.x+y,e.y,{baseline:"top"});const r=t.getTextWidth(l);if(e.x+=r,a.items&&a.items.length>0)for(const x of a.items){if(e.y+p(t,i)>=i.page.maxContentHeight&&(c(t,i),e.y=i.page.topmargin),x.type===s.List)n(x,g+1,!0,h,x.ordered??!1);else if(x.type===s.ListItem){const b=a.type===s.List?g:g+1;n(x,b,!0,h,f)}else e=F(t,x,e,y,i);e.x=i.page.xpading,e.y+=p(t,i)}else if(a.content){t.text(a.content,e.x+y,e.y,{baseline:"top"});const x=t.getTextWidth(a.content);e.x+=x}return e},P=(t,a,e,g,i,n,h,f,y,l=!0)=>{if(a!=null&&a.items&&(a==null?void 0:a.items.length)>0)for(const r of(a==null?void 0:a.items)??[])e=h(r,g,i,f,y,l);else{const r=g*n.page.indent,x=i?y?`${f}. `:"• ":"",b=t.splitTextToSize(x+a.content,n.page.maxContentWidth-r);e.y+b.length*p(t,n)>=n.page.maxContentHeight&&(c(t,n),e.y=n.page.topmargin),l?(e.y=H(t,x+a.content,e.x+r,e.y,n.page.maxContentWidth-r,n.page.defaultLineHeightFactor).y+p(t,n),e.x=n.page.xpading):(t.text(x+a.content,e.x+r,e.y,{baseline:"top"}),e.x+=t.getTextWidth(x+a.content),e.x>=n.page.xpading+n.page.maxContentWidth&&(c(t,n),e.x=n.page.xpading,e.y+=p(t,n)))}return e},T=(t,a,e)=>{const g=t.internal.pageSize.getWidth();return t.setLineDashPattern([1,1],0),t.setLineWidth(.1),t.line(e.page.xpading,a.y,g-e.page.xpading,a.y),t.setLineWidth(.1),t.setLineDashPattern([],0),a.y+=p(t,e),a},j=(t,a,e,g,i,n)=>{const h=g*n.page.indent;e.y+t.splitTextToSize(a.code??"",n.page.maxContentWidth-h).length*p(t,n)-2*p(t,n)>=n.page.maxContentHeight&&(c(t,n),e.y=n.page.topmargin);const f=t.splitTextToSize(a.code??"",n.page.maxContentWidth-h).length*p(t,n);return e.y+=n.page.lineSpace,t.setFillColor("#EEEEEE"),t.setDrawColor("#eee"),t.roundedRect(e.x,e.y-n.page.lineSpace,n.page.maxContentWidth,f,2,2,"FD"),t.setFontSize(10),t.text(a.lang??"",e.x+n.page.maxContentWidth-t.getTextWidth(a.lang??"")-n.page.lineSpace/2,e.y),t.setFontSize(n.page.defaultFontSize),t.text(a.code??"",e.x+4,e.y),e.y+=f,e},D=async(t,a,e)=>{const g=await S(a);console.log(g);let i={x:e.cursor.x,y:e.cursor.y};const n=(h,f=0,y=!1,l=0,r=!1,x=!0)=>{const b=f*e.page.indent;switch(i.y+t.splitTextToSize(h.content??"",e.page.maxContentWidth-b).length*p(t,e)>=e.page.maxContentHeight&&(c(t,e),i.y=e.page.topmargin),h.type){case s.Heading:i=w(t,h,i,b,e,n);break;case s.Paragraph:i=I(t,h,i,b,e,n);break;case s.List:i=u(t,h,i,f,e,n);break;case s.ListItem:i=E(t,h,i,f,e,n,l,r);break;case s.Hr:i=T(t,i,e);break;case s.Code:i=j(t,h,i,f,y,e);break;case s.Strong:case s.Em:i=F(t,h,i,b,e);break;case s.Raw:case s.Text:i=P(t,h,i,f,y,e,n,l,r,x);break;default:console.warn(`Warning: Unsupported element type encountered: ${h.type}.
|
|
2
|
-
If you believe this element type should be supported, please create an issue at:
|
|
3
|
-
https://github.com/JeelGajera/jspdf-md-renderer/issues
|
|
4
|
-
with details of the element and expected behavior. Thank you for helping improve this library!`);break}return i};for(const h of g)n(h);e.endCursorYHandler(i.y)};exports.MdTextParser=S;exports.MdTextRender=D;
|
package/dist/index.mjs
DELETED
|
@@ -1,464 +0,0 @@
|
|
|
1
|
-
import { marked as W } from "marked";
|
|
2
|
-
var s = /* @__PURE__ */ ((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))(s || {});
|
|
3
|
-
const S = async (t) => {
|
|
4
|
-
const a = await W.lexer(t, { async: !0 });
|
|
5
|
-
return d(a);
|
|
6
|
-
}, d = (t) => {
|
|
7
|
-
const a = [];
|
|
8
|
-
return t.forEach((e) => {
|
|
9
|
-
try {
|
|
10
|
-
const g = m[e.type];
|
|
11
|
-
g ? a.push(g(e)) : a.push({
|
|
12
|
-
type: s.Raw,
|
|
13
|
-
content: e.raw
|
|
14
|
-
});
|
|
15
|
-
} catch (g) {
|
|
16
|
-
console.error("Failed to handle token ==>", e, g);
|
|
17
|
-
}
|
|
18
|
-
}), a;
|
|
19
|
-
}, m = {
|
|
20
|
-
[s.Heading]: (t) => ({
|
|
21
|
-
type: s.Heading,
|
|
22
|
-
depth: t.depth,
|
|
23
|
-
content: t.text,
|
|
24
|
-
items: t.tokens ? d(t.tokens) : []
|
|
25
|
-
}),
|
|
26
|
-
[s.Paragraph]: (t) => ({
|
|
27
|
-
type: s.Paragraph,
|
|
28
|
-
content: t.text,
|
|
29
|
-
items: t.tokens ? d(t.tokens) : []
|
|
30
|
-
}),
|
|
31
|
-
[s.List]: (t) => ({
|
|
32
|
-
type: s.List,
|
|
33
|
-
ordered: t.ordered,
|
|
34
|
-
start: t.start,
|
|
35
|
-
items: t.items ? d(t.items) : []
|
|
36
|
-
}),
|
|
37
|
-
[s.ListItem]: (t) => ({
|
|
38
|
-
type: s.ListItem,
|
|
39
|
-
content: t.text,
|
|
40
|
-
items: t.tokens ? d(t.tokens) : []
|
|
41
|
-
}),
|
|
42
|
-
[s.Code]: (t) => ({
|
|
43
|
-
type: s.Code,
|
|
44
|
-
lang: t.lang,
|
|
45
|
-
code: t.text
|
|
46
|
-
}),
|
|
47
|
-
[s.Table]: (t) => ({
|
|
48
|
-
type: s.Table,
|
|
49
|
-
header: t.header.map((a) => ({
|
|
50
|
-
type: s.TableHeader,
|
|
51
|
-
content: a
|
|
52
|
-
})),
|
|
53
|
-
rows: t.rows.map(
|
|
54
|
-
(a) => a.map((e) => ({
|
|
55
|
-
type: s.TableCell,
|
|
56
|
-
content: e
|
|
57
|
-
}))
|
|
58
|
-
)
|
|
59
|
-
}),
|
|
60
|
-
[s.Image]: (t) => ({
|
|
61
|
-
type: s.Image,
|
|
62
|
-
src: t.href,
|
|
63
|
-
alt: t.text
|
|
64
|
-
}),
|
|
65
|
-
[s.Link]: (t) => ({
|
|
66
|
-
type: s.Link,
|
|
67
|
-
href: t.href,
|
|
68
|
-
text: t.text,
|
|
69
|
-
items: t.tokens ? d(t.tokens) : []
|
|
70
|
-
}),
|
|
71
|
-
[s.Strong]: (t) => ({
|
|
72
|
-
type: s.Strong,
|
|
73
|
-
content: t.text,
|
|
74
|
-
items: t.tokens ? d(t.tokens) : []
|
|
75
|
-
}),
|
|
76
|
-
[s.Em]: (t) => ({
|
|
77
|
-
type: s.Em,
|
|
78
|
-
content: t.text,
|
|
79
|
-
items: t.tokens ? d(t.tokens) : []
|
|
80
|
-
}),
|
|
81
|
-
[s.Text]: (t) => ({
|
|
82
|
-
type: s.Text,
|
|
83
|
-
content: t.text,
|
|
84
|
-
items: t.tokens ? d(t.tokens) : []
|
|
85
|
-
}),
|
|
86
|
-
[s.Hr]: (t) => ({
|
|
87
|
-
type: s.Hr,
|
|
88
|
-
content: t.raw,
|
|
89
|
-
items: t.tokens ? d(t.tokens) : []
|
|
90
|
-
}),
|
|
91
|
-
[s.CodeSpan]: (t) => ({
|
|
92
|
-
type: s.CodeSpan,
|
|
93
|
-
content: t.text,
|
|
94
|
-
items: t.tokens ? d(t.tokens) : []
|
|
95
|
-
})
|
|
96
|
-
}, c = (t, a) => {
|
|
97
|
-
var e, g;
|
|
98
|
-
typeof a.pageBreakHandler == "function" ? a.pageBreakHandler() : t.addPage((e = a.page) == null ? void 0 : e.format, (g = a.page) == null ? void 0 : g.orientation);
|
|
99
|
-
}, p = (t, a) => t.getTextDimensions("H").h * a.page.defaultLineHeightFactor, w = (t, a, e, g, i, n) => {
|
|
100
|
-
const h = 6 - ((a == null ? void 0 : a.depth) ?? 0) > 0 ? 6 - ((a == null ? void 0 : a.depth) ?? 0) : 0;
|
|
101
|
-
if (t.setFontSize(i.page.defaultFontSize + h), a != null && a.items && (a == null ? void 0 : a.items.length) > 0)
|
|
102
|
-
for (const r of (a == null ? void 0 : a.items) ?? [])
|
|
103
|
-
e = n(r, g, !1);
|
|
104
|
-
else
|
|
105
|
-
t.text((a == null ? void 0 : a.content) ?? "", e.x + g, e.y, {
|
|
106
|
-
align: "left",
|
|
107
|
-
maxWidth: i.page.maxContentWidth - g
|
|
108
|
-
}), e.y += 1.5 * p(t, i);
|
|
109
|
-
return t.setFontSize(i.page.defaultFontSize), e;
|
|
110
|
-
}, L = (t, a, e, g, i, n, h, r, y) => {
|
|
111
|
-
const l = (r - e) / (a.length - 1);
|
|
112
|
-
let f = i;
|
|
113
|
-
const x = n + g * h;
|
|
114
|
-
for (const b of a)
|
|
115
|
-
t.text(b.text, f, x, {
|
|
116
|
-
align: "justify",
|
|
117
|
-
lineHeightFactor: y,
|
|
118
|
-
maxWidth: r
|
|
119
|
-
}), f += b.wordLength + l;
|
|
120
|
-
}, z = (t, a, e, g, i, n, h, r) => {
|
|
121
|
-
const y = t.map((l) => l.text).join(" ");
|
|
122
|
-
a.text(y, e, g + i * n, {
|
|
123
|
-
align: "justify",
|
|
124
|
-
lineHeightFactor: r,
|
|
125
|
-
maxWidth: h
|
|
126
|
-
});
|
|
127
|
-
}, H = (t, a, e, g, i, n) => {
|
|
128
|
-
const h = {
|
|
129
|
-
x: e,
|
|
130
|
-
y: g
|
|
131
|
-
}, r = t.getTextDimensions("A").h * n, y = a.split(" ");
|
|
132
|
-
let l = 0, f = [], x = 0;
|
|
133
|
-
for (const b of y) {
|
|
134
|
-
const C = t.getTextWidth(b + "a");
|
|
135
|
-
C + x >= i && (L(
|
|
136
|
-
t,
|
|
137
|
-
f,
|
|
138
|
-
x,
|
|
139
|
-
l++,
|
|
140
|
-
e,
|
|
141
|
-
g,
|
|
142
|
-
r,
|
|
143
|
-
i,
|
|
144
|
-
n
|
|
145
|
-
), f = [], x = 0), f.push({ text: b, wordLength: C }), x += C;
|
|
146
|
-
}
|
|
147
|
-
return f.length > 0 && z(
|
|
148
|
-
f,
|
|
149
|
-
t,
|
|
150
|
-
e,
|
|
151
|
-
g,
|
|
152
|
-
l,
|
|
153
|
-
r,
|
|
154
|
-
i,
|
|
155
|
-
n
|
|
156
|
-
), h.y = g + l * r, h.x = t.getTextWidth(a) + e, h;
|
|
157
|
-
}, I = (t, a, e, g, i, n) => {
|
|
158
|
-
t.setFontSize(i.page.defaultFontSize);
|
|
159
|
-
let h = a.content;
|
|
160
|
-
const r = t.getTextDimensions("A").h * i.page.defaultLineHeightFactor;
|
|
161
|
-
if (a != null && a.items && (a == null ? void 0 : a.items.length) > 0)
|
|
162
|
-
for (const y of (a == null ? void 0 : a.items) ?? [])
|
|
163
|
-
e = n(y, g, !1);
|
|
164
|
-
else {
|
|
165
|
-
if (e.y + t.splitTextToSize(
|
|
166
|
-
h ?? "",
|
|
167
|
-
i.page.maxContentWidth - g
|
|
168
|
-
).length * r - 3 * r >= i.page.maxContentHeight) {
|
|
169
|
-
const y = t.splitTextToSize(
|
|
170
|
-
h ?? "",
|
|
171
|
-
i.page.maxContentWidth - g
|
|
172
|
-
), l = [], f = e.y;
|
|
173
|
-
for (let x = 0; x < y.length; x++)
|
|
174
|
-
if (e.y - 2 * r < i.page.maxContentHeight)
|
|
175
|
-
l.push(y[x]), e.y += i.page.lineSpace;
|
|
176
|
-
else {
|
|
177
|
-
x <= y.length - 1 && (h = y.slice(x).join(""));
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
l.length > 0 && (e = H(
|
|
181
|
-
t,
|
|
182
|
-
l.join(" "),
|
|
183
|
-
e.x + g,
|
|
184
|
-
f,
|
|
185
|
-
i.page.maxContentWidth - g,
|
|
186
|
-
i.page.defaultLineHeightFactor
|
|
187
|
-
)), c(t, i), e.y = i.page.topmargin;
|
|
188
|
-
}
|
|
189
|
-
e.y = H(
|
|
190
|
-
t,
|
|
191
|
-
h ?? "",
|
|
192
|
-
e.x + g,
|
|
193
|
-
e.y,
|
|
194
|
-
i.page.maxContentWidth - g,
|
|
195
|
-
i.page.defaultLineHeightFactor
|
|
196
|
-
).y + p(t, i), e.x = i.page.xpading;
|
|
197
|
-
}
|
|
198
|
-
return e;
|
|
199
|
-
}, E = (t, a, e, g, i, n) => {
|
|
200
|
-
var h;
|
|
201
|
-
t.setFontSize(i.page.defaultFontSize);
|
|
202
|
-
for (const [r, y] of ((h = a == null ? void 0 : a.items) == null ? void 0 : h.entries()) ?? []) {
|
|
203
|
-
const l = a.ordered ? (a.start ?? 0) + r : a.start;
|
|
204
|
-
e = n(
|
|
205
|
-
y,
|
|
206
|
-
g + 1,
|
|
207
|
-
!0,
|
|
208
|
-
l,
|
|
209
|
-
a.ordered
|
|
210
|
-
), e.y += p(t, i) * 0.2;
|
|
211
|
-
}
|
|
212
|
-
return e;
|
|
213
|
-
}, F = (t, a, e, g, i) => {
|
|
214
|
-
const n = t.getFont().fontName, h = t.getFont().fontStyle, r = t.getFontSize(), y = (f) => {
|
|
215
|
-
switch (f) {
|
|
216
|
-
case "normal":
|
|
217
|
-
return 0;
|
|
218
|
-
case "bold":
|
|
219
|
-
return 1;
|
|
220
|
-
case "italic":
|
|
221
|
-
return 1.5;
|
|
222
|
-
case "bolditalic":
|
|
223
|
-
return 2.5;
|
|
224
|
-
default:
|
|
225
|
-
return 0;
|
|
226
|
-
}
|
|
227
|
-
}, l = (f, x) => {
|
|
228
|
-
x === "bold" ? t.setFont(
|
|
229
|
-
i.font.bold.name && i.font.bold.name !== "" ? i.font.bold.name : n,
|
|
230
|
-
i.font.bold.style || "bold"
|
|
231
|
-
) : x === "italic" ? t.setFont(i.font.regular.name, "italic") : x === "bolditalic" ? t.setFont(
|
|
232
|
-
i.font.bold.name && i.font.bold.name !== "" ? i.font.bold.name : n,
|
|
233
|
-
"bolditalic"
|
|
234
|
-
) : t.setFont(i.font.regular.name, h);
|
|
235
|
-
const b = t.getTextWidth(f);
|
|
236
|
-
e.x + b > i.page.xpading + i.page.maxContentWidth && (e.x = i.page.xpading, e.y += p(t, i));
|
|
237
|
-
const C = t.getTextWidth(" ");
|
|
238
|
-
t.text(f, e.x + g, e.y, { baseline: "top" }), e.x += b + C * y(x) + (x === "normal" ? g * 0.7 : 0);
|
|
239
|
-
};
|
|
240
|
-
if (a.type === "text" && a.items && a.items.length > 0)
|
|
241
|
-
for (const f of a.items)
|
|
242
|
-
if (f.type === "em" || f.type === "strong") {
|
|
243
|
-
const x = f.type === "em" ? "italic" : "bold";
|
|
244
|
-
if (f.items && f.items.length > 0)
|
|
245
|
-
for (const b of f.items)
|
|
246
|
-
b.type === "strong" && x === "italic" || b.type === "em" && x === "bold" ? l(
|
|
247
|
-
b.content || "",
|
|
248
|
-
"bolditalic"
|
|
249
|
-
) : l(
|
|
250
|
-
b.content || "",
|
|
251
|
-
x
|
|
252
|
-
);
|
|
253
|
-
else
|
|
254
|
-
l(f.content || "", x);
|
|
255
|
-
} else
|
|
256
|
-
l(f.content || "", "normal");
|
|
257
|
-
else a.type === "em" ? l(a.content || "", "italic") : a.type === "strong" ? l(a.content || "", "bold") : l(a.content || "", "normal");
|
|
258
|
-
return t.setFont(n, h), t.setFontSize(r), e;
|
|
259
|
-
}, u = (t, a, e, g, i, n, h, r) => {
|
|
260
|
-
const y = g * i.page.indent, l = r ? `${h}. ` : "• ";
|
|
261
|
-
e.y + p(t, i) >= i.page.maxContentHeight && (c(t, i), e.y = i.page.topmargin), t.setFont(i.font.regular.name, i.font.regular.style), t.text(l, e.x + y, e.y, { baseline: "top" });
|
|
262
|
-
const f = t.getTextWidth(l);
|
|
263
|
-
if (e.x += f, a.items && a.items.length > 0)
|
|
264
|
-
for (const x of a.items) {
|
|
265
|
-
if (e.y + p(t, i) >= i.page.maxContentHeight && (c(t, i), e.y = i.page.topmargin), x.type === s.List)
|
|
266
|
-
n(x, g + 1, !0, h, x.ordered ?? !1);
|
|
267
|
-
else if (x.type === s.ListItem) {
|
|
268
|
-
const b = a.type === s.List ? g : g + 1;
|
|
269
|
-
n(x, b, !0, h, r);
|
|
270
|
-
} else
|
|
271
|
-
e = F(
|
|
272
|
-
t,
|
|
273
|
-
x,
|
|
274
|
-
e,
|
|
275
|
-
y,
|
|
276
|
-
i
|
|
277
|
-
);
|
|
278
|
-
e.x = i.page.xpading, e.y += p(t, i);
|
|
279
|
-
}
|
|
280
|
-
else if (a.content) {
|
|
281
|
-
t.text(
|
|
282
|
-
a.content,
|
|
283
|
-
e.x + y,
|
|
284
|
-
e.y,
|
|
285
|
-
{ baseline: "top" }
|
|
286
|
-
);
|
|
287
|
-
const x = t.getTextWidth(a.content);
|
|
288
|
-
e.x += x;
|
|
289
|
-
}
|
|
290
|
-
return e;
|
|
291
|
-
}, P = (t, a, e, g, i, n, h, r, y, l = !0) => {
|
|
292
|
-
if (a != null && a.items && (a == null ? void 0 : a.items.length) > 0)
|
|
293
|
-
for (const f of (a == null ? void 0 : a.items) ?? [])
|
|
294
|
-
e = h(
|
|
295
|
-
f,
|
|
296
|
-
g,
|
|
297
|
-
i,
|
|
298
|
-
r,
|
|
299
|
-
y,
|
|
300
|
-
l
|
|
301
|
-
);
|
|
302
|
-
else {
|
|
303
|
-
const f = g * n.page.indent, x = i ? y ? `${r}. ` : "• " : "", b = t.splitTextToSize(
|
|
304
|
-
x + a.content,
|
|
305
|
-
n.page.maxContentWidth - f
|
|
306
|
-
);
|
|
307
|
-
e.y + b.length * p(t, n) >= n.page.maxContentHeight && (c(t, n), e.y = n.page.topmargin), l ? (e.y = H(
|
|
308
|
-
t,
|
|
309
|
-
x + a.content,
|
|
310
|
-
e.x + f,
|
|
311
|
-
e.y,
|
|
312
|
-
n.page.maxContentWidth - f,
|
|
313
|
-
n.page.defaultLineHeightFactor
|
|
314
|
-
).y + p(t, n), e.x = n.page.xpading) : (t.text(
|
|
315
|
-
x + a.content,
|
|
316
|
-
e.x + f,
|
|
317
|
-
e.y,
|
|
318
|
-
{ baseline: "top" }
|
|
319
|
-
), e.x += t.getTextWidth(x + a.content), e.x >= n.page.xpading + n.page.maxContentWidth && (c(t, n), e.x = n.page.xpading, e.y += p(t, n)));
|
|
320
|
-
}
|
|
321
|
-
return e;
|
|
322
|
-
}, j = (t, a, e) => {
|
|
323
|
-
const g = t.internal.pageSize.getWidth();
|
|
324
|
-
return t.setLineDashPattern([1, 1], 0), t.setLineWidth(0.1), t.line(
|
|
325
|
-
e.page.xpading,
|
|
326
|
-
a.y,
|
|
327
|
-
g - e.page.xpading,
|
|
328
|
-
a.y
|
|
329
|
-
), t.setLineWidth(0.1), t.setLineDashPattern([], 0), a.y += p(t, e), a;
|
|
330
|
-
}, D = (t, a, e, g, i, n) => {
|
|
331
|
-
const h = g * n.page.indent;
|
|
332
|
-
e.y + t.splitTextToSize(
|
|
333
|
-
a.code ?? "",
|
|
334
|
-
n.page.maxContentWidth - h
|
|
335
|
-
).length * p(t, n) - 2 * p(t, n) >= n.page.maxContentHeight && (c(t, n), e.y = n.page.topmargin);
|
|
336
|
-
const r = t.splitTextToSize(
|
|
337
|
-
a.code ?? "",
|
|
338
|
-
n.page.maxContentWidth - h
|
|
339
|
-
).length * p(t, n);
|
|
340
|
-
return e.y += n.page.lineSpace, t.setFillColor("#EEEEEE"), t.setDrawColor("#eee"), t.roundedRect(
|
|
341
|
-
e.x,
|
|
342
|
-
e.y - n.page.lineSpace,
|
|
343
|
-
n.page.maxContentWidth,
|
|
344
|
-
r,
|
|
345
|
-
2,
|
|
346
|
-
2,
|
|
347
|
-
"FD"
|
|
348
|
-
), t.setFontSize(10), t.text(
|
|
349
|
-
a.lang ?? "",
|
|
350
|
-
e.x + n.page.maxContentWidth - t.getTextWidth(a.lang ?? "") - n.page.lineSpace / 2,
|
|
351
|
-
e.y
|
|
352
|
-
), t.setFontSize(n.page.defaultFontSize), t.text(a.code ?? "", e.x + 4, e.y), e.y += r, e;
|
|
353
|
-
}, R = async (t, a, e) => {
|
|
354
|
-
const g = await S(a);
|
|
355
|
-
console.log(g);
|
|
356
|
-
let i = {
|
|
357
|
-
x: e.cursor.x,
|
|
358
|
-
y: e.cursor.y
|
|
359
|
-
};
|
|
360
|
-
const n = (h, r = 0, y = !1, l = 0, f = !1, x = !0) => {
|
|
361
|
-
const b = r * e.page.indent;
|
|
362
|
-
switch (i.y + t.splitTextToSize(
|
|
363
|
-
h.content ?? "",
|
|
364
|
-
e.page.maxContentWidth - b
|
|
365
|
-
).length * p(t, e) >= e.page.maxContentHeight && (c(t, e), i.y = e.page.topmargin), h.type) {
|
|
366
|
-
case s.Heading:
|
|
367
|
-
i = w(
|
|
368
|
-
t,
|
|
369
|
-
h,
|
|
370
|
-
i,
|
|
371
|
-
b,
|
|
372
|
-
e,
|
|
373
|
-
n
|
|
374
|
-
);
|
|
375
|
-
break;
|
|
376
|
-
case s.Paragraph:
|
|
377
|
-
i = I(
|
|
378
|
-
t,
|
|
379
|
-
h,
|
|
380
|
-
i,
|
|
381
|
-
b,
|
|
382
|
-
e,
|
|
383
|
-
n
|
|
384
|
-
);
|
|
385
|
-
break;
|
|
386
|
-
case s.List:
|
|
387
|
-
i = E(
|
|
388
|
-
t,
|
|
389
|
-
h,
|
|
390
|
-
i,
|
|
391
|
-
r,
|
|
392
|
-
e,
|
|
393
|
-
n
|
|
394
|
-
);
|
|
395
|
-
break;
|
|
396
|
-
case s.ListItem:
|
|
397
|
-
i = u(
|
|
398
|
-
t,
|
|
399
|
-
h,
|
|
400
|
-
i,
|
|
401
|
-
r,
|
|
402
|
-
e,
|
|
403
|
-
n,
|
|
404
|
-
l,
|
|
405
|
-
f
|
|
406
|
-
);
|
|
407
|
-
break;
|
|
408
|
-
case s.Hr:
|
|
409
|
-
i = j(t, i, e);
|
|
410
|
-
break;
|
|
411
|
-
case s.Code:
|
|
412
|
-
i = D(
|
|
413
|
-
t,
|
|
414
|
-
h,
|
|
415
|
-
i,
|
|
416
|
-
r,
|
|
417
|
-
y,
|
|
418
|
-
e
|
|
419
|
-
);
|
|
420
|
-
break;
|
|
421
|
-
case s.Strong:
|
|
422
|
-
case s.Em:
|
|
423
|
-
i = F(
|
|
424
|
-
t,
|
|
425
|
-
h,
|
|
426
|
-
i,
|
|
427
|
-
b,
|
|
428
|
-
e
|
|
429
|
-
);
|
|
430
|
-
break;
|
|
431
|
-
case s.Raw:
|
|
432
|
-
case s.Text:
|
|
433
|
-
i = P(
|
|
434
|
-
t,
|
|
435
|
-
h,
|
|
436
|
-
i,
|
|
437
|
-
r,
|
|
438
|
-
y,
|
|
439
|
-
e,
|
|
440
|
-
n,
|
|
441
|
-
l,
|
|
442
|
-
f,
|
|
443
|
-
x
|
|
444
|
-
);
|
|
445
|
-
break;
|
|
446
|
-
default:
|
|
447
|
-
console.warn(
|
|
448
|
-
`Warning: Unsupported element type encountered: ${h.type}.
|
|
449
|
-
If you believe this element type should be supported, please create an issue at:
|
|
450
|
-
https://github.com/JeelGajera/jspdf-md-renderer/issues
|
|
451
|
-
with details of the element and expected behavior. Thank you for helping improve this library!`
|
|
452
|
-
);
|
|
453
|
-
break;
|
|
454
|
-
}
|
|
455
|
-
return i;
|
|
456
|
-
};
|
|
457
|
-
for (const h of g)
|
|
458
|
-
n(h);
|
|
459
|
-
e.endCursorYHandler(i.y);
|
|
460
|
-
};
|
|
461
|
-
export {
|
|
462
|
-
S as MdTextParser,
|
|
463
|
-
R as MdTextRender
|
|
464
|
-
};
|