-
Notifications
You must be signed in to change notification settings - Fork 41
Update Migrate Export MarkDown, Html, PDF #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,9 @@ import { useCallback } from "react"; | |
| import { useSelector } from "react-redux"; | ||
| import { selectDocument } from "../store/documentSlice"; | ||
| import { selectEditor } from "../store/editorSlice"; | ||
| import { useExportFileMutation } from "./api/file"; | ||
| import { marked } from "marked"; | ||
| import { jsPDF } from "jspdf"; | ||
| import html2canvas from "html2canvas"; | ||
|
|
||
| export const enum FileExtension { | ||
| Markdown = "markdown", | ||
|
|
@@ -24,28 +26,91 @@ export const useFileExport = (): UseFileExportReturn => { | |
| const editorStore = useSelector(selectEditor); | ||
| const documentStore = useSelector(selectDocument); | ||
|
|
||
| const exportFileMutation = useExportFileMutation(); | ||
|
|
||
| const handleExportFile = useCallback( | ||
| async (exportType: string) => { | ||
| try { | ||
|
Comment on lines
29
to
31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Tighten types: use FileExtension instead of string. Prevents unsupported values and improves autocomplete. - async (exportType: string) => {
+ async (exportType: FileExtension) => {Also applies to: 42-113 🤖 Prompt for AI Agents |
||
| const markdown = editorStore.doc?.getRoot().content?.toString() || ""; | ||
| const documentName = documentStore.data?.title || "codepair_document"; | ||
|
|
||
| enqueueSnackbar(`${exportType.toUpperCase()} file export started`, { | ||
| variant: "info", | ||
| }); | ||
|
|
||
| const response = await exportFileMutation.mutateAsync({ | ||
| exportType, | ||
| content: markdown, | ||
| fileName: documentName, | ||
| }); | ||
| let blob: Blob; | ||
| let fileName: string; | ||
|
|
||
| switch (exportType) { | ||
| case FileExtension.Markdown: | ||
| blob = new Blob([markdown], { type: "text/markdown" }); | ||
| fileName = `${documentName}.md`; | ||
| break; | ||
| case FileExtension.HTML: | ||
| { | ||
| const html = await marked(markdown); | ||
| blob = new Blob([html], { type: "text/html" }); | ||
| fileName = `${documentName}.html`; | ||
| } | ||
| break; | ||
|
Comment on lines
+47
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sanitize HTML before innerHTML to prevent XSS. marked output is unsanitized; assigning to innerHTML is exploitable even off‑screen. Sanitize for both HTML and PDF paths. +import DOMPurify from "dompurify";
...
- {
- const html = await marked(markdown);
- blob = new Blob([html], { type: "text/html" });
+ {
+ const { marked } = await import("marked");
+ const rawHtml = await marked(markdown);
+ const safeHtml = DOMPurify.sanitize(rawHtml);
+ blob = new Blob([safeHtml], { type: "text/html;charset=utf-8" });
fileName = `${documentName}.html`;
}
...
- {
- const html = await marked(markdown);
+ {
+ const [{ default: html2canvas }, { jsPDF }, { marked }] = await Promise.all([
+ import("html2canvas"),
+ import("jspdf"),
+ import("marked"),
+ ]);
+ const rawHtml = await marked(markdown);
+ const safeHtml = DOMPurify.sanitize(rawHtml);
const element = document.createElement("div");
- element.innerHTML = html;
+ element.innerHTML = safeHtml;Based on static analysis hints. Also applies to: 56-66, 58-71 🤖 Prompt for AI Agents |
||
| case FileExtension.PDF: | ||
| { | ||
| const html = await marked(markdown); | ||
|
|
||
| const element = document.createElement("div"); | ||
| element.innerHTML = html; | ||
| element.style.width = "794px"; // A4 | ||
| element.style.padding = "40px"; | ||
| element.style.fontFamily = "Arial, sans-serif"; | ||
| element.style.fontSize = "14px"; | ||
| element.style.lineHeight = "1.6"; | ||
| element.style.color = "#000000"; | ||
| element.style.backgroundColor = "#ffffff"; | ||
| element.style.position = "absolute"; | ||
| element.style.left = "-9999px"; | ||
| element.style.top = "0"; | ||
|
|
||
| document.body.appendChild(element); | ||
|
|
||
| try { | ||
| const canvas = await html2canvas(element, { | ||
| scale: 2, | ||
| useCORS: true, | ||
| logging: false, | ||
| backgroundColor: "#ffffff", | ||
| }); | ||
|
|
||
| const imgData = canvas.toDataURL("image/png"); | ||
| const pdf = new jsPDF({ | ||
| orientation: "portrait", | ||
| unit: "mm", | ||
| format: "a4", | ||
| }); | ||
|
|
||
| const imgWidth = 210; | ||
| const pageHeight = 297; // A4 | ||
| const imgHeight = (canvas.height * imgWidth) / canvas.width; | ||
| let heightLeft = imgHeight; | ||
| let position = 0; | ||
|
|
||
| pdf.addImage(imgData, "PNG", 0, position, imgWidth, imgHeight); | ||
| heightLeft -= pageHeight; | ||
|
|
||
| const contentDisposition = response.headers["content-disposition"]; | ||
| const fileNameMatch = contentDisposition?.match(/filename="?(.+)"?\s*$/i); | ||
| const fileName = fileNameMatch ? fileNameMatch[1] : `${documentName}.${exportType}`; | ||
| while (heightLeft > 0) { | ||
| position = heightLeft - imgHeight; | ||
| pdf.addPage(); | ||
| pdf.addImage(imgData, "PNG", 0, position, imgWidth, imgHeight); | ||
| heightLeft -= pageHeight; | ||
| } | ||
|
|
||
| const blob = new Blob([response.data], { type: response.headers["content-type"] }); | ||
| blob = pdf.output("blob"); | ||
| fileName = `${documentName}.pdf`; | ||
| } finally { | ||
| document.body.removeChild(element); | ||
| } | ||
| } | ||
| break; | ||
| default: | ||
| throw new Error(`Unsupported export type: ${exportType}`); | ||
| } | ||
|
|
||
| const url = window.URL.createObjectURL(blob); | ||
| const link = document.createElement("a"); | ||
|
|
@@ -66,7 +131,7 @@ export const useFileExport = (): UseFileExportReturn => { | |
| }); | ||
| } | ||
| }, | ||
| [editorStore, documentStore, enqueueSnackbar, exportFileMutation] | ||
| [editorStore, documentStore, enqueueSnackbar] | ||
| ); | ||
|
|
||
| const handleExportToPDF = () => handleExportFile(FileExtension.PDF); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Add HTML sanitization dependency (DOMPurify) to mitigate XSS.
You render marked output via innerHTML in the PDF path. Marked does not sanitize by default; sanitize before injecting.
Apply this to dependencies (pick latest stable):
"dependencies": { + "dompurify": "^3.0.0", "html2canvas": "^1.4.1", "jspdf": "^3.0.3", "marked": "^16.3.0",Please confirm the latest safe DOMPurify version and whether a caret range is acceptable in this repo.
🏁 Script executed:
Length of output: 3604
🌐 Web query:
💡 Result:
The latest stable DOMPurify release is 3.2.7 (released Sep 17, 2025). [1][2]
Sources:
[1] GitHub Releases — cure53/DOMPurify (3.2.7).
[2] Snyk / npm package info for dompurify (latest: 3.2.7).
Add DOMPurify v3.2.7 dependency for sanitizing marked output. You render marked output via innerHTML in frontend/src/hooks/useFileExport.ts; sanitize before injecting. Add to dependencies:
"dependencies": { + "dompurify": "^3.2.7",📝 Committable suggestion
🤖 Prompt for AI Agents