Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index headings nested in custom elements #66

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions javascripts/discourse/initializers/disco-toc-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
return;
}

let dTocHeadingSelectors =
":scope > h1, :scope > h2, :scope > h3, :scope > h4, :scope > h5";
const headings = el.querySelectorAll(dTocHeadingSelectors);
const headings = el.querySelectorAll(generateSelectors(settings.TOC_custom_selector));

if (headings.length < settings.TOC_min_heading) {
return;
Expand Down Expand Up @@ -295,6 +293,22 @@
},
};

function generateSelectors(customSelector) {
let rootSelector = ":scope";
let headings = ["h1", "h2", "h3", "h4", "h5"];
let rootSelectors;
let customSelectors;

rootSelectors =

Check failure on line 302 in javascripts/discourse/initializers/disco-toc-main.js

View workflow job for this annotation

GitHub Actions / ci / linting

Trailing spaces not allowed
headings.map(heading => `${rootSelector} > ${heading}`).join(", ");
if(customSelector) {
customSelectors =

Check failure on line 305 in javascripts/discourse/initializers/disco-toc-main.js

View workflow job for this annotation

GitHub Actions / ci / linting

Trailing spaces not allowed
headings.map(heading => `${rootSelector} > ${customSelector} > ${heading}`).join(", ");
}

return `${rootSelectors}, ${customSelectors}`;
}

function parentsUntil(el, selector, filter) {
const result = [];
const matchesSelector =
Expand Down
1 change: 1 addition & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ en:
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: automatically enable TOC on topics in these categories
auto_TOC_tags: automatically enable TOC on topics with these tags
TOC_custom_selector: By default DiscoTOC only indexes headings in the post's root element. Here you can specify an intermediate CSS selector.<br>Ie. `.my-div` will be interpreted as `:root > .my-div > [h1, h2, ...]`.
2 changes: 2 additions & 0 deletions settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ TOC_min_heading:
default: 3
min: 1
max: 10000
TOC_custom_selector:
default: ""
Loading