Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions web/core/components/pages/modals/export-page-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,29 @@ export const ExportPageModal: React.FC<Props> = (props) => {
const selectedPageFormat = watch("page_format");
const selectedContentVariety = watch("content_variety");
const isPDFSelected = selectedExportFormat === "pdf";
const fileName = pageTitle?.toLowerCase()?.replace(/ /g, "-");
const fileName = pageTitle
?.toLowerCase()
?.replace(/[^a-z0-9-_]/g, "-")
.replace(/-+/g, "-");
// handle modal close
const handleClose = () => {
onClose();
setTimeout(() => {
reset();
}, 300);
};

const initiateDownload = (blob: Blob, filename: string) => {
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
link.click();
setTimeout(() => {
URL.revokeObjectURL(url);
}, 1000);
};

// handle export as a PDF
const handleExportAsPDF = async () => {
try {
Expand All @@ -127,13 +142,7 @@ export const ExportPageModal: React.FC<Props> = (props) => {
});

const blob = await pdf(<PDFDocument content={parsedPageContent} pageFormat={selectedPageFormat} />).toBlob();
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `${fileName}-${selectedPageFormat.toString().toLowerCase()}.pdf`;
link.click();

URL.revokeObjectURL(url);
initiateDownload(blob, `${fileName}-${selectedPageFormat.toString().toLowerCase()}.pdf`);
} catch (error) {
throw new Error(`Error in exporting as a PDF: ${error}`);
}
Expand All @@ -148,13 +157,7 @@ export const ExportPageModal: React.FC<Props> = (props) => {
});

const blob = new Blob([parsedMarkdownContent], { type: "text/markdown" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `${fileName}.md`;
link.click();

URL.revokeObjectURL(url);
initiateDownload(blob, `${fileName}.md`);
} catch (error) {
throw new Error(`Error in exporting as markdown: ${error}`);
}
Expand Down
1 change: 0 additions & 1 deletion web/core/constants/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ const EDITOR_PDF_CODE_STYLES: Styles = {
fontSize: convertRemToPixel(0.7),
},
// inline code block
// TODO: update width
"[data-node-type='inline-code-block']": {
margin: 0,
paddingVertical: convertRemToPixel(0.25 / 4 + 0.25 / 8),
Expand Down
3 changes: 2 additions & 1 deletion web/helpers/editor.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export const replaceCustomComponentsFromMarkdownContent = (props: {
let parsedMarkdownContent = markdownContent;
// replace the matched mention components with [label](redirect_uri)
const mentionRegex = /<mention-component[^>]*label="([^"]+)"[^>]*redirect_uri="([^"]+)"[^>]*><\/mention-component>/g;
const originUrl = window?.location?.origin ?? "";
const originUrl = typeof window !== "undefined" && window.location.origin ? window.location.origin : "";

parsedMarkdownContent = parsedMarkdownContent.replace(
mentionRegex,
(_match, label, redirectUri) => `[${label}](${originUrl}/${redirectUri})`
Expand Down
11 changes: 11 additions & 0 deletions web/helpers/file.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
* @returns
*/
export const getBase64Image = async (url: string): Promise<string> => {
if (!url || typeof url !== "string") {
throw new Error("Invalid URL provided");
}

// Try to create a URL object to validate the URL
try {
new URL(url);
} catch (error) {
throw new Error("Invalid URL format");
}

const response = await fetch(url);
// check if the response is OK
if (!response.ok) {
Expand Down