title: "让 Hexo 自动生成 Tag Cloud 标签云页面" date: 2015-10-25 12:08:49 categories: - 术业专攻 tags: - Hexo - TagCloud toc: comments: original: permalink: hexo-tag-cloud ---

前言

参看上篇 [创建404页面][1] 的文章,我们可以轻易地创建一个链接为 `http://"主页"/tags/` 的页面,然后编辑对应 .md 文件设置样式。在此基础上,我们还需要在主题 layout 布局中添加代码,引入辅助函数,才能自动生成所需的标签云页面。下面简单介绍下实现过程。 [1]: /2015/10/16/hexo-404-page/ ![tag-cloud](/resources/tag-cloud.png) ## 匹配页面 在主题文章的 layout 中合适位置插入下列 if 语句,让代码只在标签云页面生效。 ``` actionscript <% if (page.path === "tags/index.html"){ %> <引入 tags 页面的代码> <% } %> ``` 以主题 [Yelee][2] 为例,须在 `/layout/_partial/article.ejs`, div `article-entry` 中插入代码。 ``` actionscript
<% if (post.excerpt && index){ %> <%- post.excerpt %> <% } else { %> <% if (page.path === "tags/index.html"){ %> <引入 tags 页面的代码> <% } %> <%- post.content %> <% } %>
``` ## 插入标签云 使用 Hexo 辅助函数 `<%- tagcloud([tags], [options]) %>` 插入标签云,参数详解请查看 [官方文档][3]。 [2]: https://github.com/MOxFIVE/hexo-theme-yelee "简而不减 Hexo 双栏博客主题" [3]: https://hexo.io/zh-cn/docs/helpers.html#tagcloud "tagcloud: 插入标签云" ``` actionscript <%- tagcloud({ min_font: 16, max_font: 35, amount: 999, color: true, start_color: 'gray', end_color: 'black', }) %> ``` ## 自定义样式 审查元素,按需修改相关样式。下面是目前本博客 [标签云页面](/tags) 所添加的代码,附上供参考。 {% codeblock /layout/_partial/article.ejs lang:erb https://github.com/MOxFIVE/hexo-theme-yelee/commit/3a82bb65b325826b3f71cae10e39e5314ccb63cb tag cloud page %} <% if (page.path === "tags/index.html"){ %>

<%- list_categories({ depth: 1, }) %>
<%- tagcloud({ min_font: 16, max_font: 35, amount: 999, color: true, start_color: 'gray', end_color: 'black', }) %>
<% } %> {% endcodeblock %} >知道怎么在特定页面中引入 Hexo 自带的函数和变量后,大家应该就可以比较自在地在文章中引入需要的内容了,标签列表,文章列表,文章地址,日期时间等都可以。 ## 参考资料 1. ***tagcloud: 插入标签云*** by **Hexo** on 2015: 1. ***变量使用*** by **Hexo** on 2015: 1. ***feat: tags cloud page 标签云页面*** by **MOxFIVE** on 2015/10/12: 1. ***统计分类及标签数*** by **MOxFIVE** on 2015/12/1: