Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"enum": [
"rawFilename",
"noExtension",
"toSpaces"
"toSpaces",
"uniqueId"
],
"default": "rawFilename",
"description": "When offering completion options for `wiki-note.md`, get `rawFilename` OR `noExtension` (`wiki-link`) or `toSpaces` (`wiki link`). Default is rawFilename. NB: when using workspaceFilenameConvention, only rawFilename is returned."
Expand All @@ -79,6 +80,11 @@
"default": "uniqueFilenames",
"description": "By default, expect 'uniqueFilenames' for every `.md` file in workspace and treat `file.md` as link to file in any subdirectory. If you expect collisions in filenames for notes (eg, `note1/note.md` `note2/note.md`) use 'relativePaths' to render links between files."
},
"vscodeMarkdownNotes.uniqueIdTemplate": {
"type": "string",
"default": "",
"description": "Set regex pattern of unique id for new notes. Defaults to empty string."
},
"vscodeMarkdownNotes.slugifyCharacter": {
"type": "string",
"enum": [
Expand Down
27 changes: 27 additions & 0 deletions src/NoteWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum NoteCompletionConvention {
rawFilename = 'rawFilename',
noExtension = 'noExtension',
toSpaces = 'toSpaces',
uniqueId = 'uniqueId',
}
enum WorkspaceFilenameConvention {
uniqueFilenames = 'uniqueFilenames',
Expand Down Expand Up @@ -47,6 +48,7 @@ type Config = {
createNoteOnGoToDefinitionWhenMissing: boolean;
defaultFileExtension: string;
noteCompletionConvention: NoteCompletionConvention;
uniqueIdTemplate: string;
slugifyCharacter: SlugifyCharacter;
slugifyMethod: SlugifyMethod;
workspaceFilenameConvention: WorkspaceFilenameConvention;
Expand Down Expand Up @@ -86,6 +88,7 @@ export class NoteWorkspace {
compileSuggestionDetails: false,
defaultFileExtension: 'md',
noteCompletionConvention: NoteCompletionConvention.rawFilename,
uniqueIdTemplate: '',
slugifyCharacter: SlugifyCharacter.dash,
slugifyMethod: SlugifyMethod.classic,
workspaceFilenameConvention: WorkspaceFilenameConvention.uniqueFilenames,
Expand Down Expand Up @@ -116,6 +119,7 @@ export class NoteWorkspace {
) as boolean,
defaultFileExtension: c.get('defaultFileExtension') as string,
noteCompletionConvention: c.get('noteCompletionConvention') as NoteCompletionConvention,
uniqueIdTemplate: c.get('uniqueIdTemplate') as string,
slugifyCharacter: c.get('slugifyCharacter') as SlugifyCharacter,
slugifyMethod: c.get('slugifyMethod') as SlugifyMethod,
workspaceFilenameConvention: c.get(
Expand All @@ -137,6 +141,10 @@ export class NoteWorkspace {
};
}

static uniqueIdTemplate(): string {
return this.cfg().uniqueIdTemplate;
}

static slugifyChar(): string {
return this.cfg().slugifyCharacter;
}
Expand Down Expand Up @@ -238,6 +246,8 @@ export class NoteWorkspace {
return this.stripExtension(filename).replace(/[-_]+/g, ' ');
} else if (convention == 'noExtension') {
return this.stripExtension(filename);
} else if (convention == 'uniqueId') {
return this.normalizeNoteNameToUniqueId(filename);
} else {
return filename;
}
Expand All @@ -260,6 +270,15 @@ export class NoteWorkspace {
return noteName.replace(NoteWorkspace.rxFileExtensions(), '');
}

static normalizeNoteNameToUniqueId(noteName: string): string {
let match_result = noteName.match(this.uniqueIdTemplate());
if (match_result) {
return match_result[0];
} else {
return "";
}
}

static normalizeNoteNameForFuzzyMatch(noteName: string): string {
// remove the brackets:
let n = noteName.replace(/[\[\]]/g, '');
Expand Down Expand Up @@ -332,13 +351,21 @@ export class NoteWorkspace {
return (
this.normalizeNoteNameForFuzzyMatch(left).toLowerCase() ==
this.normalizeNoteNameForFuzzyMatchText(right).toLowerCase()
||
(this.normalizeNoteNameToUniqueId(left) != '') &&
(this.normalizeNoteNameToUniqueId(left) ==
this.normalizeNoteNameToUniqueId(right))
);
}

static noteNamesFuzzyMatchText(left: string, right: string): boolean {
return (
this.normalizeNoteNameForFuzzyMatchText(left).toLowerCase() ==
this.normalizeNoteNameForFuzzyMatchText(right).toLowerCase()
||
(this.normalizeNoteNameToUniqueId(left) != '') &&
(this.normalizeNoteNameToUniqueId(left) ==
this.normalizeNoteNameToUniqueId(right))
);
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/jest/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ describe('NoteWorkspace.noteNamesFuzzyMatch', () => {
expect(NoteWorkspace.noteNamesFuzzyMatch('[[wiki-link]]', 'wiki-link.md')).toBeTruthy();
expect(NoteWorkspace.noteNamesFuzzyMatch('[[wiki link]]', 'wiki-link.md')).toBeTruthy();
expect(NoteWorkspace.noteNamesFuzzyMatch('[[链接]]', '链接.md')).toBeTruthy();

setConfig({uniqueIdTemplate: '\\d{12}'});
expect(NoteWorkspace.noteNamesFuzzyMatch('[[202405030000]]', '202405030000-wiki-ling.md')).toBeTruthy();
// TODO: if we add support for #headings, we will want these tests to pass:
// expect(NoteWorkspace.noteNamesFuzzyMatch('[[wiki-link.md#with-heading]]', 'wiki-link.md')).toBeTruthy();
// expect(NoteWorkspace.noteNamesFuzzyMatch('[[wiki-link#with-heading]]', 'wiki-link.md')).toBeTruthy();
Expand Down Expand Up @@ -125,6 +128,11 @@ describe('NoteWorkspace.noteNamesFuzzyMatch', () => {
expect(
NoteWorkspace._wikiLinkCompletionForConvention('rawFilename', 'the-note-name.md')
).toEqual('the-note-name.md');

setConfig({uniqueIdTemplate: '\\d{12}'})
expect(
NoteWorkspace._wikiLinkCompletionForConvention('uniqueId', '202405022338-the-note-name.md')
).toEqual('202405022338');
// TODO: how should this behaving with #headings?
});
});
Expand Down