saicaca / hexo-theme-vivia

A clean and minimalist theme for Hexo.
https://saicaca.github.io/vivia-preview/
MIT License
360 stars 43 forks source link

Add toc to sidebar #36

Closed Movelocity closed 11 months ago

Movelocity commented 11 months ago

增加 toc 目录为 sidebar 的可选组件

Demo: http://139.159.188.217:7900/2023/10/09/hexo/hexo-tricks/

原理

使用 hexo 引擎内置函数 toc(), <%- toc(page.content) %>,位于 node_modeules/hexo/plugins/helper/toc.js。可以简单地在sidebar sticky 部分加入一个 toc,方便看长篇博客的时候快速导航。

改动说明

  1. 修改 _partial/sidebar.ejs,渲染时加入判断,如果是toc就取消cache,防止内部获取不到page变量

  2. 在 css/_partial/sidebar.styl 修改toc侧栏的 <ol> 样式,防止toc渲染过程中出现<ol>自带的数字干扰

  3. 在配置模板s idebar 中增加了toc的配置选项

  4. 增加 toc 对应的简中繁中翻译

saicaca commented 11 months ago

感谢 PR,我可能需要再调整一下样式