souporserious / renoun

Documentation that matches the quality of your product.
https://renoun.dev
GNU Affero General Public License v3.0
420 stars 8 forks source link

[Question] How to get collection title #183

Closed noxify closed 1 month ago

noxify commented 2 months ago

Hi,

while creating a new collection via createCollection, I can specify a title in the options. Is there currently a an option/method to get this title?

Example:

export const OmnidocDocsCollection = createCollection<DocSchema>(
  "@content/docs/omnidoc-docs/**/*.{ts,tsx,mdx}",
  {
    title: "Omnidoc Docs",
    baseDirectory: "content/docs/omnidoc-docs",
    basePath: "docs/omnidoc-docs",
  },
)

I see currently only getPath, getSource and getSources for OmnidocDocsCollection.

Did a quick check and it seems it could be enough to extend the CollectionSource type: https://github.com/souporserious/omnidoc/blob/main/packages/omnidoc/src/collections/index.tsx#L176-L179

If you want, I can give it a try and provide a PR.

souporserious commented 2 months ago

I originally had a getTitle method on collection sources, but removed it since it wasn't exactly the same as what happens with file and export sources so wanted to keep it consistent and missed removing the title option from createCollection.

The original intention was to be able to iterate over multiple collections and use the title to build index pages, but I'm not sure how practical that is since those would be mostly one-off situations so you could render the title yourself pretty easily. I have thought about using the term of label for use in navigational items so it seems to make sense to bring that back for collections so you can generate navigations. Difference between title and label being title is used for pages and label is meant to be the shorter version used in navigations.

Curious what you'd like to use it for? 🙂

noxify commented 2 months ago

Hi,

The original intention was to be able to iterate over multiple collections

That's exactly what I'm currently doing. I created an export which contains all available collections: https://github.com/noxify/omnidoc-docs-template/blob/main/src/collections.ts#L34

The collections are iterated in the ui to provide the values for the switcher:

image

Currently I'm using Object.keys(collections) to generate the values, but it would be good to have a method on collection level to make it more "human readable".

The switcher is inspired based on the nextjs docs where you can choose between "app" and "pages". Want to show something similar.

Real world example could be Omnidoc ;) One collection for the "core" and one for the "plugins" ( or for each plugin? ).

...and use the title to build index pages...

I solved this by adding the collection keys also to the slugs array in generateStaticParams: https://github.com/noxify/omnidoc-docs-template/blob/main/src/app/docs/%5B...slug%5D/page.tsx#L50-L58

and checking the slug length and load the /docs/<product>/index.mdx if needed: https://github.com/noxify/omnidoc-docs-template/blob/main/src/app/docs/%5B...slug%5D/page.tsx#L82-L84

since those would be mostly one-off situations so you could render the title yourself pretty easily

I agree, having static props ( like title or label ) are very limited.

What do you think about an meta object? Have seen this at tanstack/table for the column definitions. There I use the meta to specify custom class names and other stuff.

Since meta is just an object, I can specify whatever I want and I'm not "limited" to just some static props. And I think it would be a "low brainer" to implement it, since we already have everything - so we just have to rename some props and provide a getter to expose the meta data.

As always, if you need help, let me know and I can give it a try ✌️

noxify commented 1 month ago

based on the discord chat, i will close this.

Solution is to have a named export metadata in the docs/<collection-name/index.mdx. Here we can specify the relevant information.

Ref: https://discord.com/channels/1276665813906096159/1282243655146405899/1286721049689591808

souporserious commented 1 month ago

Forgot to link, but I also wrote up a recipe here. I'll do an expanded guide on metadata in general in the future.