From 332cb3e003c8c9201171dd16e45abe6c095cdf7a Mon Sep 17 00:00:00 2001 From: Kortina Date: Wed, 8 Jul 2020 21:53:47 -0700 Subject: [PATCH] use that.rxFileExtensions in noteFiles() move cleanTitle inside slugifyTitle add one last check before creating new note on missing definition --- src/MarkdownDefinitionProvider.ts | 27 +++++++++++---------------- src/NoteWorkspace.ts | 31 ++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/MarkdownDefinitionProvider.ts b/src/MarkdownDefinitionProvider.ts index d0ee069..e1d6db9 100644 --- a/src/MarkdownDefinitionProvider.ts +++ b/src/MarkdownDefinitionProvider.ts @@ -22,41 +22,29 @@ export class MarkdownDefinitionProvider implements vscode.DefinitionProvider { position: vscode.Position, token: vscode.CancellationToken ) { - // FIXME: this whole function really needs to be cleaned up - // It's just kind of gross calling methods from all over the place - // - // console.debug('provideDefinition'); - const ref = getRefAt(document, position); - // debugRef(ref); if (ref.type != RefType.WikiLink) { - // console.debug('getRefAt was not WikiLink'); return []; } - // TODO: parameterize extensions. return if we don't have a filename and we require extensions - // const markdownFileRegex = /[\w\.\-\_\/\\]+\.(md|markdown)/i; - const selectedWord = ref.word; - // console.debug('selectedWord', selectedWord); let files: Array = []; - // selectedWord might be either: + // ref.word might be either: // a basename for a unique file in the workspace // or, a relative path to a file - // Since, selectedWord is just a string of text from a document, + // Since, ref.word is just a string of text from a document, // there is no guarantee useUniqueFilenames will tell us // it is not a relative path. // However, only check for basenames in the entire project if: if (NoteWorkspace.useUniqueFilenames()) { - // there should be exactly 1 file with name = selectedWord + // there should be exactly 1 file with name = ref.word files = (await NoteWorkspace.noteFiles()).filter((f) => { - // files = (await vscode.workspace.findFiles('**/*')).filter((f) => { return NoteWorkspace.noteNamesFuzzyMatch(f.fsPath, ref.word); }); } // If we did not find any files in the workspace, // see if a file exists at the relative path: if (files.length == 0) { - const relativePath = selectedWord; + const relativePath = ref.word; let fromDir = dirname(document.uri.fsPath.toString()); const absPath = resolve(fromDir, relativePath); if (existsSync(absPath)) { @@ -99,6 +87,13 @@ export class MarkdownDefinitionProvider implements vscode.DefinitionProvider { // TODO: could convert this to an option (to, eg, create in workspace root) const path = join(dirname(filename), mdFilename); const title = titleCaseFilename(ref.word); + // do one final check to make sure we are definitely NOT overwriting an existing file: + if (existsSync(path)) { + vscode.window.showWarningMessage( + `Error creating note, file at path already exists: ${path}` + ); + return; + } writeFileSync(path, `# ${title}\n\n`); return path; } diff --git a/src/NoteWorkspace.ts b/src/NoteWorkspace.ts index d456435..70520e8 100644 --- a/src/NoteWorkspace.ts +++ b/src/NoteWorkspace.ts @@ -162,7 +162,7 @@ export class NoteWorkspace { static normalizeNoteNameForFuzzyMatchText(noteName: string): string { // remove the brackets: - let n = noteName.replace(/[\[\]]/g, ''); + let n = noteName.replace(/[\[\]]/g, ''); // remove the extension: n = this.stripExtension(n); // slugify (to normalize spaces) @@ -173,25 +173,34 @@ export class NoteWorkspace { // Compare 2 wiki-links for a fuzzy match. // All of the following will return true static noteNamesFuzzyMatch(left: string, right: string): boolean { - return this.normalizeNoteNameForFuzzyMatch(left).toLowerCase() == this.normalizeNoteNameForFuzzyMatchText(right).toLowerCase(); + return ( + this.normalizeNoteNameForFuzzyMatch(left).toLowerCase() == + this.normalizeNoteNameForFuzzyMatchText(right).toLowerCase() + ); } static noteNamesFuzzyMatchText(left: string, right: string): boolean { - return this.normalizeNoteNameForFuzzyMatchText(left).toLowerCase() == this.normalizeNoteNameForFuzzyMatchText(right).toLowerCase(); + return ( + this.normalizeNoteNameForFuzzyMatchText(left).toLowerCase() == + this.normalizeNoteNameForFuzzyMatchText(right).toLowerCase() + ); } - static cleanTitle (title: string): string { - return title.toLowerCase() // lower + static cleanTitle(title: string): string { + return title + .toLowerCase() // lower .replace(/[-_-_ ]*$/g, ''); // removing trailing slug chars } static slugifyTitle(title: string): string { - return this.slugifyChar() == 'NONE' ? - title : - title.replace(/[!"\#$%&'()*+,\-./:;<=>?@\[\\\]^_‘{|}~\s]+/gi, this.slugifyChar()); // punctuation and whitespace to hyphens (or underscores) + let t = + this.slugifyChar() == 'NONE' + ? title + : title.replace(/[!"\#$%&'()*+,\-./:;<=>?@\[\\\]^_‘{|}~\s]+/gi, this.slugifyChar()); // punctuation and whitespace to hyphens (or underscores) + return this.cleanTitle(t); } static noteFileNameFromTitle(title: string): string { - let t = this.cleanTitle(this.slugifyTitle(title)); + let t = this.slugifyTitle(title); return t.match(this.rxFileExtensions()) ? t : `${t}.${this.defaultFileExtension()}`; } @@ -261,9 +270,9 @@ export class NoteWorkspace { } static async noteFiles(): Promise> { + let that = this; let files = (await vscode.workspace.findFiles('**/*')).filter( - // TODO: parameterize extensions. Add $ to end? - (f) => f.scheme == 'file' && f.path.match(/\.(md|markdown)/i) + (f) => f.scheme == 'file' && f.path.match(that.rxFileExtensions()) ); return files; }