diff --git a/docs/about/release-notes.md b/docs/about/release-notes.md index 15086f1d..b0e1ef60 100644 --- a/docs/about/release-notes.md +++ b/docs/about/release-notes.md @@ -25,6 +25,10 @@ The current members of the MkDocs-NG team. ## Version 1.7.1 (Unreleased) +### Fixed + +* Fix anchor link validation so `mkdocs build -v --strict` still fails when missing anchors are reported as warnings. #30 + ### Maintenance * Remove the unmaintained `mergedeep` dependency by replacing it with an internal deep-merge helper for inherited YAML configuration. #29 diff --git a/mkdocs/structure/pages.py b/mkdocs/structure/pages.py index 1990f84d..b5115fdd 100644 --- a/mkdocs/structure/pages.py +++ b/mkdocs/structure/pages.py @@ -303,7 +303,7 @@ def render(self, config: MkDocsConfig, files: Files) -> None: self.present_anchor_ids = ( extract_anchors_ext.present_anchor_ids | raw_html_ext.present_anchor_ids ) - if log.getEffectiveLevel() > logging.DEBUG: + if config.validation.links.anchors > logging.DEBUG: self.links_to_anchors = relative_path_ext.links_to_anchors present_anchor_ids: set[str] | None = None diff --git a/mkdocs/tests/build_tests.py b/mkdocs/tests/build_tests.py index 62b6fa76..1d2461c2 100644 --- a/mkdocs/tests/build_tests.py +++ b/mkdocs/tests/build_tests.py @@ -3,6 +3,7 @@ import contextlib import io +import logging import os.path import re import textwrap @@ -769,6 +770,29 @@ def test_anchor_warning(self, site_dir, docs_dir): with self._assert_build_logs(expected_logs): build.build(cfg) + @tempdir( + files={ + "index.md": "[missing anchor](#missing)", + } + ) + @tempdir() + def test_anchor_warning_with_debug_logging(self, site_dir, docs_dir): + cfg = load_config( + docs_dir=docs_dir, site_dir=site_dir, validation={"anchors": "warn"} + ) + + pages_log = logging.getLogger("mkdocs.structure.pages") + original_level = pages_log.level + pages_log.setLevel(logging.DEBUG) + try: + expected_logs = """ + WARNING:Doc file 'index.md' contains a link '#missing', but there is no such anchor on this page. + """ + with self._assert_build_logs(expected_logs): + build.build(cfg) + finally: + pages_log.setLevel(original_level) + @tempdir( files={ "test/foo.md": "[bar](bar.md#heading1)",