From c9eaf7cfdebe3ad0d8fa352522e619d2c1d170bf Mon Sep 17 00:00:00 2001 From: Will <1261268+codeallthethingz@users.noreply.github.com> Date: Thu, 14 Nov 2024 08:11:58 -0800 Subject: [PATCH] tools: External markdowns are treated as internal links (#12) --- tools/github_readme_sync/hierarchy.py | 2 ++ .../github_readme_sync/tests/hierarchy_test.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/tools/github_readme_sync/hierarchy.py b/tools/github_readme_sync/hierarchy.py index 2722b89..1402cc9 100644 --- a/tools/github_readme_sync/hierarchy.py +++ b/tools/github_readme_sync/hierarchy.py @@ -149,6 +149,8 @@ def check_links(path): errors = [] for match in md_link_matches: + if match[1].startswith(("http://", "https://", "mailto:")): + continue path_to_check = os.path.join(current_dir, match[1].split("#")[0]) path_to_check = os.path.normpath(path_to_check) if any(placeholder in match[1] for placeholder in IGNORE_DOCS): diff --git a/tools/github_readme_sync/tests/hierarchy_test.py b/tools/github_readme_sync/tests/hierarchy_test.py index 1227701..8d295da 100644 --- a/tools/github_readme_sync/tests/hierarchy_test.py +++ b/tools/github_readme_sync/tests/hierarchy_test.py @@ -13,6 +13,7 @@ import tempfile import threading import unittest +from pathlib import Path from tools.github_readme_sync.hierarchy import ( CATEGORY_PREFIX, @@ -21,6 +22,7 @@ INDENTATION_UNIT, check_external, check_hierarchy_file, + check_links, create_hierarchy_file, extract_links, ) @@ -233,6 +235,21 @@ def extract(input_string, expected_output): ["https://a.com/quotes"], ) + def test_check_links_ignores_external(self): + """Test that check_links ignores external links (http/https/mailto).""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f: + f.write(""" + [External Link](https://example.com/page.md) + [Another External](http://test.com/doc.md) + [Email Link](mailto:test@example.md) + """) + temp_path = f.name + + try: + self.assertEqual(check_links(temp_path), []) + finally: + Path(temp_path).unlink() + if __name__ == "__main__": unittest.main()