Skip to content

Commit

Permalink
fix: Fix replacing multiple links at once
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Oct 31, 2020
1 parent 8bd7863 commit 2d70222
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,21 @@ describe('replaceRefs()', () => {
}),
).toBe(initialContent);
});

it('should replace multiple links at once', async () => {
expect(
replaceRefs({
refs: [
{ old: 'test-ref', new: 'folder2/new-test-ref' },
{ old: 'folder1/test-ref', new: 'folder2/new-test-ref' },
],
document: await workspace.openTextDocument({
language: 'markdown',
content: '[[test-ref]] [[folder1/test-ref|Test Label]]',
}),
}),
).toBe('[[folder2/new-test-ref]] [[folder2/new-test-ref|Test Label]]');
});
});

describe('isLongRef()', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ export const replaceRefs = ({
if (new RegExp(pattern, 'i').exec(content)) {
let replacedOnce = false;

const nextContent = content.replace(new RegExp(pattern, 'gi'), ($0, $1, offset) => {
const content = nextContent.replace(new RegExp(pattern, 'gi'), ($0, $1, offset) => {
const pos = document.positionAt(offset);

if (
Expand All @@ -494,7 +494,7 @@ export const replaceRefs = ({

return {
updatedOnce: true,
nextContent,
nextContent: content,
};
}

Expand Down

0 comments on commit 2d70222

Please sign in to comment.