Is it possible to generate a table of contents using kramdown in a page other than a post? If so, how?

穿精又带淫゛_ 提交于 2019-12-06 05:23:17

问题


The index page on my Jekyll site is meant to be exactly like a post with only one extra bit of code for navigating the other posts. I'm not really using it to generate a blog per se, but a bunch of press pages.

Now within each post I am using kramdown to generate a table of contents like so:

* This line is a placeholder to generate the table of contents
{:toc}

That works fine for all the posts, but when I try to use it on anything but a post, it doesn't generate. The output is simply empty. It doesn't show the raw kramdown above or anything, it just disappears and nothing (especially not a TOC) appears in it's place.

Some of the comments on this post: How to create a table of contents to Jekyll blog post?

Suggest putting a <li> before the above code, but I tried every possible iteration of that and it didn't work. Another comment suggested something with an <h1> tag is necessary, which exists.

I realize there's a plugin to generate a table of contents, the non-javascript version is older than the implementation of TOC using kramdown from what I've seen (that plugin is here: https://github.com/dafi/jekyll-toc-generator ).

And the other plugin ( https://github.com/ghiculescu/jekyll-table-of-contents ) is a bit more recent, but I would prefer not to use javascript to do what kramdown already can do.

Is it possible to use the standard kramdown table of contents markdown to generate one in my case on a page instead of a post?


回答1:


If your index page doesn't have YAML front-matter, then Jekyll won't invoke the kramdown processor. It's a guess but this could be your problem.




回答2:


There's no way to do this with a kramdown option nor can you use the {:toc} option with the {{ content }} of a Jekyll page because {:toc} won't work with generated HTML.

I realize this is an older question, but for anyone who stumbles upon this question more recently. I wrote up a solution to this problem here that uses only Liquid so it'll run perfectly fine on GitHub pages (no plug-in involved) and with JavaScript disabled. My solution works with the generated HTML given by Jekyll so there's no need for wonky work arounds.

I also realize "link only answers" are frowned upon, but I'd rather not have to maintain the same code snippet in multiple places. So I hope you don't mind.



来源:https://stackoverflow.com/questions/25336661/is-it-possible-to-generate-a-table-of-contents-using-kramdown-in-a-page-other-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!