问题描述
这是我想要的最终输出:
Here's the final output of what I'd like to have:
<article itemscope itemtype="http://schema.org/BlogPosting">
<header>
<h1>Jekyll Table of Contents with Kramdown
</h1>
</header>
<nav aria-label="Table of Contents">
<ul>
<li>Topic 1</li>
<li>Topic 2</li>
<li>Topic 3</li>
</ul>
<nav>
<section itemprop="articleBody">
<p>the main body of the article</p>
</section>
</article>
使用默认的Jekyll安装,Kramdown可以使用以下方式创建TOC
With a default Jekyll install Kramdown can create a TOC with
* TOC
{:toc}
但是, HTML包含或当前不支持Markdown或布局文件.我尝试使用[Capture and Markdownify}( https://github.com/jekyll/jekyll/issues/6166#issuecomment-322771527 ),将上述TOC调用添加到布局文件失败
However Markdown is not currently supported in HTML includes or Layout files.I tried using [Capture and Markdownify}(https://github.com/jekyll/jekyll/issues/6166#issuecomment-322771527) to add the above TOC call to the layout file without success
// _layouts/post.html
<article>
<header>
<h1>Jekyll Table of Contents with Kramdown
</h1>
</header>
{% capture toc %}{% include toc.md %}{% endcapture %}
{{ toc | markdownify }}
<section itemprop="articleBody">
<p>the main body of the article</p>
</section>
</article>
添加内联markdownify可用于纯markdown,但不适用于Kramdown TOC调用.
Adding a inline markdownify works for plain markdown but not the Kramdown TOC call.
// this works
{% capture md %}
## Heading 2
*Stuff added in my layout*
{% endcapture %}
{{ md | markdownify }}
// This doesn't work
{% capture md %}
* TOC
{:toc}
{% endcapture %}
{{ md | markdownify }}
我看到的唯一方法是在帖子的markdown文件中包含一些布局标记.
The only way I see around this is to include some of the layout markup in the post's markdown file.
// _layouts/post.html
<article>
<header>
<h1>Jekyll Table of Contents with Kramdown
</h1>
</header>
{{ content }}
</article>
// _posts/post.md
---
layout: post
---
<nav aria-label="Table of Contents">
* TOC
{:toc}
</nav>
<section itemprop="articleBody">
## My Heading
Standard markdown content here
</section>
这里的缺点是,我现在在帖子中添加了页面标记,该页面标记很容易损坏,并分散了内容编辑的注意力.
The draw back here is that I've now got page markup in my post that can be easily corrupted and a distraction to content editors.
有人看到这样的方法吗?
Does anybody see a way round this?
推荐答案
我发现了这个出色的Ruby Gem jekyll -toc -它会生成一个TOC,您可以将其放置在布局文件中的任何位置.
I found this excellent Ruby Gem jekyll-toc — it generates a TOC that you can place anywhere in your layout files.
我现在已经在我的_layouts/post.html
中成功使用了以下内容:
I'm now successfully using the following in my _layouts/post.html
:
<nav aria-label="Table of Contents">
{{ content | toc_only }}
</nav>
<section itemprop="articleBody">
{{ content }}
</section>
这篇关于Jekyll Kramdown T.O.C.不建设的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!