From 55f83a913c0ddcb42d90845e14c97877799b12ad Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 4 Nov 2022 09:41:16 +0530 Subject: [PATCH] FEATURE: new theme setting to disable TOC in posts with less headings. (#47) The theme setting `TOC_min_heading` will decide whether the TOC should display or not based on the number of headings available in the post. --- .../discourse/initializers/disco-toc-main.js | 2 +- settings.yml | 4 +++ test/acceptance/toc-test.js | 25 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/javascripts/discourse/initializers/disco-toc-main.js b/javascripts/discourse/initializers/disco-toc-main.js index 13fd7ce..e9a47f3 100644 --- a/javascripts/discourse/initializers/disco-toc-main.js +++ b/javascripts/discourse/initializers/disco-toc-main.js @@ -41,7 +41,7 @@ export default { ":scope > h1, :scope > h2, :scope > h3, :scope > h4, :scope > h5"; const headings = el.querySelectorAll(dTocHeadingSelectors); - if (headings.length < 1) { + if (headings.length < settings.TOC_min_heading) { return; } diff --git a/settings.yml b/settings.yml index 4ca834a..2374341 100644 --- a/settings.yml +++ b/settings.yml @@ -20,3 +20,7 @@ auto_TOC_tags: type: list list_type: tag default: "" +TOC_min_heading: + default: 3 + min: 1 + max: 10000 diff --git a/test/acceptance/toc-test.js b/test/acceptance/toc-test.js index 0b38e69..17d5e62 100644 --- a/test/acceptance/toc-test.js +++ b/test/acceptance/toc-test.js @@ -125,6 +125,7 @@ acceptance("DiscoTOC - with categories", function (needs) { acceptance("DiscoTOC - non-text headings", function (needs) { needs.pretender((server, helper) => { + settings.TOC_min_heading = 1; const topicResponse = cloneJSON(topicFixtures["/t/280/1.json"]); topicResponse.post_stream.posts[0].cooked = `

@@ -150,3 +151,27 @@ acceptance("DiscoTOC - non-text headings", function (needs) { ); }); }); + +acceptance("DiscoTOC - setting TOC_min_heading", function (needs) { + needs.pretender((server, helper) => { + settings.TOC_min_heading = 3; + const topicResponse = cloneJSON(topicFixtures["/t/280/1.json"]); + topicResponse.post_stream.posts[0].cooked = + '

\n帖子控制

\nWelcome' + + TOC_MARKUP; + + server.get("/t/280.json", () => helper.response(topicResponse)); + server.get("/t/280/:post_number.json", () => + helper.response(topicResponse) + ); + }); + + test("hiding TOC element", async function (assert) { + await visit("/t/internationalization-localization/280"); + + assert.notOk( + exists(".d-toc-timeline-visible .d-toc-main"), + "TOC element not visible" + ); + }); +});