jspdf-md-renderer 3.4.1 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,641 +1,1148 @@
1
- import { marked as e } from "marked";
2
- import t from "jspdf-autotable";
3
- //#region src/enums/mdTokenType.ts
4
- var n = /* @__PURE__ */ function(e) {
5
- 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.Br = "br", e;
6
- }({}), r = "__jmr_", i = /(!\[[^\]]*\]\()([^)]+)(\))\s*\{([^}]+)\}/g, a = /(\w+)\s*=\s*(\w+)/g, o = [
1
+ /*!
2
+ * jspdf-md-renderer
3
+ *
4
+ * MIT License
5
+ *
6
+ * Copyright (c) 2026 Jeel Gajera
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ *
26
+ */
27
+ import { marked } from "marked";
28
+ import autoTable from "jspdf-autotable";
29
+ //#region src/parser/imageExtension.ts
30
+ /**
31
+ * Internal hash prefix used to encode image attributes in the URL fragment.
32
+ * This is stripped during token conversion and never reaches the image fetcher.
33
+ */
34
+ const ATTR_HASH_PREFIX = "__jmr_";
35
+ /**
36
+ * Regex to match an image tag followed by an attribute block.
37
+ * Captures:
38
+ * Group 1: Everything before the closing `)` (i.e., `![alt](url`)
39
+ * Group 2: The image URL inside the parentheses
40
+ * Group 3: The attribute block content (e.g., `width=200 height=150 align=center`)
41
+ *
42
+ * Pattern: ![...](url){key=value ...}
43
+ */
44
+ const IMAGE_WITH_ATTRS_REGEX = /(!\[[^\]]*\]\()([^)]+)(\))\s*\{([^}]+)\}/g;
45
+ /**
46
+ * Regex to extract individual key=value pairs from the attribute block.
47
+ */
48
+ const ATTR_PAIR_REGEX = /(\w+)\s*=\s*(\w+)/g;
49
+ /** Valid alignment values */
50
+ const VALID_ALIGNMENTS = [
7
51
  "left",
8
52
  "center",
9
53
  "right"
10
- ], s = (e) => {
11
- let t = [];
12
- 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 ? `#${r}${t.join("&")}` : "";
13
- }, c = (e) => {
14
- let t = {}, n;
15
- for (; (n = a.exec(e)) !== null;) {
16
- let e = n[1].toLowerCase(), r = n[2];
17
- switch (e) {
54
+ ];
55
+ /**
56
+ * Encodes image attributes into a URL hash fragment.
57
+ * Example: {width: 200, height: 100, align: 'center'} → '#__jmr_w=200&h=100&a=center'
58
+ */
59
+ const encodeAttrsToFragment = (attrs) => {
60
+ const parts = [];
61
+ if (attrs.width !== void 0) parts.push(`w=${attrs.width}`);
62
+ if (attrs.height !== void 0) parts.push(`h=${attrs.height}`);
63
+ if (attrs.align) parts.push(`a=${attrs.align}`);
64
+ return parts.length > 0 ? `#${ATTR_HASH_PREFIX}${parts.join("&")}` : "";
65
+ };
66
+ /**
67
+ * Parses an attribute string like "width=200 height=150 align=center"
68
+ * into a structured object.
69
+ */
70
+ const parseRawAttributes = (attrString) => {
71
+ const attrs = {};
72
+ let match;
73
+ while ((match = ATTR_PAIR_REGEX.exec(attrString)) !== null) {
74
+ const key = match[1].toLowerCase();
75
+ const value = match[2];
76
+ switch (key) {
18
77
  case "width":
19
78
  case "w": {
20
- let e = parseInt(r, 10);
21
- !isNaN(e) && e > 0 && (t.width = e);
79
+ const num = parseInt(value, 10);
80
+ if (!isNaN(num) && num > 0) attrs.width = num;
22
81
  break;
23
82
  }
24
83
  case "height":
25
84
  case "h": {
26
- let e = parseInt(r, 10);
27
- !isNaN(e) && e > 0 && (t.height = e);
85
+ const num = parseInt(value, 10);
86
+ if (!isNaN(num) && num > 0) attrs.height = num;
28
87
  break;
29
88
  }
30
89
  case "align": {
31
- let e = r.toLowerCase();
32
- o.includes(e) && (t.align = e);
90
+ const alignVal = value.toLowerCase();
91
+ if (VALID_ALIGNMENTS.includes(alignVal)) attrs.align = alignVal;
33
92
  break;
34
93
  }
35
94
  }
36
95
  }
37
- return t;
38
- }, l = (e) => e.replace(i, (e, t, n, r, i) => `${t}${n}${s(c(i))}${r}`), u = (e) => {
39
- let t = e.indexOf(`#${r}`);
40
- if (t === -1) return {
41
- cleanHref: e,
96
+ return attrs;
97
+ };
98
+ /**
99
+ * Pre-processes markdown text to embed image attributes into URL fragments.
100
+ *
101
+ * Transforms `![alt](url){width=200 align=center}` into
102
+ * `![alt](url#__jmr_w=200&a=center)` so that each image token
103
+ * carries its own attributes — no shared state needed.
104
+ *
105
+ * Supported attributes:
106
+ * - `width` or `w`: Image width in pixels (number)
107
+ * - `height` or `h`: Image height in pixels (number)
108
+ * - `align`: Image alignment - 'left', 'center', or 'right'
109
+ *
110
+ * @param text - The raw markdown text
111
+ * @returns The cleaned markdown text with attributes encoded in URLs
112
+ */
113
+ const preprocessImageAttributes = (text) => {
114
+ return text.replace(IMAGE_WITH_ATTRS_REGEX, (_fullMatch, before, url, closeParen, attrsContent) => {
115
+ return `${before}${url}${encodeAttrsToFragment(parseRawAttributes(attrsContent))}${closeParen}`;
116
+ });
117
+ };
118
+ /**
119
+ * Extracts image attributes from a URL that may contain an encoded fragment.
120
+ * Returns the clean URL (without the attribute fragment) and parsed attributes.
121
+ *
122
+ * @param href - The image URL, possibly containing `#__jmr_...` fragment
123
+ * @returns Object with cleanHref and parsed attrs
124
+ */
125
+ const parseImageAttrsFromHref = (href) => {
126
+ const fragmentIdx = href.indexOf(`#${ATTR_HASH_PREFIX}`);
127
+ if (fragmentIdx === -1) return {
128
+ cleanHref: href,
42
129
  attrs: {}
43
130
  };
44
- let n = e.substring(0, t), i = e.substring(t + 1 + 6), a = {}, s = i.split("&");
45
- for (let e of s) {
46
- let [t, n] = e.split("=");
47
- switch (t) {
131
+ const cleanHref = href.substring(0, fragmentIdx);
132
+ const fragment = href.substring(fragmentIdx + 1 + 6);
133
+ const attrs = {};
134
+ const pairs = fragment.split("&");
135
+ for (const pair of pairs) {
136
+ const [key, value] = pair.split("=");
137
+ switch (key) {
48
138
  case "w": {
49
- let e = parseInt(n, 10);
50
- !isNaN(e) && e > 0 && (a.width = e);
139
+ const num = parseInt(value, 10);
140
+ if (!isNaN(num) && num > 0) attrs.width = num;
51
141
  break;
52
142
  }
53
143
  case "h": {
54
- let e = parseInt(n, 10);
55
- !isNaN(e) && e > 0 && (a.height = e);
144
+ const num = parseInt(value, 10);
145
+ if (!isNaN(num) && num > 0) attrs.height = num;
56
146
  break;
57
147
  }
58
148
  case "a":
59
- o.includes(n) && (a.align = n);
149
+ if (VALID_ALIGNMENTS.includes(value)) attrs.align = value;
60
150
  break;
61
151
  }
62
152
  }
63
153
  return {
64
- cleanHref: n,
65
- attrs: a
154
+ cleanHref,
155
+ attrs
66
156
  };
67
- }, d = async (t) => {
68
- let n = l(t);
69
- return f(await e.lexer(n, {
70
- async: !0,
71
- gfm: !0
157
+ };
158
+ //#endregion
159
+ //#region src/parser/MdTextParser.ts
160
+ /**
161
+ * Parses markdown into tokens and converts to a custom parsed structure.
162
+ *
163
+ * @param text - The markdown content to parse.
164
+ * @returns Parsed markdown elements.
165
+ */
166
+ const MdTextParser = async (text) => {
167
+ const processedText = preprocessImageAttributes(text);
168
+ return convertTokens(await marked.lexer(processedText, {
169
+ async: true,
170
+ gfm: true
72
171
  }));
73
- }, f = (e) => {
74
- let t = [];
75
- return e.forEach((e) => {
172
+ };
173
+ /**
174
+ * Convert the markdown tokens to ParsedElements.
175
+ *
176
+ * @param tokens - The list of markdown tokens.
177
+ * @returns Parsed elements in a custom structure.
178
+ */
179
+ const convertTokens = (tokens) => {
180
+ const parsedElements = [];
181
+ tokens.forEach((token) => {
76
182
  try {
77
- let r = p[e.type];
78
- r ? t.push(r(e)) : t.push({
79
- type: n.Raw,
80
- content: e.raw
183
+ const handler = tokenHandlers[token.type];
184
+ if (handler) parsedElements.push(handler(token));
185
+ else parsedElements.push({
186
+ type: "raw",
187
+ content: token.raw
81
188
  });
82
- } catch (t) {
83
- console.error("Failed to handle token ==>", e, t);
84
- }
85
- }), t;
86
- }, p = {
87
- [n.Heading]: (e) => ({
88
- type: n.Heading,
89
- depth: e.depth,
90
- content: e.text,
91
- items: e.tokens ? f(e.tokens) : []
189
+ } catch (error) {
190
+ console.error("Failed to handle token ==>", token, error);
191
+ }
192
+ });
193
+ return parsedElements;
194
+ };
195
+ /**
196
+ * Map each token type to its handler function.
197
+ */
198
+ const tokenHandlers = {
199
+ ["heading"]: (token) => ({
200
+ type: "heading",
201
+ depth: token.depth,
202
+ content: token.text,
203
+ items: token.tokens ? convertTokens(token.tokens) : []
92
204
  }),
93
- [n.Paragraph]: (e) => ({
94
- type: n.Paragraph,
95
- content: e.text,
96
- items: e.tokens ? f(e.tokens) : []
205
+ ["paragraph"]: (token) => ({
206
+ type: "paragraph",
207
+ content: token.text,
208
+ items: token.tokens ? convertTokens(token.tokens) : []
97
209
  }),
98
- [n.List]: (e) => ({
99
- type: n.List,
100
- ordered: e.ordered,
101
- start: e.start,
102
- items: e.items ? f(e.items) : []
210
+ ["list"]: (token) => ({
211
+ type: "list",
212
+ ordered: token.ordered,
213
+ start: token.start,
214
+ items: token.items ? convertTokens(token.items) : []
103
215
  }),
104
- [n.ListItem]: (e) => ({
105
- type: n.ListItem,
106
- content: e.text,
107
- items: e.tokens ? f(e.tokens) : []
216
+ ["list_item"]: (token) => ({
217
+ type: "list_item",
218
+ content: token.text,
219
+ items: token.tokens ? convertTokens(token.tokens) : []
108
220
  }),
109
- [n.Code]: (e) => ({
110
- type: n.Code,
111
- lang: e.lang,
112
- code: e.text
221
+ ["code"]: (token) => ({
222
+ type: "code",
223
+ lang: token.lang,
224
+ code: token.text
113
225
  }),
114
- [n.Table]: (e) => ({
115
- type: n.Table,
116
- header: e.header.map((e) => ({
117
- type: n.TableHeader,
118
- content: e.text
226
+ ["table"]: (token) => ({
227
+ type: "table",
228
+ header: token.header.map((header) => ({
229
+ type: "table_header",
230
+ content: header.text
119
231
  })),
120
- rows: e.rows.map((e) => e.map((e) => ({
121
- type: n.TableCell,
122
- content: e.text
232
+ rows: token.rows.map((row) => row.map((cell) => ({
233
+ type: "table_cell",
234
+ content: cell.text
123
235
  })))
124
236
  }),
125
- [n.Image]: (e) => {
126
- let { cleanHref: t, attrs: r } = u(e.href);
237
+ ["image"]: (token) => {
238
+ const { cleanHref, attrs } = parseImageAttrsFromHref(token.href);
127
239
  return {
128
- type: n.Image,
129
- src: t,
130
- alt: e.text,
131
- width: r.width,
132
- height: r.height,
133
- align: r.align
240
+ type: "image",
241
+ src: cleanHref,
242
+ alt: token.text,
243
+ width: attrs.width,
244
+ height: attrs.height,
245
+ align: attrs.align
134
246
  };
135
247
  },
136
- [n.Link]: (e) => ({
137
- type: n.Link,
138
- href: e.href,
139
- text: e.text,
140
- items: e.tokens ? f(e.tokens) : []
248
+ ["link"]: (token) => ({
249
+ type: "link",
250
+ href: token.href,
251
+ text: token.text,
252
+ items: token.tokens ? convertTokens(token.tokens) : []
141
253
  }),
142
- [n.Strong]: (e) => ({
143
- type: n.Strong,
144
- content: e.text,
145
- items: e.tokens ? f(e.tokens) : []
254
+ ["strong"]: (token) => ({
255
+ type: "strong",
256
+ content: token.text,
257
+ items: token.tokens ? convertTokens(token.tokens) : []
146
258
  }),
147
- [n.Em]: (e) => ({
148
- type: n.Em,
149
- content: e.text,
150
- items: e.tokens ? f(e.tokens) : []
259
+ ["em"]: (token) => ({
260
+ type: "em",
261
+ content: token.text,
262
+ items: token.tokens ? convertTokens(token.tokens) : []
151
263
  }),
152
- [n.Text]: (e) => ({
153
- type: n.Text,
154
- content: e.text,
155
- items: e.tokens ? f(e.tokens) : []
264
+ ["text"]: (token) => ({
265
+ type: "text",
266
+ content: token.text,
267
+ items: token.tokens ? convertTokens(token.tokens) : []
156
268
  }),
157
- [n.Hr]: (e) => ({
158
- type: n.Hr,
159
- content: e.raw,
160
- items: e.tokens ? f(e.tokens) : []
269
+ ["hr"]: (token) => ({
270
+ type: "hr",
271
+ content: token.raw,
272
+ items: token.tokens ? convertTokens(token.tokens) : []
161
273
  }),
162
- [n.CodeSpan]: (e) => ({
163
- type: n.CodeSpan,
164
- content: e.text,
165
- items: e.tokens ? f(e.tokens) : []
274
+ ["codespan"]: (token) => ({
275
+ type: "codespan",
276
+ content: token.text,
277
+ items: token.tokens ? convertTokens(token.tokens) : []
166
278
  }),
167
- [n.Blockquote]: (e) => ({
168
- type: n.Blockquote,
169
- content: e.text,
170
- items: e.tokens ? f(e.tokens) : []
279
+ ["blockquote"]: (token) => ({
280
+ type: "blockquote",
281
+ content: token.text,
282
+ items: token.tokens ? convertTokens(token.tokens) : []
171
283
  }),
172
- [n.Html]: (e) => {
173
- let t = String(e.raw ?? e.text ?? "").trim();
174
- return /^<br\s*\/?>$/i.test(t) ? {
175
- type: n.Br,
284
+ ["html"]: (token) => {
285
+ const rawHtml = String(token.raw ?? token.text ?? "").trim();
286
+ if (/^<br\s*\/?>$/i.test(rawHtml)) return {
287
+ type: "br",
176
288
  content: "\n"
177
- } : {
178
- type: n.Raw,
179
- content: e.raw ?? e.text ?? ""
289
+ };
290
+ return {
291
+ type: "raw",
292
+ content: token.raw ?? token.text ?? ""
180
293
  };
181
294
  },
182
- [n.Br]: () => ({
183
- type: n.Br,
295
+ ["br"]: () => ({
296
+ type: "br",
184
297
  content: "\n"
185
298
  })
186
- }, m = (e) => e.getTextDimensions("H").h, h = (e) => e.getTextDimensions("H").w, g = (e, t, n, r, i) => {
187
- let a = 6 - (t?.depth ?? 0) > 0 ? 6 - (t?.depth ?? 0) : 1;
188
- if (e.setFontSize(r.options.page.defaultFontSize + a), t?.items && t?.items.length > 0) for (let e of t?.items ?? []) i(e, n, r, !1);
189
- else {
190
- let i = m(e);
191
- e.text(t?.content ?? "", r.X + n, r.Y, {
192
- align: "left",
193
- maxWidth: r.options.page.maxContentWidth - n,
194
- baseline: "top"
195
- }), r.recordContentY(r.Y + i), r.updateY(m(e), "add");
196
- }
197
- e.setFontSize(r.options.page.defaultFontSize), r.updateX(r.options.page.xpading, "set");
198
- }, _ = (e, t) => {
199
- typeof t.options.pageBreakHandler == "function" ? t.options.pageBreakHandler(e) : e.addPage(t.options.page?.format, t.options.page?.orientation), t.updateY(t.options.page.topmargin), t.updateX(t.options.page.xpading);
200
- }, v = 96, y = (e, t = "mm") => {
201
- switch (t) {
202
- case "pt": return e * 72 / v;
203
- case "in": return e / v;
204
- case "px": return e;
205
- default: return e * 25.4 / v;
206
- }
207
- }, b = (e) => {
299
+ };
300
+ //#endregion
301
+ //#region src/utils/doc-helpers.ts
302
+ const getCharHight = (doc) => {
303
+ return doc.getTextDimensions("H").h;
304
+ };
305
+ const getCharWidth = (doc) => {
306
+ return doc.getTextDimensions("H").w;
307
+ };
308
+ //#endregion
309
+ //#region src/utils/handlePageBreak.ts
310
+ const HandlePageBreaks = (doc, store) => {
311
+ if (typeof store.options.pageBreakHandler === "function") store.options.pageBreakHandler(doc);
312
+ else doc.addPage(store.options.page?.format, store.options.page?.orientation);
313
+ store.updateY(store.options.page.topmargin);
314
+ store.updateX(store.options.page.xpading);
315
+ };
316
+ //#endregion
317
+ //#region src/utils/image-utils.ts
318
+ /**
319
+ * Standard DPI for web/screen pixels.
320
+ */
321
+ const DEFAULT_DPI = 96;
322
+ /**
323
+ * Converts pixel values to the document's unit system.
324
+ * Uses 96 DPI as the standard web pixel density.
325
+ *
326
+ * @param px - Value in pixels
327
+ * @param unit - The document unit ('mm' | 'pt' | 'in' | 'px')
328
+ * @returns Value in document units
329
+ */
330
+ const pxToDocUnit = (px, unit = "mm") => {
331
+ switch (unit) {
332
+ case "pt": return px * 72 / DEFAULT_DPI;
333
+ case "in": return px / DEFAULT_DPI;
334
+ case "px": return px;
335
+ default: return px * 25.4 / DEFAULT_DPI;
336
+ }
337
+ };
338
+ /**
339
+ * Extracts width and height from an SVG data URI if possible.
340
+ */
341
+ const extractSvgDimensions = (dataUri) => {
208
342
  try {
209
- let t = "";
210
- if (e.includes("base64,")) {
211
- let n = e.split("base64,")[1];
212
- 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)));
213
- } else t = decodeURIComponent(e.split(",")[1] || "");
214
- 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;
215
- if ((!a || !o) && i) {
216
- let e = i[0].match(/viewBox=(?:'|")([^'"]+)(?:'|")/i);
217
- if (e) {
218
- let t = e[1].split(/[ ,]+/).filter(Boolean).map(parseFloat);
219
- t.length >= 4 && (a ||= t[2], o ||= t[3]);
343
+ let svgString = "";
344
+ if (dataUri.includes("base64,")) {
345
+ const base64 = dataUri.split("base64,")[1];
346
+ if (typeof window !== "undefined" && typeof window.atob === "function") svgString = decodeURIComponent(escape(window.atob(base64)));
347
+ else if (typeof Buffer !== "undefined") svgString = Buffer.from(base64, "base64").toString("utf-8");
348
+ else svgString = decodeURIComponent(escape(atob(base64)));
349
+ } else svgString = decodeURIComponent(dataUri.split(",")[1] || "");
350
+ const widthMatch = svgString.match(/<svg[^>]*\swidth=(?:'|")([0-9.]+)[a-zA-Z]*(?:'|")/i);
351
+ const heightMatch = svgString.match(/<svg[^>]*\sheight=(?:'|")([0-9.]+)[a-zA-Z]*(?:'|")/i);
352
+ const viewBoxMatch = svgString.match(/<svg[^>]*\sviewBox=(?:'|")[^'"]*(?:'|")/i);
353
+ let w = widthMatch ? parseFloat(widthMatch[1]) : 0;
354
+ let h = heightMatch ? parseFloat(heightMatch[1]) : 0;
355
+ if ((!w || !h) && viewBoxMatch) {
356
+ const viewBoxStr = viewBoxMatch[0].match(/viewBox=(?:'|")([^'"]+)(?:'|")/i);
357
+ if (viewBoxStr) {
358
+ const parts = viewBoxStr[1].split(/[ ,]+/).filter(Boolean).map(parseFloat);
359
+ if (parts.length >= 4) {
360
+ w = w || parts[2];
361
+ h = h || parts[3];
362
+ }
220
363
  }
221
364
  }
222
- if (a > 0 && o > 0) return {
223
- width: a,
224
- height: o
365
+ if (w > 0 && h > 0) return {
366
+ width: w,
367
+ height: h
225
368
  };
226
369
  } catch (e) {
227
370
  console.warn("Failed to extract SVG dimensions:", e);
228
371
  }
229
372
  return null;
230
- }, x = (e, t, n, r, i = "mm") => {
231
- if (!t.data) return {
373
+ };
374
+ /**
375
+ * Calculates final dimensions for an image, respecting intrinsic size,
376
+ * user-specified attributes, and page bounds.
377
+ */
378
+ const calculateImageDimensions = (doc, element, maxWidth, maxHeight, docUnit = "mm") => {
379
+ if (!element.data) return {
232
380
  finalWidth: 0,
233
381
  finalHeight: 0
234
382
  };
235
- let a = t.naturalWidth || 0, o = t.naturalHeight || 0;
236
- if (!a || !o) if (t.data.startsWith("data:image/svg")) {
237
- let e = b(t.data);
238
- e && (a = e.width, o = e.height);
239
- } else try {
240
- let n = e.getImageProperties(t.data);
241
- a = n.width, o = n.height;
383
+ let intrinsicPxW = element.naturalWidth || 0;
384
+ let intrinsicPxH = element.naturalHeight || 0;
385
+ if (!intrinsicPxW || !intrinsicPxH) if (!element.data.startsWith("data:image/svg")) try {
386
+ const props = doc.getImageProperties(element.data);
387
+ intrinsicPxW = props.width;
388
+ intrinsicPxH = props.height;
242
389
  } catch (e) {
243
390
  console.warn("Failed to get image properties for intrinsic sizing:", e);
244
391
  }
245
- let s = o > 0 ? a / o : 1, c, l;
246
- if (t.width && t.height ? (c = y(t.width, i), l = y(t.height, i)) : t.width ? (c = y(t.width, i), l = c / s) : t.height ? (l = y(t.height, i), c = l * s) : (c = y(a, i), l = y(o, i)), c > n) {
247
- let e = n / c;
248
- c = n, l *= e;
392
+ else {
393
+ const svgDims = extractSvgDimensions(element.data);
394
+ if (svgDims) {
395
+ intrinsicPxW = svgDims.width;
396
+ intrinsicPxH = svgDims.height;
397
+ }
398
+ }
399
+ const aspectRatio = intrinsicPxH > 0 ? intrinsicPxW / intrinsicPxH : 1;
400
+ let finalWidth;
401
+ let finalHeight;
402
+ if (element.width && element.height) {
403
+ finalWidth = pxToDocUnit(element.width, docUnit);
404
+ finalHeight = pxToDocUnit(element.height, docUnit);
405
+ } else if (element.width) {
406
+ finalWidth = pxToDocUnit(element.width, docUnit);
407
+ finalHeight = finalWidth / aspectRatio;
408
+ } else if (element.height) {
409
+ finalHeight = pxToDocUnit(element.height, docUnit);
410
+ finalWidth = finalHeight * aspectRatio;
411
+ } else {
412
+ finalWidth = pxToDocUnit(intrinsicPxW, docUnit);
413
+ finalHeight = pxToDocUnit(intrinsicPxH, docUnit);
414
+ }
415
+ if (finalWidth > maxWidth) {
416
+ const scale = maxWidth / finalWidth;
417
+ finalWidth = maxWidth;
418
+ finalHeight = finalHeight * scale;
249
419
  }
250
- if (l > r) {
251
- let e = r / l;
252
- l = r, c *= e;
420
+ if (finalHeight > maxHeight) {
421
+ const scale = maxHeight / finalHeight;
422
+ finalHeight = maxHeight;
423
+ finalWidth = finalWidth * scale;
253
424
  }
254
425
  return {
255
- finalWidth: c,
256
- finalHeight: l
426
+ finalWidth,
427
+ finalHeight
257
428
  };
258
- }, S = async (e) => {
259
- for (let t of e) {
260
- if (t.type === n.Image && t.src) try {
261
- if (t.src.startsWith("data:")) t.data = t.src;
429
+ };
430
+ /**
431
+ * Recursively traverses parsed elements and loads image data for Image tokens.
432
+ * @param elements - The parsed elements to process.
433
+ */
434
+ const prefetchImages = async (elements) => {
435
+ for (const element of elements) {
436
+ if (element.type === "image" && element.src) try {
437
+ if (element.src.startsWith("data:")) element.data = element.src;
262
438
  else {
263
- let e = await fetch(t.src);
264
- if (!e.ok) throw Error(`Failed to fetch image: ${e.statusText}`);
265
- let n = await e.blob();
266
- t.data = await new Promise((e, t) => {
267
- let r = new FileReader();
268
- r.onloadend = () => {
269
- typeof r.result == "string" ? e(r.result) : t(/* @__PURE__ */ Error("Failed to convert image to base64 string"));
270
- }, r.onerror = t, r.readAsDataURL(n);
439
+ const response = await fetch(element.src);
440
+ if (!response.ok) throw new Error(`Failed to fetch image: ${response.statusText}`);
441
+ const blob = await response.blob();
442
+ element.data = await new Promise((resolve, reject) => {
443
+ const reader = new FileReader();
444
+ reader.onloadend = () => {
445
+ if (typeof reader.result === "string") resolve(reader.result);
446
+ else reject(/* @__PURE__ */ new Error("Failed to convert image to base64 string"));
447
+ };
448
+ reader.onerror = reject;
449
+ reader.readAsDataURL(blob);
271
450
  });
272
451
  }
273
- t.data && t.data.startsWith("data:image/svg") && typeof window < "u" && typeof document < "u" && (t.data = await new Promise((e) => {
274
- let n = new Image();
275
- n.onload = () => {
276
- let r = document.createElement("canvas"), i = b(t.data), a = i ? i.width : n.width || 300, o = i ? i.height : n.height || 150;
277
- t.naturalWidth = a, t.naturalHeight = o, r.width = a * 4, r.height = o * 4;
278
- let s = r.getContext("2d");
279
- s ? (s.scale(4, 4), s.drawImage(n, 0, 0, a, o), e(r.toDataURL("image/png"))) : e(t.data);
280
- }, n.onerror = () => e(t.data), n.src = t.data;
281
- }));
282
- } catch (e) {
283
- console.warn(`[jspdf-md-renderer] Warning: Failed to load image at ${t.src}. It will be skipped.`, e);
452
+ if (element.data && element.data.startsWith("data:image/svg")) {
453
+ if (typeof window !== "undefined" && typeof document !== "undefined") element.data = await new Promise((resolve) => {
454
+ const img = new Image();
455
+ img.onload = () => {
456
+ const canvas = document.createElement("canvas");
457
+ const dims = extractSvgDimensions(element.data);
458
+ const w = dims ? dims.width : img.width || 300;
459
+ const h = dims ? dims.height : img.height || 150;
460
+ element.naturalWidth = w;
461
+ element.naturalHeight = h;
462
+ const scale = 4;
463
+ canvas.width = w * scale;
464
+ canvas.height = h * scale;
465
+ const ctx = canvas.getContext("2d");
466
+ if (ctx) {
467
+ ctx.scale(scale, scale);
468
+ ctx.drawImage(img, 0, 0, w, h);
469
+ resolve(canvas.toDataURL("image/png"));
470
+ } else resolve(element.data);
471
+ };
472
+ img.onerror = () => resolve(element.data);
473
+ img.src = element.data;
474
+ });
475
+ }
476
+ } catch (error) {
477
+ console.warn(`[jspdf-md-renderer] Warning: Failed to load image at ${element.src}. It will be skipped.`, error);
284
478
  }
285
- t.items && t.items.length > 0 && await S(t.items);
479
+ if (element.items && element.items.length > 0) await prefetchImages(element.items);
286
480
  }
287
- }, C = class {
288
- static getCodespanOptions(e) {
289
- let t = e.options.codespan ?? {};
481
+ };
482
+ //#endregion
483
+ //#region src/utils/justifiedTextRenderer.ts
484
+ /**
485
+ * JustifiedTextRenderer - Renders mixed inline elements with proper alignment.
486
+ *
487
+ * Features:
488
+ * - Handles bold, italic, codespan, links mixed in paragraph
489
+ * - Proper word spacing distribution for justified alignment
490
+ * - Supports left, right, center, and justify alignments
491
+ * - Page break handling
492
+ * - Preserves link clickability
493
+ * - Codespan background rendering
494
+ */
495
+ var JustifiedTextRenderer = class {
496
+ static getCodespanOptions(store) {
497
+ const opts = store.options.codespan ?? {};
290
498
  return {
291
- backgroundColor: t.backgroundColor ?? "#EEEEEE",
292
- padding: t.padding ?? .5,
293
- showBackground: t.showBackground !== !1,
294
- fontSizeScale: t.fontSizeScale ?? .9
499
+ backgroundColor: opts.backgroundColor ?? "#EEEEEE",
500
+ padding: opts.padding ?? .5,
501
+ showBackground: opts.showBackground !== false,
502
+ fontSizeScale: opts.fontSizeScale ?? .9
295
503
  };
296
504
  }
297
- static applyStyle(e, t, n) {
298
- let r = e.getFont().fontName, i = e.getFontSize(), a = () => {
299
- let e = n.options.font.bold?.name;
300
- return e && e !== "" ? e : r;
301
- }, o = () => {
302
- let e = n.options.font.regular?.name;
303
- return e && e !== "" ? e : r;
505
+ /**
506
+ * Apply font style to the jsPDF document.
507
+ */
508
+ static applyStyle(doc, style, store) {
509
+ const currentFont = doc.getFont().fontName;
510
+ const currentFontSize = doc.getFontSize();
511
+ const getBoldFont = () => {
512
+ const boldName = store.options.font.bold?.name;
513
+ return boldName && boldName !== "" ? boldName : currentFont;
514
+ };
515
+ const getRegularFont = () => {
516
+ const regularName = store.options.font.regular?.name;
517
+ return regularName && regularName !== "" ? regularName : currentFont;
304
518
  };
305
- switch (t) {
519
+ switch (style) {
306
520
  case "bold":
307
- e.setFont(a(), n.options.font.bold?.style || "bold");
521
+ doc.setFont(getBoldFont(), store.options.font.bold?.style || "bold");
308
522
  break;
309
523
  case "italic":
310
- e.setFont(o(), "italic");
524
+ doc.setFont(getRegularFont(), "italic");
311
525
  break;
312
526
  case "bolditalic":
313
- e.setFont(a(), "bolditalic");
527
+ doc.setFont(getBoldFont(), "bolditalic");
314
528
  break;
315
529
  case "codespan":
316
- e.setFont("courier", "normal"), e.setFontSize(i * this.getCodespanOptions(n).fontSizeScale);
530
+ const codeFont = store.options.font.code || {
531
+ name: "courier",
532
+ style: "normal"
533
+ };
534
+ doc.setFont(codeFont.name, codeFont.style);
535
+ doc.setFontSize(currentFontSize * this.getCodespanOptions(store).fontSizeScale);
317
536
  break;
318
537
  default:
319
- e.setFont(o(), e.getFont().fontStyle);
538
+ doc.setFont(getRegularFont(), doc.getFont().fontStyle);
320
539
  break;
321
540
  }
322
541
  }
323
- static measureWordWidth(e, t, n, r) {
324
- let i = e.getFont(), a = e.getFontSize();
325
- this.applyStyle(e, n, r);
326
- let o = e.getTextWidth(t), s = e.getCharSpace?.() ?? 0, c = o + t.length * s;
327
- return e.setFont(i.fontName, i.fontStyle), e.setFontSize(a), c;
542
+ /**
543
+ * Measure word width with a specific style applied.
544
+ * NOTE: jsPDF's getTextWidth() does NOT include charSpace in its calculation,
545
+ * so we must manually add it: effectiveWidth = getTextWidth(text) + (text.length * charSpace)
546
+ */
547
+ static measureWordWidth(doc, text, style, store) {
548
+ const savedFont = doc.getFont();
549
+ const savedSize = doc.getFontSize();
550
+ this.applyStyle(doc, style, store);
551
+ const baseWidth = doc.getTextWidth(text);
552
+ const charSpace = doc.getCharSpace?.() ?? 0;
553
+ const effectiveWidth = baseWidth + text.length * charSpace;
554
+ doc.setFont(savedFont.fontName, savedFont.fontStyle);
555
+ doc.setFontSize(savedSize);
556
+ return effectiveWidth;
328
557
  }
329
- static getStyleFromType(e, t) {
330
- switch (e) {
331
- case "strong": return t === "italic" ? "bolditalic" : "bold";
332
- case "em": return t === "bold" ? "bolditalic" : "italic";
558
+ /**
559
+ * Extract style from element type string.
560
+ */
561
+ static getStyleFromType(type, parentStyle) {
562
+ switch (type) {
563
+ case "strong":
564
+ if (parentStyle === "italic") return "bolditalic";
565
+ return "bold";
566
+ case "em":
567
+ if (parentStyle === "bold") return "bolditalic";
568
+ return "italic";
333
569
  case "codespan": return "codespan";
334
- default: return t || "normal";
335
- }
336
- }
337
- static flattenToWords(e, t, n, r = "normal", i = !1, a) {
338
- let o = [];
339
- for (let s of t) {
340
- let t = this.getStyleFromType(s.type, r), c = s.type === "link" || i, l = s.href || a;
341
- if (s.items && s.items.length > 0) {
342
- let r = this.flattenToWords(e, s.items, n, t, c, l);
343
- o.push(...r);
344
- } else if (s.type === "image") {
345
- let r = n.options.page.maxContentHeight - n.options.page.topmargin, { finalWidth: i, finalHeight: a } = x(e, s, n.options.page.maxContentWidth - n.options.page.indent * 0, r, n.options.page.unit || "mm");
346
- o.push({
570
+ default: return parentStyle || "normal";
571
+ }
572
+ }
573
+ /**
574
+ * Flatten ParsedElement tree into an array of StyledWordInfo.
575
+ * Handles nested inline elements.
576
+ */
577
+ static flattenToWords(doc, elements, store, parentStyle = "normal", isLink = false, href) {
578
+ const result = [];
579
+ for (const el of elements) {
580
+ const style = this.getStyleFromType(el.type, parentStyle);
581
+ const elIsLink = el.type === "link" || isLink;
582
+ const elHref = el.href || href;
583
+ if (el.items && el.items.length > 0) {
584
+ const nested = this.flattenToWords(doc, el.items, store, style, elIsLink, elHref);
585
+ result.push(...nested);
586
+ } else if (el.type === "image") {
587
+ const maxH = store.options.page.maxContentHeight - store.options.page.topmargin;
588
+ const { finalWidth, finalHeight } = calculateImageDimensions(doc, el, store.options.page.maxContentWidth - store.options.page.indent * 0, maxH, store.options.page.unit || "mm");
589
+ result.push({
347
590
  text: "",
348
- width: i,
349
- style: t,
350
- isLink: c,
351
- href: l,
352
- linkColor: c ? n.options.link?.linkColor || [
591
+ width: finalWidth,
592
+ style,
593
+ isLink: elIsLink,
594
+ href: elHref,
595
+ linkColor: elIsLink ? store.options.link?.linkColor || [
353
596
  0,
354
597
  0,
355
598
  255
356
599
  ] : void 0,
357
- isImage: !0,
358
- imageElement: s,
359
- imageHeight: a
600
+ isImage: true,
601
+ imageElement: el,
602
+ imageHeight: finalHeight
360
603
  });
361
- } else if (s.type === "br") o.push({
604
+ } else if (el.type === "br") result.push({
362
605
  text: "",
363
606
  width: 0,
364
- style: t,
365
- isBr: !0
607
+ style,
608
+ isBr: true
366
609
  });
367
610
  else {
368
- let r = s.content || s.text || "";
369
- if (!r) continue;
370
- if (/^\s/.test(r) && o.length > 0 && (o[o.length - 1].hasTrailingSpace = !0), t === "codespan") {
371
- let i = r.trim();
372
- i && o.push({
373
- text: i,
374
- width: this.measureWordWidth(e, i, t, n),
375
- style: t,
376
- isLink: c,
377
- href: l,
378
- linkColor: c ? n.options.link?.linkColor || [
611
+ const text = el.content || el.text || "";
612
+ if (!text) continue;
613
+ if (/^\s/.test(text) && result.length > 0) result[result.length - 1].hasTrailingSpace = true;
614
+ if (style === "codespan") {
615
+ const trimmedText = text.trim();
616
+ if (trimmedText) result.push({
617
+ text: trimmedText,
618
+ width: this.measureWordWidth(doc, trimmedText, style, store),
619
+ style,
620
+ isLink: elIsLink,
621
+ href: elHref,
622
+ linkColor: elIsLink ? store.options.link?.linkColor || [
379
623
  0,
380
624
  0,
381
625
  255
382
626
  ] : void 0,
383
- hasTrailingSpace: /\s$/.test(r)
627
+ hasTrailingSpace: /\s$/.test(text)
384
628
  });
385
629
  continue;
386
630
  }
387
- let i = r.trim().split(/\s+/).filter((e) => e.length > 0);
388
- for (let a = 0; a < i.length; a++) {
389
- let s = a !== i.length - 1 || /\s$/.test(r);
390
- o.push({
391
- text: i[a],
392
- width: this.measureWordWidth(e, i[a], t, n),
393
- style: t,
394
- isLink: c,
395
- href: l,
396
- linkColor: c ? n.options.link?.linkColor || [
397
- 0,
398
- 0,
399
- 255
400
- ] : void 0,
401
- hasTrailingSpace: s
631
+ const lines = text.split("\n");
632
+ for (let partIndex = 0; partIndex < lines.length; partIndex++) {
633
+ const lineStr = lines[partIndex];
634
+ const words = lineStr.trim().split(/[ \t\r\v\f]+/).filter((w) => w.length > 0);
635
+ for (let i = 0; i < words.length; i++) {
636
+ const hasTrailingSpace = !(i === words.length - 1) || /[ \t\r\v\f]$/.test(lineStr);
637
+ result.push({
638
+ text: words[i],
639
+ width: this.measureWordWidth(doc, words[i], style, store),
640
+ style,
641
+ isLink: elIsLink,
642
+ href: elHref,
643
+ linkColor: elIsLink ? store.options.link?.linkColor || [
644
+ 0,
645
+ 0,
646
+ 255
647
+ ] : void 0,
648
+ hasTrailingSpace
649
+ });
650
+ }
651
+ if (partIndex < lines.length - 1) result.push({
652
+ text: "",
653
+ width: 0,
654
+ style,
655
+ isBr: true
402
656
  });
403
657
  }
404
658
  }
405
659
  }
406
- return o;
407
- }
408
- static breakIntoLines(e, t, n, r) {
409
- let i = [], a = [], o = 0, s = 0, c = m(e) * r.options.page.defaultLineHeightFactor, l = e.getTextWidth(" ");
410
- for (let u = 0; u < t.length; u++) {
411
- let d = t[u], f = a[a.length - 1]?.hasTrailingSpace ? l + d.width : d.width, p = d.isImage && d.imageHeight ? d.imageHeight : m(e) * r.options.page.defaultLineHeightFactor;
412
- if (d.isBr) {
413
- i.push({
414
- words: a,
415
- totalTextWidth: o,
416
- isLastLine: !0,
417
- lineHeight: c
418
- }), a = [], o = 0, s = 0, c = m(e) * r.options.page.defaultLineHeightFactor;
660
+ return result;
661
+ }
662
+ /**
663
+ * Break a flat list of words into lines that fit within maxWidth.
664
+ * Correctly tracks totalTextWidth (sum of word widths only) for justification.
665
+ */
666
+ static breakIntoLines(doc, words, maxWidth, store) {
667
+ const lines = [];
668
+ let currentLine = [];
669
+ let currentTextWidth = 0;
670
+ let currentLineWidth = 0;
671
+ let currentLineHeight = getCharHight(doc) * store.options.page.defaultLineHeightFactor;
672
+ const spaceWidth = doc.getTextWidth(" ");
673
+ for (let i = 0; i < words.length; i++) {
674
+ const word = words[i];
675
+ const neededWidthWithSpace = currentLine[currentLine.length - 1]?.hasTrailingSpace ? spaceWidth + word.width : word.width;
676
+ const itemHeight = word.isImage && word.imageHeight ? word.imageHeight : getCharHight(doc) * store.options.page.defaultLineHeightFactor;
677
+ if (word.isBr) {
678
+ lines.push({
679
+ words: currentLine,
680
+ totalTextWidth: currentTextWidth,
681
+ isLastLine: true,
682
+ lineHeight: currentLineHeight
683
+ });
684
+ currentLine = [];
685
+ currentTextWidth = 0;
686
+ currentLineWidth = 0;
687
+ currentLineHeight = getCharHight(doc) * store.options.page.defaultLineHeightFactor;
419
688
  continue;
420
689
  }
421
- s + f > n && a.length > 0 ? (i.push({
422
- words: a,
423
- totalTextWidth: o,
424
- isLastLine: !1,
425
- lineHeight: c
426
- }), a = [d], o = d.width, s = d.width, c = p) : (a.push(d), o += d.width, s += f, c = Math.max(c, p));
427
- }
428
- return a.length > 0 && i.push({
429
- words: a,
430
- totalTextWidth: o,
431
- isLastLine: !0,
432
- lineHeight: c
433
- }), i;
434
- }
435
- static renderWord(e, t, n, r, i) {
436
- let a = e.getFont(), o = e.getFontSize(), s = e.getTextColor();
437
- if (this.applyStyle(e, t.style, i), t.isLink && t.linkColor && e.setTextColor(...t.linkColor), t.isImage && t.imageElement && t.imageElement.data) try {
438
- let i = "JPEG";
439
- if (t.imageElement.data.startsWith("data:image/png")) i = "PNG";
440
- else if (t.imageElement.data.startsWith("data:image/webp")) i = "WEBP";
441
- else if (t.imageElement.data.startsWith("data:image/gif")) i = "GIF";
442
- else if (t.imageElement.src) {
443
- let e = t.imageElement.src.split("?")[0].split("#")[0].split(".").pop()?.toUpperCase();
444
- e && [
690
+ if (currentLineWidth + neededWidthWithSpace > maxWidth && currentLine.length > 0) {
691
+ lines.push({
692
+ words: currentLine,
693
+ totalTextWidth: currentTextWidth,
694
+ isLastLine: false,
695
+ lineHeight: currentLineHeight
696
+ });
697
+ currentLine = [word];
698
+ currentTextWidth = word.width;
699
+ currentLineWidth = word.width;
700
+ currentLineHeight = itemHeight;
701
+ } else {
702
+ currentLine.push(word);
703
+ currentTextWidth += word.width;
704
+ currentLineWidth += neededWidthWithSpace;
705
+ currentLineHeight = Math.max(currentLineHeight, itemHeight);
706
+ }
707
+ }
708
+ if (currentLine.length > 0) lines.push({
709
+ words: currentLine,
710
+ totalTextWidth: currentTextWidth,
711
+ isLastLine: true,
712
+ lineHeight: currentLineHeight
713
+ });
714
+ return lines;
715
+ }
716
+ /**
717
+ * Render a single word with its style applied.
718
+ */
719
+ static renderWord(doc, word, x, y, store) {
720
+ const savedFont = doc.getFont();
721
+ const savedSize = doc.getFontSize();
722
+ const savedColor = doc.getTextColor();
723
+ this.applyStyle(doc, word.style, store);
724
+ if (word.isLink && word.linkColor) doc.setTextColor(...word.linkColor);
725
+ if (word.isImage && word.imageElement && word.imageElement.data) try {
726
+ let imgFormat = "JPEG";
727
+ if (word.imageElement.data.startsWith("data:image/png")) imgFormat = "PNG";
728
+ else if (word.imageElement.data.startsWith("data:image/webp")) imgFormat = "WEBP";
729
+ else if (word.imageElement.data.startsWith("data:image/gif")) imgFormat = "GIF";
730
+ else if (word.imageElement.src) {
731
+ const ext = word.imageElement.src.split("?")[0].split("#")[0].split(".").pop()?.toUpperCase();
732
+ if (ext && [
445
733
  "PNG",
446
734
  "JPEG",
447
735
  "JPG",
448
736
  "WEBP",
449
737
  "GIF"
450
- ].includes(e) && (i = e === "JPG" ? "JPEG" : e);
738
+ ].includes(ext)) imgFormat = ext === "JPG" ? "JPEG" : ext;
451
739
  }
452
- if (t.width > 0 && (t.imageHeight || 0) > 0) {
453
- let a = t.imageHeight || 0, o = r;
454
- e.addImage(t.imageElement.data, i, n, o, t.width, a);
740
+ if (word.width > 0 && (word.imageHeight || 0) > 0) {
741
+ const imgH = word.imageHeight || 0;
742
+ const imgY = y;
743
+ doc.addImage(word.imageElement.data, imgFormat, x, imgY, word.width, imgH);
455
744
  }
456
745
  } catch (e) {
457
746
  console.warn("Failed to render inline image", e);
458
747
  }
459
748
  else {
460
- if (t.style === "codespan") {
461
- let a = this.getCodespanOptions(i);
462
- if (a.showBackground) {
463
- let i = m(e), o = a.padding;
464
- e.setFillColor(a.backgroundColor), e.rect(n - o, r - o, t.width + o * 2, i + o * 2, "F"), e.setFillColor("#000000");
749
+ if (word.style === "codespan") {
750
+ const codespanOpts = this.getCodespanOptions(store);
751
+ if (codespanOpts.showBackground) {
752
+ const h = getCharHight(doc);
753
+ const pad = codespanOpts.padding;
754
+ doc.setFillColor(codespanOpts.backgroundColor);
755
+ doc.rect(x - pad, y - pad, word.width + pad * 2, h + pad * 2, "F");
756
+ doc.setFillColor("#000000");
465
757
  }
466
758
  }
467
- e.text(t.text, n, r, { baseline: "top" });
759
+ doc.text(word.text, x, y, { baseline: "top" });
468
760
  }
469
- if (t.isLink && t.href) {
470
- let i = t.isImage && t.imageHeight ? t.imageHeight : m(e);
471
- e.link(n, r, t.width, i, { url: t.href });
761
+ if (word.isLink && word.href) {
762
+ const h = word.isImage && word.imageHeight ? word.imageHeight : getCharHight(doc);
763
+ doc.link(x, y, word.width, h, { url: word.href });
472
764
  }
473
- e.setFont(a.fontName, a.fontStyle), e.setFontSize(o), e.setTextColor(s);
765
+ doc.setFont(savedFont.fontName, savedFont.fontStyle);
766
+ doc.setFontSize(savedSize);
767
+ doc.setTextColor(savedColor);
474
768
  }
475
- static renderAlignedLine(e, t, n, r, i, a, o = "left") {
476
- let { words: s, totalTextWidth: c, isLastLine: l } = t;
477
- if (s.length === 0) return;
478
- let u = e.getTextWidth(" "), d = n, f = u, p = c, h = 0;
479
- for (let e = 0; e < s.length - 1; e++) s[e].hasTrailingSpace && (p += u, h++);
480
- switch (o) {
769
+ /**
770
+ * Render a single line with specified alignment.
771
+ */
772
+ static renderAlignedLine(doc, line, x, y, maxWidth, store, alignment = "left") {
773
+ const { words, totalTextWidth, isLastLine } = line;
774
+ if (words.length === 0) return;
775
+ const normalSpaceWidth = doc.getTextWidth(" ");
776
+ let startX = x;
777
+ let wordSpacing = normalSpaceWidth;
778
+ let lineWidthWithNormalSpaces = totalTextWidth;
779
+ let expandableSpacesCount = 0;
780
+ for (let i = 0; i < words.length - 1; i++) if (words[i].hasTrailingSpace) {
781
+ lineWidthWithNormalSpaces += normalSpaceWidth;
782
+ expandableSpacesCount++;
783
+ }
784
+ switch (alignment) {
481
785
  case "right":
482
- d = n + i - p;
786
+ startX = x + maxWidth - lineWidthWithNormalSpaces;
483
787
  break;
484
788
  case "center":
485
- d = n + (i - p) / 2;
789
+ startX = x + (maxWidth - lineWidthWithNormalSpaces) / 2;
486
790
  break;
487
791
  case "justify":
488
- !l && h > 0 && (f = (i - c) / h);
792
+ if (!isLastLine && expandableSpacesCount > 0) wordSpacing = (maxWidth - totalTextWidth) / expandableSpacesCount;
489
793
  break;
490
794
  default: break;
491
795
  }
492
- let g = d, _ = m(e) * a.options.page.defaultLineHeightFactor;
493
- for (let n = 0; n < s.length; n++) {
494
- let i = s[n], o = r, c = i.isImage && i.imageHeight ? i.imageHeight : _;
495
- i.isImage ? o = r : c < t.lineHeight && (o = r + (t.lineHeight - c)), this.renderWord(e, i, g, o, a), g += i.width, n < s.length - 1 && i.hasTrailingSpace && (g += f);
496
- }
497
- }
498
- static renderStyledParagraph(e, t, n, r, i, a, o) {
499
- let s = o ?? a.options.content?.textAlignment ?? "left", c = this.flattenToWords(e, t, a);
500
- if (c.length === 0) return;
501
- let l = this.breakIntoLines(e, c, i, a), u = r;
502
- for (let t of l) u + t.lineHeight > a.options.page.maxContentHeight && (_(e, a), u = a.Y), this.renderAlignedLine(e, t, n, u, i, a, s), a.recordContentY(u + t.lineHeight), u += t.lineHeight, a.updateY(t.lineHeight, "add");
503
- let d = l[l.length - 1];
504
- if (d) {
505
- let t = 0;
506
- for (let e = 0; e < d.words.length - 1; e++) d.words[e].hasTrailingSpace && t++;
507
- let r = d.totalTextWidth + t * e.getTextWidth(" ");
508
- a.updateX(n + r, "set");
509
- }
510
- }
511
- static renderJustifiedParagraph(e, t, n, r, i, a) {
512
- this.renderStyledParagraph(e, t, n, r, i, a);
513
- }
514
- }, w = class {
515
- static renderText(e, t, n, r = n.X, i = n.Y, a, o = !1) {
516
- let s = e.splitTextToSize(t, a), c = m(e), l = c * n.options.page.defaultLineHeightFactor, u = i;
517
- for (let t = 0; t < s.length; t++) {
518
- let i = s[t];
519
- u + l > n.options.page.maxContentHeight && (_(e, n), u = n.Y), o ? t === s.length - 1 ? e.text(i, r, u, { baseline: "top" }) : e.text(i, r, u, {
520
- maxWidth: a,
796
+ let currentX = startX;
797
+ const textHeight = getCharHight(doc) * store.options.page.defaultLineHeightFactor;
798
+ for (let i = 0; i < words.length; i++) {
799
+ const word = words[i];
800
+ let drawY = y;
801
+ const elementHeight = word.isImage && word.imageHeight ? word.imageHeight : textHeight;
802
+ if (word.isImage) drawY = y;
803
+ else if (elementHeight < line.lineHeight) drawY = y + (line.lineHeight - elementHeight);
804
+ this.renderWord(doc, word, currentX, drawY, store);
805
+ currentX += word.width;
806
+ if (i < words.length - 1 && word.hasTrailingSpace) currentX += wordSpacing;
807
+ }
808
+ }
809
+ /**
810
+ * Main entry point: Render a paragraph with mixed inline elements.
811
+ * Respects user's textAlignment option from store.
812
+ *
813
+ * @param doc jsPDF instance
814
+ * @param elements Array of ParsedElement (inline items in a paragraph)
815
+ * @param x Starting X coordinate
816
+ * @param y Starting Y coordinate
817
+ * @param maxWidth Maximum width for text wrapping
818
+ * @param store RenderStore instance to use
819
+ * @param alignment Optional alignment override (defaults to store option)
820
+ */
821
+ static renderStyledParagraph(doc, elements, x, y, maxWidth, store, alignment) {
822
+ const textAlignment = alignment ?? store.options.content?.textAlignment ?? "left";
823
+ const words = this.flattenToWords(doc, elements, store);
824
+ if (words.length === 0) return;
825
+ const lines = this.breakIntoLines(doc, words, maxWidth, store);
826
+ let currentY = y;
827
+ for (const line of lines) {
828
+ if (currentY + line.lineHeight > store.options.page.maxContentHeight) {
829
+ HandlePageBreaks(doc, store);
830
+ currentY = store.Y;
831
+ }
832
+ this.renderAlignedLine(doc, line, x, currentY, maxWidth, store, textAlignment);
833
+ store.recordContentY(currentY + line.lineHeight);
834
+ currentY += line.lineHeight;
835
+ store.updateY(line.lineHeight, "add");
836
+ }
837
+ const lastLine = lines[lines.length - 1];
838
+ if (lastLine) {
839
+ let actualSpacesCount = 0;
840
+ for (let i = 0; i < lastLine.words.length - 1; i++) if (lastLine.words[i].hasTrailingSpace) actualSpacesCount++;
841
+ const lastLineWidth = lastLine.totalTextWidth + actualSpacesCount * doc.getTextWidth(" ");
842
+ store.updateX(x + lastLineWidth, "set");
843
+ }
844
+ }
845
+ /**
846
+ * @deprecated Use renderStyledParagraph instead
847
+ */
848
+ static renderJustifiedParagraph(doc, elements, x, y, maxWidth, store) {
849
+ this.renderStyledParagraph(doc, elements, x, y, maxWidth, store);
850
+ }
851
+ };
852
+ //#endregion
853
+ //#region src/renderer/components/heading.ts
854
+ /**
855
+ * Renders heading elements.
856
+ */
857
+ const renderHeading = (doc, element, indent, store, parentElementRenderer) => {
858
+ const size = 6 - (element?.depth ?? 0) > 0 ? 6 - (element?.depth ?? 0) : 1;
859
+ doc.setFontSize(store.options.page.defaultFontSize + size);
860
+ if (element?.items && element?.items.length > 0) {
861
+ const originalLineHeightFactor = store.options.page.defaultLineHeightFactor;
862
+ store.options.page.defaultLineHeightFactor = 1;
863
+ JustifiedTextRenderer.renderStyledParagraph(doc, element.items, store.X + indent, store.Y, store.options.page.maxContentWidth - indent, store, "left");
864
+ store.options.page.defaultLineHeightFactor = originalLineHeightFactor;
865
+ } else {
866
+ const charHeight = getCharHight(doc);
867
+ doc.text(element?.content ?? "", store.X + indent, store.Y, {
868
+ align: "left",
869
+ maxWidth: store.options.page.maxContentWidth - indent,
870
+ baseline: "top"
871
+ });
872
+ store.recordContentY(store.Y + charHeight);
873
+ store.updateY(getCharHight(doc), "add");
874
+ }
875
+ doc.setFontSize(store.options.page.defaultFontSize);
876
+ store.updateX(store.options.page.xpading, "set");
877
+ };
878
+ //#endregion
879
+ //#region src/utils/text-renderer.ts
880
+ var TextRenderer = class {
881
+ /**
882
+ * Renders text with automatic line wrapping and page breaking.
883
+ * @param doc jsPDF instance
884
+ * @param text Text to render
885
+ * @param store RenderStore instance to use
886
+ * @param x X coordinate (if not provided, uses store.X)
887
+ * @param y Y coordinate (if not provided, uses store.Y)
888
+ * @param maxWidth Max width for text wrapping
889
+ * @param justify Whether to justify the text
890
+ */
891
+ static renderText(doc, text, store, x = store.X, y = store.Y, maxWidth, justify = false) {
892
+ const lines = doc.splitTextToSize(text, maxWidth);
893
+ const charHeight = getCharHight(doc);
894
+ const lineHeight = charHeight * store.options.page.defaultLineHeightFactor;
895
+ let currentY = y;
896
+ for (let i = 0; i < lines.length; i++) {
897
+ const line = lines[i];
898
+ if (currentY + lineHeight > store.options.page.maxContentHeight) {
899
+ HandlePageBreaks(doc, store);
900
+ currentY = store.Y;
901
+ }
902
+ if (justify) if (i === lines.length - 1) doc.text(line, x, currentY, { baseline: "top" });
903
+ else doc.text(line, x, currentY, {
904
+ maxWidth,
521
905
  align: "justify",
522
906
  baseline: "top"
523
- }) : e.text(i, r, u, { baseline: "top" }), n.recordContentY(u + c), u += l, n.updateY(l, "add");
907
+ });
908
+ else doc.text(line, x, currentY, { baseline: "top" });
909
+ store.recordContentY(currentY + charHeight);
910
+ currentY += lineHeight;
911
+ store.updateY(lineHeight, "add");
524
912
  }
525
- return u;
913
+ return currentY;
526
914
  }
527
- }, T = (e, t, r, i, a) => {
528
- i.activateInlineLock(), e.setFontSize(i.options.page.defaultFontSize);
529
- let o = i.options.page.maxContentWidth - r;
530
- if (t?.items && t?.items.length > 0) {
531
- if (t.items.length === 1 && t.items[0].type === "image") {
532
- a(t.items[0], r, i, !1), i.updateX(i.options.page.xpading), i.deactivateInlineLock();
915
+ };
916
+ //#endregion
917
+ //#region src/renderer/components/paragraph.ts
918
+ /**
919
+ * Renders paragraph elements with proper text alignment.
920
+ * Handles mixed inline styles (bold, italic, codespan) and links.
921
+ * Respects user's textAlignment option from RenderStore.
922
+ */
923
+ const renderParagraph = (doc, element, indent, store, parentElementRenderer) => {
924
+ store.activateInlineLock();
925
+ doc.setFontSize(store.options.page.defaultFontSize);
926
+ const maxWidth = store.options.page.maxContentWidth - indent;
927
+ if (element?.items && element?.items.length > 0) {
928
+ if (element.items.length === 1 && element.items[0].type === "image") {
929
+ parentElementRenderer(element.items[0], indent, store, false);
930
+ store.updateX(store.options.page.xpading);
931
+ store.deactivateInlineLock();
533
932
  return;
534
933
  }
535
- let s = [
536
- n.Strong,
537
- n.Em,
538
- n.Text,
539
- n.CodeSpan,
540
- n.Link,
541
- n.Image,
542
- n.Br
934
+ const inlineTypes = [
935
+ "strong",
936
+ "em",
937
+ "text",
938
+ "codespan",
939
+ "link",
940
+ "image",
941
+ "br"
543
942
  ];
544
- if (t.items.some((e) => !s.includes(e.type))) {
545
- let n = [], c = () => {
546
- n.length > 0 && (C.renderStyledParagraph(e, n, i.X + r, i.Y, o, i), n.length = 0);
943
+ if (element.items.some((item) => !inlineTypes.includes(item.type))) {
944
+ const inlineBuffer = [];
945
+ const flushInlineBuffer = () => {
946
+ if (inlineBuffer.length > 0) {
947
+ JustifiedTextRenderer.renderStyledParagraph(doc, inlineBuffer, store.X + indent, store.Y, maxWidth, store);
948
+ inlineBuffer.length = 0;
949
+ }
547
950
  };
548
- for (let e of t.items) s.includes(e.type) ? n.push(e) : (c(), a(e, r, i, !1));
549
- c();
550
- } else C.renderStyledParagraph(e, t.items, i.X + r, i.Y, o, i);
951
+ for (const item of element.items) if (inlineTypes.includes(item.type)) inlineBuffer.push(item);
952
+ else {
953
+ flushInlineBuffer();
954
+ parentElementRenderer(item, indent, store, false);
955
+ }
956
+ flushInlineBuffer();
957
+ } else JustifiedTextRenderer.renderStyledParagraph(doc, element.items, store.X + indent, store.Y, maxWidth, store);
551
958
  } else {
552
- let n = t.content ?? "", a = i.options.content?.textAlignment ?? "left";
553
- n.trim() && w.renderText(e, n, i, i.X + r, i.Y, o, a === "justify");
554
- }
555
- i.updateX(i.options.page.xpading), i.deactivateInlineLock();
556
- }, E = (e, t, n, r, i) => {
557
- e.setFontSize(r.options.page.defaultFontSize);
558
- for (let [e, a] of t?.items?.entries() ?? []) {
559
- let o = t.ordered ? (t.start ?? 0) + e : t.start;
560
- i(a, n + 1, r, !0, o, t.ordered);
561
- }
562
- }, D = (e, t, r, i, a, o, s) => {
563
- i.Y + m(e) >= i.options.page.maxContentHeight && _(e, i);
564
- let c = i.options, l = r * c.page.indent, u = s ? `${o}. ` : "• ", d = c.page.xpading;
565
- i.updateX(d, "set"), e.setFont(c.font.regular.name, c.font.regular.style), e.text(u, d + l, i.Y, { baseline: "top" });
566
- let f = e.getTextWidth(u), p = d + l + f, h = c.page.maxContentWidth - l - f;
567
- if (t.items && t.items.length > 0) {
568
- let c = [], l = () => {
569
- c.length > 0 && (C.renderStyledParagraph(e, c, p, i.Y, h, i), c.length = 0, i.updateX(d, "set"));
959
+ const content = element.content ?? "";
960
+ const textAlignment = store.options.content?.textAlignment ?? "left";
961
+ if (content.trim()) TextRenderer.renderText(doc, content, store, store.X + indent, store.Y, maxWidth, textAlignment === "justify");
962
+ }
963
+ store.updateX(store.options.page.xpading);
964
+ store.deactivateInlineLock();
965
+ };
966
+ //#endregion
967
+ //#region src/renderer/components/list.ts
968
+ const renderList = (doc, element, indentLevel, store, parentElementRenderer) => {
969
+ doc.setFontSize(store.options.page.defaultFontSize);
970
+ for (const [i, point] of element?.items?.entries() ?? []) {
971
+ const _start = element.ordered ? (element.start ?? 0) + i : element.start;
972
+ parentElementRenderer(point, indentLevel + 1, store, true, _start, element.ordered);
973
+ }
974
+ };
975
+ //#endregion
976
+ //#region src/renderer/components/listItem.ts
977
+ /**
978
+ * Render a single list item, including bullets/numbering, inline text, and any nested lists.
979
+ */
980
+ const renderListItem = (doc, element, indentLevel, store, parentElementRenderer, start, ordered) => {
981
+ if (store.Y + getCharHight(doc) >= store.options.page.maxContentHeight) HandlePageBreaks(doc, store);
982
+ const options = store.options;
983
+ const baseIndent = indentLevel * options.page.indent;
984
+ const bullet = ordered ? `${start}. ` : "• ";
985
+ const xLeft = options.page.xpading;
986
+ store.updateX(xLeft, "set");
987
+ doc.setFont(options.font.regular.name, options.font.regular.style);
988
+ doc.text(bullet, xLeft + baseIndent, store.Y, { baseline: "top" });
989
+ const bulletWidth = doc.getTextWidth(bullet);
990
+ const contentX = xLeft + baseIndent + bulletWidth;
991
+ const textMaxWidth = options.page.maxContentWidth - baseIndent - bulletWidth;
992
+ if (element.items && element.items.length > 0) {
993
+ const inlineBuffer = [];
994
+ const flushInlineBuffer = () => {
995
+ if (inlineBuffer.length > 0) {
996
+ JustifiedTextRenderer.renderStyledParagraph(doc, inlineBuffer, contentX, store.Y, textMaxWidth, store);
997
+ inlineBuffer.length = 0;
998
+ store.updateX(xLeft, "set");
999
+ }
570
1000
  };
571
- for (let e of t.items) e.type === n.List ? (l(), a(e, r, i, !0, o, e.ordered ?? !1)) : e.type === n.ListItem ? (l(), a(e, r, i, !0, o, s)) : c.push(e);
572
- l();
573
- } else if (t.content) {
574
- let n = c.content?.textAlignment ?? "left";
575
- w.renderText(e, t.content, i, p, i.Y, h, n === "justify");
576
- }
577
- }, O = (e, t, n, r, i, a, o, s, c = !0) => {
578
- if (t?.items && t?.items.length > 0) for (let e of t?.items ?? []) a(e, n, r, i, o, s, c);
1001
+ for (const subItem of element.items) if (subItem.type === "list") {
1002
+ flushInlineBuffer();
1003
+ parentElementRenderer(subItem, indentLevel, store, true, start, subItem.ordered ?? false);
1004
+ } else if (subItem.type === "list_item") {
1005
+ flushInlineBuffer();
1006
+ parentElementRenderer(subItem, indentLevel, store, true, start, ordered);
1007
+ } else inlineBuffer.push(subItem);
1008
+ flushInlineBuffer();
1009
+ } else if (element.content) {
1010
+ const textAlignment = options.content?.textAlignment ?? "left";
1011
+ TextRenderer.renderText(doc, element.content, store, contentX, store.Y, textMaxWidth, textAlignment === "justify");
1012
+ }
1013
+ };
1014
+ //#endregion
1015
+ //#region src/renderer/components/rawItem.ts
1016
+ const renderRawItem = (doc, element, indentLevel, store, hasRawBullet, parentElementRenderer, start, ordered, justify = true) => {
1017
+ if (element?.items && element?.items.length > 0) for (const item of element?.items ?? []) parentElementRenderer(item, indentLevel, store, hasRawBullet, start, ordered, justify);
579
1018
  else {
580
- let a = r.options, l = n * a.page.indent, u = i ? s ? `${o}. ` : "• " : "", d = t.content || "", f = a.page.xpading;
581
- if (!d && !u) return;
582
- if (!d.trim() && !u) {
583
- let t = (d.match(/\n/g) || []).length;
584
- if (t > 1) {
585
- let n = (t - 1) * (e.getTextDimensions("A").h * a.page.defaultLineHeightFactor);
586
- r.Y + n > a.page.maxContentHeight ? _(e, r) : (r.updateY(n, "add"), r.recordContentY(r.Y));
1019
+ const options = store.options;
1020
+ const indent = indentLevel * options.page.indent;
1021
+ const bullet = hasRawBullet ? ordered ? `${start}. ` : "• " : "";
1022
+ const content = element.content || "";
1023
+ const xLeft = options.page.xpading;
1024
+ if (!content && !bullet) return;
1025
+ if (!content.trim() && !bullet) {
1026
+ const newlines = (content.match(/\n/g) || []).length;
1027
+ if (newlines > 1) {
1028
+ const addedHeight = (newlines - 1) * (doc.getTextDimensions("A").h * options.page.defaultLineHeightFactor);
1029
+ if (store.Y + addedHeight > options.page.maxContentHeight) HandlePageBreaks(doc, store);
1030
+ else {
1031
+ store.updateY(addedHeight, "add");
1032
+ store.recordContentY(store.Y);
1033
+ }
587
1034
  }
588
1035
  return;
589
1036
  }
590
- if (r.updateX(f, "set"), i && u) {
591
- let t = e.getTextWidth(u), n = a.page.maxContentWidth - l - t;
592
- e.setFont(a.font.regular.name, a.font.regular.style), e.text(u, f + l, r.Y, { baseline: "top" }), w.renderText(e, d, r, f + l + t, r.Y, n, c);
1037
+ store.updateX(xLeft, "set");
1038
+ if (hasRawBullet && bullet) {
1039
+ const bulletWidth = doc.getTextWidth(bullet);
1040
+ const textMaxWidth = options.page.maxContentWidth - indent - bulletWidth;
1041
+ doc.setFont(options.font.regular.name, options.font.regular.style);
1042
+ doc.text(bullet, xLeft + indent, store.Y, { baseline: "top" });
1043
+ TextRenderer.renderText(doc, content, store, xLeft + indent + bulletWidth, store.Y, textMaxWidth, justify);
593
1044
  } else {
594
- let t = a.page.maxContentWidth - l;
595
- w.renderText(e, d, r, f + l, r.Y, t, c);
596
- }
597
- r.updateX(f, "set");
598
- }
599
- }, k = (e, t) => {
600
- let n = e.internal.pageSize.getWidth();
601
- e.setLineDashPattern([1, 1], 0), e.setLineWidth(.1), e.line(t.options.page.xpading, t.Y, n - t.options.page.xpading, t.Y), e.setLineWidth(.1), e.setLineDashPattern([], 0), t.updateY(m(e), "add");
602
- }, A = (e, t, n, r) => {
603
- let i = e.getFont(), a = e.getFontSize();
604
- e.setFont("courier", "normal");
605
- let o = r.options.page.defaultFontSize * .9;
606
- e.setFontSize(o);
607
- let s = n * r.options.page.indent, c = r.options.page.maxContentWidth - s - 8, l = e.getLineHeightFactor(), u = o / e.internal.scaleFactor * l, d = (t.code ?? "").replace(/[\r\n\s]+$/, "");
608
- if (!d) {
609
- e.setFont(i.fontName, i.fontStyle), e.setFontSize(a);
1045
+ const textMaxWidth = options.page.maxContentWidth - indent;
1046
+ TextRenderer.renderText(doc, content, store, xLeft + indent, store.Y, textMaxWidth, justify);
1047
+ }
1048
+ store.updateX(xLeft, "set");
1049
+ }
1050
+ };
1051
+ //#endregion
1052
+ //#region src/renderer/components/hr.ts
1053
+ const renderHR = (doc, store) => {
1054
+ const pageWidth = doc.internal.pageSize.getWidth();
1055
+ doc.setLineDashPattern([1, 1], 0);
1056
+ doc.setLineWidth(.1);
1057
+ doc.line(store.options.page.xpading, store.Y, pageWidth - store.options.page.xpading, store.Y);
1058
+ doc.setLineWidth(.1);
1059
+ doc.setLineDashPattern([], 0);
1060
+ store.updateY(getCharHight(doc), "add");
1061
+ };
1062
+ //#endregion
1063
+ //#region src/renderer/components/code.ts
1064
+ const renderCodeBlock = (doc, element, indentLevel, store) => {
1065
+ const savedFont = doc.getFont();
1066
+ const savedFontSize = doc.getFontSize();
1067
+ const codeFont = store.options.font.code || {
1068
+ name: "courier",
1069
+ style: "normal"
1070
+ };
1071
+ doc.setFont(codeFont.name, codeFont.style);
1072
+ const codeFontSize = store.options.page.defaultFontSize * .9;
1073
+ doc.setFontSize(codeFontSize);
1074
+ const indent = indentLevel * store.options.page.indent;
1075
+ const maxWidth = store.options.page.maxContentWidth - indent - 8;
1076
+ const lineHeightFactor = doc.getLineHeightFactor();
1077
+ const lineHeight = codeFontSize / doc.internal.scaleFactor * lineHeightFactor;
1078
+ const content = (element.code ?? "").replace(/[\r\n\s]+$/, "");
1079
+ if (!content) {
1080
+ doc.setFont(savedFont.fontName, savedFont.fontStyle);
1081
+ doc.setFontSize(savedFontSize);
610
1082
  return;
611
1083
  }
612
- let f = e.splitTextToSize(d, c);
613
- for (; f.length > 0 && f[f.length - 1].trim() === "";) f.pop();
614
- if (f.length === 0) {
615
- e.setFont(i.fontName, i.fontStyle), e.setFontSize(a);
1084
+ const lines = doc.splitTextToSize(content, maxWidth);
1085
+ while (lines.length > 0 && lines[lines.length - 1].trim() === "") lines.pop();
1086
+ if (lines.length === 0) {
1087
+ doc.setFont(savedFont.fontName, savedFont.fontStyle);
1088
+ doc.setFontSize(savedFontSize);
616
1089
  return;
617
1090
  }
618
- let p = 0;
619
- for (; p < f.length;) {
620
- let n = r.options.page.maxContentHeight - r.Y, i = f.length - p, a = n - 8, o = Math.floor(a / u);
621
- if (o <= 0) {
622
- _(e, r);
1091
+ const padding = 4;
1092
+ const bgColor = "#EEEEEE";
1093
+ const drawColor = "#DDDDDD";
1094
+ let currentLineIndex = 0;
1095
+ while (currentLineIndex < lines.length) {
1096
+ const availableHeight = store.options.page.maxContentHeight - store.Y;
1097
+ const remainingLines = lines.length - currentLineIndex;
1098
+ const effectiveAvailable = availableHeight - padding * 2;
1099
+ let linesToRenderCount = Math.floor(effectiveAvailable / lineHeight);
1100
+ if (linesToRenderCount <= 0) {
1101
+ HandlePageBreaks(doc, store);
623
1102
  continue;
624
1103
  }
625
- o > i && (o = i);
626
- let s = f.slice(p, p + o), c = p === 0, l = p + o >= f.length, d = o * u;
627
- if (c && r.updateY(4, "add"), e.setFillColor("#EEEEEE"), e.setDrawColor("#DDDDDD"), e.roundedRect(r.X, r.Y - 4, r.options.page.maxContentWidth, d + (c ? 4 : 0) + (l ? 4 : 0), 2, 2, "FD"), c && t.lang) {
628
- let n = e.getFontSize();
629
- e.setFontSize(10), e.setTextColor("#666666"), e.text(t.lang, r.X + r.options.page.maxContentWidth - e.getTextWidth(t.lang) - 4, r.Y, { baseline: "top" }), e.setFontSize(n), e.setTextColor("#000000");
1104
+ if (linesToRenderCount > remainingLines) linesToRenderCount = remainingLines;
1105
+ const linesToRender = lines.slice(currentLineIndex, currentLineIndex + linesToRenderCount);
1106
+ const isFirstChunk = currentLineIndex === 0;
1107
+ const isLastChunk = currentLineIndex + linesToRenderCount >= lines.length;
1108
+ const textBlockHeight = linesToRenderCount * lineHeight;
1109
+ if (isFirstChunk) store.updateY(padding, "add");
1110
+ doc.setFillColor(bgColor);
1111
+ doc.setDrawColor(drawColor);
1112
+ doc.roundedRect(store.X, store.Y - padding, store.options.page.maxContentWidth, textBlockHeight + (isFirstChunk ? padding : 0) + (isLastChunk ? padding : 0), 2, 2, "FD");
1113
+ if (isFirstChunk && element.lang) {
1114
+ const savedCodeFontSize = doc.getFontSize();
1115
+ doc.setFontSize(10);
1116
+ doc.setTextColor("#666666");
1117
+ doc.text(element.lang, store.X + store.options.page.maxContentWidth - doc.getTextWidth(element.lang) - 4, store.Y, { baseline: "top" });
1118
+ doc.setFontSize(savedCodeFontSize);
1119
+ doc.setTextColor("#000000");
630
1120
  }
631
- let m = r.Y;
632
- for (let t of s) e.text(t, r.X + 4, m, { baseline: "top" }), m += u;
633
- r.updateY(d, "add"), r.recordContentY(r.Y + (l ? 4 : 0)), l && r.updateY(4, "add"), p += o, p < f.length && _(e, r);
1121
+ let yPos = store.Y;
1122
+ for (const line of linesToRender) {
1123
+ doc.text(line, store.X + 4, yPos, { baseline: "top" });
1124
+ yPos += lineHeight;
1125
+ }
1126
+ store.updateY(textBlockHeight, "add");
1127
+ store.recordContentY(store.Y + (isLastChunk ? padding : 0));
1128
+ if (isLastChunk) store.updateY(padding, "add");
1129
+ currentLineIndex += linesToRenderCount;
1130
+ if (currentLineIndex < lines.length) HandlePageBreaks(doc, store);
634
1131
  }
635
- e.setFont(i.fontName, i.fontStyle), e.setFontSize(a);
636
- }, j = (e, t, n, r) => {
637
- let i = e.getFont().fontName, a = e.getFont().fontStyle, o = e.getFontSize(), s = (e) => {
638
- switch (e) {
1132
+ doc.setFont(savedFont.fontName, savedFont.fontStyle);
1133
+ doc.setFontSize(savedFontSize);
1134
+ };
1135
+ //#endregion
1136
+ //#region src/renderer/components/inlineText.ts
1137
+ /**
1138
+ * Renders inline text elements (Strong, Em, and Text) with proper inline styling.
1139
+ */
1140
+ const renderInlineText = (doc, element, indent, store) => {
1141
+ const currentFont = doc.getFont().fontName;
1142
+ const currentFontStyle = doc.getFont().fontStyle;
1143
+ const currentFontSize = doc.getFontSize();
1144
+ const spaceMultiplier = (style) => {
1145
+ switch (style) {
639
1146
  case "normal": return 0;
640
1147
  case "bold": return 1;
641
1148
  case "italic": return 1.5;
@@ -643,192 +1150,338 @@ var n = /* @__PURE__ */ function(e) {
643
1150
  case "codespan": return .5;
644
1151
  default: return 0;
645
1152
  }
646
- }, c = (t, c) => {
647
- c === "bold" ? e.setFont(r.options.font.bold.name && r.options.font.bold.name !== "" ? r.options.font.bold.name : i, r.options.font.bold.style || "bold") : c === "italic" ? e.setFont(r.options.font.regular.name, "italic") : c === "bolditalic" ? e.setFont(r.options.font.bold.name && r.options.font.bold.name !== "" ? r.options.font.bold.name : i, "bolditalic") : c === "codespan" ? (e.setFont("courier", "normal"), e.setFontSize(o * .9)) : e.setFont(r.options.font.regular.name, a);
648
- let l = r.options.page.maxContentWidth - n - r.X, u = e.splitTextToSize(t, l), d = c === "codespan", f = "#EEEEEE";
649
- if (r.isInlineLockActive) for (let t = 0; t < u.length; t++) {
650
- if (d) {
651
- let i = e.getTextWidth(u[t]) + h(e), a = m(e);
652
- e.setFillColor(f), e.roundedRect(r.X + n - 1, r.Y - 1, i + 2, a + 2, 2, 2, "F"), e.setFillColor("#000000");
1153
+ };
1154
+ const renderTextWithStyle = (text, style) => {
1155
+ if (style === "bold") doc.setFont(store.options.font.bold.name && store.options.font.bold.name !== "" ? store.options.font.bold.name : currentFont, store.options.font.bold.style || "bold");
1156
+ else if (style === "italic") doc.setFont(store.options.font.regular.name, "italic");
1157
+ else if (style === "bolditalic") doc.setFont(store.options.font.bold.name && store.options.font.bold.name !== "" ? store.options.font.bold.name : currentFont, "bolditalic");
1158
+ else if (style === "codespan") {
1159
+ const codeFont = store.options.font.code || {
1160
+ name: "courier",
1161
+ style: "normal"
1162
+ };
1163
+ doc.setFont(codeFont.name, codeFont.style);
1164
+ doc.setFontSize(currentFontSize * .9);
1165
+ } else doc.setFont(store.options.font.regular.name, currentFontStyle);
1166
+ const availableWidth = store.options.page.maxContentWidth - indent - store.X;
1167
+ const textLines = doc.splitTextToSize(text, availableWidth);
1168
+ const isCodeSpan = style === "codespan";
1169
+ const codePadding = 1;
1170
+ const codeBgColor = "#EEEEEE";
1171
+ if (store.isInlineLockActive) for (let i = 0; i < textLines.length; i++) {
1172
+ if (isCodeSpan) {
1173
+ const lineWidth = doc.getTextWidth(textLines[i]) + getCharWidth(doc);
1174
+ const lineHeight = getCharHight(doc);
1175
+ doc.setFillColor(codeBgColor);
1176
+ doc.roundedRect(store.X + indent - codePadding, store.Y - codePadding, lineWidth + codePadding * 2, lineHeight + codePadding * 2, 2, 2, "F");
1177
+ doc.setFillColor("#000000");
653
1178
  }
654
- e.text(u[t], r.X + n, r.Y, {
1179
+ doc.text(textLines[i], store.X + indent, store.Y, {
655
1180
  baseline: "top",
656
- maxWidth: l
657
- }), r.updateX(e.getTextDimensions(u[t]).w + (d ? 2 : 1), "add"), t < u.length - 1 && (r.updateY(m(e), "add"), r.updateX(r.options.page.xpading, "set"));
658
- }
659
- else if (u.length > 1) {
660
- let t = u[0], i = u?.slice(1)?.join(" ");
661
- if (d) {
662
- let i = e.getTextWidth(t) + h(e), a = m(e);
663
- e.setFillColor(f), e.roundedRect(r.X + (n >= 2 ? n + 2 : 0) - 1, r.Y - 1, i + 2, a + 2, 2, 2, "F"), e.setFillColor("#000000");
1181
+ maxWidth: availableWidth
1182
+ });
1183
+ store.updateX(doc.getTextDimensions(textLines[i]).w + (isCodeSpan ? codePadding * 2 : 1), "add");
1184
+ if (i < textLines.length - 1) {
1185
+ store.updateY(getCharHight(doc), "add");
1186
+ store.updateX(store.options.page.xpading, "set");
1187
+ }
1188
+ }
1189
+ else if (textLines.length > 1) {
1190
+ const firstLine = textLines[0];
1191
+ const restContent = textLines?.slice(1)?.join(" ");
1192
+ if (isCodeSpan) {
1193
+ const w = doc.getTextWidth(firstLine) + getCharWidth(doc);
1194
+ const h = getCharHight(doc);
1195
+ doc.setFillColor(codeBgColor);
1196
+ doc.roundedRect(store.X + (indent >= 2 ? indent + 2 : 0) - codePadding, store.Y - codePadding, w + codePadding * 2, h + codePadding * 2, 2, 2, "F");
1197
+ doc.setFillColor("#000000");
664
1198
  }
665
- e.text(t, r.X + (n >= 2 ? n + 2 * s(c) : 0), r.Y, {
1199
+ doc.text(firstLine, store.X + (indent >= 2 ? indent + 2 * spaceMultiplier(style) : 0), store.Y, {
666
1200
  baseline: "top",
667
- maxWidth: l
668
- }), r.updateX(r.options.page.xpading + n), r.updateY(m(e), "add");
669
- let a = r.options.page.maxContentWidth - n - r.options.page.xpading;
670
- e.splitTextToSize(i, a).forEach((t) => {
671
- if (d) {
672
- let n = e.getTextWidth(t) + h(e), i = m(e);
673
- e.setFillColor(f), e.roundedRect(r.X + h(e) - 1, r.Y - 1, n + 2, i + 2, 2, 2, "F"), e.setFillColor("#000000");
1201
+ maxWidth: availableWidth
1202
+ });
1203
+ store.updateX(store.options.page.xpading + indent);
1204
+ store.updateY(getCharHight(doc), "add");
1205
+ const maxWidthForRest = store.options.page.maxContentWidth - indent - store.options.page.xpading;
1206
+ doc.splitTextToSize(restContent, maxWidthForRest).forEach((line) => {
1207
+ if (isCodeSpan) {
1208
+ const w = doc.getTextWidth(line) + getCharWidth(doc);
1209
+ const h = getCharHight(doc);
1210
+ doc.setFillColor(codeBgColor);
1211
+ doc.roundedRect(store.X + getCharWidth(doc) - codePadding, store.Y - codePadding, w + codePadding * 2, h + codePadding * 2, 2, 2, "F");
1212
+ doc.setFillColor("#000000");
674
1213
  }
675
- e.text(t, r.X + h(e), r.Y, {
1214
+ doc.text(line, store.X + getCharWidth(doc), store.Y, {
676
1215
  baseline: "top",
677
- maxWidth: a
1216
+ maxWidth: maxWidthForRest
678
1217
  });
679
1218
  });
680
1219
  } else {
681
- if (d) {
682
- let i = e.getTextWidth(t) + h(e), a = m(e);
683
- e.setFillColor(f), e.roundedRect(r.X + n - 1, r.Y - 1, i + 2, a + 2, 2, 2, "F"), e.setFillColor("#000000");
1220
+ if (isCodeSpan) {
1221
+ const w = doc.getTextWidth(text) + getCharWidth(doc);
1222
+ const h = getCharHight(doc);
1223
+ doc.setFillColor(codeBgColor);
1224
+ doc.roundedRect(store.X + indent - codePadding, store.Y - codePadding, w + codePadding * 2, h + codePadding * 2, 2, 2, "F");
1225
+ doc.setFillColor("#000000");
684
1226
  }
685
- e.text(t, r.X + n, r.Y, {
1227
+ doc.text(text, store.X + indent, store.Y, {
686
1228
  baseline: "top",
687
- maxWidth: l
688
- }), r.updateX(e.getTextDimensions(t).w + (n >= 2 ? t.split(" ").length + 2 : 2) * s(c) * .5 + (d ? 2 : 0), "add");
1229
+ maxWidth: availableWidth
1230
+ });
1231
+ store.updateX(doc.getTextDimensions(text).w + (indent >= 2 ? text.split(" ").length + 2 : 2) * spaceMultiplier(style) * .5 + (isCodeSpan ? codePadding * 2 : 0), "add");
689
1232
  }
690
1233
  };
691
- if (t.type === "text" && t.items && t.items.length > 0) for (let e of t.items) if (e.type === "codespan") c(e.content || "", "codespan");
692
- else if (e.type === "em" || e.type === "strong") {
693
- let t = e.type === "em" ? "italic" : "bold";
694
- if (e.items && e.items.length > 0) for (let n of e.items) n.type === "strong" && t === "italic" || n.type === "em" && t === "bold" ? c(n.content || "", "bolditalic") : c(n.content || "", t);
695
- else c(e.content || "", t);
696
- } else c(e.content || "", "normal");
697
- else t.type === "em" ? c(t.content || "", "italic") : t.type === "strong" ? c(t.content || "", "bold") : t.type === "codespan" ? c(t.content || "", "codespan") : c(t.content || "", "normal");
698
- e.setFont(i, a), e.setFontSize(o);
699
- }, M = (e, t, n, r) => {
700
- let i = e.getFont().fontName, a = e.getFont().fontStyle, o = e.getFontSize(), s = e.getTextColor(), c = r.options.link?.linkColor || [
1234
+ if (element.type === "text" && element.items && element.items.length > 0) for (const item of element.items) if (item.type === "codespan") renderTextWithStyle(item.content || "", "codespan");
1235
+ else if (item.type === "em" || item.type === "strong") {
1236
+ const baseStyle = item.type === "em" ? "italic" : "bold";
1237
+ if (item.items && item.items.length > 0) for (const subItem of item.items) if (subItem.type === "strong" && baseStyle === "italic") renderTextWithStyle(subItem.content || "", "bolditalic");
1238
+ else if (subItem.type === "em" && baseStyle === "bold") renderTextWithStyle(subItem.content || "", "bolditalic");
1239
+ else renderTextWithStyle(subItem.content || "", baseStyle);
1240
+ else renderTextWithStyle(item.content || "", baseStyle);
1241
+ } else renderTextWithStyle(item.content || "", "normal");
1242
+ else if (element.type === "em") renderTextWithStyle(element.content || "", "italic");
1243
+ else if (element.type === "strong") renderTextWithStyle(element.content || "", "bold");
1244
+ else if (element.type === "codespan") renderTextWithStyle(element.content || "", "codespan");
1245
+ else renderTextWithStyle(element.content || "", "normal");
1246
+ doc.setFont(currentFont, currentFontStyle);
1247
+ doc.setFontSize(currentFontSize);
1248
+ };
1249
+ //#endregion
1250
+ //#region src/renderer/components/link.ts
1251
+ /**
1252
+ * Renders link elements with proper styling and URL handling.
1253
+ * Links are rendered in blue color and underlined to distinguish them from regular text.
1254
+ */
1255
+ const renderLink = (doc, element, indent, store) => {
1256
+ const currentFont = doc.getFont().fontName;
1257
+ const currentFontStyle = doc.getFont().fontStyle;
1258
+ const currentFontSize = doc.getFontSize();
1259
+ const currentTextColor = doc.getTextColor();
1260
+ const linkColor = store.options.link?.linkColor || [
701
1261
  0,
702
1262
  0,
703
1263
  255
704
1264
  ];
705
- e.setTextColor(...c);
706
- let l = r.options.page.maxContentWidth - n - r.X, u = t.text || t.content || "", d = t.href || "", f = e.splitTextToSize(u, l);
707
- if (r.isInlineLockActive) for (let t = 0; t < f.length; t++) {
708
- let i = e.getTextDimensions(f[t]).w, a = m(e) / 2;
709
- e.link(r.X + n, r.Y, i, a, { url: d }), e.text(f[t], r.X + n, r.Y, {
1265
+ doc.setTextColor(...linkColor);
1266
+ const availableWidth = store.options.page.maxContentWidth - indent - store.X;
1267
+ const linkText = element.text || element.content || "";
1268
+ const linkUrl = element.href || "";
1269
+ const textLines = doc.splitTextToSize(linkText, availableWidth);
1270
+ if (store.isInlineLockActive) for (let i = 0; i < textLines.length; i++) {
1271
+ const textWidth = doc.getTextDimensions(textLines[i]).w;
1272
+ const textHeight = getCharHight(doc) / 2;
1273
+ doc.link(store.X + indent, store.Y, textWidth, textHeight, { url: linkUrl });
1274
+ doc.text(textLines[i], store.X + indent, store.Y, {
710
1275
  baseline: "top",
711
- maxWidth: l
712
- }), r.updateX(i + 1, "add"), r.X + i > r.options.page.maxContentWidth - n && (r.updateY(a, "add"), r.updateX(r.options.page.xpading + n, "set")), t < f.length - 1 && (r.updateY(a, "add"), r.updateX(r.options.page.xpading + n, "set"));
1276
+ maxWidth: availableWidth
1277
+ });
1278
+ store.updateX(textWidth + 1, "add");
1279
+ if (store.X + textWidth > store.options.page.maxContentWidth - indent) {
1280
+ store.updateY(textHeight, "add");
1281
+ store.updateX(store.options.page.xpading + indent, "set");
1282
+ }
1283
+ if (i < textLines.length - 1) {
1284
+ store.updateY(textHeight, "add");
1285
+ store.updateX(store.options.page.xpading + indent, "set");
1286
+ }
713
1287
  }
714
- else if (f.length > 1) {
715
- let t = f[0], i = f?.slice(1)?.join(" "), a = e.getTextDimensions(t).w, o = m(e) / 2;
716
- e.link(r.X + n, r.Y, a, o, { url: d }), e.text(t, r.X + n, r.Y, {
1288
+ else if (textLines.length > 1) {
1289
+ const firstLine = textLines[0];
1290
+ const restContent = textLines?.slice(1)?.join(" ");
1291
+ const firstLineWidth = doc.getTextDimensions(firstLine).w;
1292
+ const textHeight = getCharHight(doc) / 2;
1293
+ doc.link(store.X + indent, store.Y, firstLineWidth, textHeight, { url: linkUrl });
1294
+ doc.text(firstLine, store.X + indent, store.Y, {
717
1295
  baseline: "top",
718
- maxWidth: l
719
- }), r.updateX(r.options.page.xpading + n), r.updateY(o, "add");
720
- let s = r.options.page.maxContentWidth - n - r.options.page.xpading;
721
- e.splitTextToSize(i, s).forEach((t) => {
722
- let n = e.getTextDimensions(t).w;
723
- e.link(r.X + h(e), r.Y, n, o, { url: d }), e.text(t, r.X + h(e), r.Y, {
1296
+ maxWidth: availableWidth
1297
+ });
1298
+ store.updateX(store.options.page.xpading + indent);
1299
+ store.updateY(textHeight, "add");
1300
+ const maxWidthForRest = store.options.page.maxContentWidth - indent - store.options.page.xpading;
1301
+ doc.splitTextToSize(restContent, maxWidthForRest).forEach((line) => {
1302
+ const lineWidth = doc.getTextDimensions(line).w;
1303
+ doc.link(store.X + getCharWidth(doc), store.Y, lineWidth, textHeight, { url: linkUrl });
1304
+ doc.text(line, store.X + getCharWidth(doc), store.Y, {
724
1305
  baseline: "top",
725
- maxWidth: s
1306
+ maxWidth: maxWidthForRest
726
1307
  });
727
1308
  });
728
1309
  } else {
729
- let t = e.getTextDimensions(u).w, i = m(e) / 2;
730
- e.link(r.X + n, r.Y, t, i, { url: d }), e.text(u, r.X + n, r.Y, {
1310
+ const textWidth = doc.getTextDimensions(linkText).w;
1311
+ const textHeight = getCharHight(doc) / 2;
1312
+ doc.link(store.X + indent, store.Y, textWidth, textHeight, { url: linkUrl });
1313
+ doc.text(linkText, store.X + indent, store.Y, {
731
1314
  baseline: "top",
732
- maxWidth: l
733
- }), r.updateX(t + 2, "add");
734
- }
735
- e.setFont(i, a), e.setFontSize(o), e.setTextColor(s);
736
- }, N = (e, t, n, r, i) => {
737
- let a = r.options, o = n + 1, s = r.X + n * a.page.indent, c = r.Y, l = s + a.page.indent / 2, u = c, d = e.internal.getCurrentPageInfo().pageNumber;
738
- t.items && t.items.length > 0 && t.items.forEach((e) => {
739
- i(e, o, r);
1315
+ maxWidth: availableWidth
1316
+ });
1317
+ store.updateX(textWidth + 2, "add");
1318
+ }
1319
+ doc.setFont(currentFont, currentFontStyle);
1320
+ doc.setFontSize(currentFontSize);
1321
+ doc.setTextColor(currentTextColor);
1322
+ };
1323
+ //#endregion
1324
+ //#region src/renderer/components/blockquote.ts
1325
+ const renderBlockquote = (doc, element, indentLevel, store, renderElement) => {
1326
+ const options = store.options;
1327
+ const blockquoteIndent = indentLevel + 1;
1328
+ const currentX = store.X + indentLevel * options.page.indent;
1329
+ const currentY = store.Y;
1330
+ const barX = currentX + options.page.indent / 2;
1331
+ const startY = currentY;
1332
+ const startPage = doc.internal.getCurrentPageInfo().pageNumber;
1333
+ if (element.items && element.items.length > 0) element.items.forEach((item) => {
1334
+ renderElement(item, blockquoteIndent, store);
740
1335
  });
741
- let f = r.Y, p = e.internal.getCurrentPageInfo().pageNumber;
742
- e.setDrawColor(100), e.setLineWidth(1);
743
- for (let t = d; t <= p; t++) {
744
- e.setPage(t);
745
- let n = t === d, r = t === p, i = n ? u : a.page.topmargin, o = r ? f : a.page.maxContentHeight;
746
- e.line(l, i, l, o);
747
- }
748
- r.recordContentY(), e.setPage(p);
749
- }, P = (e) => {
750
- if (e.data) {
751
- if (e.data.startsWith("data:image/png")) return "PNG";
752
- if (e.data.startsWith("data:image/jpeg") || e.data.startsWith("data:image/jpg")) return "JPEG";
753
- if (e.data.startsWith("data:image/webp") || e.data.startsWith("data:image/webp")) return "WEBP";
754
- if (e.data.startsWith("data:image/gif")) return "GIF";
755
- }
756
- if (e.src) {
757
- let t = e.src.split("?")[0].split("#")[0].split(".").pop()?.toUpperCase();
758
- if (t && [
1336
+ const endY = store.Y;
1337
+ const endPage = doc.internal.getCurrentPageInfo().pageNumber;
1338
+ doc.setDrawColor(100);
1339
+ doc.setLineWidth(1);
1340
+ for (let p = startPage; p <= endPage; p++) {
1341
+ doc.setPage(p);
1342
+ const isStart = p === startPage;
1343
+ const isEnd = p === endPage;
1344
+ const lineTop = isStart ? startY : options.page.topmargin;
1345
+ const lineBottom = isEnd ? endY : options.page.maxContentHeight;
1346
+ doc.line(barX, lineTop, barX, lineBottom);
1347
+ }
1348
+ store.recordContentY();
1349
+ doc.setPage(endPage);
1350
+ };
1351
+ //#endregion
1352
+ //#region src/renderer/components/image.ts
1353
+ /**
1354
+ * Detects the image format from element data and source.
1355
+ */
1356
+ const detectImageFormat = (element) => {
1357
+ if (element.data) {
1358
+ if (element.data.startsWith("data:image/png")) return "PNG";
1359
+ if (element.data.startsWith("data:image/jpeg") || element.data.startsWith("data:image/jpg")) return "JPEG";
1360
+ if (element.data.startsWith("data:image/webp")) return "WEBP";
1361
+ if (element.data.startsWith("data:image/webp")) return "WEBP";
1362
+ if (element.data.startsWith("data:image/gif")) return "GIF";
1363
+ }
1364
+ if (element.src) {
1365
+ const ext = element.src.split("?")[0].split("#")[0].split(".").pop()?.toUpperCase();
1366
+ if (ext && [
759
1367
  "PNG",
760
1368
  "JPEG",
761
1369
  "JPG",
762
1370
  "WEBP",
763
1371
  "GIF"
764
- ].includes(t)) return t === "JPG" ? "JPEG" : t;
1372
+ ].includes(ext)) return ext === "JPG" ? "JPEG" : ext;
765
1373
  }
766
1374
  return "JPEG";
767
- }, F = (e, t, n, r) => {
768
- if (!t.data) return;
769
- let i = r.options, a = i.page.unit || "mm", o = n * i.page.indent, s = i.page.maxContentWidth - o, c = r.X + o, l = r.Y;
1375
+ };
1376
+ /**
1377
+ * Renders an image element into the jsPDF document with smart sizing and alignment.
1378
+ *
1379
+ * Sizing logic (in order of priority):
1380
+ * 1. If both width & height are specified by user → convert from px, use as-is
1381
+ * 2. If only width is specified → convert from px, calculate height from aspect ratio
1382
+ * 3. If only height is specified → convert from px, calculate width from aspect ratio
1383
+ * 4. If nothing specified → use intrinsic dimensions (converted from px to doc units)
1384
+ * 5. Always clamp to page bounds (scale down proportionally if needed)
1385
+ *
1386
+ * Alignment: 'left' (default) | 'center' | 'right'
1387
+ * Can be set per-image via markdown attributes or globally via RenderOption.image.defaultAlign
1388
+ */
1389
+ const renderImage = (doc, element, indentLevel, store) => {
1390
+ if (!element.data) return;
1391
+ const options = store.options;
1392
+ const docUnit = options.page.unit || "mm";
1393
+ const indent = indentLevel * options.page.indent;
1394
+ const maxWidth = options.page.maxContentWidth - indent;
1395
+ const pageLeftX = store.X + indent;
1396
+ let currentY = store.Y;
770
1397
  try {
771
- let { finalWidth: n, finalHeight: o } = x(e, t, s, i.page.maxContentHeight - i.page.topmargin, a);
772
- l + o > i.page.maxContentHeight && (_(e, r), l = r.Y);
773
- let u = t.align || i.image?.defaultAlign || "left", d;
774
- switch (u) {
1398
+ const { finalWidth, finalHeight } = calculateImageDimensions(doc, element, maxWidth, options.page.maxContentHeight - options.page.topmargin, docUnit);
1399
+ if (currentY + finalHeight > options.page.maxContentHeight) {
1400
+ HandlePageBreaks(doc, store);
1401
+ currentY = store.Y;
1402
+ }
1403
+ const align = element.align || options.image?.defaultAlign || "left";
1404
+ let drawX;
1405
+ switch (align) {
775
1406
  case "right":
776
- d = c + s - n;
1407
+ drawX = pageLeftX + maxWidth - finalWidth;
777
1408
  break;
778
1409
  case "center":
779
- d = c + (s - n) / 2;
1410
+ drawX = pageLeftX + (maxWidth - finalWidth) / 2;
780
1411
  break;
781
1412
  default:
782
- d = c;
1413
+ drawX = pageLeftX;
783
1414
  break;
784
1415
  }
785
- let f = P(t);
786
- n > 0 && o > 0 && e.addImage(t.data, f, d, l, n, o), r.updateY(o, "add"), r.recordContentY();
1416
+ const imgFormat = detectImageFormat(element);
1417
+ if (finalWidth > 0 && finalHeight > 0) doc.addImage(element.data, imgFormat, drawX, currentY, finalWidth, finalHeight);
1418
+ store.updateY(finalHeight, "add");
1419
+ store.recordContentY();
787
1420
  } catch (e) {
788
1421
  console.warn("Failed to render image", e);
789
1422
  }
790
- }, I = () => {
791
- let e = t;
792
- if (typeof t == "function") return t;
793
- if (typeof e.default == "function") return e.default;
794
- if (typeof e.autoTable == "function") return e.autoTable;
795
- throw Error("Could not resolve jspdf-autotable export. Expected a callable export.");
796
- }, L = (e, t, n, r) => {
797
- if (!t.header || !t.rows) return;
798
- let i = r.options, a = i.page.xmargin + n * i.page.indent, o = [t.header.map((e) => e.content || "")], s = t.rows.map((e) => e.map((e) => e.content || "")), c = i.table || {};
799
- I()(e, {
800
- head: o,
801
- body: s,
802
- startY: r.Y,
1423
+ };
1424
+ //#endregion
1425
+ //#region src/renderer/components/table.ts
1426
+ const resolveAutoTable = () => {
1427
+ const autoTableCandidate = autoTable;
1428
+ if (typeof autoTable === "function") return autoTable;
1429
+ if (typeof autoTableCandidate.default === "function") return autoTableCandidate.default;
1430
+ if (typeof autoTableCandidate.autoTable === "function") return autoTableCandidate.autoTable;
1431
+ throw new Error("Could not resolve jspdf-autotable export. Expected a callable export.");
1432
+ };
1433
+ const renderTable = (doc, element, indentLevel, store) => {
1434
+ if (!element.header || !element.rows) return;
1435
+ const options = store.options;
1436
+ const marginLeft = options.page.xmargin + indentLevel * options.page.indent;
1437
+ const head = [element.header.map((h) => h.content || "")];
1438
+ const body = element.rows.map((row) => row.map((cell) => cell.content || ""));
1439
+ const userTableOptions = options.table || {};
1440
+ resolveAutoTable()(doc, {
1441
+ head,
1442
+ body,
1443
+ startY: store.Y,
803
1444
  margin: {
804
- left: a,
805
- right: i.page.xmargin
1445
+ left: marginLeft,
1446
+ right: options.page.xmargin
806
1447
  },
807
- ...c,
808
- didDrawPage: (e) => {
809
- c.didDrawPage && c.didDrawPage(e);
1448
+ ...userTableOptions,
1449
+ didDrawPage: (data) => {
1450
+ if (userTableOptions.didDrawPage) userTableOptions.didDrawPage(data);
810
1451
  },
811
- didDrawCell: (e) => {
812
- c.didDrawCell && c.didDrawCell(e);
1452
+ didDrawCell: (data) => {
1453
+ if (userTableOptions.didDrawCell) userTableOptions.didDrawCell(data);
813
1454
  }
814
1455
  });
815
- let l = e.lastAutoTable?.finalY;
816
- typeof l == "number" && (r.updateY(l + i.page.lineSpace, "set"), r.updateX(i.page.xpading, "set"), r.recordContentY());
817
- }, R = class {
818
- constructor(e) {
1456
+ const finalY = doc.lastAutoTable?.finalY;
1457
+ if (typeof finalY === "number") {
1458
+ store.updateY(finalY + options.page.lineSpace, "set");
1459
+ store.updateX(options.page.xpading, "set");
1460
+ store.recordContentY();
1461
+ }
1462
+ };
1463
+ //#endregion
1464
+ //#region src/store/renderStore.ts
1465
+ var RenderStore = class {
1466
+ constructor(options) {
819
1467
  this.cursor = {
820
1468
  x: 0,
821
1469
  y: 0
822
- }, this.lastContentY_ = 0, this.inlineLock = !1, this.options_ = e, this.cursor = {
823
- x: e.cursor.x,
824
- y: e.cursor.y
825
- }, this.lastContentY_ = e.cursor.y;
1470
+ };
1471
+ this.lastContentY_ = 0;
1472
+ this.inlineLock = false;
1473
+ this.options_ = options;
1474
+ this.cursor = {
1475
+ x: options.cursor.x,
1476
+ y: options.cursor.y
1477
+ };
1478
+ this.lastContentY_ = options.cursor.y;
826
1479
  }
827
1480
  getCursor() {
828
1481
  return this.cursor;
829
1482
  }
830
- setCursor(e) {
831
- this.cursor = e;
1483
+ setCursor(newCursor) {
1484
+ this.cursor = newCursor;
832
1485
  }
833
1486
  get options() {
834
1487
  return this.options_;
@@ -837,20 +1490,43 @@ var n = /* @__PURE__ */ function(e) {
837
1490
  return this.inlineLock;
838
1491
  }
839
1492
  activateInlineLock() {
840
- this.inlineLock = !0;
1493
+ this.inlineLock = true;
841
1494
  }
842
1495
  deactivateInlineLock() {
843
- this.inlineLock = !1;
1496
+ this.inlineLock = false;
844
1497
  }
845
- updateX(e, t = "set") {
846
- t === "set" ? this.cursor.x = e : t === "add" && (this.cursor.x += e);
1498
+ /**
1499
+ * Updates the x pointer of the cursor.
1500
+ * @param value The value to set or add.
1501
+ * @param operation 'set' to assign a new value, 'add' to increment the current value.
1502
+ * @default operation = 'set'
1503
+ */
1504
+ updateX(value, operation = "set") {
1505
+ if (operation === "set") this.cursor.x = value;
1506
+ else if (operation === "add") this.cursor.x += value;
847
1507
  }
848
- updateY(e, t = "set") {
849
- t === "set" ? this.cursor.y = e : t === "add" && (this.cursor.y += e);
1508
+ /**
1509
+ * Updates the y pointer of the cursor.
1510
+ * @param value The value to set or add.
1511
+ * @param operation 'set' to assign a new value, 'add' to increment the current value.
1512
+ * @default operation = 'set'
1513
+ */
1514
+ updateY(value, operation = "set") {
1515
+ if (operation === "set") this.cursor.y = value;
1516
+ else if (operation === "add") this.cursor.y += value;
850
1517
  }
851
- recordContentY(e) {
852
- this.lastContentY_ = e === void 0 ? this.cursor.y : e;
1518
+ /**
1519
+ * Records a Y position as the bottom of rendered content.
1520
+ * This is useful for container components (like blockquotes) to know
1521
+ * where their actual text content ends, ignoring any trailing margins.
1522
+ * @param specificY Optional Y value to record. Defaults to current cursor Y.
1523
+ */
1524
+ recordContentY(specificY) {
1525
+ this.lastContentY_ = specificY !== void 0 ? specificY : this.cursor.y;
853
1526
  }
1527
+ /**
1528
+ * Gets the last Y position recorded as content bottom.
1529
+ */
854
1530
  get lastContentY() {
855
1531
  return this.lastContentY_;
856
1532
  }
@@ -860,7 +1536,10 @@ var n = /* @__PURE__ */ function(e) {
860
1536
  get Y() {
861
1537
  return this.cursor.y;
862
1538
  }
863
- }, z = {
1539
+ };
1540
+ //#endregion
1541
+ //#region src/utils/options-validation.ts
1542
+ const defaultOptions = {
864
1543
  page: {
865
1544
  indent: 10,
866
1545
  maxContentWidth: 190,
@@ -887,88 +1566,111 @@ var n = /* @__PURE__ */ function(e) {
887
1566
  light: {
888
1567
  name: "helvetica",
889
1568
  style: "light"
1569
+ },
1570
+ code: {
1571
+ name: "courier",
1572
+ style: "normal"
890
1573
  }
891
1574
  },
892
1575
  image: { defaultAlign: "left" }
893
- }, B = (e) => {
894
- if (!e) throw Error("RenderOption is required");
895
- let t = {
896
- ...z.page,
897
- ...e.page
898
- }, n = {
899
- ...z.font,
900
- ...e.font
901
- }, r = {
902
- ...z.image,
903
- ...e.image
1576
+ };
1577
+ const validateOptions = (options) => {
1578
+ if (!options) throw new Error("RenderOption is required");
1579
+ const mergedPage = {
1580
+ ...defaultOptions.page,
1581
+ ...options.page
1582
+ };
1583
+ const mergedFont = {
1584
+ ...defaultOptions.font,
1585
+ ...options.font
1586
+ };
1587
+ const mergedImage = {
1588
+ ...defaultOptions.image,
1589
+ ...options.image
904
1590
  };
905
- return t.maxContentWidth ||= 190, t.maxContentHeight ||= 277, {
906
- ...e,
907
- page: t,
908
- font: n,
909
- image: r
1591
+ if (!mergedPage.maxContentWidth) mergedPage.maxContentWidth = 190;
1592
+ if (!mergedPage.maxContentHeight) mergedPage.maxContentHeight = 277;
1593
+ return {
1594
+ ...options,
1595
+ page: mergedPage,
1596
+ font: mergedFont,
1597
+ image: mergedImage
910
1598
  };
911
- }, V = async (e, t, r) => {
912
- let i = B(r), a = new R(i), o = await d(t);
913
- await S(o);
914
- let s = (t, r = 0, a, o = !1, c = 0, l = !1) => {
915
- let u = r * i.page.indent;
916
- switch (t.type) {
917
- case n.Heading:
918
- g(e, t, u, a, s);
1599
+ };
1600
+ //#endregion
1601
+ //#region src/renderer/MdTextRender.ts
1602
+ /**
1603
+ * Renders parsed markdown text into jsPDF document.
1604
+ *
1605
+ * @param doc - The jsPDF document.
1606
+ * @param text - The markdown content to render.
1607
+ * @param options - The render options (fonts, page margins, etc.).
1608
+ */
1609
+ const MdTextRender = async (doc, text, options) => {
1610
+ const validOptions = validateOptions(options);
1611
+ const store = new RenderStore(validOptions);
1612
+ const parsedElements = await MdTextParser(text);
1613
+ await prefetchImages(parsedElements);
1614
+ const renderElement = (element, indentLevel = 0, store, hasRawBullet = false, start = 0, ordered = false) => {
1615
+ const indent = indentLevel * validOptions.page.indent;
1616
+ switch (element.type) {
1617
+ case "heading":
1618
+ renderHeading(doc, element, indent, store, renderElement);
919
1619
  break;
920
- case n.Paragraph:
921
- T(e, t, u, a, s);
1620
+ case "paragraph":
1621
+ renderParagraph(doc, element, indent, store, renderElement);
922
1622
  break;
923
- case n.List:
924
- E(e, t, r, a, s);
1623
+ case "list":
1624
+ renderList(doc, element, indentLevel, store, renderElement);
925
1625
  break;
926
- case n.ListItem:
927
- D(e, t, r, a, s, c, l);
1626
+ case "list_item":
1627
+ renderListItem(doc, element, indentLevel, store, renderElement, start, ordered);
928
1628
  break;
929
- case n.Hr:
930
- k(e, a);
1629
+ case "hr":
1630
+ renderHR(doc, store);
931
1631
  break;
932
- case n.Code:
933
- A(e, t, r, a);
1632
+ case "code":
1633
+ renderCodeBlock(doc, element, indentLevel, store);
934
1634
  break;
935
- case n.Strong:
936
- case n.Em:
937
- case n.CodeSpan:
938
- j(e, t, u, a);
1635
+ case "strong":
1636
+ case "em":
1637
+ case "codespan":
1638
+ renderInlineText(doc, element, indent, store);
939
1639
  break;
940
- case n.Link:
941
- M(e, t, u, a);
1640
+ case "link":
1641
+ renderLink(doc, element, indent, store);
942
1642
  break;
943
- case n.Blockquote:
944
- N(e, t, r, a, s);
1643
+ case "blockquote":
1644
+ renderBlockquote(doc, element, indentLevel, store, renderElement);
945
1645
  break;
946
- case n.Image:
947
- F(e, t, r, a);
1646
+ case "image":
1647
+ renderImage(doc, element, indentLevel, store);
948
1648
  break;
949
- case n.Br: {
950
- a.updateX(i.page.xpading, "set");
951
- let t = m(e) * i.page.defaultLineHeightFactor;
952
- a.Y + t > i.page.maxContentHeight ? _(e, a) : a.updateY(t, "add"), a.recordContentY();
1649
+ case "br": {
1650
+ store.updateX(validOptions.page.xpading, "set");
1651
+ const brHeight = getCharHight(doc) * validOptions.page.defaultLineHeightFactor;
1652
+ if (store.Y + brHeight > validOptions.page.maxContentHeight) HandlePageBreaks(doc, store);
1653
+ else store.updateY(brHeight, "add");
1654
+ store.recordContentY();
953
1655
  break;
954
1656
  }
955
- case n.Table:
956
- L(e, t, r, a);
1657
+ case "table":
1658
+ renderTable(doc, element, indentLevel, store);
957
1659
  break;
958
- case n.Raw:
959
- case n.Text:
960
- O(e, t, r, a, o, s, c, l, i.content?.textAlignment === "justify");
1660
+ case "raw":
1661
+ case "text":
1662
+ renderRawItem(doc, element, indentLevel, store, hasRawBullet, renderElement, start, ordered, validOptions.content?.textAlignment === "justify");
961
1663
  break;
962
1664
  default:
963
- console.warn(`Warning: Unsupported element type encountered: ${t.type}.
1665
+ console.warn(`Warning: Unsupported element type encountered: ${element.type}.
964
1666
  If you believe this element type should be supported, please create an issue at:
965
1667
  https://github.com/JeelGajera/jspdf-md-renderer/issues
966
1668
  with details of the element and expected behavior. Thanks for helping to improve this library!`);
967
1669
  break;
968
1670
  }
969
1671
  };
970
- for (let e of o) s(e, 0, a);
971
- i.endCursorYHandler(a.Y);
1672
+ for (const item of parsedElements) renderElement(item, 0, store);
1673
+ validOptions.endCursorYHandler(store.Y);
972
1674
  };
973
1675
  //#endregion
974
- export { d as MdTextParser, V as MdTextRender };
1676
+ export { MdTextParser, MdTextRender };