Last Updated: February 25, 2016
·
3.474K
· kuroir

GitHub Wiki: Table of Contents

As soon as I began using GitHub Wiki's seriously, I noticed that there's no easy way to generate a Table of Contents, based on a list of files within a directory.

When making a new GitHub Wiki, usually what I do is go into command line and begin creating empty files, which are basically placeholders for stuff I need to work on.

You're probably wondering: But Mario, why don't you just do [[SomethingThatDoesntExist]]? That'll highlight it as red (nonexistent).

The reason for that, is that it means I need to manually keep track of the Contents of the directory, and I need to update the files manually. Who has time for that? I don't.

So lets say we have the following Directory structure:

Something.wiki/
  SomeSubject/
    1.-Interesting-Subject.md
    2.-Interesting-Subject.md
    3.-Interesting-Subject.md
  SomeSubject.md

Where SomeSubject contains all the files you want to have in the Table of Contents and the file SomeSubject.md is the file that will contain teh Table of Contents.

TOC="SomeSubject"; ls -l $TOC | awk 'NF >= 7 { sub(/\.md/, "", $9); sub(/\-/, " ", $9); printf("* [[%s]]%s\n", $9, ($5 == 0) ? " (PENDING)" : ""); }' > $TOC.md

That command will generate the following:

* [[1. Interesting Subject]] (PENDING)
* [[2. Interesting Subject]] (PENDING)
* [[3. Interesting Subject]] (PENDING)

As you can see, at the end (PENDING) is added. This is a flag that states that the file is empty ( 0 bydes ).

And there you have it, an easy way to do a Table of Contents on GitHub Wikis.