Skip to content

Instantly share code, notes, and snippets.

@optozorax
Last active September 19, 2022 12:10
Show Gist options
  • Save optozorax/2501ed8cd9fce4c903f38298c3923b75 to your computer and use it in GitHub Desktop.
Save optozorax/2501ed8cd9fce4c903f38298c3923b75 to your computer and use it in GitHub Desktop.
Automatic TOC creation

Your readme

Some text

Table Of Contents

Explanation

According to StackOverflow, this supposed to be comment in MarkDown:

[//]: # "some-text"

You put this text in your file where TOC should be:

[//]: # "start-toc"
[//]: # "end-toc"

So, script takes content before such comment, after it, and write TOC before them.

You could see RAW version of this file te see how this script works.

You could use mdtoc2, if your file contains non-ascii symbols. mdtoc2 will unescape these symbols.

Dependencies

Put gh-md-to to on of your PATH directory.

#!/bin/bash
cat README.md | grep start-toc --before-context 99999999 > temp.md
cat README.md | gh-md-toc - >> temp.md
echo >> temp.md
cat README.md | grep end-toc --after-context 99999999 >> temp.md
rm README.md
mv temp.md README.md
#!/bin/bash
cat README.md | grep start-toc --before-context 99999999 > temp.md
cat README.md | gh-md-toc - > toc.md
printf '%b\n' "$(<toc.md)" >> temp.md
echo >> temp.md
cat README.md | grep end-toc --after-context 99999999 >> temp.md
rm README.md toc.md
mv temp.md README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment