diff --git a/README.md b/README.md index f260ae7b..2d56596e 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,22 @@ To enjoy all features, you can use VSCode 1.52 (November 2020), which can be dow ![Creating notes on the fly](./help/Attachments/Creating%20notes%20from%20links.png) + - Also supporting configurable rules to specify the destination of the newly created notes. + The following snippet is an example to create daily notes in directory `Daily` and all other notes in `Notes`. + + ```json + [{ + "rule": "/([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))\\.md$", + "comment": "Daily notes yyyy-mm-dd", + "folder": "/Daily" + }, + { + "rule": "\\.md$", + "comment": "All other notes", + "folder": "/Notes" + ]} + ``` + - 🖇 **Backlinks panel** ![Backlinks panel](./help/Attachments/Backlinks%20panel.png) diff --git a/package.json b/package.json index 6d732a59..41b3622a 100644 --- a/package.json +++ b/package.json @@ -126,6 +126,12 @@ "description": "Whether to enable references search for links in the editor. Reload required!", "type": "boolean" }, + "memo.links.rules": { + "default": [], + "scope": "resource", + "description": "Rules specifying the location where new files should be created for short links. Applicable only if links format is set to short in the settings. In case if multiple rules matching the file path, the rule that comes first will be applied.", + "type": "array" + }, "memo.links.sync.enabled": { "default": true, "scope": "resource", diff --git a/src/commands/openDocumentByReference.spec.ts b/src/commands/openDocumentByReference.spec.ts index da2114a2..7f1b5a14 100644 --- a/src/commands/openDocumentByReference.spec.ts +++ b/src/commands/openDocumentByReference.spec.ts @@ -10,6 +10,7 @@ import { getOpenedPaths, closeEditorsAndCleanWorkspace, toPlainObject, + updateMemoConfigProperty, } from '../test/testUtils'; describe('openDocumentByReference command', () => { @@ -201,4 +202,60 @@ describe('openDocumentByReference command', () => { executeCommandSpy.mockRestore(); }); + + it('should create a note in a configured sub dir', async () => { + await updateMemoConfigProperty('links.rules', [ + { + rule: '.*\\.md$', + comment: 'all notes', + folder: '/Notes', + }, + ]); + + const name = rndName(); + + expect(getOpenedFilenames()).not.toContain(`${name}.md`); + + await openDocumentByReference({ reference: name }); + + expect(getOpenedPaths()).toContain( + `${path.join(getWorkspaceFolder()!, 'Notes', `${name}.md`)}`, + ); + }); + + it('should not create a note in a configured sub dir for long links', async () => { + await updateMemoConfigProperty('links.rules', [ + { + rule: '.*\\.md$', + comment: 'all notes', + folder: '/Notes', + }, + ]); + + const name = `dir/${rndName()}`; + + expect(getOpenedFilenames()).not.toContain(`${name}.md`); + + await openDocumentByReference({ reference: name }); + + expect(getOpenedPaths()).toContain(`${path.join(getWorkspaceFolder()!, `${name}.md`)}`); + }); + + it('should create a note in a root dir when no matching rule found', async () => { + await updateMemoConfigProperty('links.rules', [ + { + rule: '.*\\.txt$', + comment: 'all text', + folder: '/Text', + }, + ]); + + const name = rndName(); + + expect(getOpenedFilenames()).not.toContain(`${name}.md`); + + await openDocumentByReference({ reference: name }); + + expect(getOpenedPaths()).toContain(`${path.join(getWorkspaceFolder()!, `${name}.md`)}`); + }); }); diff --git a/src/commands/openDocumentByReference.ts b/src/commands/openDocumentByReference.ts index 9365696f..d2ce93c8 100644 --- a/src/commands/openDocumentByReference.ts +++ b/src/commands/openDocumentByReference.ts @@ -8,6 +8,8 @@ import { ensureDirectoryExists, parseRef, getWorkspaceFolder, + getMemoConfigProperty, + isLongRef, } from '../utils'; const openDocumentByReference = async ({ @@ -28,10 +30,28 @@ const openDocumentByReference = async ({ if (workspaceFolder) { const paths = ref.split('/'); const refExt = path.parse(ref).ext; - const pathsWithExt = [ + + const resolvedRef = path.join( ...paths.slice(0, -1), `${paths.slice(-1)}${refExt !== '.md' && refExt !== '' ? '' : '.md'}`, - ]; + ); + + let defaultPath; + try { + // Apply default folder rule if it's a short ref(i.e. doesn't have an existing dir in ref). + defaultPath = !isLongRef(ref) + ? getMemoConfigProperty('links.rules', []).find((rule) => + new RegExp(rule.rule).test(resolvedRef), + )?.folder + : undefined; + } catch (error) { + vscode.window.showWarningMessage( + `Fail to decide path to create the file, please check config. error: ${error}`, + ); + return; + } + + const pathsWithExt = (defaultPath ? [defaultPath] : []).concat([resolvedRef]); const filePath = path.join(workspaceFolder, ...pathsWithExt); // don't override file content if it already exists diff --git a/src/types.ts b/src/types.ts index edb6584e..5485f9b3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,3 +18,9 @@ export type FoundRefT = { location: Location; matchText: string; }; + +export type LinkRuleT = { + rule: string; + comment?: string; + folder: string; +}; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 0b945307..c93e81fb 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -3,7 +3,7 @@ import path from 'path'; import { sort as sortPaths } from 'cross-path-sort'; import fs from 'fs'; -import { WorkspaceCache, RefT, FoundRefT } from '../types'; +import { WorkspaceCache, RefT, FoundRefT, LinkRuleT } from '../types'; import { isInCodeSpan, isInFencedCodeBlock } from './externalUtils'; import { default as createDailyQuickPick } from './createDailyQuickPick'; @@ -260,6 +260,11 @@ export function getMemoConfigProperty( fallback: null | number, ): number; +export function getMemoConfigProperty( + property: 'links.rules', + fallback: null | Array, +): LinkRuleT[]; + export function getMemoConfigProperty(property: MemoBoolConfigProp, fallback: boolean): boolean; export function getMemoConfigProperty(property: string, fallback: T): T {