Skip to content

Commit

Permalink
Bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextoumbourou committed Jan 19, 2025
1 parent cfe1fe9 commit a2d099b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
28 changes: 28 additions & 0 deletions main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,4 +766,32 @@ describe('LinkUpdater', () => {
expect(await vault.read(sourceFile)).toEqual('Here is a [Hello 2](note2.md) and another [My Project Name](note3.md)');
});
});

describe('Edge cases', () => {
it('should not modify markdown links when preceded by standalone double brackets', async () => {
setupTest(
{
'note1.md': '[[ \n\n[Doggos](dogs.md)',
'dogs.md': 'Content about dogs'
},
{
'note1.md': {
links: [{
link: 'dogs.md',
original: '[Doggos](dogs.md)',
}],
frontmatter: undefined
} as CachedMetadata,
'dogs.md': {
frontmatter: { title: 'Doggos' },
headings: [],
links: []
} as CachedMetadata
}
);

const updatedCount = await linkUpdater.updateLinksInNote(sourceFile);
expect(await vault.read(sourceFile)).toBe('[[ \n\n[Doggos](dogs.md)');
});
});
});
28 changes: 24 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,18 @@ export class LinkUpdater {

let updatedCount = 0;

// First handle Markdown links, excluding checkbox patterns
// Markdown link regex breakdown:
// (?<!\[[ x]) - Negative lookbehind: don't match if preceded by checkbox pattern '[ ]' or '[x]'
// \[ - Match literal opening bracket
// ([^\]\n]+) - Group 1: Capture chars that aren't closing bracket or newline
// \] - Match literal closing bracket
// \( - Match literal opening parenthesis
// ([^)\n]+) - Group 2: Capture chars that aren't closing paren or newline
// \) - Match literal closing parenthesis
const markdownLinkRegex = /(?<!\[[ x])\[([^\]\n]+)\]\(([^)\n]+)\)/g;

let newFileContent = fileContent.replace(
/(?<!\[[ x])\[([^\]]+)\]\(([^)]+)\)/g,
markdownLinkRegex,
(_, linkText, linkUrl) => {
const linkUrlDecoded = decodeURIComponent(linkUrl);
// Remove any #subheading from the link before looking up the file
Expand Down Expand Up @@ -108,9 +117,20 @@ export class LinkUpdater {
}
);

// Then handle wikilinks
// Wikilinks regex breakdown:
// \[\[ - Match literal opening double brackets
// ([^\]\[\n]+?) - Group 1: Capture chars that aren't brackets or newline, non-greedy
// (?: - Start non-capturing group
// #([^\]\[\n]+?) - Group 2: Optional subheading after #, no brackets/newline, non-greedy
// )? - End optional non-capturing group
// (?: - Start non-capturing group
// \|([^\]\[\n]+?) - Group 3: Optional display text after |, no brackets/newline, non-greedy
// )? - End optional non-capturing group
// \]\] - Match literal closing double brackets
const wikilinkRegex = /\[\[([^\]\[\n]+?)(?:#([^\]\[\n]+?))?(?:\|([^\]\[\n]+?))?\]\]/g

newFileContent = newFileContent.replace(
/\[\[(.*?)(?:#(.*?))?(?:\|(.*?))?\]\]/g,
wikilinkRegex,
(match, linkPath, subheading, linkText) => {
const linkedFile = this.metadataCache.getFirstLinkpathDest(linkPath, file.path);
if (linkedFile) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "title-as-link-text",
"name": "Title As Link Text",
"version": "1.1.2",
"version": "1.1.3",
"minAppVersion": "0.15.0",
"description": "Use the title as the link text for Markdown links.",
"author": "Lex Toumbourou",
Expand Down

0 comments on commit a2d099b

Please sign in to comment.