From 41741c324517194e90924a94c7001dd258c7ef3f Mon Sep 17 00:00:00 2001 From: Hugues Tennier Date: Sun, 17 Nov 2024 12:35:00 -0500 Subject: [PATCH 1/4] Add new tableOfContent script --- docs/scripts/index.js | 2 ++ docs/scripts/tableOfContent.js | 24 ++++++++++++++++++++++++ docs/themes/v2/layout/page.ejs | 2 +- docs/themes/v2/source/css/styles.css | 4 ++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 docs/scripts/tableOfContent.js diff --git a/docs/scripts/index.js b/docs/scripts/index.js index 64375b22a33c..60cce7314809 100644 --- a/docs/scripts/index.js +++ b/docs/scripts/index.js @@ -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 }); diff --git a/docs/scripts/tableOfContent.js b/docs/scripts/tableOfContent.js new file mode 100644 index 000000000000..343a45f4b909 --- /dev/null +++ b/docs/scripts/tableOfContent.js @@ -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 += `
  • ${item.text}
  • ` + }); + + return `
      ${html}
    `; + }; +}; diff --git a/docs/themes/v2/layout/page.ejs b/docs/themes/v2/layout/page.ejs index 747d3d214b30..31d962c62f65 100644 --- a/docs/themes/v2/layout/page.ejs +++ b/docs/themes/v2/layout/page.ejs @@ -21,7 +21,7 @@ <% if (showToc) { %>
    <%- 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, list_number: false, class: "toc-list"}) %>
    <% } %>
    diff --git a/docs/themes/v2/source/css/styles.css b/docs/themes/v2/source/css/styles.css index 14b699594001..26e9ed88b29b 100644 --- a/docs/themes/v2/source/css/styles.css +++ b/docs/themes/v2/source/css/styles.css @@ -1223,6 +1223,10 @@ ul { margin-top: 1em; } +.toc-list-level-3 { + padding-left: 1em; +} + .toc-list-child { list-style: none; padding-left: 0.825em; From 45777d0db66d83d40bacefea0eff8d4930290429 Mon Sep 17 00:00:00 2001 From: Hugues Tennier Date: Sun, 17 Nov 2024 12:46:16 -0500 Subject: [PATCH 2/4] fix CSS padding --- docs/themes/v2/source/css/styles.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/themes/v2/source/css/styles.css b/docs/themes/v2/source/css/styles.css index 26e9ed88b29b..e166bd56cfde 100644 --- a/docs/themes/v2/source/css/styles.css +++ b/docs/themes/v2/source/css/styles.css @@ -1223,7 +1223,7 @@ ul { margin-top: 1em; } -.toc-list-level-3 { +.toc-list-level-2~.toc-list-level-3 { padding-left: 1em; } From 9942b7ad26a0f15f16aa9f177cd82c3b08777895 Mon Sep 17 00:00:00 2001 From: Hugues Tennier Date: Sun, 17 Nov 2024 13:14:12 -0500 Subject: [PATCH 3/4] show toc even if no h2 --- docs/themes/v2/layout/page.ejs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/themes/v2/layout/page.ejs b/docs/themes/v2/layout/page.ejs index 31d962c62f65..0bab53da86f7 100644 --- a/docs/themes/v2/layout/page.ejs +++ b/docs/themes/v2/layout/page.ejs @@ -1,8 +1,7 @@ <% const isEnterpriseTheme = theme.tier === "enterprise" %> -<% const pageContainsHeadings = page.content.includes(" <% const tocPageTypes = page.type === "guide" || page.type === "blog" || page.type === "templates" || page.type === "tags" %> -<% const showToc = pageContainsHeadings && tocPageTypes %> +<% const showToc = tocPageTypes %> <% const formattedContent = removeContent(page.content) %> <% const tocContent = replaceContent(formattedContent) %> @@ -21,7 +20,7 @@ <% if (showToc) { %>
    <%- partial("component/text", {text: "In this article", tag: "h3", size: "Eyebrow"}) %> - <%- tableOfContent(tocContent, {max_depth: 3, list_number: false, class: "toc-list"}) %> + <%- tableOfContent(tocContent, {max_depth: 3}) %>
    <% } %>
    From 2d331949e9e3b43c112cbbe42c7dda7fc7c77b8c Mon Sep 17 00:00:00 2001 From: Hugues Tennier Date: Mon, 18 Nov 2024 10:30:39 -0500 Subject: [PATCH 4/4] =?UTF-8?q?hide=20toc=20if=20there=E2=80=99s=20no=20he?= =?UTF-8?q?ading=20at=20all?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/themes/v2/layout/page.ejs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/themes/v2/layout/page.ejs b/docs/themes/v2/layout/page.ejs index 0bab53da86f7..3b455616b79d 100644 --- a/docs/themes/v2/layout/page.ejs +++ b/docs/themes/v2/layout/page.ejs @@ -1,10 +1,10 @@ <% const isEnterpriseTheme = theme.tier === "enterprise" %> <% const tocPageTypes = page.type === "guide" || page.type === "blog" || page.type === "templates" || page.type === "tags" %> -<% const showToc = tocPageTypes %> - <% const formattedContent = removeContent(page.content) %> <% const tocContent = replaceContent(formattedContent) %> +<% const pageHasHeading = /]*>/i.test(formattedContent); %> +<% const showToc = tocPageTypes && pageHasHeading %>