nuxt / content

The file-based CMS for your Nuxt application, powered by Markdown and Vue components.
https://content.nuxt.com
MIT License
3.08k stars 624 forks source link

findSurround() with cycle option #2414

Open vdlbk opened 10 months ago

vdlbk commented 10 months ago

Is your feature request related to a problem? Please describe

I like the idea behind the findSurround feature, but it's missing an option in my opinion. I want to be able to cycle the content I have instead of stopping at the end of the content list. For me, it's key as it allows my users to continue their navigation and makes them see more content because they usually visit all the content until they already visited one. Without that feature, they might not know there is more content and just stop at the end of the list.

Do you think it's something achievable easily? or should I consider another solution?

Describe the solution you'd like

Let's say I have 3 articles. If I request the surround content for my last articles, I'd like the first as the next content.
Or if I request the surround content for my first articles, I'd like to have the last articles as the previous content.

// pseudo code
content = [...]
previousIndex = indexOfPath - option.before
lastIndex = content.length -1
previous = content[previousIndex  >= 0 ? previousIndex  : lastIndex  ]
next = content[(indexOfPath + option.after) % content.length]
const [prev, next] = await queryContent()
  .only(['_path', 'title'])
  .sort({ date: 1})
  .where({ isArchived: false })
  .findSurround('/articles/article-3', {cycle: true})

// Returns
[
  {
    title: 'Article 2',
    _path: '/articles/article-2'
    //...
  },
   {
    title: 'Article 1',
    _path: '/articles/article-1'
    //...
  } // return the first one instead of null
]

Describe alternatives you've considered

An alternative would be to do it myself if I detect that prev or next is null. But I'm afraid it'll be less performant as it'll require more requests and wait time. Also, it'll be a bit more complicated to implement.

github-actions[bot] commented 1 week ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.