From a2f5a12a5fde233f113eb4c1d89f4dabf20de565 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 25 Apr 2018 17:24:13 +0100 Subject: [PATCH] Determine active page by considering parent too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the logic that works out which page to highlight as ‘active’ in the top navigation is quite naïve (only compares URLs directly). This commit makes it a bit smarter so that a page can define its `parent`, then the navigation will highlight the matching ‘parent’ navigation item. Adapted from: https://github.com/alphagov/govuk-developer-docs/commit/7e5ac5144d87aebf28ca98297b4250c307416c8e --- docs/frontmatter.md | 10 ++++++++++ example/source/child-of-expired-page.html.md | 8 ++++++++ lib/govuk_tech_docs.rb | 8 ++++++++ lib/source/layouts/_header.erb | 6 +++--- spec/features/integration_spec.rb | 19 +++++++++++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 example/source/child-of-expired-page.html.md diff --git a/docs/frontmatter.md b/docs/frontmatter.md index 9ddaf0a0..0fa4b144 100644 --- a/docs/frontmatter.md +++ b/docs/frontmatter.md @@ -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 diff --git a/example/source/child-of-expired-page.html.md b/example/source/child-of-expired-page.html.md new file mode 100644 index 00000000..769dd1aa --- /dev/null +++ b/example/source/child-of-expired-page.html.md @@ -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. diff --git a/lib/govuk_tech_docs.rb b/lib/govuk_tech_docs.rb index e4ca6f4f..95c77735 100644 --- a/lib/govuk_tech_docs.rb +++ b/lib/govuk_tech_docs.rb @@ -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 diff --git a/lib/source/layouts/_header.erb b/lib/source/layouts/_header.erb index 32d2b46a..f20d8548 100644 --- a/lib/source/layouts/_header.erb +++ b/lib/source/layouts/_header.erb @@ -31,9 +31,9 @@