From b3a40035cd4bee4a0bcdbdea123f21f93ff1af6b Mon Sep 17 00:00:00 2001 From: The Wobbler Date: Wed, 9 Apr 2025 18:10:29 +0200 Subject: [PATCH] Now also Markdown links get recognized. --- omg.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/omg.py b/omg.py index 477dda3..9e93970 100644 --- a/omg.py +++ b/omg.py @@ -132,6 +132,32 @@ class OMG: if not tag == "": file_metadata["tags"].append(tag.lower()) + # ==== markdown links ==== + if "[" in line and "]" in line and "(" in line and ")" in line: + for link in line.split("["): + if not "]" in link: + continue + + tokens = link.split("]") + + display_text = tokens[0] + url = tokens[1] + + if url == "" or not url[0] == "(" or not url[-1] == ")": # link has no proper url enclosure + continue + + url = url[1:-1] + url = url.split()[0] + + link = url.split("/")[-1].strip() + relpath = os.path.relpath(os.path.join(path, "../" + url), self.path).strip() + + if not relpath.endswith(".md"): + relpath += ".md" + + link = {"link": link, "relativePath": relpath, "displayText": display_text} + file_metadata["links"].append(link) + # ==== wikilinks ==== if not "[[" in line or not "]]" in line: continue @@ -146,8 +172,14 @@ class OMG: tokens = link.split("|") # ["relPath, "link"] - link = tokens[0].split("/")[-1] - relpath = os.path.relpath(os.path.join(path, "../" + tokens[0]), self.path) + ".md" + link = tokens[0].split("/")[-1].strip().replace("\\", "") + relpath = os.path.relpath( + os.path.join(path, "../" + tokens[0]), + self.path + ).strip().replace("\\", "") + + if not relpath.endswith(".md"): + relpath += ".md" link_data = {"link": link, "relativePath": relpath}