Skip to content
Open
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
19 changes: 18 additions & 1 deletion src/AppHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class AppHelper {

async createOrModifyNote(title: string, markdown: string, front_matter: any): Promise<TFile> {
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)}
---
Expand All @@ -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<void> {
const cached_front_matter = this.app.metadataCache.getFileCache(file).frontmatter;
const front_matter = {...cached_front_matter, ...new_front_matter};
Expand Down