Skip to content

Commit cc678d5

Browse files
committed
Remove hack changing the order of plugin execution
Instead, now informs and warns users
1 parent c4951da commit cc678d5

File tree

4 files changed

+63
-31
lines changed

4 files changed

+63
-31
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ plugins:
3737
- enumerate-headings
3838
```
3939
40+
> MkDocs executes plugins in the order they are defined. If you use any other plugins that alter the navigation, make sure to define them *before* the `enumerate-headings` plugin.
41+
4042
> If you have no `plugins` entry in your config file yet, you'll likely also want to add the `search` plugin. MkDocs enables it by default if there is no `plugins` entry set.
4143

4244
## Usage

mkdocs_enumerate_headings_plugin/plugin.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,39 @@ def on_pre_build(self, config, **kwargs):
3232

3333
def on_config(self, config, **kwargs):
3434

35-
# Move plugin to be last in the plugins list
36-
# Because some plugins alter the navigation, and we need the final navigation
37-
# This hack might not be necessary in future MkDocs versions, when `nav` is available on other later events as well
38-
plugins = config["plugins"]
39-
plugins.move_to_end("enumerate-headings")
40-
41-
# Move plugin's on_nav event to be last to run
42-
# Because some plugins alter the navigation, and we need the final navigation
43-
# This hack might not be necessary in future MkDocs versions, when `nav` is available on other later events as well
44-
nav_events = plugins.events["nav"]
45-
46-
def get_plugin_name(bound_method):
47-
return type(bound_method.__self__).__name__
48-
49-
plugin_nav_event = [
50-
e for e in nav_events if get_plugin_name(e) == "EnumerateHeadingsPlugin"
51-
][0]
52-
nav_events.append(nav_events.pop(nav_events.index(plugin_nav_event)))
53-
plugins.events["nav"] = nav_events
54-
55-
config["plugins"] = plugins
35+
# This plugin needs the navigation
36+
# But some plugins alter the navigation
37+
# MkDocs executes plugins in order they are defined
38+
# So we can do some checks on other plugins defined.
39+
40+
plugins = [*dict(config["plugins"])]
41+
42+
def check_position(plugin):
43+
if plugin in plugins:
44+
if plugins.index("enumerate-headings") < plugins.index(plugin):
45+
logging.warning(
46+
"[enumerate-headings-plugin] enumerate-headings should be defined after the %s plugin in your mkdocs.yml file"
47+
% plugin
48+
)
49+
50+
# Check list of plugins that alter the navigation
51+
# To make sure they are not defined after the enumerate-heading plugin
52+
# taken from https://github.com/mkdocs/mkdocs/wiki/MkDocs-Plugins#navigation--page-building
53+
check_plugins = [
54+
"monorepo",
55+
"exclude",
56+
"select-files",
57+
"awesome-pages",
58+
"mkdocs-nav-enhancements",
59+
"navtitles",
60+
"encryptcontent",
61+
"awesome-list",
62+
"toc-sidebar",
63+
"mkdocs-simple-hooks",
64+
]
65+
for p in check_plugins:
66+
check_position(p)
67+
5668
return config
5769

5870
def on_nav(self, nav, config, files, **kwargs):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="mkdocs-enumerate-headings-plugin",
8-
version="0.4.1",
8+
version="0.4.2",
99
description="MkDocs Plugin to enumerate the headings (h1-h6) across site pages",
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",

tests/test_builds.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,19 @@ def test_compatibility_monorepo_plugin1(self, tmp_path):
183183
assert re.search(r"2.</span> Hello world!", contents)
184184

185185
@pytest.mark.skip(reason="monorepo v0.4.9 is broken in production")
186-
def test_compatibility_monorepo_plugin2(self, tmp_path):
186+
def test_compatibility_monorepo_plugin2(self, tmp_path, caplog):
187187
tmp_proj = setup_clean_mkdocs_folder(
188188
"tests/fixtures/projects/monorepo_ok/mkdocs_enum_first.yml", tmp_path
189189
)
190190
result = build_docs_setup(tmp_proj)
191191
assert result.exit_code == 0, "'mkdocs build' command failed"
192192

193+
# Make sure warning is raised
194+
assert (
195+
"[enumerate-headings-plugin] enumerate-headings should be defined after"
196+
in caplog.text
197+
)
198+
193199
page = tmp_proj / "site/test/index.html"
194200
contents = page.read_text(encoding="utf-8")
195201
assert re.search(r"2.</span> Hello world!", contents)
@@ -207,14 +213,20 @@ def test_compatibility_monorepo_plugin3(self, tmp_path):
207213
assert re.search(r"7.</span> Changelog", contents)
208214

209215
@pytest.mark.skip(reason="monorepo v0.4.9 is broken in production")
210-
def test_compatibility_monorepo_plugin4(self, tmp_path):
216+
def test_compatibility_monorepo_plugin4(self, tmp_path, caplog):
211217
tmp_proj = setup_clean_mkdocs_folder(
212218
"tests/fixtures/projects/monorepo_sample_docs/mkdocs_enum_first.yml",
213219
tmp_path,
214220
)
215221
result = build_docs_setup(tmp_proj)
216222
assert result.exit_code == 0, "'mkdocs build' command failed"
217223

224+
# Make sure warning is raised
225+
assert (
226+
"[enumerate-headings-plugin] enumerate-headings should be defined after"
227+
in caplog.text
228+
)
229+
218230
page = tmp_proj / "site/versions/v2/changelog/index.html"
219231
contents = page.read_text(encoding="utf-8")
220232
assert re.search(r"7.</span> Changelog", contents)
@@ -236,20 +248,26 @@ def test_compatibility_awesomepages_plugin1(tmp_path):
236248
assert re.search(r"1.</span> Page 4", contents)
237249

238250

239-
def test_compatibility_awesomepages_plugin2(tmp_path):
251+
def test_compatibility_awesomepages_plugin2(tmp_path, caplog):
240252
tmp_proj = setup_clean_mkdocs_folder(
241253
"tests/fixtures/projects/awesome_pages/mkdocs_enum_first.yml", tmp_path
242254
)
243255
result = build_docs_setup(tmp_proj)
244256
assert result.exit_code == 0, "'mkdocs build' command failed"
245257

246-
page = tmp_proj / "site/index.html"
247-
contents = page.read_text(encoding="utf-8")
248-
assert re.search(r"5.</span> Homepage", contents)
258+
# Make sure warning is raised
259+
assert (
260+
"[enumerate-headings-plugin] enumerate-headings should be defined after"
261+
in caplog.text
262+
)
249263

250-
page = tmp_proj / "site/section2/page4/index.html"
251-
contents = page.read_text(encoding="utf-8")
252-
assert re.search(r"1.</span> Page 4", contents)
264+
# page = tmp_proj / "site/index.html"
265+
# contents = page.read_text(encoding="utf-8")
266+
# assert re.search(r"5.</span> Homepage", contents)
267+
268+
# page = tmp_proj / "site/section2/page4/index.html"
269+
# contents = page.read_text(encoding="utf-8")
270+
# assert re.search(r"1.</span> Page 4", contents)
253271

254272

255273
def test_compatibility_material(tmp_path):

0 commit comments

Comments
 (0)