Skip to content

Commit 979d8b8

Browse files
docs: Add new tableOfContent script (#6667)
1 parent 73a32ce commit 979d8b8

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

docs/scripts/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var breadcrumb = require("./breadcrumb")(hexo);
22
var removeContent = require("./removeContent")(hexo);
33
var replaceContent = require("./replaceContent")(hexo);
4+
var tableOfContent = require("./tableOfContent")(hexo);
45

56
hexo.extend.helper.register("breadcrumb", breadcrumb, { async: true });
67
hexo.extend.helper.register("removeContent", removeContent, { async: true });
78
hexo.extend.helper.register("replaceContent", replaceContent, { async: true });
9+
hexo.extend.helper.register("tableOfContent", tableOfContent, { async: true });

docs/scripts/tableOfContent.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { tocObj } = require('hexo-util');
2+
3+
module.exports = function () {
4+
return function tableOfContent(str, options = {}) {
5+
6+
options = Object.assign({
7+
min_depth: 1,
8+
max_depth: 6,
9+
}, options);
10+
11+
const data = tocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth });
12+
13+
14+
if (!data.length) return '';
15+
16+
let html = '';
17+
18+
data.forEach(item => {
19+
html += `<li class="toc-list-level-${item.level}"><a href="#${item.id}">${item.text}</a></li>`
20+
});
21+
22+
return `<ol class="toc-list">${html}</ol>`;
23+
};
24+
};

docs/themes/v2/layout/page.ejs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<% const isEnterpriseTheme = theme.tier === "enterprise" %>
2-
<% const pageContainsHeadings = page.content.includes("<h2") %>
32
<% const tocPageTypes = page.type === "guide" || page.type === "blog" || page.type === "templates" || page.type === "tags" %>
43

5-
<% const showToc = pageContainsHeadings && tocPageTypes %>
6-
74
<% const formattedContent = removeContent(page.content) %>
85
<% const tocContent = replaceContent(formattedContent) %>
6+
<% const pageHasHeading = /<h[1-6]\b[^>]*>/i.test(formattedContent); %>
7+
<% const showToc = tocPageTypes && pageHasHeading %>
98

109
<div class="content-grid">
1110
<div class="content-markdown">
@@ -21,7 +20,7 @@
2120
<% if (showToc) { %>
2221
<div class="toc">
2322
<%- partial("component/text", {text: "In this article", tag: "h3", size: "Eyebrow"}) %>
24-
<%- toc(tocContent, {max_depth: 3, list_number: false, class: "toc-list"}) %>
23+
<%- tableOfContent(tocContent, {max_depth: 3}) %>
2524
</div>
2625
<% } %>
2726
<div class="toc-enterprise-cta">

docs/themes/v2/source/css/styles.css

+4
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,10 @@ ul {
12231223
margin-top: 1em;
12241224
}
12251225

1226+
.toc-list-level-2~.toc-list-level-3 {
1227+
padding-left: 1em;
1228+
}
1229+
12261230
.toc-list-child {
12271231
list-style: none;
12281232
padding-left: 0.825em;

0 commit comments

Comments
 (0)