lea*_*vst 5 html ruby tableofcontents jekyll web
我已经建立了一个基于Apache Buildr网站的Jekyll代码的网站.Buildr网站根据textile格式文件中的标题自动为每个页面生成目录.
例如,您使用纺织品标记页面,就像这样标题..
h2(#why). Why are we doing this?
BLah blah balh etc ..
h2(#something). Some other header
BLah blah balh etc ..
Run Code Online (Sandbox Code Playgroud)
然后在默认的HTML中,你有一些代码将内容传递给被调用的东西toc然后你把内容放到后面.例如 ...
<div id='content'>
<h1 id='{{ page.title | downcase | replace(' ', '_') }}'>{{ page.title }}</h1>
{{ content | toc }}
{{ content }}
</div>
Run Code Online (Sandbox Code Playgroud)
在Apache站点上,他们获得了所需的结果(显示toc后跟内容).但是,在我的网站上,内容呈现两次.没有生成目录.
此外,如果我直接从github克隆Apache Buildr项目并jekyll --server在该doc项目的文件夹中运行,那么也不会生成目录.
我错过了什么?
我向 Builder 开发者邮件列表发送了电子邮件,有人告诉我到这里寻找灵感。原来相关的代码片段是......
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)
Run Code Online (Sandbox Code Playgroud)
在站点的源文件夹中创建一个名为 的文件夹_plugins,然后将此代码粘贴到TocFilter.rb该文件夹中名为 的文件中。
有用!!
| 归档时间: |
|
| 查看次数: |
3690 次 |
| 最近记录: |