是否可以在帖子以外的页面中使用kramdown生成目录?如果是这样,怎么办? [英] Is it possible to generate a table of contents using kramdown in a page other than a post? If so, how?

查看:92
本文介绍了是否可以在帖子以外的页面中使用kramdown生成目录?如果是这样,怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Jekyll网站上的索引页面的意图完全像是一篇帖子,仅具有一点点代码即可浏览其他帖子.我并不是真的用它来生成博客本身,而是一堆新闻页面.

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.

现在在每个帖子中,我都使用kramdown生成目录,如下所示:

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}

对于所有帖子都可以正常工作,但是当我尝试在帖子以外的任何地方使用它时,它不会生成.输出只是空的.它没有在上面显示原始的kramdown或任何东西,它只是消失了而没有任何东西(尤其是没有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.

对此帖子的一些评论:如何为Jekyll博客文章创建目录?

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

建议在上面的代码之前放置一个<li>,但是我尝试了所有可能的迭代,但没有成功.另一条评论建议带有<h1>标记的东西是必需的.

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.

我意识到有一个插件可以生成目录,非JavaScript版本比使用kramdown实现TOC的版本要老(我在此看到的插件位于:

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 ).

和其他插件( https://github.com/ghiculescu/jekyll-目录)是较新的版本,但我宁愿不要使用javascript来执行kramdown已经可以完成的工作.

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.

在我的情况下,是否可以使用标准的kramdown目录降价表在页面上生成一个而不是帖子?

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?

推荐答案

无法通过kramdown选项执行此操作,也无法在Jekyll页面的{{ content }}中使用{:toc}选项,因为赢得了{:toc}无法使用生成的HTML.

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.

我意识到这是一个比较老的问题,但是对于最近偶然发现此问题的任何人来说.我写了此问题的解决方案,该方法使用了只有Liquid,因此它可以在GitHub页面(不涉及插件)上完美运行,并且禁用了JavaScript.我的解决方案可与Jekyll提供的生成的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.

这篇关于是否可以在帖子以外的页面中使用kramdown生成目录?如果是这样,怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆