mirror of
https://github.com/discourse/DiscoTOC.git
synced 2026-09-29 04:44:01 -04:00
FEATURE: Automatically add TOC based on topic category / tags (#42)
This PR adds two settings. One allows admins to select a list of categories that will automatically enable the TOC if the topic category matches. The other does the same but with tags. Note that the topic still needs to have the other requirements for TOC, such as having headings. Also, this will still only apply to the first post. This PR also moves some setting descriptions to the appropriate place. It also uses `pretender` for the tests instead of generating the fixture manually.
This commit is contained in:
@@ -11,6 +11,12 @@ export default {
|
||||
|
||||
initialize() {
|
||||
withPluginApi("1.0.0", (api) => {
|
||||
const autoTocCategoryIds = settings.auto_TOC_categories
|
||||
.split("|")
|
||||
.map((id) => parseInt(id, 10));
|
||||
|
||||
const autoTocTags = settings.auto_TOC_tags.split("|");
|
||||
|
||||
api.decorateCookedElement(
|
||||
(el, helper) => {
|
||||
if (helper) {
|
||||
@@ -19,7 +25,14 @@ export default {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!el.querySelector(`[data-theme-toc="true"]`)) {
|
||||
const topicCategory = helper.getModel().topic.category_id;
|
||||
const topicTags = helper.getModel().topic.tags;
|
||||
|
||||
const hasTOCmarkup = el?.querySelector(`[data-theme-toc="true"]`);
|
||||
const tocCategory = autoTocCategoryIds?.includes(topicCategory);
|
||||
const tocTag = topicTags?.some((tag) => autoTocTags?.includes(tag));
|
||||
|
||||
if (!hasTOCmarkup && !tocCategory && !tocTag) {
|
||||
document.body.classList.remove("d-toc-timeline-visible");
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user