@@ -406,3 +406,92 @@ def test_build_documentation_file_not_found(temp_output_path: Path) -> None:
406406 input_files = [Path ("/nonexistent/file.md" )],
407407 output_path = temp_output_path ,
408408 )
409+
410+
411+ # Tests for rewrite_internal_links
412+
413+
414+ def test_rewrite_internal_links_rewrites_section_links (tmp_path : Path ) -> None :
415+ """Test that links to markdown files that are sections get rewritten."""
416+ readme = tmp_path / "README.md"
417+ readme .write_text (
418+ "# Main\n \n See the [CHANGELOG](CHANGELOG.md) for details." ,
419+ encoding = "utf-8" ,
420+ )
421+
422+ changelog = tmp_path / "CHANGELOG.md"
423+ changelog .write_text ("# Changelog\n \n ## v1.0" , encoding = "utf-8" )
424+
425+ output = tmp_path / "output.html"
426+ build_documentation (
427+ input_files = [readme , changelog ],
428+ output_path = output ,
429+ )
430+
431+ content = output .read_text (encoding = "utf-8" )
432+ # Link should be rewritten to section anchor
433+ assert 'href="#changelog"' in content
434+ # Original file link should not be present
435+ assert "CHANGELOG.md" not in content or 'href="#changelog"' in content
436+
437+
438+ def test_rewrite_internal_links_preserves_external_links (tmp_path : Path ) -> None :
439+ """Test that external links are not rewritten."""
440+ readme = tmp_path / "README.md"
441+ readme .write_text (
442+ "# Main\n \n Visit [GitHub](https://github.com)" ,
443+ encoding = "utf-8" ,
444+ )
445+
446+ output = tmp_path / "output.html"
447+ build_documentation (
448+ input_files = [readme ],
449+ output_path = output ,
450+ )
451+
452+ content = output .read_text (encoding = "utf-8" )
453+ # External link should remain unchanged
454+ assert 'href="https://github.com"' in content
455+
456+
457+ def test_rewrite_internal_links_preserves_non_section_markdown_links (
458+ tmp_path : Path ,
459+ ) -> None :
460+ """Test that links to markdown files that aren't sections remain unchanged."""
461+ readme = tmp_path / "README.md"
462+ readme .write_text (
463+ "# Main\n \n See [other doc](other.md)" ,
464+ encoding = "utf-8" ,
465+ )
466+
467+ output = tmp_path / "output.html"
468+ build_documentation (
469+ input_files = [readme ],
470+ output_path = output ,
471+ )
472+
473+ content = output .read_text (encoding = "utf-8" )
474+ # Link to non-section file should remain as-is
475+ assert 'href="other.md"' in content
476+
477+
478+ def test_rewrite_internal_links_case_insensitive (tmp_path : Path ) -> None :
479+ """Test that link rewriting is case-insensitive."""
480+ readme = tmp_path / "README.md"
481+ readme .write_text (
482+ "# Main\n \n See [Action](ACTION.md)" ,
483+ encoding = "utf-8" ,
484+ )
485+
486+ action = tmp_path / "ACTION.md"
487+ action .write_text ("# Action\n \n Details" , encoding = "utf-8" )
488+
489+ output = tmp_path / "output.html"
490+ build_documentation (
491+ input_files = [readme , action ],
492+ output_path = output ,
493+ )
494+
495+ content = output .read_text (encoding = "utf-8" )
496+ # Link should be rewritten to lowercase section anchor
497+ assert 'href="#action"' in content
0 commit comments