Skip to content

Commit f83948b

Browse files
committed
♻️ REFACTOR: Change insert_toctrees to a transform
So we can apply it earlier.
1 parent 3b76ec6 commit f83948b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

sphinx_external_toc/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def setup(app: "Sphinx") -> dict:
1313
"""Initialize the Sphinx extension."""
1414
from .events import (
1515
add_changed_toctrees,
16-
insert_toctrees,
16+
InsertToctrees,
1717
parse_toc_to_env,
1818
TableofContents,
1919
)
@@ -28,7 +28,6 @@ def setup(app: "Sphinx") -> dict:
2828
app.connect("config-inited", parse_toc_to_env, priority=900)
2929
app.connect("env-get-outdated", add_changed_toctrees)
3030
app.add_directive("tableofcontents", TableofContents)
31-
# Note: this needs to occur before `TocTreeCollector.process_doc` (default priority 500)
32-
app.connect("doctree-read", insert_toctrees, priority=100)
31+
app.add_transform(InsertToctrees)
3332

3433
return {"version": __version__, "parallel_read_safe": True}

sphinx_external_toc/events.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from sphinx.config import Config
1010
from sphinx.environment import BuildEnvironment
1111
from sphinx.errors import ExtensionError
12+
from sphinx.transforms import SphinxTransform
1213
from sphinx.util import docname_join, logging
1314
from sphinx.util.docutils import SphinxDirective
1415
from sphinx.util.matching import Matcher, patfilter, patmatch
@@ -262,3 +263,16 @@ def insert_toctrees(app: Sphinx, doctree: nodes.document) -> None:
262263
# since `TocTreeCollector.process_doc` expects it in a section
263264
# TODO check if there is this is always ok
264265
doctree.children[-1].extend(node_list)
266+
267+
268+
class InsertToctrees(SphinxTransform):
269+
"""Create the toctree nodes and add it to the document.
270+
271+
This needs to occur at least before the ``DoctreeReadEvent`` (priority 880),
272+
which triggers the `TocTreeCollector.process_doc` event (priority 500)
273+
"""
274+
275+
default_priority = 100
276+
277+
def apply(self, **kwargs: Any) -> None:
278+
insert_toctrees(self.app, self.document)

0 commit comments

Comments
 (0)