Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Determine active page by considering parent too #19

Merged
merged 1 commit into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,14 @@ title: My beautiful page
---
```

## `parent`

The page that should be highlighted as ‘active’ in the navigation.

```yaml
---
parent: shaving-yaks.html
---
```

[mm]: https://middlemanapp.com/basics/frontmatter
8 changes: 8 additions & 0 deletions example/source/child-of-expired-page.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: This is a child of expired page
parent: expired-page.html
---

# This is a child of expired page

Expired page should highlight in the navigation.
8 changes: 8 additions & 0 deletions lib/govuk_tech_docs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ def current_page_review
def format_date(date)
date.strftime('%-e %B %Y')
end

def active_page(page_path)
[
page_path == "/" && current_page.path == "index.html",
("/" + current_page.path) == page_path,
current_page.data.parent != nil && ("/" + current_page.data.parent.to_s) == page_path,
].any?
end
end

context.page '/*.xml', layout: false
Expand Down
6 changes: 3 additions & 3 deletions lib/source/layouts/_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

<nav id="navigation" class="header__navigation js-nav" aria-label="Top Level Navigation" aria-hidden="true">
<ul>
<% config[:tech_docs][:header_links].each do |title, url| %>
<li<% if url == current_page.url %> class="active"<% end %>>
<a href="<%= url %>">
<% config[:tech_docs][:header_links].each do |title, path| %>
<li<% if active_page(path) %> class="active"<% end %>>
<a href="<%= path %>">
<%= title %>
</a>
</li>
Expand Down
19 changes: 19 additions & 0 deletions spec/features/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
and_i_visit_the_homepage
then_there_is_a_heading
then_there_is_a_source_footer
then_the_page_highlighted_in_the_navigation_is("Documentation")

and_there_are_proper_meta_tags
and_redirects_are_working
and_frontmatter_redirects_are_working

when_i_view_a_proxied_page
then_there_is_another_source_footer

when_i_view_expired_page
then_the_page_highlighted_in_the_navigation_is("Expired page")

when_i_view_child_of_expired_page
then_the_page_highlighted_in_the_navigation_is("Expired page")
end

def when_the_site_is_created
Expand Down Expand Up @@ -46,6 +53,10 @@ def then_there_is_a_source_footer
end
end

def then_the_page_highlighted_in_the_navigation_is(link_label)
expect(page.find('li.active a').text).to eq(link_label)
end

def when_i_view_a_proxied_page
visit '/a-proxied-page.html'
end
Expand All @@ -68,4 +79,12 @@ def and_frontmatter_redirects_are_working
visit '/something/old-as-well.html'
expect(page.body).to match '<link rel="canonical" href="/" />'
end

def when_i_view_expired_page
visit '/expired-page.html'
end

def when_i_view_child_of_expired_page
visit '/child-of-expired-page.html'
end
end