contentlayerdev / contentlayerdev/contentlayer

Support content "sections"

Aperta
#209 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub
feature
Lingua principale
TypeScript
Stelle
3.5k
Fork
192
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

One of the things that I really like about [Hugo](https://gohugo.io) is its [sections](https://gohugo.io/content-management/sections/) feature, which enables you to separate nested directories of content into well-defined groups. So instead of a kind of "flat" array of files you end up getting a nested structure like this:

```
Documentation
\_ Getting started
\_ Installation
\_ Platforms
\_ Administration
\_ Kubernetes
\_ Setup
\_ Configuration
\_ etc
```

As expected, sections can be infinitely nested and each can have its own metadata. I don't necessarily endorse the _way_ that Hugo does this (you need to sprinkle `_index.md` files throughout your tree) but I do think that having this kind of rich structure available to you is great for creating tables of content, sidebar navs, and all of that good stuff. Personally, it's the thing that I'm missing the most as I cross the chasm from mostly doing things in Hugo to doing things in JS world.

I could envision being able to do something like this in Contentlayer:

```js
export const Doc = defineDocumentType(() => ({
name: 'Doc',
filePathPattern: `**/*.md`,
fields: {
title: {
type: 'string',
description: 'The title of the doc',
required: true,
},
},
computedFields: {
url: {
type: 'string',
resolve: (post) => `/docs/${post._raw.flattenedPath}`,
},
},
}))

export default makeSource({
contentDirPath: 'docs',
documentTypes: [Doc],
// tiny little change
sections: true,
})
```

This could export an `allDocsSections` object of type `Sections` that would enable you to recurse through the tree.

```jsx


    {allDocs.sections.map(section => (

  • {section.title}




      {section.docs.map(doc => (

    • {doc.title}

    • ))}

      {section.sections.map(section => ( /* nest */ ))}

  • )}

```

This is very off the cuff but hopefully provides _some_ sense of what I'm suggesting. I'd be quite happy to help out with this if others find the idea compelling. Just wanted to register it here in this forum.

Note: Docusaurus [also supports this](https://docusaurus.io/docs/sidebar/autogenerated) using a `_category_.{json|yml}` file that creates a new "category" (basically an analogue to the sections concept) wherever it's found in the directory tree.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia esaminando gli entry point makeSource e defineDocumentType mostrati nell’issue, quindi traccia come i percorsi dei documenti e i campi calcolati diventano collection come allDocsSections. Confronta le sezioni annidate richieste e i metadati per sezione con gli esempi di Hugo e Docusaurus. Il lavoro è completato quando l’API proposta e la struttura tipizzata ricorsiva sono implementate e verificate tramite test appropriati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
content
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.