Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add new tableOfContent script #6667

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var breadcrumb = require("./breadcrumb")(hexo);
var removeContent = require("./removeContent")(hexo);
var replaceContent = require("./replaceContent")(hexo);
var tableOfContent = require("./tableOfContent")(hexo);

hexo.extend.helper.register("breadcrumb", breadcrumb, { async: true });
hexo.extend.helper.register("removeContent", removeContent, { async: true });
hexo.extend.helper.register("replaceContent", replaceContent, { async: true });
hexo.extend.helper.register("tableOfContent", tableOfContent, { async: true });
24 changes: 24 additions & 0 deletions docs/scripts/tableOfContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { tocObj } = require('hexo-util');

module.exports = function () {
return function tableOfContent(str, options = {}) {

options = Object.assign({
min_depth: 1,
max_depth: 6,
}, options);

const data = tocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth });


if (!data.length) return '';

let html = '';

data.forEach(item => {
html += `<li class="toc-list-level-${item.level}"><a href="#${item.id}">${item.text}</a></li>`
});

return `<ol class="toc-list">${html}</ol>`;
};
};
7 changes: 3 additions & 4 deletions docs/themes/v2/layout/page.ejs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<% const isEnterpriseTheme = theme.tier === "enterprise" %>
<% const pageContainsHeadings = page.content.includes("<h2") %>
<% const tocPageTypes = page.type === "guide" || page.type === "blog" || page.type === "templates" || page.type === "tags" %>

<% const showToc = pageContainsHeadings && tocPageTypes %>

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

<div class="content-grid">
<div class="content-markdown">
Expand All @@ -21,7 +20,7 @@
<% if (showToc) { %>
<div class="toc">
<%- partial("component/text", {text: "In this article", tag: "h3", size: "Eyebrow"}) %>
<%- toc(tocContent, {max_depth: 3, list_number: false, class: "toc-list"}) %>
<%- tableOfContent(tocContent, {max_depth: 3}) %>
</div>
<% } %>
<div class="toc-enterprise-cta">
Expand Down
4 changes: 4 additions & 0 deletions docs/themes/v2/source/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,10 @@ ul {
margin-top: 1em;
}

.toc-list-level-2~.toc-list-level-3 {
padding-left: 1em;
}

.toc-list-child {
list-style: none;
padding-left: 0.825em;
Expand Down