content icon indicating copy to clipboard operation
content copied to clipboard

findSurround() with cycle option

Open vdlbk opened this issue 2 years ago • 1 comments

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.

vdlbk avatar Nov 06 '23 19:11 vdlbk

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.

github-actions[bot] avatar Sep 20 '24 02:09 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity.

github-actions[bot] avatar Oct 21 '24 02:10 github-actions[bot]