Skip to content

Commit ca3af56

Browse files
committed
Optimization for Material theme / tabbed: don't render navs of other tabs
When a tab has a section index page, and it is not the currently selected tab, skip rendering its sub-items entirely. On desktop view this makes no difference, but in mobile view you will no longer be able to navigate out of the current tab and directly into sub-items of another tab -- first you'll have to navigate to the index page of the other tab. So this is basically a downgrade, but for large sites this is also a huge optimization in terms of build time and disk usage.
1 parent 145ce69 commit ca3af56

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

mkdocs_section_index/rewrites.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ def get_source(self, environment: Environment, template: str) -> Tuple[str, str,
4242

4343

4444
def _transform_material_nav_item_template(src: str) -> str:
45+
src = src.replace(
46+
"{% if nav_item.children %}",
47+
"{% if nav_item.children and not ('navigation.tabs' in features and level == 1 and not nav_item.active and nav_item.url) %}",
48+
)
49+
4550
repl = """\
4651
{% if nav_item.url %}
4752
<a href="{{ nav_item.url | url }}" class="md-nav__link{% if nav_item == page %} md-nav__link--active{% endif %}"

tests/rewrites/material-nav-item-1.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ output: |
7474
{% if nav_item.active %}
7575
{% set class = "md-nav__item md-nav__item--active" %}
7676
{% endif %}
77-
{% if nav_item.children %}
77+
{% if nav_item.children and not ('navigation.tabs' in features and level == 1 and not nav_item.active and nav_item.url) %}
7878
<li class="{{ class }} md-nav__item--nested">
7979
{% if nav_item.active %}
8080
<input class="md-nav__toggle md-toggle" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" checked>

tests/rewrites/material-nav-item-2.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ output: |
6565
{% if nav_item.active %}
6666
{% set class = "md-nav__item md-nav__item--active" %}
6767
{% endif %}
68-
{% if nav_item.children %}
68+
{% if nav_item.children and not ('navigation.tabs' in features and level == 1 and not nav_item.active and nav_item.url) %}
6969
<li class="{{ class }} md-nav__item--nested">
7070
{% set checked = "checked" if nav_item.active %}
7171
<input class="md-nav__toggle md-toggle" data-md-toggle="{{ path }}" type="checkbox" id="{{ path }}" {{ checked }}>

tests/rewrites/material-nav-item-3.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ output: |
7676
{% if nav_item.active %}
7777
{% set class = class ~ " md-nav__item--active" %}
7878
{% endif %}
79-
{% if nav_item.children %}
79+
{% if nav_item.children and not ('navigation.tabs' in features and level == 1 and not nav_item.active and nav_item.url) %}
8080
{% if "navigation.sections" in features and level == 1 + (
8181
"navigation.tabs" in features
8282
) %}

0 commit comments

Comments
 (0)