杰基尔·克拉姆当 T.O.C。不建设

Jekyll Kramdown T.O.C. not building

这是我想要的最终输出:

<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
{:toc}

不过Markdown is not currently supported in HTML includes or Layout files。 我尝试使用 [Capture and Markdownify}(https://github.com/jekyll/jekyll/issues/6166#issuecomment-322771527) 将上述 TOC 调用添加到布局文件但没有成功

// _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 调用。

// 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 }}

我认为解决这个问题的唯一方法是在 post 的降价文件中包含一些布局标记。

// _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>

这里的缺点是我的 post 中现在有页面标记,它很容易被破坏并且会分散内容编辑的注意力。

有人知道解决这个问题的方法吗?

我发现这个很棒 Ruby Gem jekyll-toc — 它生成一个目录,您可以将其放置在布局文件中的任何位置。

我现在在 _layouts/post.html 中成功使用了以下内容:

<nav aria-label="Table of Contents">
    {{ content | toc_only }}
</nav>

<section itemprop="articleBody">
    {{ content }}
</section>