Skip to content

Instantly share code, notes, and snippets.

@nisests
Forked from gotofritz/gitlab-markdown-toc.js
Created January 9, 2017 03:59
Show Gist options
  • Save nisests/d3f5c970be8161617031bcdeaa27b3a2 to your computer and use it in GitHub Desktop.
Save nisests/d3f5c970be8161617031bcdeaa27b3a2 to your computer and use it in GitHub Desktop.
creates a gitlab markdown table of contents for a README.md page
// quick and dirty snippet to creates a gitlab markdown table of contents for a README.md page
// preview gitlab page and paste in browser console
var str = "";
$('.file-content')
.find('h1, h2, h3, h4, h5, h6, h7')
.each((i, node) => {
// node.tagName is H1 H2...
let indent = Number(node.tagName[1]) - 1;
// markdown mested lists are
// - xxx
// - yyy etc
let tabs = ' '.substr(0, 3 * indent);
let linkName = node.textContent.trim();
let linkAnchor = node.querySelector('a').id;
str += `\n${tabs}- [${linkName}](#${linkAnchor})`;
});
console.log(str);
@Pacheco95
Copy link

Very nice snippet!

@quicktrick
Copy link

quicktrick commented Jun 14, 2019

On my GitLab the script adds some excessive text to each toc entry, like "user-content-", e.g.:

      - [Tips](#user-content-tips)

Otherwise everything is fine, thank you!

@federicorotolo
Copy link

federicorotolo commented Dec 22, 2021

Hi,
now you just need adding [[TOC]] or [[TOC]] to the Description section of the README.md file to get the TOC:

https://docs.gitlab.com/ee/user/markdown.html#table-of-contents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment