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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 109 additions and 198 deletions

View File

@ -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;
}

View File

@ -1,4 +1,9 @@
en:
table_of_contents: "table of contents"
insert_table_of_contents: "Insert table of contents"
post_bottom_tooltip: "Navigate to post controls"
table_of_contents: table of contents
insert_table_of_contents: Insert table of contents
post_bottom_tooltip: Navigate to post controls
theme_metadata:
settings:
minimum_trust_level_to_create_TOC: The minimum trust level a user must have in order to see the TOC button in the composer
auto_TOC_categories: automaticlly enable TOC on topics in these categories
auto_TOC_tags: automaticlly enable TOC on topics with these tags

View File

@ -1,4 +1,7 @@
zh_CN:
table_of_contents: "目录"
insert_table_of_contents: "插入目录"
post_bottom_tooltip: "帖子控制"
table_of_contents: 目录
insert_table_of_contents: 插入目录
post_bottom_tooltip: 帖子控制
theme_metadata:
settings:
minimum_trust_level_to_create_TOC: 能够在编辑器中显示 TOC 按钮的最低信任等级

View File

@ -6,12 +6,17 @@ minimum_trust_level_to_create_TOC:
- 2
- 3
- 4
description:
en: "The minimum trust level a user must have in order to see the TOC button in the composer"
zh_CN: "能够在编辑器中显示 TOC 按钮的最低信任等级"
composer_toc_text:
default: "This topic will contain a table of contents"
table_of_contents_icon:
default: "align-left"
anchor_icon:
default: "hashtag"
auto_TOC_categories:
type: list
list_type: category
default: ""
auto_TOC_tags:
type: list
list_type: tag
default: ""

File diff suppressed because one or more lines are too long