问题描述
我已经基于Jekyll代码为 Apache Buildr 建立了一个网站. Buildr网站会根据textile
格式文件中的标题自动为每个页面生成一个目录.
I have built a website based on the Jekyll code for the website for Apache Buildr. The Buildr website automatically generates a table of contents for each page based on the headers in the textile
format files.
例如,您使用纺织品写一个页面,标出了诸如此类的标题. .
For example, you write a page using textile marking out the headings like so . .
h2(#why). Why are we doing this?
BLah blah balh etc ..
h2(#something). Some other header
BLah blah balh etc ..
然后,在默认的HTML中,您将有一些代码将内容输送到称为toc
的内容中,然后将其放在后面.例如...
Then in the default HTML you have some code that pipes the content into something called toc
and then you put the contents afterward. For example ...
<div id='content'>
<h1 id='{{ page.title | downcase | replace(' ', '_') }}'>{{ page.title }}</h1>
{{ content | toc }}
{{ content }}
</div>
在Apache站点上,它们可以得到所需的结果(显示目录并带有目录).但是,在我的网站上,内容被渲染了两次.没有生成目录.
On the Apache site they get the desired results (the toc is shown followed by the contents). However, on my site, the contents are rendered twice. No table of contents is generated.
此外,如果我直接从github克隆Apache Buildr项目并在该项目的doc
文件夹中运行jekyll --server
,那么也不会生成目录.
Furthermore, if I clone the Apache Buildr project directly from github and run jekyll --server
in the doc
folder of that project, then no table of contents is generated either.
我想念什么?
推荐答案
我通过电子邮件发送了Buildr开发人员邮件列表,有人告诉我在此处查找灵感.原来相关的代码段是...
I emailed the Buildr developer mailing list and someone told me to look here for inspiration. Turns out that the relevant code snippet is ...
module TocFilter
def toc(input)
output = "<ol class=\"toc\">"
input.scan(/<(h2)(?:>|\s+(.*?)>)([^<]*)<\/\1\s*>/mi).each do |entry|
id = (entry[1][/^id=(['"])(.*)\1$/, 2] rescue nil)
title = entry[2].gsub(/<(\w*).*?>(.*?)<\/\1\s*>/m, '\2').strip
if id
output << %{<li><a href="##{id}">#{title}</a></li>}
else
output << %{<li>#{title}</li>}
end
end
output << '</ol>'
output
end
end
Liquid::Template.register_filter(TocFilter)
在站点的源文件夹中创建一个名为_plugins
的文件夹,然后将此代码粘贴到该文件夹中名为TocFilter.rb
的文件中.
Make a folder in the source folder of your site called _plugins
and then paste this code into a file called TocFilter.rb
within that folder.
行得通!
这篇关于Jekyll自动目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!