From 44268f213da7dbbd6337005806bae39b4d42b8c0 Mon Sep 17 00:00:00 2001 From: logoff Date: Fri, 9 Jan 2026 13:53:13 +0100 Subject: [PATCH] fix: resolve wikilinks across all directories Wikilinks like [[PageName]] typically reference files without full paths, expecting the link to resolve to any matching file regardless of its directory location. This is consistent with how tools like Obsidian and roamlinks handle wikilinks. Previously, wikilinks were only resolved relative to the current file's directory, which meant [[PageName]] in docs/subdir/file.md would only find docs/subdir/PageName.md, not docs/other/PageName.md. Now, wikilinks search all nodes by filename, while standard markdown links [text](path) continue to resolve relative to the current file. --- src/mkdocs_graph_plugin/graph.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/mkdocs_graph_plugin/graph.py b/src/mkdocs_graph_plugin/graph.py index f7e3285..ae845b0 100644 --- a/src/mkdocs_graph_plugin/graph.py +++ b/src/mkdocs_graph_plugin/graph.py @@ -85,11 +85,25 @@ def _find_links(self, markdown: str, node_id: str, files: Files) -> Iterator[dic if not url: continue - target_path = os.path.normpath(os.path.join(os.path.dirname(node_id), url)) - - # Check if the target is a node in the graph - if any(node["id"] == target_path for node in self.nodes): - yield {"source": node_id, "target": target_path} + # For wikilinks, search all nodes by filename since they typically + # reference files without full paths (e.g., [[PageName]] can link + # to any PageName.md regardless of directory) + is_wikilink = match.group("wikilink") is not None + if is_wikilink: + target_filename = os.path.basename(url) + for node in self.nodes: + node_filename = os.path.basename(node["id"]) + if node_filename == target_filename and node["id"] != node_id: + yield {"source": node_id, "target": node["id"]} + break + else: + # Standard markdown links: resolve relative to current file + target_path = os.path.normpath( + os.path.join(os.path.dirname(node_id), url) + ) + # Check if the target is a node in the graph + if any(node["id"] == target_path for node in self.nodes): + yield {"source": node_id, "target": target_path} def _create_edges(self, files: Files): """Create edges by parsing links from markdown files."""