11ty/eleventy-navigation

Include a page's complete frontmatter in its navigation entry

noelleleigh opened this issue · 1 comments

I have a navigation entry named writing, with several child pages. Each child page has one or more tags. To facilitate easy browsing, I want to be able to display the tags of each child page (along with other metadata) when rendering the navigation <ul>.

Example

/writing/moby-dick.md

---
title: &title Moby-Dick
author: Herman Melville
tags:
  - fiction
  - whale
eleventyNavigation:
  key: moby-dick
  title: *title
  parent: writing
---

/writing/a-brief-history-of-time.md

---
title: &title A Brief History of Time
author: Stephen Hawking
tags:
  - non-fiction
  - physics
eleventyNavigation:
  key: a-brief-history-of-time
  title: *title
  parent: writing
---

/writing/index.html (rendered)

<ul>
    <li>
        <a href="/writing/moby-dick" class="title">Moby-Dick</a> by Herman Melville
        <ul>
            <li><a href="collection/fiction" class="tag">#fiction</a></li>
            <li><a href="collection/whale" class="tag">#whale</a></li>
        </ul>
    </li>
    <li>
        <a href="/writing/a-brief-history-of-time" class="title">A Brief History of Time</a> by Stephen Hawking
        <ul>
            <li><a href="collection/non-fiction" class="tag">#non-fiction</a></li>
            <li><a href="collection/physics" class="tag">#physics</a></li>
        </ul>
    </li>
</ul>

See Archive of Our Own for a live example.

But at the moment, page-specific information like tags or author isn't included in the entry returned by eleventyNavigation, making implementing this challenging.

Proposal

Inspired by the collection item data structure, add a field data to the entry returned by eleventyNavigation. This field would contain all data for this piece of content (includes any data inherited from layouts), minus the eleventyNavigation field (to avoid infinite recursion).

With this implemented, the above example could be written as:

/writing/index.njk

{% set navPages = collections.all | eleventyNavigation(eleventyNavigation.key) %}
<ul>
    {%- for entry in navPages %}
    <li>
        <a href="{{ entry.url | url }}">{{ entry.title }}</a> by {{ entry.data.author }}
        <ul>
            {% for tag in entry.data.tags %}
            <li>
                <a href="collection/{{ tag }}">#{{ tag }}</a>
            </li>
            {% endfor %}
        </ul>
    </li>
    {%- endfor %}
</ul>

I see this issue was already brought up in #17. It would be great to have an official word on whether this could be considered for inclusion in the library itself.