Skip to content

Commit

Permalink
Sanitize filenames before saving
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhekl committed Nov 13, 2024
1 parent 50925dc commit 8d2f5ee
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/import_modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}"`)
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 8d2f5ee

Please sign in to comment.