Skip to content

Commit

Permalink
fix: note links note being replaced correctly when keepEvernoteLinkIf…
Browse files Browse the repository at this point in the history
…NoNoteFound = true (#653)

The original regexes were greedy, which would wipe out many links on the same line,
but also the resulting link still contains the original Evernote link, it wasn't getting replaced at all.
I updated the intermediate <YARLE_EVERNOTE_LINK> format to contain the original note id
to guarantee a correct match, then replace the link format approariately.
  • Loading branch information
bumper314 committed Nov 21, 2024
1 parent 2dfa5ff commit 15e0cf5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
23 changes: 11 additions & 12 deletions src/utils/apply-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,17 @@ export const applyLinks = (options: YarleOptions, outputNotebookFolders: Array<s
}

}
if (options.keepEvernoteLinkIfNoNoteFound){
if (linkDoesntExist){
const regexp = new RegExp(`<YARLE_EVERNOTE_LINK>(.)*<-->`)// replace
updatedContent = updatedContent.replace(regexp, '');
updatedContent = updatedContent.replace('</YARLE_EVERNOTE_LINK>', '');

}else {
const regexp = new RegExp(`<-->(.)*<\/YARLE_EVERNOTE_LINK>`)// replace
updatedContent = updatedContent.replace(regexp, '');
updatedContent = updatedContent.replace('<YARLE_EVERNOTE_LINK>', '');
}

if (options.keepEvernoteLinkIfNoNoteFound) {
const id = escapeStringRegexp(linkName);
const regexp = new RegExp(`<YARLE_EVERNOTE_LINK>${id}<-->(.*?)<-->(.*?)<\/YARLE_EVERNOTE_LINK>`, 'g');

updatedContent = updatedContent.replace(regexp, (match, firstGroup, secondGroup) => {
if (linkDoesntExist) {
return secondGroup;
} else {
return firstGroup.replace(new RegExp(id, 'g'), realFileNameInContent);
}
});
}
else {
const regexp = new RegExp(escapeStringRegexp(linkName), 'g');
Expand Down
2 changes: 1 addition & 1 deletion src/utils/turndown-rules/internal-links-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const wikiStyleLinksRule = {
return `${mdKeyword}[[${linkedNoteId}${extension}${renderedObsidianDisplayName}]]`;
}
if (yarleOptions.keepEvernoteLinkIfNoNoteFound){
return `<YARLE_EVERNOTE_LINK>${mdKeyword}[[${linkedNoteId}${extension}${renderedObsidianDisplayName}]]<-->[${displayName}](${linkedNoteId}${extension})</YARLE_EVERNOTE_LINK>`
return `<YARLE_EVERNOTE_LINK>${linkedNoteId}<-->${mdKeyword}[[${linkedNoteId}${extension}${renderedObsidianDisplayName}]]<-->[${displayName}](${linkedNoteId}${extension})</YARLE_EVERNOTE_LINK>`
}

return `${mdKeyword}[${displayName}](${linkedNoteId}${extension})`;
Expand Down

0 comments on commit 15e0cf5

Please sign in to comment.