jspdf-md-renderer 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -4
- package/dist/index.js +2 -2
- package/dist/index.mjs +176 -157
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,6 +69,14 @@ This section showcases how to create simple and nested lists.
|
|
|
69
69
|
1. Nested Subtopic 2.2.1
|
|
70
70
|
2. Nested Subtopic 2.2.2
|
|
71
71
|
|
|
72
|
+
### Emphasis and Strong Emphasis
|
|
73
|
+
- *Italic* text using asterisks.
|
|
74
|
+
- _Italic_ text using underscores.
|
|
75
|
+
- **Bold** text using double asterisks.
|
|
76
|
+
- __Bold__ text using double underscores.
|
|
77
|
+
- ***Bold and Italic*** text using triple asterisks.
|
|
78
|
+
- ___Bold and Italic___ text using triple underscores.
|
|
79
|
+
|
|
72
80
|
`;
|
|
73
81
|
|
|
74
82
|
const generatePDF = async () => {
|
|
@@ -146,17 +154,19 @@ The following Markdown elements are currently supported by `jspdf-md-renderer`:
|
|
|
146
154
|
- **Lists**:
|
|
147
155
|
- Unordered lists: `-`, `*`, `+`
|
|
148
156
|
- Ordered lists: `1.`, `2.`, `3.`, etc.
|
|
157
|
+
- **Horizontal Rules**: `---`, `***`, `___`
|
|
158
|
+
- **Text Styles**:
|
|
159
|
+
- Bold: `**bold**` or `__bold__`
|
|
160
|
+
- Italic: `*italic*` or `_italic_`
|
|
161
|
+
- Bold Italic: `***bold italic***` or `___bold italic___`
|
|
149
162
|
- **Code Blocks** (fenced and indented):
|
|
150
163
|
````markdown
|
|
151
164
|
```js
|
|
152
165
|
console.log('Hello, world!');
|
|
153
166
|
```
|
|
154
167
|
````
|
|
168
|
+
|
|
155
169
|
### Proposed for Future Implementation:
|
|
156
|
-
- **Emphasis**:
|
|
157
|
-
- **Bold**: `**bold text**` or `__bold text__`
|
|
158
|
-
- _Italic_: `*italic text*` or `_italic text_`
|
|
159
|
-
- **_Bold Italic_**: `***bold italic***`
|
|
160
170
|
- **Blockquotes**:
|
|
161
171
|
```markdown
|
|
162
172
|
> This is a blockquote.
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const W=require("marked");var
|
|
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
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. Thank you for helping improve this library!`);break}return i};for(const
|
|
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { marked as
|
|
2
|
-
var
|
|
3
|
-
const
|
|
4
|
-
const a = await
|
|
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
5
|
return d(a);
|
|
6
6
|
}, d = (t) => {
|
|
7
7
|
const a = [];
|
|
@@ -9,7 +9,7 @@ const W = async (t) => {
|
|
|
9
9
|
try {
|
|
10
10
|
const g = m[e.type];
|
|
11
11
|
g ? a.push(g(e)) : a.push({
|
|
12
|
-
type:
|
|
12
|
+
type: s.Raw,
|
|
13
13
|
content: e.raw
|
|
14
14
|
});
|
|
15
15
|
} catch (g) {
|
|
@@ -17,79 +17,79 @@ const W = async (t) => {
|
|
|
17
17
|
}
|
|
18
18
|
}), a;
|
|
19
19
|
}, m = {
|
|
20
|
-
[
|
|
21
|
-
type:
|
|
20
|
+
[s.Heading]: (t) => ({
|
|
21
|
+
type: s.Heading,
|
|
22
22
|
depth: t.depth,
|
|
23
23
|
content: t.text,
|
|
24
24
|
items: t.tokens ? d(t.tokens) : []
|
|
25
25
|
}),
|
|
26
|
-
[
|
|
27
|
-
type:
|
|
26
|
+
[s.Paragraph]: (t) => ({
|
|
27
|
+
type: s.Paragraph,
|
|
28
28
|
content: t.text,
|
|
29
29
|
items: t.tokens ? d(t.tokens) : []
|
|
30
30
|
}),
|
|
31
|
-
[
|
|
32
|
-
type:
|
|
31
|
+
[s.List]: (t) => ({
|
|
32
|
+
type: s.List,
|
|
33
33
|
ordered: t.ordered,
|
|
34
34
|
start: t.start,
|
|
35
35
|
items: t.items ? d(t.items) : []
|
|
36
36
|
}),
|
|
37
|
-
[
|
|
38
|
-
type:
|
|
37
|
+
[s.ListItem]: (t) => ({
|
|
38
|
+
type: s.ListItem,
|
|
39
39
|
content: t.text,
|
|
40
40
|
items: t.tokens ? d(t.tokens) : []
|
|
41
41
|
}),
|
|
42
|
-
[
|
|
43
|
-
type:
|
|
42
|
+
[s.Code]: (t) => ({
|
|
43
|
+
type: s.Code,
|
|
44
44
|
lang: t.lang,
|
|
45
45
|
code: t.text
|
|
46
46
|
}),
|
|
47
|
-
[
|
|
48
|
-
type:
|
|
47
|
+
[s.Table]: (t) => ({
|
|
48
|
+
type: s.Table,
|
|
49
49
|
header: t.header.map((a) => ({
|
|
50
|
-
type:
|
|
50
|
+
type: s.TableHeader,
|
|
51
51
|
content: a
|
|
52
52
|
})),
|
|
53
53
|
rows: t.rows.map(
|
|
54
54
|
(a) => a.map((e) => ({
|
|
55
|
-
type:
|
|
55
|
+
type: s.TableCell,
|
|
56
56
|
content: e
|
|
57
57
|
}))
|
|
58
58
|
)
|
|
59
59
|
}),
|
|
60
|
-
[
|
|
61
|
-
type:
|
|
60
|
+
[s.Image]: (t) => ({
|
|
61
|
+
type: s.Image,
|
|
62
62
|
src: t.href,
|
|
63
63
|
alt: t.text
|
|
64
64
|
}),
|
|
65
|
-
[
|
|
66
|
-
type:
|
|
65
|
+
[s.Link]: (t) => ({
|
|
66
|
+
type: s.Link,
|
|
67
67
|
href: t.href,
|
|
68
68
|
text: t.text,
|
|
69
69
|
items: t.tokens ? d(t.tokens) : []
|
|
70
70
|
}),
|
|
71
|
-
[
|
|
72
|
-
type:
|
|
71
|
+
[s.Strong]: (t) => ({
|
|
72
|
+
type: s.Strong,
|
|
73
73
|
content: t.text,
|
|
74
74
|
items: t.tokens ? d(t.tokens) : []
|
|
75
75
|
}),
|
|
76
|
-
[
|
|
77
|
-
type:
|
|
76
|
+
[s.Em]: (t) => ({
|
|
77
|
+
type: s.Em,
|
|
78
78
|
content: t.text,
|
|
79
79
|
items: t.tokens ? d(t.tokens) : []
|
|
80
80
|
}),
|
|
81
|
-
[
|
|
82
|
-
type:
|
|
81
|
+
[s.Text]: (t) => ({
|
|
82
|
+
type: s.Text,
|
|
83
83
|
content: t.text,
|
|
84
84
|
items: t.tokens ? d(t.tokens) : []
|
|
85
85
|
}),
|
|
86
|
-
[
|
|
87
|
-
type:
|
|
86
|
+
[s.Hr]: (t) => ({
|
|
87
|
+
type: s.Hr,
|
|
88
88
|
content: t.raw,
|
|
89
89
|
items: t.tokens ? d(t.tokens) : []
|
|
90
90
|
}),
|
|
91
|
-
[
|
|
92
|
-
type:
|
|
91
|
+
[s.CodeSpan]: (t) => ({
|
|
92
|
+
type: s.CodeSpan,
|
|
93
93
|
content: t.text,
|
|
94
94
|
items: t.tokens ? d(t.tokens) : []
|
|
95
95
|
})
|
|
@@ -97,8 +97,8 @@ const W = async (t) => {
|
|
|
97
97
|
var e, g;
|
|
98
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
99
|
}, p = (t, a) => t.getTextDimensions("H").h * a.page.defaultLineHeightFactor, w = (t, a, e, g, i, n) => {
|
|
100
|
-
const
|
|
101
|
-
if (t.setFontSize(i.page.defaultFontSize +
|
|
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
102
|
for (const r of (a == null ? void 0 : a.items) ?? [])
|
|
103
103
|
e = n(r, g, !1);
|
|
104
104
|
else
|
|
@@ -107,88 +107,88 @@ const W = async (t) => {
|
|
|
107
107
|
maxWidth: i.page.maxContentWidth - g
|
|
108
108
|
}), e.y += 1.5 * p(t, i);
|
|
109
109
|
return t.setFontSize(i.page.defaultFontSize), e;
|
|
110
|
-
}, L = (t, a, e, g, i, n,
|
|
111
|
-
const
|
|
112
|
-
let
|
|
113
|
-
const
|
|
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
114
|
for (const b of a)
|
|
115
|
-
t.text(b.text,
|
|
115
|
+
t.text(b.text, f, x, {
|
|
116
116
|
align: "justify",
|
|
117
117
|
lineHeightFactor: y,
|
|
118
118
|
maxWidth: r
|
|
119
|
-
}),
|
|
120
|
-
}, z = (t, a, e, g, i, n,
|
|
121
|
-
const y = t.map((
|
|
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
122
|
a.text(y, e, g + i * n, {
|
|
123
123
|
align: "justify",
|
|
124
124
|
lineHeightFactor: r,
|
|
125
|
-
maxWidth:
|
|
125
|
+
maxWidth: h
|
|
126
126
|
});
|
|
127
127
|
}, H = (t, a, e, g, i, n) => {
|
|
128
|
-
const
|
|
128
|
+
const h = {
|
|
129
129
|
x: e,
|
|
130
130
|
y: g
|
|
131
131
|
}, r = t.getTextDimensions("A").h * n, y = a.split(" ");
|
|
132
|
-
let
|
|
132
|
+
let l = 0, f = [], x = 0;
|
|
133
133
|
for (const b of y) {
|
|
134
134
|
const C = t.getTextWidth(b + "a");
|
|
135
|
-
C +
|
|
135
|
+
C + x >= i && (L(
|
|
136
136
|
t,
|
|
137
|
+
f,
|
|
137
138
|
x,
|
|
138
|
-
l
|
|
139
|
-
f++,
|
|
139
|
+
l++,
|
|
140
140
|
e,
|
|
141
141
|
g,
|
|
142
142
|
r,
|
|
143
143
|
i,
|
|
144
144
|
n
|
|
145
|
-
),
|
|
145
|
+
), f = [], x = 0), f.push({ text: b, wordLength: C }), x += C;
|
|
146
146
|
}
|
|
147
|
-
return
|
|
148
|
-
|
|
147
|
+
return f.length > 0 && z(
|
|
148
|
+
f,
|
|
149
149
|
t,
|
|
150
150
|
e,
|
|
151
151
|
g,
|
|
152
|
-
|
|
152
|
+
l,
|
|
153
153
|
r,
|
|
154
154
|
i,
|
|
155
155
|
n
|
|
156
|
-
),
|
|
157
|
-
},
|
|
156
|
+
), h.y = g + l * r, h.x = t.getTextWidth(a) + e, h;
|
|
157
|
+
}, I = (t, a, e, g, i, n) => {
|
|
158
158
|
t.setFontSize(i.page.defaultFontSize);
|
|
159
|
-
let
|
|
159
|
+
let h = a.content;
|
|
160
160
|
const r = t.getTextDimensions("A").h * i.page.defaultLineHeightFactor;
|
|
161
161
|
if (a != null && a.items && (a == null ? void 0 : a.items.length) > 0)
|
|
162
162
|
for (const y of (a == null ? void 0 : a.items) ?? [])
|
|
163
163
|
e = n(y, g, !1);
|
|
164
164
|
else {
|
|
165
165
|
if (e.y + t.splitTextToSize(
|
|
166
|
-
|
|
166
|
+
h ?? "",
|
|
167
167
|
i.page.maxContentWidth - g
|
|
168
168
|
).length * r - 3 * r >= i.page.maxContentHeight) {
|
|
169
169
|
const y = t.splitTextToSize(
|
|
170
|
-
|
|
170
|
+
h ?? "",
|
|
171
171
|
i.page.maxContentWidth - g
|
|
172
|
-
),
|
|
173
|
-
for (let
|
|
172
|
+
), l = [], f = e.y;
|
|
173
|
+
for (let x = 0; x < y.length; x++)
|
|
174
174
|
if (e.y - 2 * r < i.page.maxContentHeight)
|
|
175
|
-
|
|
175
|
+
l.push(y[x]), e.y += i.page.lineSpace;
|
|
176
176
|
else {
|
|
177
|
-
|
|
177
|
+
x <= y.length - 1 && (h = y.slice(x).join(""));
|
|
178
178
|
break;
|
|
179
179
|
}
|
|
180
|
-
|
|
180
|
+
l.length > 0 && (e = H(
|
|
181
181
|
t,
|
|
182
|
-
|
|
182
|
+
l.join(" "),
|
|
183
183
|
e.x + g,
|
|
184
|
-
|
|
184
|
+
f,
|
|
185
185
|
i.page.maxContentWidth - g,
|
|
186
186
|
i.page.defaultLineHeightFactor
|
|
187
187
|
)), c(t, i), e.y = i.page.topmargin;
|
|
188
188
|
}
|
|
189
189
|
e.y = H(
|
|
190
190
|
t,
|
|
191
|
-
|
|
191
|
+
h ?? "",
|
|
192
192
|
e.x + g,
|
|
193
193
|
e.y,
|
|
194
194
|
i.page.maxContentWidth - g,
|
|
@@ -196,23 +196,23 @@ const W = async (t) => {
|
|
|
196
196
|
).y + p(t, i), e.x = i.page.xpading;
|
|
197
197
|
}
|
|
198
198
|
return e;
|
|
199
|
-
},
|
|
200
|
-
var
|
|
199
|
+
}, E = (t, a, e, g, i, n) => {
|
|
200
|
+
var h;
|
|
201
201
|
t.setFontSize(i.page.defaultFontSize);
|
|
202
|
-
for (const [r, y] of ((
|
|
203
|
-
const
|
|
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
204
|
e = n(
|
|
205
205
|
y,
|
|
206
206
|
g + 1,
|
|
207
207
|
!0,
|
|
208
|
-
|
|
208
|
+
l,
|
|
209
209
|
a.ordered
|
|
210
210
|
), e.y += p(t, i) * 0.2;
|
|
211
211
|
}
|
|
212
212
|
return e;
|
|
213
213
|
}, F = (t, a, e, g, i) => {
|
|
214
|
-
const n = t.getFont().fontName,
|
|
215
|
-
switch (
|
|
214
|
+
const n = t.getFont().fontName, h = t.getFont().fontStyle, r = t.getFontSize(), y = (f) => {
|
|
215
|
+
switch (f) {
|
|
216
216
|
case "normal":
|
|
217
217
|
return 0;
|
|
218
218
|
case "bold":
|
|
@@ -224,80 +224,99 @@ const W = async (t) => {
|
|
|
224
224
|
default:
|
|
225
225
|
return 0;
|
|
226
226
|
}
|
|
227
|
-
},
|
|
228
|
-
|
|
227
|
+
}, l = (f, x) => {
|
|
228
|
+
x === "bold" ? t.setFont(
|
|
229
229
|
i.font.bold.name && i.font.bold.name !== "" ? i.font.bold.name : n,
|
|
230
230
|
i.font.bold.style || "bold"
|
|
231
|
-
) :
|
|
231
|
+
) : x === "italic" ? t.setFont(i.font.regular.name, "italic") : x === "bolditalic" ? t.setFont(
|
|
232
232
|
i.font.bold.name && i.font.bold.name !== "" ? i.font.bold.name : n,
|
|
233
233
|
"bolditalic"
|
|
234
|
-
) : t.setFont(i.font.regular.name,
|
|
235
|
-
const b = t.getTextWidth(
|
|
234
|
+
) : t.setFont(i.font.regular.name, h);
|
|
235
|
+
const b = t.getTextWidth(f);
|
|
236
236
|
e.x + b > i.page.xpading + i.page.maxContentWidth && (e.x = i.page.xpading, e.y += p(t, i));
|
|
237
237
|
const C = t.getTextWidth(" ");
|
|
238
|
-
t.text(
|
|
238
|
+
t.text(f, e.x + g, e.y, { baseline: "top" }), e.x += b + C * y(x) + (x === "normal" ? g * 0.7 : 0);
|
|
239
239
|
};
|
|
240
240
|
if (a.type === "text" && a.items && a.items.length > 0)
|
|
241
|
-
for (const
|
|
242
|
-
if (
|
|
243
|
-
const
|
|
244
|
-
if (
|
|
245
|
-
for (const b of
|
|
246
|
-
b.type === "strong" &&
|
|
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
247
|
b.content || "",
|
|
248
248
|
"bolditalic"
|
|
249
|
-
) :
|
|
249
|
+
) : l(
|
|
250
250
|
b.content || "",
|
|
251
|
-
|
|
251
|
+
x
|
|
252
252
|
);
|
|
253
253
|
else
|
|
254
|
-
f
|
|
254
|
+
l(f.content || "", x);
|
|
255
255
|
} else
|
|
256
|
-
f
|
|
257
|
-
else a.type === "em" ?
|
|
258
|
-
return t.setFont(n,
|
|
259
|
-
},
|
|
260
|
-
const y = g * i.page.indent,
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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) => {
|
|
273
292
|
if (a != null && a.items && (a == null ? void 0 : a.items.length) > 0)
|
|
274
|
-
for (const
|
|
275
|
-
e =
|
|
276
|
-
|
|
293
|
+
for (const f of (a == null ? void 0 : a.items) ?? [])
|
|
294
|
+
e = h(
|
|
295
|
+
f,
|
|
277
296
|
g,
|
|
278
297
|
i,
|
|
279
298
|
r,
|
|
280
299
|
y,
|
|
281
|
-
|
|
300
|
+
l
|
|
282
301
|
);
|
|
283
302
|
else {
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
n.page.maxContentWidth -
|
|
303
|
+
const f = g * n.page.indent, x = i ? y ? `${r}. ` : "• " : "", b = t.splitTextToSize(
|
|
304
|
+
x + a.content,
|
|
305
|
+
n.page.maxContentWidth - f
|
|
287
306
|
);
|
|
288
|
-
e.y + b.length * p(t, n) >= n.page.maxContentHeight && (c(t, n), e.y = n.page.topmargin),
|
|
307
|
+
e.y + b.length * p(t, n) >= n.page.maxContentHeight && (c(t, n), e.y = n.page.topmargin), l ? (e.y = H(
|
|
289
308
|
t,
|
|
290
|
-
|
|
291
|
-
e.x +
|
|
309
|
+
x + a.content,
|
|
310
|
+
e.x + f,
|
|
292
311
|
e.y,
|
|
293
|
-
n.page.maxContentWidth -
|
|
312
|
+
n.page.maxContentWidth - f,
|
|
294
313
|
n.page.defaultLineHeightFactor
|
|
295
314
|
).y + p(t, n), e.x = n.page.xpading) : (t.text(
|
|
296
|
-
|
|
297
|
-
e.x +
|
|
315
|
+
x + a.content,
|
|
316
|
+
e.x + f,
|
|
298
317
|
e.y,
|
|
299
318
|
{ baseline: "top" }
|
|
300
|
-
), e.x += t.getTextWidth(
|
|
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)));
|
|
301
320
|
}
|
|
302
321
|
return e;
|
|
303
322
|
}, j = (t, a, e) => {
|
|
@@ -309,14 +328,14 @@ const W = async (t) => {
|
|
|
309
328
|
a.y
|
|
310
329
|
), t.setLineWidth(0.1), t.setLineDashPattern([], 0), a.y += p(t, e), a;
|
|
311
330
|
}, D = (t, a, e, g, i, n) => {
|
|
312
|
-
const
|
|
331
|
+
const h = g * n.page.indent;
|
|
313
332
|
e.y + t.splitTextToSize(
|
|
314
333
|
a.code ?? "",
|
|
315
|
-
n.page.maxContentWidth -
|
|
334
|
+
n.page.maxContentWidth - h
|
|
316
335
|
).length * p(t, n) - 2 * p(t, n) >= n.page.maxContentHeight && (c(t, n), e.y = n.page.topmargin);
|
|
317
336
|
const r = t.splitTextToSize(
|
|
318
337
|
a.code ?? "",
|
|
319
|
-
n.page.maxContentWidth -
|
|
338
|
+
n.page.maxContentWidth - h
|
|
320
339
|
).length * p(t, n);
|
|
321
340
|
return e.y += n.page.lineSpace, t.setFillColor("#EEEEEE"), t.setDrawColor("#eee"), t.roundedRect(
|
|
322
341
|
e.x,
|
|
@@ -331,102 +350,102 @@ const W = async (t) => {
|
|
|
331
350
|
e.x + n.page.maxContentWidth - t.getTextWidth(a.lang ?? "") - n.page.lineSpace / 2,
|
|
332
351
|
e.y
|
|
333
352
|
), t.setFontSize(n.page.defaultFontSize), t.text(a.code ?? "", e.x + 4, e.y), e.y += r, e;
|
|
334
|
-
},
|
|
335
|
-
const g = await
|
|
353
|
+
}, R = async (t, a, e) => {
|
|
354
|
+
const g = await S(a);
|
|
336
355
|
console.log(g);
|
|
337
356
|
let i = {
|
|
338
357
|
x: e.cursor.x,
|
|
339
358
|
y: e.cursor.y
|
|
340
359
|
};
|
|
341
|
-
const n = (
|
|
360
|
+
const n = (h, r = 0, y = !1, l = 0, f = !1, x = !0) => {
|
|
342
361
|
const b = r * e.page.indent;
|
|
343
362
|
switch (i.y + t.splitTextToSize(
|
|
344
|
-
|
|
363
|
+
h.content ?? "",
|
|
345
364
|
e.page.maxContentWidth - b
|
|
346
|
-
).length * p(t, e) >= e.page.maxContentHeight && (c(t, e), i.y = e.page.topmargin),
|
|
347
|
-
case
|
|
365
|
+
).length * p(t, e) >= e.page.maxContentHeight && (c(t, e), i.y = e.page.topmargin), h.type) {
|
|
366
|
+
case s.Heading:
|
|
348
367
|
i = w(
|
|
349
368
|
t,
|
|
350
|
-
|
|
369
|
+
h,
|
|
351
370
|
i,
|
|
352
371
|
b,
|
|
353
372
|
e,
|
|
354
373
|
n
|
|
355
374
|
);
|
|
356
375
|
break;
|
|
357
|
-
case
|
|
358
|
-
i =
|
|
376
|
+
case s.Paragraph:
|
|
377
|
+
i = I(
|
|
359
378
|
t,
|
|
360
|
-
|
|
379
|
+
h,
|
|
361
380
|
i,
|
|
362
381
|
b,
|
|
363
382
|
e,
|
|
364
383
|
n
|
|
365
384
|
);
|
|
366
385
|
break;
|
|
367
|
-
case
|
|
368
|
-
i =
|
|
386
|
+
case s.List:
|
|
387
|
+
i = E(
|
|
369
388
|
t,
|
|
370
|
-
|
|
389
|
+
h,
|
|
371
390
|
i,
|
|
372
391
|
r,
|
|
373
392
|
e,
|
|
374
393
|
n
|
|
375
394
|
);
|
|
376
395
|
break;
|
|
377
|
-
case
|
|
378
|
-
i =
|
|
396
|
+
case s.ListItem:
|
|
397
|
+
i = u(
|
|
379
398
|
t,
|
|
380
|
-
|
|
399
|
+
h,
|
|
381
400
|
i,
|
|
382
401
|
r,
|
|
383
402
|
e,
|
|
384
403
|
n,
|
|
385
|
-
|
|
386
|
-
|
|
404
|
+
l,
|
|
405
|
+
f
|
|
387
406
|
);
|
|
388
407
|
break;
|
|
389
|
-
case
|
|
408
|
+
case s.Hr:
|
|
390
409
|
i = j(t, i, e);
|
|
391
410
|
break;
|
|
392
|
-
case
|
|
411
|
+
case s.Code:
|
|
393
412
|
i = D(
|
|
394
413
|
t,
|
|
395
|
-
|
|
414
|
+
h,
|
|
396
415
|
i,
|
|
397
416
|
r,
|
|
398
417
|
y,
|
|
399
418
|
e
|
|
400
419
|
);
|
|
401
420
|
break;
|
|
402
|
-
case
|
|
403
|
-
case
|
|
421
|
+
case s.Strong:
|
|
422
|
+
case s.Em:
|
|
404
423
|
i = F(
|
|
405
424
|
t,
|
|
406
|
-
|
|
425
|
+
h,
|
|
407
426
|
i,
|
|
408
427
|
b,
|
|
409
428
|
e
|
|
410
429
|
);
|
|
411
430
|
break;
|
|
412
|
-
case
|
|
413
|
-
case
|
|
414
|
-
i =
|
|
431
|
+
case s.Raw:
|
|
432
|
+
case s.Text:
|
|
433
|
+
i = P(
|
|
415
434
|
t,
|
|
416
|
-
|
|
435
|
+
h,
|
|
417
436
|
i,
|
|
418
437
|
r,
|
|
419
438
|
y,
|
|
420
439
|
e,
|
|
421
440
|
n,
|
|
441
|
+
l,
|
|
422
442
|
f,
|
|
423
|
-
x
|
|
424
|
-
l
|
|
443
|
+
x
|
|
425
444
|
);
|
|
426
445
|
break;
|
|
427
446
|
default:
|
|
428
447
|
console.warn(
|
|
429
|
-
`Warning: Unsupported element type encountered: ${
|
|
448
|
+
`Warning: Unsupported element type encountered: ${h.type}.
|
|
430
449
|
If you believe this element type should be supported, please create an issue at:
|
|
431
450
|
https://github.com/JeelGajera/jspdf-md-renderer/issues
|
|
432
451
|
with details of the element and expected behavior. Thank you for helping improve this library!`
|
|
@@ -435,11 +454,11 @@ const W = async (t) => {
|
|
|
435
454
|
}
|
|
436
455
|
return i;
|
|
437
456
|
};
|
|
438
|
-
for (const
|
|
439
|
-
n(
|
|
457
|
+
for (const h of g)
|
|
458
|
+
n(h);
|
|
440
459
|
e.endCursorYHandler(i.y);
|
|
441
460
|
};
|
|
442
461
|
export {
|
|
443
|
-
|
|
444
|
-
|
|
462
|
+
S as MdTextParser,
|
|
463
|
+
R as MdTextRender
|
|
445
464
|
};
|