From 8d2f5ee139f69d245a1da7b368c8552f0d72c1f1 Mon Sep 17 00:00:00 2001 From: Zhe Chen Date: Wed, 13 Nov 2024 13:57:17 +0900 Subject: [PATCH] Sanitize filenames before saving --- src/import_modal.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/import_modal.ts b/src/import_modal.ts index 8750a21..f0724d1 100644 --- a/src/import_modal.ts +++ b/src/import_modal.ts @@ -73,9 +73,10 @@ export class ImportModal extends Modal { } const pdfFolderPath = this.app.vault.getFolderByPath(pdfFolder)!; - const pdfPath = normalizePath( - `${pdfFolderPath.path}/${paper.title} (${paper.paperId}).pdf` + const pdfFilename = this.sanitizeFilename( + `${paper.title} (${paper.paperId}).pdf` ); + const pdfPath = normalizePath(`${pdfFolderPath.path}/${pdfFilename}`); const response = await requestUrl(paper.pdfUrl); await this.app.vault.adapter.writeBinary(pdfPath, response.arrayBuffer); @@ -88,12 +89,15 @@ export class ImportModal extends Modal { } const noteFolderPath = this.app.vault.getFolderByPath(noteFolder)!; + const noteFilename = this.sanitizeFilename( + `${paper.title} (${paper.paperId}).md` + ); const notePath = normalizePath( - `${noteFolderPath.path}/${paper.title} (${paper.paperId}).md` + `${noteFolderPath.path}/${noteFilename}` ); const noteContent = noteTemplate .replace(/{{\s*paper_id\s*}}/g, paper.paperId) - .replace(/{{\s*title\s*}}/g, paper.title) + .replace(/{{\s*title\s*}}/g, `"${paper.title}"`) .replace(/{{\s*authors\s*}}/g, paper.authors.join(", ")) .replace(/{{\s*date\s*}}/g, paper.date) .replace(/{{\s*abstract\s*}}/g, `"${paper.abstract}"`) @@ -129,4 +133,11 @@ export class ImportModal extends Modal { throw new Error("Invalid arXiv ID or URL"); } + + sanitizeFilename(filename: string): string { + return filename + .replace(/[/\\?%*:|"<>]/g, " ") + .replace(/\s+/g, " ") + .trim(); + } }