From 0aa96141fa2780af37ca61ccf7bac6fde4fe0a1b Mon Sep 17 00:00:00 2001 From: Ivan du Toit Date: Tue, 27 Aug 2024 15:37:35 -0700 Subject: [PATCH] Fix title sanitization to remove invalid chars when importing --- src/AppHelper.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/AppHelper.ts b/src/AppHelper.ts index 4757865..89fbd1f 100644 --- a/src/AppHelper.ts +++ b/src/AppHelper.ts @@ -29,7 +29,7 @@ export class AppHelper { async createOrModifyNote(title: string, markdown: string, front_matter: any): Promise { const parent = this.getParentFolder(); - const filename = normalizePath(`${parent.path}/${title}.md`); + const filename = normalizePath(`${parent.path}/${this.sanitizeFileName(title)}.md`); const file_content = `--- ${stringifyYaml(front_matter)} --- @@ -56,6 +56,23 @@ ${markdown}`; return parent; } + sanitizeFileName(name: string) { + let illegalRe = /[\/\?<>\\:\*\|"]/g; + let reservedRe = /^\.+$/; + let windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i; + let windowsTrailingRe = /[\. ]+$/; + let startsWithDotRe = /^\./; // Regular expression to match filenames starting with "." + let badLinkRe = /[\[\]#|^]/g; // Regular expression to match characters that interferes with links: [ ] # | ^ + + return name + .replace(illegalRe, '') + .replace(reservedRe, '') + .replace(windowsReservedRe, '') + .replace(windowsTrailingRe, '') + .replace(startsWithDotRe, '') + .replace(badLinkRe, ''); + } + async updateNote(file: TFile, markdown: string, new_front_matter: any): Promise { const cached_front_matter = this.app.metadataCache.getFileCache(file).frontmatter; const front_matter = {...cached_front_matter, ...new_front_matter};