Current menu item for any blog page

Dear all!

Current menu links are highlighted using

def menu_link(link_text, url, options = {})
  options[:class] << ' nav-item--is-current' if current_page.url == url
  link_to(link_text, url, options)
end

Is there any way to highlight the menu item journal not only when the blog overview is active, but also when any blog item site is the current one?

Unlike here, there is no static journal prefix, but

  blog.permalink = ":title.html"

The site uses multiple blogs, by the way.

Thanks a lot.

You can check if the current resource is a blog post with:

current_resource.is_a? Blog::BlogArticle

You could use it as another condition along with link_text === 'journal'.

Thanks a lot. I will check it out. (Yet, I’m not very familiar with helpers).

Thank you!

I ended up creating a helper using include? to check for a substring of the URL.

def link_to_page name, url
  path = current_page.url
  current = path.include? url

  class_name = current ? 'menu__item active' : 'menu__item'

  "<li class=\"#{class_name}\"><a class=\"menu__link\" href=\"#{url}\">#{name}</a></li>"
end

Then in my view I loop through:

- data.site.navigation.each do |nav|
    = link_to_page nav.title, nav.path

And my site.yml looks like this

navigation:
  -
    title: "About"
    path: "/about/"
  -
    title: "Articles"
    path: "/articles/"

  -
    title: "Contact"
    path: "/contact/"
  -
    title: "Styleguide"
    path: "/styleguide/"

Seeing as the title and path are all the same I might reduce the list to a single word

Thank you. A practical solution. (Sorry for replying so late: I was offline for quite some time.)

I’ll look into it in detail now. Thanks!