Skip to content

Commit

Permalink
use that.rxFileExtensions in noteFiles()
Browse files Browse the repository at this point in the history
move cleanTitle inside slugifyTitle
add one last check before creating new note on missing definition
  • Loading branch information
kortina committed Jul 9, 2020
1 parent 6915d8e commit 332cb3e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
27 changes: 11 additions & 16 deletions src/MarkdownDefinitionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<vscode.Uri> = [];
// 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)) {
Expand Down Expand Up @@ -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;
}
Expand Down
31 changes: 20 additions & 11 deletions src/NoteWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()}`;
}

Expand Down Expand Up @@ -261,9 +270,9 @@ export class NoteWorkspace {
}

static async noteFiles(): Promise<Array<vscode.Uri>> {
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;
}
Expand Down

0 comments on commit 332cb3e

Please sign in to comment.