Skip to content

Commit 49509b5

Browse files
author
Bittor Alaña
committed
Fix inner heading numbering logic
Make sure inner heading css styles that add the numbering prefix start from h2 and only go as far as the defined ToC depth.
1 parent 22c575e commit 49509b5

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/mkdocs_print_site_plugin/renderer.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,27 @@ def _toc(self):
231231
"""
232232

233233
def _set_inner_heading_styles(self, id: str, prefix: str, level: int) -> str:
234+
"""
235+
By "inner heading" we mean that even if the heading numbers are fully determined by
236+
the nav's hierarchy, if a page has number 3.2.1, we will add a further numbering
237+
to headings such as h2 and h3 inside the page, so that the first h2 that appears is
238+
3.2.1.1, the next one is 3.2.1.2, etc. In this case we will require that the number
239+
of items in this index be <= toc_depth, which is not the case in the ToC (as its depth
240+
is fully determined by the nav's depth).
241+
"""
234242
result = ""
235243
toc_depth = self.plugin_config.get("toc_depth") or 1
236-
h_level = level + 2
237-
counter_names = [f"counter-{id}-{i}" for i in range(h_level, toc_depth + 1)]
238-
while h_level <= toc_depth:
239-
counters_to_reset = " ".join([f"{x} 1" for x in counter_names[h_level - 1 :]])
244+
# Start from h2's
245+
h_level = 2
246+
counter_names = [f"counter-{id}-{i}" for i in range(h_level, toc_depth - level + 1)]
247+
while h_level <= toc_depth - level:
248+
counters_to_reset = " ".join([f"{x} " for x in counter_names[h_level - 1 :]])
249+
counter_reset = f" counter-reset: {counters_to_reset}; " if len(counters_to_reset) > 0 else ""
240250
counters_to_display = " '.' ".join([f"counter({x})" for x in counter_names[: h_level - 1]])
241251
result += f"""
242-
.print-site-enumerate-headings #{id} h{h_level} {{ counter-reset: {counters_to_reset}; counter-increment: counter-{id}-{h_level} }}
243252
.print-site-enumerate-headings #{id} h{h_level}:before {{ content: '{prefix}.' {counters_to_display} ' ' }}
253+
.print-site-enumerate-headings #{id} h{h_level} {{ {counter_reset} counter-increment: counter-{id}-{h_level} }}
244254
"""
245255
h_level += 1
246256

247-
return result
257+
return result

0 commit comments

Comments
 (0)