diff --git a/src/utils/replaceUtils.ts b/src/utils/replaceUtils.ts index 58dd367e..1caf09bc 100644 --- a/src/utils/replaceUtils.ts +++ b/src/utils/replaceUtils.ts @@ -14,7 +14,7 @@ import { escapeForRegExp, } from './utils'; import { isInCodeSpan, isInFencedCodeBlock } from './externalUtils'; -import { search } from './searchUtils'; +import { search, refsToSearchRegExpStr } from './searchUtils'; type RenamedFile = { readonly oldUri: Uri; @@ -117,7 +117,10 @@ export const resolveRefsReplaceMap = async ( return {}; } - const fsPaths = await search([`[[${oldShortRef}]]`, `[[${oldLongRef}]]`], workspaceFolder); + const fsPaths = await search( + refsToSearchRegExpStr([`[[${oldShortRef}]]`, `[[${oldLongRef}]]`]), + workspaceFolder, + ); const searchUris = fsPaths.length ? newUris.filter(({ fsPath }) => fsPaths.includes(fsPath)) diff --git a/src/utils/searchUtils.spec.ts b/src/utils/searchUtils.spec.ts index b2d05cbe..dc0fb44b 100644 --- a/src/utils/searchUtils.spec.ts +++ b/src/utils/searchUtils.spec.ts @@ -1,4 +1,4 @@ -import { search } from './searchUtils'; +import { search, refsToSearchRegExpStr } from './searchUtils'; import { closeEditorsAndCleanWorkspace, createFile, @@ -16,7 +16,7 @@ describe('search()', () => { const uri = await createFile(`${name}.md`, '[[ref]]'); - const [path] = await search(['[[ref]]'], getWorkspaceFolder()); + const [path] = await search(refsToSearchRegExpStr(['[[ref]]']), getWorkspaceFolder()); expect(uri?.fsPath).toBe(path); }); @@ -26,7 +26,7 @@ describe('search()', () => { const uri = await createFile(`${name}.md`, '[[image.png]]'); - const [path] = await search(['[[image.png]]'], getWorkspaceFolder()); + const [path] = await search(refsToSearchRegExpStr(['[[image.png]]']), getWorkspaceFolder()); expect(uri?.fsPath).toBe(path); }); @@ -36,7 +36,7 @@ describe('search()', () => { const uri = await createFile(`${name}.md`, '![[image.png]]'); - const [path] = await search(['[[image.png]]'], getWorkspaceFolder()); + const [path] = await search(refsToSearchRegExpStr(['[[image.png]]']), getWorkspaceFolder()); expect(uri?.fsPath).toBe(path); }); @@ -56,7 +56,7 @@ describe('search()', () => { await createFile(`${name1}.md`); - const paths = await search(['[[ref]]'], getWorkspaceFolder()); + const paths = await search(refsToSearchRegExpStr(['[[ref]]']), getWorkspaceFolder()); expect(paths.length).toBe(1); expect(uri?.fsPath).toBe(paths[0]); @@ -67,7 +67,7 @@ describe('search()', () => { await createFile(`${name}.txt`, '[[ref]]'); - const paths = await search(['[[ref]]'], getWorkspaceFolder()); + const paths = await search(refsToSearchRegExpStr(['[[ref]]']), getWorkspaceFolder()); expect(paths.length).toBe(0); }); @@ -78,7 +78,10 @@ describe('search()', () => { await createFile(`${name}-1.md`, '[[ref0]]'); await createFile(`${name}-2.md`, '[[ref1]]'); - const paths = await search(['[[ref0]]', '[[ref1]]'], getWorkspaceFolder()); + const paths = await search( + refsToSearchRegExpStr(['[[ref0]]', '[[ref1]]']), + getWorkspaceFolder(), + ); expect(paths.length).toBe(2); }); diff --git a/src/utils/searchUtils.ts b/src/utils/searchUtils.ts index c83df8dd..8e2b1470 100644 --- a/src/utils/searchUtils.ts +++ b/src/utils/searchUtils.ts @@ -45,10 +45,12 @@ const findRipgrepPath = async (): Promise => { return; }; -export const search = async (refs: string[], dirPath: string = '.'): Promise => { +export const refsToSearchRegExpStr = (refs: string[]) => `(${refs.map(escapeForRegExp).join('|')})`; + +export const search = async (regExpStr: string, dirPath: string = '.'): Promise => { const ripgrepPath = await findRipgrepPath(); - if (!ripgrepPath || !refs.length) { + if (!ripgrepPath || !regExpStr) { return []; } @@ -60,7 +62,7 @@ export const search = async (refs: string[], dirPath: string = '.'): Promise require('../../utils'); // private methods (not exported via workspace/index.ts and should not be used outside of workspace folder) export const cacheRefs = async () => { - workspaceCache.danglingRefsByFsPath = await utils().findDanglingRefsByFsPath( - workspaceCache.markdownUris, - ); + const { search, getWorkspaceFolder } = utils(); + + const fsPaths = await search('\\[\\[([^\\[\\]]+?)\\]\\]', getWorkspaceFolder()!); + + const searchUris = fsPaths.length + ? workspaceCache.markdownUris.filter(({ fsPath }) => fsPaths.includes(fsPath)) + : workspaceCache.markdownUris; + + workspaceCache.danglingRefsByFsPath = await utils().findDanglingRefsByFsPath(searchUris); workspaceCache.danglingRefs = sortPaths( Array.from(new Set(Object.values(workspaceCache.danglingRefsByFsPath).flatMap((refs) => refs))), { shallowFirst: true },