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:
Joe
2022-07-26 01:23:19 +08:00
committed by GitHub
parent 3343c6b19f
commit 58a1400f55
5 changed files with 109 additions and 198 deletions
@@ -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;
}