@@ -771,7 +771,7 @@ def check_consistency(self) -> None:
771
771
# Call _check_toc_parents here rather than in _get_toctree_ancestors()
772
772
# because that method is called multiple times per document and would
773
773
# lead to duplicate warnings.
774
- _check_toc_parents (self .toctree_includes )
774
+ _check_toc_parents (self .toctree_includes , self . config . root_doc )
775
775
776
776
# call check-consistency for all extensions
777
777
self .domains ._check_consistency ()
@@ -819,19 +819,28 @@ def _traverse_toctree(
819
819
traversed .add (sub_docname )
820
820
821
821
822
- def _check_toc_parents (toctree_includes : dict [str , list [str ]]) -> None :
822
+ def _check_toc_parents (toctree_includes : dict [str , list [str ]], root_doc : str ) -> None :
823
+ """Checks if document is referenced in multiple toctrees.
824
+ Based on the current implementation of `global_toctree_for_doc`,
825
+ it considers only the descendants of root_doc and not the whole graph.
826
+ """
823
827
toc_parents : dict [str , list [str ]] = {}
824
- for parent , children in toctree_includes .items ():
825
- for child in children :
826
- toc_parents .setdefault (child , []).append (parent )
827
828
829
+ def _find_toc_parents_dfs (node : str ) -> None :
830
+ for child in toctree_includes .get (node , []):
831
+ toc_parents .setdefault (child , []).append (node )
832
+ is_child_already_visited = len (toc_parents [child ]) > 1
833
+ if not is_child_already_visited :
834
+ _find_toc_parents_dfs (child )
835
+
836
+ _find_toc_parents_dfs (root_doc )
828
837
for doc , parents in sorted (toc_parents .items ()):
829
838
if len (parents ) > 1 :
830
839
logger .info (
831
840
__ (
832
841
'document is referenced in multiple toctrees: %s, selecting: %s <- %s'
833
842
),
834
- parents ,
843
+ sorted ( parents ) ,
835
844
max (parents ),
836
845
doc ,
837
846
location = doc ,
0 commit comments