Q42 / sanity-plugin-page-tree

MIT License
23 stars 3 forks source link

Feature request: Add hidden uri field #8

Closed magicspon closed 6 months ago

magicspon commented 6 months ago

Hello..

It would be great if each tree entry was automatically given a hidden uri field, generated from it's parents (and parents parents) slugs.

example groqd query.

q("*")
  .filter('_type == "page"')
  .grab$({
    slug: q.slug('slug'),
    parent: q('parent').deref().select({
      "_type != 'home'": {
        title: q.string(),
        slug: q.slug('slug'),
        parent: q('parent').deref().select({
          "_type != 'home'": {
            title: q.string(),
            slug: q.slug('slug'),
            parent: q('parent').deref().select({
              "_type != 'home'": {
                title: q.string(),
                slug: q.slug('slug'),
              }
            })
          }
        })
      }
    })
  })

PixelSnap 2023-12-20 at 15 07 23

if I want to get /a/b/c/d i need to write a query like so:

q("*")
  .filter('_type == "page" && slug.current == "d" && parent->slug.current == "c" && parent->parent->slug.current == "b" && parent->parent->parent->slug.current == "a"')
  .grab$({
    slug: q.slug('slug'),
  })

which is a bit knarly!

what would be great is an uri field

q("*")
  .filter('_type == "page" && uri == "a/b/c/d")
  .grab$({
    slug: q.slug('slug'),
  })

Would something like this be possible to add

tstikvoort commented 6 months ago

You indeed need to have information (parent and slug) for all the pages in order to resolve URLs. This package also includes a client with a method called getAllPageMetadata. It retrieves the bare minimum data for all the pages (_id, type, slug, and parent) and resolves the URL for each page. With the returned data, you can retrieve the documentId for a URL and fetch your content afterward.

We don’t have plans to store a uri property on every page document. We did consider it, saving this information before the publish action, but the editor experience would be worse and would make the implemenation more complex. Especially when you change, for example, a slug of one of the pages higher in the tree and have to update all the children.

However, it’s possible to implement this yourself by creating a custom publish action and updating all the child pages. Or do this async with a webhook.

magicspon commented 6 months ago

fair enough. i imagine it would be a bit of a pain to implement.