From 8f7668d59c2e1a36a943d119d42370521d89eb3d Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Fri, 19 Jan 2024 09:25:35 +0100 Subject: [PATCH] Fix links to local files in the documentation sheet The server doesn't put the entire path into the link label anymore, so we need to apply additional logic to parse the path from the link target. --- plugin.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/plugin.py b/plugin.py index 56d0fb6..cf449fc 100644 --- a/plugin.py +++ b/plugin.py @@ -1086,13 +1086,13 @@ def on_result(self, response: str) -> None: # The `encoded_position` argument for the open_file command was introduced in ST 4127 # https://github.com/sublimehq/sublime_text/issues/4800 markdown_content = re.sub( - r"\[(.+?:\d+)\]\(file:///.+?#\d+\)", - r"""\1""", + r"\[(.+?:\d+)\]\((file:///.+?)#(\d+)\)", + self._link_replacement, markdown_content) else: markdown_content = re.sub( - r"\[(.+?)(:\d+)\]\(file:///.+?#\d+\)", - r"""\1\2""", + r"\[(.+?:\d+)\]\((file:///.+?)#\d+\)", + lambda match: self._link_replacement(match, False), markdown_content) content = frontmatter + toolbar + markdown_content @@ -1103,6 +1103,13 @@ def input(self, args: dict) -> Optional[sublime_plugin.TextInputHandler]: if "word" not in args: return WordInputHandler() + def _link_replacement(self, match: 're.Match', encoded_position: bool = True) -> str: + path = parse_uri(match.group(2))[1].replace('\\', '\\\\') + if encoded_position: + return """{}""".format( + path, match.group(3), match.group(1)) + return """{}""".format(path, match.group(1)) + class WordInputHandler(sublime_plugin.TextInputHandler): def placeholder(self) -> str: