Closed cirosantilli closed 3 years ago
You can automatically generate TOC via http://doctoc.herokuapp.com/. There is also an npm package available here to do it on the command line.
But yes, these are ultimately just manual methods because you need to manually copy and paste the TOC into the markdown. But one benefit of it is that the toc is maintained everwhere, even across apps that don't use GFM for rendering.
I hope GitHub fix some standard extension syntax, possibly backing up Kramdown's, and potentially settle the existing extension mess with it's influence.
Here's my try to solve this issue without any addional software (on linux) — github-markdown-toc
:+1:
+1
+1
Another workaround...
Although I'd like to see TOC support in GFM, if you want TOC's rendered by GitHub today than you could switch to asciidoc. Asciidoc has built-in support for TOC, and with the right options it will be rendered by GitHub.
I'm a fan of Markdown so I'm hesitant to asciidoc, but the article on Living the Future of Technical Writing by GitHub's @schacon, which talks about switching from Markdown to Asciidoc, has made willing to consider the jump. There's also several other features other features in asciidoc-samples - besides TOC - that don't seem to have working GFM equivalents. Footnotes, for example.
If you want give asciidoc a shot, you can use pandoc to convert your existing markdown:
pandoc -t asciidoc -f markdown README.md > README.asciidoc
@maxlinc thanks a lot for the schacon link! I was really curious to why he used Asciidoc on Pro Git 2.
@maxlinc - I subscribe the thanksgiving by cirosantilli! :+1:
@ekalinin I suppose this won't work for private repos?
@ekalinin I suppose this won't work for private repos?
@mike-charles yes, unfortunately github-markdown-toc won't work for private repos
+1
+1
+1
+1 No offline approaches are preferred than the GH online dynamic generation of TOC, as all offline approaches will be a burden and hard to maintain the TOC correctly.
+1
Why does a solution have to extend markdown?
@ekalinin's code can already create a ToC markdown file, so GH could just generate a new ToC on every change to readme.md, and display that file on the project's homepage above the real readme.md. Everyone gets a ToC for free with their readme.md files (and presumably can opt-in/out if they wish) without changing a single line of code.
Why does a solution have to extend markdown?
@MikeyBurkman - It does not need to, but if it did it, it would become part of the specification, and all libraries that works against that specification would support it (e.g.: github-markdown preview of the readme file in your IDE/editor of choice).
Preview of the TOC is important: on large documents is easy to get the levels wrong, for example.
+1
+1
+1
@quasipedia Surely you aren't saying that the necessity of a preview tool is a good reason to change the MD spec! That's like saying that not having some MD tool installed on my computer means I can't edit MD. Github already have a MD preview tool... just extend it to also preview the TOC.
As for external tools: the script to generate TOC from MD is publicly available. They can integrate if and when they want, and that integration would not be much different than supporting another MD spec. Only now your tools have to adjust, not every single user.
@MikeyBurkman - Surely you aren't saying that improving on standards is bad! ;)
Seriously: I can't but repeat myself: the solution does not need to extentd MD. GitHub has a history of creating standards that are to all effects "forks" of previously existing ones (GFM, TOML...). Since GFM is already a different standard than MD, and since GitHub is its creator and maintainer, it wouldn't surprise me if they would implement the TOC as part if it...
...but if that doesn't happen, I am just fine! :)
+1
+1
Have tried MarkdownTOC for sublime text today. Which might have been a alternative but sadly it doesn't work nearly well enough. No links and seems broken as it doesn't parse my markdown headings correctly.
[EDIT] npm install -g doctor && doctoc .
is good though not integrated into ST. Probably worth making a ST command entry for it.
FWIW, I made this. https://github.com/ajkochanowicz/Smooth-ToCer
Obviously, it won't work with github readme files, but for any other markdown content that needs a TOC and can run JS...
+1
+1
+1
+1
+1
+1
:+1:
Bump :+1:
+1
Just going to repeat myself here, and say that github should adopt the doctoc
tool for auto-generating TOCs in markdown files. It's the best one available out there.
@dreamcat4 ditto here
yas
+1
+1
+1
My colleague @schmiedc and I have created a GreaseMonkey script that installs a new TOC
button left of the h1
button which uses the excellent markdown-js
library to add/refresh a table of contents.
The advantage over solutions like doctoc is that it integrates into GitHub's wiki editor and does not need users to work on their command-line (and require users to install tools like node.js
). In Chrome, it works by drag 'n dropping into the Extensions page, in Firefox you will need to install the GreaseMonkey extension.
It will work with plain markdown (i.e. it does not handle code blocks correctly, as that is a GitHub extension to markdown). Contributions welcome.
@dscho does it work only with wiki pages or with any markdown file?
@bumbu we specifically designed it to trigger only when the user edits wiki pages. But it should be really trivial to adjust by appending appropriate @include
lines and extending this regular expression to accept more URLs than the "edit wiki page" ones.
Why not give it a shot yourself? As I mentioned, contributions are warmly welcomed.
+1
+1
+1
+1
@ekalinin I suppose this won't work for private repos?
@mike-charles yes, unfortunately github-markdown-toc won't work for private repos
@mike-charles just added an ability to work with local markdown files. So seems it may work with private repos.
@ekalinin thanks!
Edit March 2021: GitHub now added a workaround mentioned at: https://github.com/isaacs/github/issues/215#issuecomment-807688648 That is a good step, but I'll keep this open until they actually add a way to add it inside the rendered output (which can be e.g. searched more easily with Ctrl+F).
When I see a manually generated table of contents, it makes me sad.
When I see a huge README that is impossible to navigate without it, it makes me even sadder.
LaTeX has it. Gollum has it. Pandoc has it. So why not GFM?
There are some tools that automate the generation, but they're just an ugly hack:
Now the bitter part: what syntax to use?
Whatever is chosen, it should be a standard way to extend Markdown, so that other extensions can be added later on.
One possibility is to use Kramdown extension syntax and insert the TOC with
{:toc max_level=3 }
.Redcarpet already has a command that generates the TOC:
Redcarpet::Render::HTML_TOC
, but no way to insert it, and no standard way to extend markdown.Same for GitLab: http://feedback.gitlab.com/forums/176466-general/suggestions/5790538-extension-for-inserting-a-table-of-contents-toc
StackOverflow: 66 upvotes as of 2014-06: http://stackoverflow.com/questions/9721944/automatic-toc-in-github-flavoured-markdown
There is an issue for the library GitHub uses to render markdown at: https://github.com/github/markup/issues/904
Steven! Ragnarök replied with the neutral:
Asciidoc
I got tired of waiting and started using Asciidoc. I recommend it, the format is really good. Just look at the typographical beauty of this: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/7d9102373d60bd159920abfe96d636420afedd67/README.adoc