Skip to content

Commit

Permalink
cleanup and bump to 0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
kortina committed Aug 16, 2020
1 parent e81c877 commit 13b2464
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 109 deletions.
13 changes: 13 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,29 @@
"cSpell.words": [
"Backlink",
"Backlinks",
"Berdn",
"Devops",
"Suping",
"ahazxm",
"andermerwed",
"basenames",
"davovscapcom",
"digiguru",
"filepath",
"fullwidth",
"jumplist",
"kolloch",
"lukakemperle",
"lukesmurray",
"nevermind",
"notetaking",
"nvalt",
"pomdtr",
"prefill",
"qbikez",
"quickfold",
"regexes",
"thomaskoppelaar",
"unist",
"vpackage",
"vpublish",
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-markdown-notes",
"displayName": "Markdown Notes",
"description": "Navigate notes with [[wiki-links]], backlinks, and #tags (like Bear, Roam, etc). Automatically create notes from new inline [[wiki-links]]. Use Peek Definition to preview linked notes.",
"version": "0.0.13",
"version": "0.0.14",
"publisher": "kortina",
"repository": {
"url": "https://github.com/kortina/vscode-markdown-notes.git",
Expand Down Expand Up @@ -98,7 +98,7 @@
"vscodeMarkdownNotes.allowPipedWikiLinks": {
"type": "boolean",
"default": false,
"description": "States whether or not to use piped wikilinks. When set to false, this will ignore any '|' character and slugify it as normal."
"description": "States whether or not to use piped wiki-links. When set to false, this will ignore any '|' character and slugify it as normal."
},
"vscodeMarkdownNotes.pipedWikiLinksSyntax": {
"type": "string",
Expand All @@ -107,20 +107,20 @@
"file|desc"
],
"default": "file|desc",
"description": "Describes what piped wikilink syntax to use: either [[file|description]], or [[description|file]]."
"description": "Describes what piped wiki-link syntax to use: either [[file|description]], or [[description|file]]."
},
"vscodeMarkdownNotes.pipedWikiLinksSeparator": {
"type": "string",
"default": "\\|",
"pattern": "[^\\[\\]]+",
"patternErrorMessage": "Warning: This separator will unfortunately not work - It would break the wikilink syntax.",
"patternErrorMessage": "Warning: This separator will unfortunately not work - It would break the wiki-link syntax.",
"markdownDescription": "States what separator should be used - **Note: Special regex characters should be escaped! Example: '|' should be '\\\\|'**"
}
},
"vscodeMarkdownNotes.triggerSuggestOnReplacement": {
"type": "boolean",
"default": true,
"description": "Trigger suggest on both insertion AND replacement of new character inside a wiki-link. Defaults true. Set false to only trigger suggest on insertion. See PR #69 for details."
"type": "boolean",
"default": true,
"description": "Trigger suggest on both insertion AND replacement of new character inside a wiki-link. Defaults true. Set false to only trigger suggest on insertion. See PR #69 for details."
}
},
"views": {
Expand Down
36 changes: 16 additions & 20 deletions src/NoteWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ enum SlugifyCharacter {
none = 'NONE',
}

enum PipedWikilinksSyntax {
filedesc = 'file|desc',
descfile = 'desc|file',
enum PipedWikiLinksSyntax {
fileDesc = 'file|desc',
descFile = 'desc|file',
}

type Config = {
Expand All @@ -37,7 +37,7 @@ type Config = {
newNoteTemplate: string;
triggerSuggestOnReplacement: boolean;
allowPipedWikiLinks: boolean;
pipedWikiLinksSyntax: PipedWikilinksSyntax;
pipedWikiLinksSyntax: PipedWikiLinksSyntax;
pipedWikiLinksSeparator: string;
};

Expand All @@ -50,7 +50,7 @@ export class NoteWorkspace {
// This will allow us to potentially expose these as settings.
static _rxTagNoAnchors = '\\#[\\w\\-\\_]+'; // used to match tags that appear within lines
static _rxTagWithAnchors = '^\\#[\\w\\-\\_]+$'; // used to match entire words
static _rxWikiLink = "\\[\\[[^sep\\]]+(sep[^sep\\]]+)?\\]\\]"; // [[wiki-link-regex(|with potential pipe)?]] Note: "sep" will be replaced with pipedWikiLinksSeparator on compile
static _rxWikiLink = '\\[\\[[^sep\\]]+(sep[^sep\\]]+)?\\]\\]'; // [[wiki-link-regex(|with potential pipe)?]] Note: "sep" will be replaced with pipedWikiLinksSeparator on compile
static _rxMarkdownWordPattern = '([\\_\\w\\#\\.\\/\\\\]+)'; // had to add [".", "/", "\"] to get relative path completion working and ["#"] to get tag completion working
static _rxFileExtensions = '\\.(md|markdown|mdx|fountain)$';
static _defaultFileExtension = 'md';
Expand All @@ -68,8 +68,8 @@ export class NoteWorkspace {
newNoteTemplate: NoteWorkspace._defaultNoteTemplate,
triggerSuggestOnReplacement: NoteWorkspace._defaultTriggerSuggestOnReplacement,
allowPipedWikiLinks: false,
pipedWikiLinksSyntax: PipedWikilinksSyntax.descfile,
pipedWikiLinksSeparator: "\\|",
pipedWikiLinksSyntax: PipedWikiLinksSyntax.descFile,
pipedWikiLinksSeparator: '\\|',
};
static DOCUMENT_SELECTOR = [
// { scheme: 'file', language: 'markdown' },
Expand All @@ -93,7 +93,7 @@ export class NoteWorkspace {
newNoteTemplate: c.get('newNoteTemplate') as string,
triggerSuggestOnReplacement: c.get('triggerSuggestOnReplacement') as boolean,
allowPipedWikiLinks: c.get('allowPipedWikiLinks') as boolean,
pipedWikiLinksSyntax: c.get('pipedWikiLinksSyntax') as PipedWikilinksSyntax,
pipedWikiLinksSyntax: c.get('pipedWikiLinksSyntax') as PipedWikiLinksSyntax,
pipedWikiLinksSeparator: c.get('pipedWikiLinksSeparator') as string,
};
}
Expand All @@ -112,6 +112,7 @@ export class NoteWorkspace {

static triggerSuggestOnReplacement() {
return this.cfg().triggerSuggestOnReplacement;
}

static allowPipedWikiLinks(): boolean {
return this.cfg().allowPipedWikiLinks;
Expand All @@ -138,7 +139,7 @@ export class NoteWorkspace {
static rxWikiLink(): RegExp {
// NB: MUST have g flag to match multiple words per line
// return /\[\[[\w\.\-\_\/\\]+/i; // [[wiki-link-regex
this._rxWikiLink = this._rxWikiLink.replace(/sep/g,NoteWorkspace.pipedWikiLinksSeparator());
this._rxWikiLink = this._rxWikiLink.replace(/sep/g, NoteWorkspace.pipedWikiLinksSeparator());
return new RegExp(this._rxWikiLink, 'gi');
}
static rxMarkdownWordPattern(): RegExp {
Expand Down Expand Up @@ -203,30 +204,25 @@ export class NoteWorkspace {
}

static cleanPipedWikiLink(noteName: string): string {

// Check whether or not we should remove the description

if (NoteWorkspace.allowPipedWikiLinks()) {

let separator: string = NoteWorkspace.pipedWikiLinksSeparator();
let capturegroup = "[^\\["+separator+"]+";
let captureGroup = '[^\\[' + separator + ']+';
let regex: RegExp;

if (NoteWorkspace.pipedWikiLinksSyntax() == 'file|desc') {

// Should capture the "|desc" at the end of a wikilink
regex = new RegExp(separator + capturegroup +"$");

// Should capture the "|desc" at the end of a wiki-link
regex = new RegExp(separator + captureGroup + '$');
} else {

// Should capture the "desc|" at the beginning of a wikilink
regex = new RegExp("^"+capturegroup+separator);
// Should capture the "desc|" at the beginning of a wiki-link
regex = new RegExp('^' + captureGroup + separator);
}

noteName = noteName.replace(regex, ''); // Remove description from the end
return noteName;

// If piped wikilinks aren't used, don't alter the notename.
// If piped wiki-links aren't used, don't alter the note name.
} else {
return noteName;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ export function getRefAt(document: vscode.TextDocument, position: vscode.Positio
let r = new vscode.Range(s, e);
ref = document.getText(r);
if (ref) {

// Check for piped wikilinks
// Check for piped wiki-links
ref = NoteWorkspace.cleanPipedWikiLink(ref);

return {
Expand Down
116 changes: 38 additions & 78 deletions src/test/jest/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,37 +99,16 @@ test('noteNamesFuzzyMatchSlashes', () => {
'link-topic'
);
// lower case is expected because 'slugifyTitle' includes toLowerCase
expect(NoteWorkspace.slugifyTitle('Link/Topic')).toEqual(
'link-topic'
);
expect(NoteWorkspace.normalizeNoteNameForFuzzyMatchText('Link/Topic')).toEqual(
'link-topic'
);
expect(
NoteWorkspace.noteNamesFuzzyMatch('dir/sub/link-topic.md', 'Link/Topic')
).toBeTruthy();
expect(
NoteWorkspace.noteNamesFuzzyMatch('dir/sub/Link-Topic.md', 'Link/Topic')
).toBeTruthy();
expect(
NoteWorkspace.noteNamesFuzzyMatch('dir/sub/link-topic.md', 'link/topic')
).toBeTruthy();
expect(
NoteWorkspace.noteNamesFuzzyMatch('dir/sub/Link-Topic.md', 'link/topic')
).toBeTruthy();
expect(
NoteWorkspace.noteNamesFuzzyMatch('dir/sub/link-topic.md', 'Link/topic')
).toBeTruthy();
expect(
NoteWorkspace.noteNamesFuzzyMatch('dir/sub/link-topic.md', 'link/Topic')
).toBeTruthy();
expect(
NoteWorkspace.noteNamesFuzzyMatch('dir/sub/Link-Topic.md', 'Link/topic')
).toBeTruthy();
expect(
NoteWorkspace.noteNamesFuzzyMatch('dir/sub/Link-Topic.md', 'link/Topic')
).toBeTruthy();

expect(NoteWorkspace.slugifyTitle('Link/Topic')).toEqual('link-topic');
expect(NoteWorkspace.normalizeNoteNameForFuzzyMatchText('Link/Topic')).toEqual('link-topic');
expect(NoteWorkspace.noteNamesFuzzyMatch('dir/sub/link-topic.md', 'Link/Topic')).toBeTruthy();
expect(NoteWorkspace.noteNamesFuzzyMatch('dir/sub/Link-Topic.md', 'Link/Topic')).toBeTruthy();
expect(NoteWorkspace.noteNamesFuzzyMatch('dir/sub/link-topic.md', 'link/topic')).toBeTruthy();
expect(NoteWorkspace.noteNamesFuzzyMatch('dir/sub/Link-Topic.md', 'link/topic')).toBeTruthy();
expect(NoteWorkspace.noteNamesFuzzyMatch('dir/sub/link-topic.md', 'Link/topic')).toBeTruthy();
expect(NoteWorkspace.noteNamesFuzzyMatch('dir/sub/link-topic.md', 'link/Topic')).toBeTruthy();
expect(NoteWorkspace.noteNamesFuzzyMatch('dir/sub/Link-Topic.md', 'Link/topic')).toBeTruthy();
expect(NoteWorkspace.noteNamesFuzzyMatch('dir/sub/Link-Topic.md', 'link/Topic')).toBeTruthy();
});

test('noteNamesFuzzyMatch', () => {
Expand Down Expand Up @@ -199,8 +178,7 @@ test('Note.tagSet', () => {
expect(tags).toEqual(new Set(['#another_tag', '#tag']));
});


describe("Wikilinks", () => {
describe('WikiLinks', () => {
beforeEach(() => {
NoteWorkspace.cfg = () => {
let config = NoteWorkspace.DEFAULT_CONFIG;
Expand All @@ -210,74 +188,60 @@ describe("Wikilinks", () => {
});

test('cleanPipedWikiLinks', () => {

expect(NoteWorkspace.cleanPipedWikiLink("description|file")).toEqual(
"file"
);
expect(NoteWorkspace.cleanPipedWikiLink("description with lots of spaces, and other symbols|file.md")).toEqual(
"file.md"
);
expect(NoteWorkspace.cleanPipedWikiLink("description|file")).toEqual(
"file"
);

expect(NoteWorkspace.cleanPipedWikiLink('description|file')).toEqual('file');
expect(
NoteWorkspace.cleanPipedWikiLink('description with lots of spaces, and other symbols|file.md')
).toEqual('file.md');
expect(NoteWorkspace.cleanPipedWikiLink('description|file')).toEqual('file');

// Odd case, but I suppose it should be treated
expect(NoteWorkspace.cleanPipedWikiLink("description|file|but-with-a-pipe-symbol.md")).toEqual(
"file|but-with-a-pipe-symbol.md"
expect(NoteWorkspace.cleanPipedWikiLink('description|file|but-with-a-pipe-symbol.md')).toEqual(
'file|but-with-a-pipe-symbol.md'
);

});
test("NoteWorkspace.noteNamesFuzzyMatch", () => {
test('NoteWorkspace.noteNamesFuzzyMatch', () => {
expect(
NoteWorkspace.noteNamesFuzzyMatch('filename.md', 'description|filename.md')
).toBeTruthy();

expect(
NoteWorkspace.noteNamesFuzzyMatch('filename.md', 'description |filename.md')
).toBeTruthy();

});

// Tests the different settings for piped wikilinks
test("Config changes", () => {

test('Config changes', () => {
// 1: Disable piped wikilinks
NoteWorkspace.cfg = () => {
let config = NoteWorkspace.DEFAULT_CONFIG;
config.allowPipedWikiLinks = false;
return config;
};
// Because of this change, these should not match anymore...
expect(
NoteWorkspace.noteNamesFuzzyMatch('filename.md', 'description|filename.md')
).toBeFalsy();
expect(NoteWorkspace.noteNamesFuzzyMatch('filename.md', 'description|filename.md')).toBeFalsy();

// ... And cleanPipedWikiLink should return the original string.
expect(NoteWorkspace.cleanPipedWikiLink("description|file")).toEqual(
"description|file"
);
expect(NoteWorkspace.cleanPipedWikiLink('description|file')).toEqual('description|file');

// 2: Use a different separator
NoteWorkspace.cfg = () => {
let config = NoteWorkspace.DEFAULT_CONFIG;
config.allowPipedWikiLinks = true;
config.pipedWikiLinksSeparator = "@";
config.pipedWikiLinksSeparator = '@';
return config;
};

expect(
NoteWorkspace.noteNamesFuzzyMatch('filename.md', 'description@filename.md')
).toBeTruthy();

expect(NoteWorkspace.cleanPipedWikiLink("description@file")).toEqual(
"file"
);
expect(NoteWorkspace.cleanPipedWikiLink('description@file')).toEqual('file');

// 3: Use a different syntax
NoteWorkspace.cfg = () => {
let config = NoteWorkspace.DEFAULT_CONFIG;
config.allowPipedWikiLinks = true;
config.pipedWikiLinksSeparator = "\\|";
config.pipedWikiLinksSeparator = '\\|';
return config;
};

Expand All @@ -289,47 +253,43 @@ describe("Wikilinks", () => {
NoteWorkspace.noteNamesFuzzyMatch('filename.md', 'filename.md|description')
).toBeTruthy();

expect(NoteWorkspace.cleanPipedWikiLink("file|description")).toEqual(
"file"
);

expect(NoteWorkspace.cleanPipedWikiLink('file|description')).toEqual('file');
});

});

describe('NoteWorkspace.newNoteContent', () => {
const newNote = (template: string, title: string) => {
NoteWorkspace.cfg = () => {
return {
...NoteWorkspace.DEFAULT_CONFIG,
newNoteTemplate: template
newNoteTemplate: template,
};
};

return NoteWorkspace.newNoteContent(title);
};
it('handles noteName tag', () => {
const template = "# ${noteName}\n\nThis is ${noteName}";
const content = newNote(template, "this is my test note!");
const template = '# ${noteName}\n\nThis is ${noteName}';

const content = newNote(template, 'this is my test note!');

expect(content).toBe('# this is my test note!\n\nThis is this is my test note!');
});

it('handles escaped newlines', () => {
const template = "# Title\\n\\nContent";
const template = '# Title\\n\\nContent';

const content = newNote(template, 'nevermind');

expect(content).toBe('# Title\n\nContent');
});

it('handles timestamp', () => {
const template = "# Title\n\nCreated: ${timestamp}\n";
const template = '# Title\n\nCreated: ${timestamp}\n';

const content = newNote(template, 'nevermind');
const regex = /# Title\n\nCreated: (.*)\n/;

expect(content).toMatch(regex);
const matches = regex.exec(content);
const date1 = Date.parse(matches![1]);
Expand Down
Loading

0 comments on commit 13b2464

Please sign in to comment.