-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocx-export.js
More file actions
669 lines (605 loc) · 22.3 KB
/
docx-export.js
File metadata and controls
669 lines (605 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// DOCX Export Functionality
const docxBtn = document.getElementById("docxBtn");
/**
* Extract title from editor content (first H1)
*/
function extractDocxTitle() {
const firstHeading = editor.querySelector("h1");
if (firstHeading && firstHeading.textContent.trim()) {
return firstHeading.textContent.trim();
}
return "Document";
}
/**
* Generate filename for DOCX
*/
function generateDocxFilename() {
const title = extractDocxTitle();
const sanitized = title
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, "")
.replace(/\s+/g, "-")
.substring(0, 50);
const timestamp = Date.now();
return `${sanitized}-${timestamp}.docx`;
}
/**
* Convert SVG element to PNG data URL
*/
async function svgToPngDataUrl(svgElement, maxWidth = 1200) {
return new Promise((resolve, reject) => {
try {
// Clone the SVG to avoid modifying the original
const svgClone = svgElement.cloneNode(true);
// Get dimensions - prefer getBoundingClientRect() for actual rendered size,
// since SVG attributes may be percentages or small viewBox units
const bbox = svgElement.getBoundingClientRect();
const attrWidth = parseFloat(svgClone.getAttribute("width"));
const attrHeight = parseFloat(svgClone.getAttribute("height"));
const rawAttr = svgClone.getAttribute("width") || "";
// Reject attribute values that are percentages or suspiciously small
const attrIsReliable = attrWidth > 10 && !rawAttr.includes("%");
let width = (bbox.width > 10 ? bbox.width : null)
|| (attrIsReliable ? attrWidth : null)
|| 400;
let height = (bbox.height > 10 ? bbox.height : null)
|| (attrIsReliable ? attrHeight : null)
|| 300;
// Scale down if too wide
if (width > maxWidth) {
const scale = maxWidth / width;
height = height * scale;
width = maxWidth;
}
// Round dimensions
width = Math.round(width);
height = Math.round(height);
// Ensure SVG has proper attributes
svgClone.setAttribute("width", width);
svgClone.setAttribute("height", height);
svgClone.setAttribute("xmlns", "http://www.w3.org/2000/svg");
svgClone.setAttribute(
"xmlns:xlink",
"http://www.w3.org/1999/xlink"
);
// Remove any external references that could taint the canvas
svgClone.querySelectorAll("use").forEach((use) => {
const href =
use.getAttribute("href") || use.getAttribute("xlink:href");
if (href && href.startsWith("http")) {
use.remove();
}
});
// Inline all computed styles to avoid external CSS issues
const allElements = svgClone.querySelectorAll("*");
allElements.forEach((el) => {
const computed = window.getComputedStyle(
svgElement.querySelector(el.tagName) || el
);
// Only inline essential styles
if (computed.fill && computed.fill !== "none") {
el.style.fill = computed.fill;
}
if (computed.stroke && computed.stroke !== "none") {
el.style.stroke = computed.stroke;
}
if (computed.fontFamily) {
el.style.fontFamily = "Arial, sans-serif"; // Use safe font
}
});
// Add white background
const bgRect = document.createElementNS(
"http://www.w3.org/2000/svg",
"rect"
);
bgRect.setAttribute("width", "100%");
bgRect.setAttribute("height", "100%");
bgRect.setAttribute("fill", "white");
svgClone.insertBefore(bgRect, svgClone.firstChild);
// Convert to data URL directly (not blob URL to avoid tainting)
const svgData = new XMLSerializer().serializeToString(svgClone);
const svgBase64 = btoa(unescape(encodeURIComponent(svgData)));
const svgDataUrl = "data:image/svg+xml;base64," + svgBase64;
// Create image and canvas
const img = new Image();
img.crossOrigin = "anonymous";
img.onload = () => {
try {
const canvas = document.createElement("canvas");
canvas.width = width * 2; // 2x for better quality
canvas.height = height * 2;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.scale(2, 2);
ctx.drawImage(img, 0, 0, width, height);
const pngDataUrl = canvas.toDataURL("image/png");
resolve({ dataUrl: pngDataUrl, width: width, height: height });
} catch (canvasError) {
console.error("[DOCX] Canvas error:", canvasError);
// Fallback: return SVG data URL
resolve({
dataUrl: svgDataUrl,
width: width,
height: height,
isSvg: true,
});
}
};
img.onerror = (err) => {
console.error("[DOCX] Image load error:", err);
reject(new Error("Failed to load SVG as image"));
};
img.src = svgDataUrl;
} catch (error) {
reject(error);
}
});
}
// Store mermaid images for DOCX export
let mermaidImagesForDocx = [];
/**
* Pre-process mermaid diagrams for DOCX export
*/
async function prepareMermaidForDocx() {
mermaidImagesForDocx = [];
const wrappers = editor.querySelectorAll(".mermaid-wrapper");
for (let i = 0; i < wrappers.length; i++) {
const wrapper = wrappers[i];
const svg = wrapper.querySelector("svg");
if (svg) {
try {
const imageData = await svgToPngDataUrl(svg);
mermaidImagesForDocx.push({
index: i,
...imageData,
});
} catch (error) {
console.error("[DOCX] Failed to convert mermaid SVG:", error);
mermaidImagesForDocx.push({
index: i,
error: true,
});
}
}
}
}
/**
* Parse inline text with formatting (bold, italic, code, links)
*/
function parseInlineContent(element) {
const children = [];
function processNode(node) {
if (node.nodeType === Node.TEXT_NODE) {
const text = node.textContent;
if (text) {
children.push(new docx.TextRun({ text: text }));
}
} else if (node.nodeType === Node.ELEMENT_NODE) {
const tagName = node.tagName.toLowerCase();
if (tagName === "strong" || tagName === "b") {
const innerText = node.textContent;
children.push(new docx.TextRun({ text: innerText, bold: true }));
} else if (tagName === "em" || tagName === "i") {
const innerText = node.textContent;
children.push(
new docx.TextRun({ text: innerText, italics: true })
);
} else if (tagName === "code") {
const innerText = node.textContent;
children.push(
new docx.TextRun({
text: innerText,
font: "Courier New",
shading: { fill: "F4F4F4" },
})
);
} else if (tagName === "a") {
const linkText = node.textContent;
const href = node.getAttribute("href") || "";
children.push(
new docx.ExternalHyperlink({
children: [
new docx.TextRun({ text: linkText, style: "Hyperlink" }),
],
link: href,
})
);
} else if (tagName === "br") {
children.push(new docx.TextRun({ break: 1 }));
} else {
// Recursively process child nodes for nested elements
node.childNodes.forEach((child) => processNode(child));
}
}
}
element.childNodes.forEach((child) => processNode(child));
return children;
}
/**
* Convert HTML element to DOCX paragraph(s)
*/
function htmlElementToDocx(element) {
const tagName = element.tagName ? element.tagName.toLowerCase() : "";
const paragraphs = [];
switch (tagName) {
case "h1":
paragraphs.push(
new docx.Paragraph({
children: parseInlineContent(element),
heading: docx.HeadingLevel.HEADING_1,
spacing: { before: 400, after: 200 },
})
);
break;
case "h2":
paragraphs.push(
new docx.Paragraph({
children: parseInlineContent(element),
heading: docx.HeadingLevel.HEADING_2,
spacing: { before: 300, after: 150 },
})
);
break;
case "h3":
paragraphs.push(
new docx.Paragraph({
children: parseInlineContent(element),
heading: docx.HeadingLevel.HEADING_3,
spacing: { before: 250, after: 120 },
})
);
break;
case "h4":
case "h5":
case "h6":
paragraphs.push(
new docx.Paragraph({
children: parseInlineContent(element),
heading: docx.HeadingLevel.HEADING_4,
spacing: { before: 200, after: 100 },
})
);
break;
case "p":
paragraphs.push(
new docx.Paragraph({
children: parseInlineContent(element),
spacing: { after: 200 },
})
);
break;
case "ul":
element.querySelectorAll(":scope > li").forEach((li) => {
paragraphs.push(
new docx.Paragraph({
children: parseInlineContent(li),
bullet: { level: 0 },
spacing: { after: 100 },
})
);
});
break;
case "ol":
element.querySelectorAll(":scope > li").forEach((li, index) => {
paragraphs.push(
new docx.Paragraph({
children: parseInlineContent(li),
numbering: { reference: "default-numbering", level: 0 },
spacing: { after: 100 },
})
);
});
break;
case "blockquote":
paragraphs.push(
new docx.Paragraph({
children: parseInlineContent(element),
indent: { left: 720 },
border: {
left: {
style: docx.BorderStyle.SINGLE,
size: 24,
color: "3498DB",
},
},
spacing: { after: 200 },
})
);
break;
case "pre":
const codeContent = element.textContent || "";
const codeLines = codeContent.split("\n");
codeLines.forEach((line, index) => {
paragraphs.push(
new docx.Paragraph({
children: [
new docx.TextRun({
text: line || " ",
font: "Courier New",
size: 20,
}),
],
shading: { fill: "F4F4F4" },
spacing: { after: index === codeLines.length - 1 ? 200 : 0 },
})
);
});
break;
case "hr":
paragraphs.push(
new docx.Paragraph({
children: [],
border: {
bottom: {
style: docx.BorderStyle.SINGLE,
size: 6,
color: "CCCCCC",
},
},
spacing: { before: 400, after: 400 },
})
);
break;
case "table":
const tableRows = [];
element.querySelectorAll("tr").forEach((tr) => {
const cells = [];
tr.querySelectorAll("th, td").forEach((cell) => {
const isHeader = cell.tagName.toLowerCase() === "th";
cells.push(
new docx.TableCell({
children: [
new docx.Paragraph({
children: parseInlineContent(cell),
...(isHeader ? { bold: true } : {}),
}),
],
shading: isHeader ? { fill: "F5F5F5" } : {},
margins: { top: 100, bottom: 100, left: 100, right: 100 },
})
);
});
if (cells.length > 0) {
tableRows.push(new docx.TableRow({ children: cells }));
}
});
if (tableRows.length > 0) {
paragraphs.push(
new docx.Table({
rows: tableRows,
width: { size: 100, type: docx.WidthType.PERCENTAGE },
})
);
// Add spacing after table
paragraphs.push(
new docx.Paragraph({ children: [], spacing: { after: 200 } })
);
}
break;
case "div":
// Handle mermaid diagram wrappers
if (
element.classList &&
element.classList.contains("mermaid-wrapper")
) {
// Find the index of this wrapper
const allWrappers = Array.from(
editor.querySelectorAll(".mermaid-wrapper")
);
const wrapperIndex = allWrappers.indexOf(element);
// Check if we have pre-rendered image data
const imageData = mermaidImagesForDocx.find(
(img) => img.index === wrapperIndex
);
if (imageData && !imageData.error && imageData.dataUrl) {
try {
// Convert data URL to base64
const base64Data = imageData.dataUrl.split(",")[1];
// Scale image to fit DOCX page width (6.5 inches at 96 DPI = 624px)
const maxDocxWidth = 624;
let imgWidth = imageData.width;
let imgHeight = imageData.height;
if (imgWidth > maxDocxWidth) {
const scale = maxDocxWidth / imgWidth;
imgHeight = Math.round(imgHeight * scale);
imgWidth = maxDocxWidth;
} else if (imgWidth < maxDocxWidth * 0.5) {
// Scale up small diagrams to at least 50% of page width
const scale = (maxDocxWidth * 0.7) / imgWidth;
imgHeight = Math.round(imgHeight * scale);
imgWidth = Math.round(imgWidth * scale);
}
// Create image for DOCX
const image = new docx.ImageRun({
data: Uint8Array.from(atob(base64Data), (c) =>
c.charCodeAt(0)
),
transformation: {
width: imgWidth,
height: imgHeight,
},
type: "png",
});
paragraphs.push(
new docx.Paragraph({
children: [image],
alignment: docx.AlignmentType.CENTER,
spacing: { before: 200, after: 200 },
})
);
} catch (error) {
console.error("[DOCX] Failed to add mermaid image:", error);
// Fallback: add placeholder text
paragraphs.push(
new docx.Paragraph({
children: [
new docx.TextRun({
text: "[Mermaid Diagram]",
italics: true,
}),
],
alignment: docx.AlignmentType.CENTER,
spacing: { before: 200, after: 200 },
})
);
}
} else {
// Fallback: add the source code
const sourceElement = element.querySelector(".mermaid-source");
if (sourceElement) {
const source = sourceElement.textContent || "";
paragraphs.push(
new docx.Paragraph({
children: [
new docx.TextRun({
text: "[Mermaid Diagram]",
italics: true,
}),
],
alignment: docx.AlignmentType.CENTER,
spacing: { before: 200, after: 100 },
})
);
}
}
}
break;
default:
// For unknown elements, try to get text content
if (element.textContent && element.textContent.trim()) {
paragraphs.push(
new docx.Paragraph({
children: [new docx.TextRun({ text: element.textContent })],
spacing: { after: 200 },
})
);
}
}
return paragraphs;
}
/**
* Convert entire editor content to DOCX document
*/
function convertHtmlToDocxElements() {
const docxElements = [];
const editorChildren = editor.children;
for (let i = 0; i < editorChildren.length; i++) {
const child = editorChildren[i];
const elements = htmlElementToDocx(child);
docxElements.push(...elements);
}
// If no content, add empty paragraph
if (docxElements.length === 0) {
docxElements.push(new docx.Paragraph({ children: [] }));
}
return docxElements;
}
/**
* Generate and download DOCX file
*/
async function generateDOCX() {
console.log("[DOCX] Starting DOCX generation");
// Pre-process mermaid diagrams to images
await prepareMermaidForDocx();
const title = extractDocxTitle();
const docxElements = convertHtmlToDocxElements();
const doc = new docx.Document({
title: title,
creator: "Marky Markdown Editor",
description: "Document created with Marky",
numbering: {
config: [
{
reference: "default-numbering",
levels: [
{
level: 0,
format: docx.LevelFormat.DECIMAL,
text: "%1.",
alignment: docx.AlignmentType.START,
style: {
paragraph: {
indent: { left: 720, hanging: 360 },
},
},
},
],
},
],
},
styles: {
paragraphStyles: [
{
id: "Normal",
name: "Normal",
basedOn: "Normal",
next: "Normal",
run: {
font: "Arial",
size: 24, // 12pt
},
paragraph: {
spacing: { line: 276 }, // 1.15 line spacing
},
},
],
},
sections: [
{
properties: {
page: {
margin: {
top: 1440, // 1 inch
right: 1440,
bottom: 1440,
left: 1440,
header: 720, // 0.5 inch
footer: 720,
gutter: 0,
},
},
},
children: docxElements,
},
],
});
const filename = generateDocxFilename();
console.log("[DOCX] Generating file:", filename);
const blob = await docx.Packer.toBlob(doc);
saveAs(blob, filename);
console.log("[DOCX] DOCX generated successfully");
return { success: true, filename: filename };
}
// DOCX Button Event Handler
docxBtn.addEventListener("click", async () => {
const loadingIndicator = docxBtn.querySelector(
".docx-loading-indicator"
);
const btnText = docxBtn.querySelector(".docx-btn-text");
try {
docxBtn.disabled = true;
btnText.style.display = "none";
loadingIndicator.style.display = "inline-block";
const result = await generateDOCX();
btnText.style.display = "inline";
loadingIndicator.style.display = "none";
const originalText = btnText.textContent;
btnText.textContent = "✓ Saved!";
setTimeout(() => {
btnText.textContent = originalText;
docxBtn.disabled = false;
}, 2000);
} catch (error) {
console.error("[DOCX] Error:", error);
btnText.style.display = "inline";
loadingIndicator.style.display = "none";
const originalText = btnText.textContent;
btnText.textContent = "✗ Failed";
setTimeout(() => {
btnText.textContent = originalText;
docxBtn.disabled = false;
}, 2000);
alert(`Failed to generate DOCX: ${error.message}`);
}
});