diff --git a/pyproject.toml b/pyproject.toml index f62f9cd3..e3360954 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -172,6 +172,9 @@ npm = ["jlpm"] source_dir = "jupyterlab/packages" build_dir = "jupyterlab/jupyterlab_jupytext/labextension" +[tool.hatch.build.hooks.custom] +path = "tools/absolute_links_in_readme.py" + [tool.check-wheel-contents] ignore = ["W002"] diff --git a/tools/absolute_links_in_readme.py b/tools/absolute_links_in_readme.py new file mode 100644 index 00000000..8d29b3b7 --- /dev/null +++ b/tools/absolute_links_in_readme.py @@ -0,0 +1,28 @@ +import re + +from hatchling.builders.hooks.plugin.interface import BuildHookInterface + + +class AbsoluteLinksInReadme(BuildHookInterface): + def initialize(self, version, build_data): + readme_path = "README.md" + base_url = "https://github.com/mwouts/jupytext/blob/main/" + self.convert_links(readme_path, base_url) + + def convert_links(self, readme_path, base_url): + with open(readme_path, "r") as file: + content = file.read() + + # Regex to find markdown links + pattern = re.compile(r"\[([^\]]+)\]\(([^)]+)\)") + + def replace_link(match): + text, url = match.groups() + if not url.startswith(("http://", "https://")): + url = base_url + url + return f"[{text}]({url})" + + new_content = pattern.sub(replace_link, content) + + with open(readme_path, "w") as file: + file.write(new_content)