如果我在Jekyll中有一个带标题的页面/文章,是否可以自动生成导航用的目录/大纲?类似于维基百科文章的上方素材。
这是标记解析器的任务。
对于Markdown语法,其中一个语法扩展定义了“自动生成目录”的功能:Automatic generation of the table of contents:
* This will become a table of contents (this text will be scrapped).
{:toc}
我在我的Jekyll博客上有一个类似于维基百科目录的TOC。
因此,我的所有Jekyll文章都有一个目录部分。只需使用kramdown即可实现。
将此代码放入您的文章中,您希望TOC出现的位置。
* Do not remove this line (it will not be displayed)
{:toc}
并使用这个CSS将其样式设置为类似于维基百科的目录
// Adding 'Contents' headline to the TOC
#markdown-toc::before {
content: "Contents";
font-weight: bold;
}
// Using numbers instead of bullets for listing
#markdown-toc ul {
list-style: decimal;
}
#markdown-toc {
border: 1px solid #aaa;
padding: 1.5em;
list-style: decimal;
display: inline-block;
}
使用适合您博客的合适颜色。
就是这样!
如果上述方法不起作用,也可以使用jekyll-table-of-contents进行目录制作。该方法使用Jquery和一个js文件。
这是我制作目录的详细指南:Jekyll TOC
https://github.com/allejo/jekyll-toc提供了一种非常简单的方法来为您的jekyll页面添加目录。
我猜你是指内容中所有H1、H2元素等的列表?我认为Jekyll没有内置处理这个的功能。我怀疑最简单的方法是让Javascript在页面渲染后为您生成它,使用类似这个jQuery插件或其他许多可用的插件/片段之类的东西。
在您的内容之前,您可以使用code。
<link rel="stylesheet" href="http://yandex.st/highlightjs/6.2/styles/googlecode.min.css">
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="http://yandex.st/highlightjs/6.2/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
$(document).ready(function(){
$("h2,h3,h4,h5,h6").each(function(i,item){
var tag = $(item).get(0).localName;
$(item).attr("id","wow"+i);
$("#category").append('<a class="new'+tag+'" href="#wow'+i+'">'+$(this).text()+'</a></br>');
$(".newh2").css("margin-left",0);
$(".newh3").css("margin-left",20);
$(".newh4").css("margin-left",40);
$(".newh5").css("margin-left",60);
$(".newh6").css("margin-left",80);
});
});
</script>
<div id="category"></div>
{:toc}
是不够的。{:toc}
必须立即放置在列表项之后。 - TachyonVortex