hexo控制文章目录是否显示

在 Hexo 的 2.4.1 版本中, tommy351 给 Hexo 增加了 Table of contents 功能, 并提供了相关的使用方法 TOC (Table of contents) helper #408.

使用方法

因为有些文章不一定会有分级标题, 因此在这种时候就没有必要使用文章目录, 所以有必要添加一个控制文章目录的是否显示的post.toc。

例如, 我按照默认主题 light 修改时在 \layout_partial\article.ejs 中如下位置添加

1
2
3
4
5
6
7
8
9
10
<% if(post.toc !== false){ %>
<!-- 文章目录开始 -->
<% if (!index){ %>
<div id="toc" class="toc-article">
<strong class="toc-title" style="cursor:pointer"><%= __('contents') %></strong>
<%- toc(page.content) %>
</div>
<% } %>
<!-- 文章目录结束 -->
<% } %>

这样, 在文章不需要使用文章目录的时候可以在 front-matter 前加上 toc: false 即可.(模板文件位置:hexo\scaffolds\post.md)
例如

1
2
3
4
5
6
title: 启
date: 2014-01-03 21:45:56
tags: Zhishui
category: Leaf
author: 止水糖
toc: false

转自:http://lukang.me/2014/hexo-toc.html