question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating a Table of Contents for All Content

See original GitHub issue

I’m curious how to easily create a Table of Contents for all content files, divided up into sections (directories), pretty much like how it is in the Docs Theme on the left-hand sidebar. I feel like it’s super-easy, but can’t find any examples anywhere.

This is for a project already in production (so can’t use the Nuxt Content Docs module).

On the one hand it looks like its all in the root directory of content, but then how is it organized?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jhullcommented, Nov 10, 2020

Oh, that’s a great idea about the numbering rules. I will definitely steal that one, and hopefully down the line there can be an easier built-in solution.

Thanks!

1reaction
NozomuIkutacommented, Nov 10, 2020

@jhull

I’m glad to hear that it worked! 👍

And yeah, I also think it’s somewhat confusing at first that relationship between position property and directory structure.

In short, as you guess, we are supposed to have position set in sequence from beginning to end.


If I understand it correctly based on my code reading, the situation is like the following:

  • Nuxt Content internally calls fs.readdir() method (recursively) to retrieve all the file names in the content directly.
  • fs.readdir() doesn’t guarantee the order of file names (reference).
  • As each file appears in the array returned by fs.readdir(), it is transformed and pushed to an array (database’s internal behavior).
  • As a result, files in database know nothing about “position” of themselves (e.g. who are neighbors).

I believe this is why authors provide position property. With this property, Nuxt Content can sort files according to information architecture, independently of physical directory structure. Indeed, without such property, we can’t fetch prev/next files and have a control on the order of navigation links.


Or is there-hopefully-a better way to do it?

By the way, I also wish there were better way. 🤔 What we want to do is to order pages, and not numbering every single of them, right?

With current approach, the more the website grows, the harder it would likely to become to organize files/pages. FYI, I’m avoiding such situation by defining numbering rules, where I can add pages between others later (e.g. increment position by 5 for pages, by 100 for directories (categories)).

/content
    /category-1 // virtual position: 0
        /doc-1.md // position: 1
        /doc-2.md // position: 6
        /doc-3.md // position: 11
    /category-2 // virtual position: 100
        /doc-4.md // position: 101
Read more comments on GitHub >

github_iconTop Results From Across the Web

Word Tips: How to Create a Table of Contents in Word
Now for the easy part! Once you've applied heading styles, you can insert your table of contents in just a few clicks. Navigate...
Read more >
Table of Contents in Word - CustomGuide
1. Click in the document where you want to insert a Table of Contents. 2. Click the References tab on the ribbon. 3....
Read more >
How to Create a Table of Contents in Word in 7 Easy Steps
4. Click the Table of Contents command. Go to Ribbon > References > Table of Contents. Choose from one of the two automatic...
Read more >
How to build a table of contents in Microsoft Word
1. Go to each page of your document and implement a heading style to the title via Home > Styles. You can also...
Read more >
The Wicked Easy Way to Create a Table of Contents in Word
Click in your document where you want to create the table of contents. If you'd like it to appear on its own page,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found