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

Prev/Next for items in eleventyNavigation items #22

Open
khoivan88 opened this issue Nov 23, 2020 · 3 comments
Open

Prev/Next for items in eleventyNavigation items #22

khoivan88 opened this issue Nov 23, 2020 · 3 comments

Comments

@khoivan88
Copy link

Hi, thank you for a very useful plugin. I was wondering of something like Prev/Next button is possible for items that prepared by eleventyNavigation?

Some think like:

{% set previousChapter = collections.chapters | eleventyNavigation | getPreviousCollectionItem(page) %}
{% set nextChapter = collections.chapters | eleventyNavigation | getNextCollectionItem(page) %}

Right now, if I have nested navigation, the Prev/Next code in this section but as you might have guess, the items would not be in the same order.

Thank you very much

@aomarks
Copy link

aomarks commented Apr 7, 2021

I also ran into this, so I added a filter to flatten the navigation hierarchy, and add prev and next properties:

/**
 * Flatten a navigation object into an array, and add "next" and "prev"
 * properties.
 */
eleventyConfig.addFilter('flattenNavigationAndAddNextPrev', (nav) => {
  const flat = [];
  const visit = (items) => {
    for (const item of items) {
      flat.push(item);
      visit(item.children);
    }
  };
  visit(nav);
  for (let i = 0; i < flat.length; i++) {
    const item = flat[i];
    item.prev = flat[i - 1];
    item.next = flat[i + 1];
  }
  return flat;
});

Then in my template I did something like this:

{% set flatNavItems = collections.docs | eleventyNavigation | flattenNavigationAndAddNextPrev %}
{% for item in flatNavItems %}
  {% if item.url === page.url %}
    {% if item.prev %}
      <a href="{{ item.prev.url }}">
        Previous: {{ item.prev.title }}
      </a>
    {% endif %}

    {% if item.next %}
      <a href="{{ item.next.url }}">
        Next: {{ item.next.title }}
      </a>
    {% endif %}
  {% endif %}
{% endfor %}

Hope that helps.

@khoivan88
Copy link
Author

Wow, that's cool. Thank you very much for sharing your solution. It has been a while so i don't remember what i did but this would be super helpful for my future project 😃. Thank you

@Muhammed-Rahif
Copy link

I also ran into this, so I added a filter to flatten the navigation hierarchy, and add prev and next properties:

/**
 * Flatten a navigation object into an array, and add "next" and "prev"
 * properties.
 */
eleventyConfig.addFilter('flattenNavigationAndAddNextPrev', (nav) => {
  const flat = [];
  const visit = (items) => {
    for (const item of items) {
      flat.push(item);
      visit(item.children);
    }
  };
  visit(nav);
  for (let i = 0; i < flat.length; i++) {
    const item = flat[i];
    item.prev = flat[i - 1];
    item.next = flat[i + 1];
  }
  return flat;
});

Then in my template I did something like this:

{% set flatNavItems = collections.docs | eleventyNavigation | flattenNavigationAndAddNextPrev %}
{% for item in flatNavItems %}
  {% if item.url === page.url %}
    {% if item.prev %}
      <a href="{{ item.prev.url }}">
        Previous: {{ item.prev.title }}
      </a>
    {% endif %}

    {% if item.next %}
      <a href="{{ item.next.url }}">
        Next: {{ item.next.title }}
      </a>
    {% endif %}
  {% endif %}
{% endfor %}

Hope that helps.

This worked like a charm when I edited the code like below, in template:

- {% set flatNavItems = collections.docs | eleventyNavigation | flattenNavigationAndAddNextPrev %}
+ {% set flatNavItems = collections.all | eleventyNavigation | flattenNavigationAndAddNextPrev %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants