Skip to content

Commit

Permalink
Use correct method for checking resolving links.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextoumbourou committed Apr 11, 2023
1 parent 3380ad5 commit 41e1178
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 84 deletions.
101 changes: 18 additions & 83 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {

const path = require("path");

interface BetterMarkdownLinksSettings {}

export default class BetterMarkdownLinksPlugin extends Plugin {
settings: BetterMarkdownLinksSettings;

export default class TitleAsLinkTextPlugin extends Plugin {
async onload() {
this.registerEvent(
this.app.vault.on("rename", async (file: TFile, oldPath) => {
Expand Down Expand Up @@ -82,13 +78,7 @@ export default class BetterMarkdownLinksPlugin extends Plugin {
(_, linkText, linkUrl) => {
const linkUrlDecoded = decodeURIComponent(linkUrl);
if (path.basename(linkUrlDecoded) === path.basename(oldPath)) {
const normedLink = this.normalizePathForLink(
file.path,
// @ts-ignore
this.app.vault.getConfig("newLinkFormat"),
path.dirname(note.path)
);
return `[${title}](${normedLink})`;
return `[${title}](${linkUrl})`;
}
return `[${linkText}](${linkUrl})`;
}
Expand All @@ -110,7 +100,7 @@ export default class BetterMarkdownLinksPlugin extends Plugin {
}

getCachedNotesThatHaveLinkToFile(filePath: string): TFile[] {
let notes: TFile[] = [];
let notesWithBacklinks: TFile[] = [];
let allNotes = this.app.vault.getMarkdownFiles();

if (allNotes) {
Expand All @@ -121,65 +111,28 @@ export default class BetterMarkdownLinksPlugin extends Plugin {
continue;
}

let embeds = this.app.metadataCache.getCache(notePath)?.embeds;
if (embeds) {
for (let link_data of embeds) {
let linkPath = link_data.link;

const isSamePath = this.comparePaths(
linkPath,
filePath,
path.dirname(notePath),
//@ts-ignore
this.app.vault.getConfig("newLinkFormat")
);
if (isSamePath) {
notes.push(note);
}
}
}

let links = this.app.metadataCache.getCache(notePath)?.links;
if (links) {
for (let link_data of links) {
let linkPath = link_data.link;

const isSamePath = this.comparePaths(
linkPath,
filePath,
path.dirname(notePath),
//@ts-ignore
this.app.vault.getConfig("newLinkFormat")
const noteCache = this.app.metadataCache.getCache(notePath);
const embedsAndLinks = [
...(noteCache?.embeds || []),
...(noteCache?.links || []),
];
if (embedsAndLinks) {
for (let link_data of embedsAndLinks) {
// getFirstLinkpathDest = Get the best match for a linkpath.
// https://marcus.se.net/obsidian-plugin-docs/reference/typescript/classes/MetadataCache
const firstLinkPath = app.metadataCache.getFirstLinkpathDest(
link_data.link,
note.path
);
if (isSamePath) {
notes.push(note);
if (firstLinkPath && firstLinkPath.path == filePath) {
notesWithBacklinks.push(note);
}
}
}
}
}

return notes;
}

comparePaths(
linkPath: string,
filePath: string,
linkDir: string,
linkFormat: string
) {
const basePath = (this.app.vault.adapter as any).basePath;

filePath = path.resolve(basePath, filePath);
linkDir = path.resolve(basePath, linkDir);

if (linkFormat == "relative") {
linkPath = path.resolve(linkDir, linkPath);
}

linkPath = path.resolve(basePath, linkPath);

return linkPath == filePath;
return notesWithBacklinks;
}

getPageTitle(cache: CachedMetadata, filePath: string): string {
Expand All @@ -193,22 +146,4 @@ export default class BetterMarkdownLinksPlugin extends Plugin {
path.basename(filePath).replace(".md", "")
);
}

normalizePathForLink(
pathToNorm: string,
linkFormat: string,
noteDir: string
): string {
if (linkFormat == "relative") {
const basePath = (this.app.vault.adapter as any).basePath;
noteDir = path.resolve(basePath, noteDir);
pathToNorm = path.resolve(basePath, pathToNorm);
pathToNorm = path.relative(noteDir, pathToNorm);
}

pathToNorm = pathToNorm.replace(/\\/gi, "/"); //replace \ to /
pathToNorm = pathToNorm.replace(/ /gi, "%20"); //replace space to %20

return pathToNorm;
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-title-as-link-text",
"name": "Title As Link Text",
"version": "1.0.4",
"version": "1.0.5",
"minAppVersion": "0.15.0",
"description": "This plugin improves the behaviour of Markdown-style links in Obsidian.",
"author": "Lex Toumbourou",
Expand Down

0 comments on commit 41e1178

Please sign in to comment.