Skip to content

Commit

Permalink
Merge pull request #25 from openzim/index-nav
Browse files Browse the repository at this point in the history
Fixed navigation oddities with single page docs
  • Loading branch information
benoit74 committed Sep 17, 2024
2 parents 2e61d1f + ffb63ca commit 653029f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/devdocs2zim/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,15 @@ def count(self) -> int:
def _contained_pages(self) -> set[str]:
return {link.path_without_fragment for link in self.links}

def contains_page(self, page_path: str) -> bool:
"""Returns whether this section contains the given page."""
def opens_for_page(self, page_path: str) -> bool:
"""Returns whether this section should be rendered open for the given page."""

# Some docs like Lua or CoffeeScript have all of their content in the index.
# Others like RequireJS are split between index and additional pages.
# We don't want sections opening when the user navigates to the index.
if page_path == "index":
return False

return page_path in self._contained_pages


Expand Down
2 changes: 1 addition & 1 deletion src/devdocs2zim/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<a href="{{rel_prefix | safe}}index" class="_list-item">{{devdocs_metadata.name}}</a>
<div class="_list">
{% for section in nav_sections %}
<details {% if section.contains_page(path) %}open{% endif %}>
<details {% if section.opens_for_page(path) %}open{% endif %}>
<summary>
<span class="_list-item" style="display:inline; box-shadow: none;">
<span class="_list-count">{{ section.count | safe}}</span>
Expand Down
19 changes: 15 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_count_non_empty(self):

self.assertEqual(1, got)

def test_contains_page(self):
def test_opens_for_page(self):
section = NavigationSection(
name="",
links=[
Expand All @@ -290,9 +290,20 @@ def test_contains_page(self):
],
)

self.assertTrue(section.contains_page("foo"))
self.assertTrue(section.contains_page("bar"))
self.assertFalse(section.contains_page("bazz"))
self.assertTrue(section.opens_for_page("foo"))
self.assertTrue(section.opens_for_page("bar"))
self.assertFalse(section.opens_for_page("bazz"))

def test_opens_for_page_index(self):
section = NavigationSection(
name="",
links=[
DevdocsIndexEntry(name="Index", path="index", type=None),
],
)

# Links to the index are special cases and shouldn't open.
self.assertFalse(section.opens_for_page("index"))


class TestDevdocsIndex(TestCase):
Expand Down

0 comments on commit 653029f

Please sign in to comment.