Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Invalid note links breaking other links during replacement #585

Merged
merged 2 commits into from
Jan 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/utils/turndown-rules/internal-links-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,25 @@ export const removeDoubleBackSlashes = (str: string): string => {
return str.replace(/\\/g, '');
};
const isEvernoteLink = (value: string): boolean => {
return value.startsWith('evernote:///view') || value.startsWith('https://www.evernote.com')
let url;
try {
url = new URL(value);
} catch(e){
return false;
}
// NOTE: URL automatically converts to lowercase
if (url.protocol === 'evernote:') {
// Internal link format: evernote:///view/92167309/s714/00ba720b-e3f1-49fd-9b43-5d915a3bca8a/00ba720b-e3f1-49fd-9b43-5d915a3bca8a/
const pathSpl = url.pathname.split('/').filter(Boolean); // Split and removes empty strings
if (pathSpl[0] !== 'view' || pathSpl.length < 4) return false;
return true;
} else if ((url.protocol === 'http:' || url.protocol === 'https:') && url.host === 'www.evernote.com') {
// External link format: https://www.evernote.com/shard/s714/nl/92167309/00ba720b-e3f1-49fd-9b43-5d915a3bca8a/
const pathSpl = url.pathname.split('/').filter(Boolean); // Removes empty strings
if (pathSpl[0] !== 'shard' || pathSpl.length < 5) return false;
return true;
}
return false;
}
const getEvernoteUniqueId = (value: string): string => {
const urlSpl = value.split('/').reverse()
Expand Down Expand Up @@ -61,7 +79,9 @@ export const wikiStyleLinksRule = {
? `![[${realValue}]]`
: getShortLinkIfPossible(token, value);
}
if (value.match(/^(https?:|tel:|www\.|file:|busycalevent:|ftp:|mailto:)/) && !value.startsWith("https://www.evernote.com")) {

const isValueEvernoteLink = isEvernoteLink(value);
if (value.match(/^(https?:|tel:|www\.|file:|busycalevent:|ftp:|mailto:)/) && !isValueEvernoteLink) {
return getShortLinkIfPossible(token, value);
}

Expand All @@ -74,7 +94,7 @@ export const wikiStyleLinksRule = {
&& yarleOptions.obsidianSettings?.omitLinkDisplayName;
const renderedObsidianDisplayName = omitObsidianLinksDisplayName ? '' : `|${displayName}`;

if (isEvernoteLink(value) ) {
if (isValueEvernoteLink) {
const fileName = normalizeTitle(token['text']);
const noteIdNameMap = RuntimePropertiesSingleton.getInstance();
const uniqueEnd = getUniqueId();
Expand Down
Loading