2

My site uses Jekyll with Redcarpet markdown parser. I'd like to make it so my headings are automatically generated with ids:

# My heading

Becomes:

<h1 id="my-heading">My heading</h1>

But I haven't found a way.

I'd like to stick with redcarpet as my markdown parser, but it's not essential. If there's another one that supports all the same features then I'd happily consider it. I especially like that redcarpet generates code blocks in pygments format.

Ben
  • 51,770
  • 36
  • 127
  • 149
Robin Winslow
  • 10,908
  • 8
  • 62
  • 91
  • I'm not entirely sure about how you would do it, but maybe [creating a custom renderer is the way](https://github.com/vmg/redcarpet#and-you-can-even-cook-your-own). Good luck! – Carlos Agarie Jun 29 '13 at 18:23

2 Answers2

2

My preferred solutions is @maul-esel's Github Issues answer:

The redcarpet README mentions the config option with_toc_data. This generates IDs for headings that look like id="toc_0" etc.

You can enable it by adding this into your _config.yml Jekyll config:

markdown: redcarpet
redcarpet:
    extensions: [with_toc_data]

It's not ideal - I'd prefer more semantic IDs like id="my-heading" - but it's Good Enough and it's super easy - it means I don't need to set up and learn another markdown parser (or work out how to extend the redcarpet).

Robin Winslow
  • 10,908
  • 8
  • 62
  • 91
1

If it helps, it does work with Kramdown

$ kramdown <<< '# My heading'
<h1 id="my-heading">My heading</h1>
Zombo
  • 1
  • 62
  • 391
  • 407
  • I looked at kramdown. It's a potential solution, but I much prefer the way redcarpet generates code blocks. – Robin Winslow Jun 29 '13 at 12:45
  • Maybe I just don't know how. With redcarpet I enclose my code in `\`\`\` css \ ... \ \`\`\`` and it puts pygments ``s around all the relevant bits of code for highlighting. If I do the equivalent `~~~ css \ ... \ ~~~` for kramdown, it just surrounds my code with `
    ...
    ` but no pygments markup.
    – Robin Winslow Jun 30 '13 at 10:26
  • Yeah I don't care about that. I'm not really a Ruby guy, and I prefer language-agnostic solutions. I just want to get this working with the least effort possible. – Robin Winslow Jun 30 '13 at 10:52