mirror of
https://github.com/discourse/DiscoTOC.git
synced 2026-11-01 01:48:38 -05:00
Add support for headings inside wrap blocks (#133)
Modified processPostContent to find headings in two locations: 1. Direct descendants of body (existing behavior) 2. Inside .wrap blocks (new - for email filtering via [wrap=no-email]) This allows table of contents to include headings that are wrapped for email filtering purposes, which is a common use case in Discourse. The headings are combined and sorted by document order to maintain proper structure in the TOC.
This commit is contained in:
@@ -77,10 +77,21 @@ export default class TocProcessor extends Service {
|
||||
if (this.containsHeadings(content)) {
|
||||
const parsedPost = new DOMParser().parseFromString(content, "text/html");
|
||||
|
||||
// direct descendants to avoid picking up headings in quotes
|
||||
const headings = parsedPost.querySelectorAll(
|
||||
"body > h1,body > h2,body > h3,body > h4,body > h5"
|
||||
);
|
||||
// Find headings that are either:
|
||||
// 1. Direct descendants of body (to avoid picking up headings in quotes)
|
||||
// 2. Inside wrap blocks (which are used for email filtering with [wrap=no-email])
|
||||
const selector =
|
||||
"body > :is(h1, h2, h3, h4, h5), body > :is(.wrap, .d-wrap) :is(h1, h2, h3, h4, h5)";
|
||||
|
||||
const allHeadings = parsedPost.querySelectorAll(selector);
|
||||
|
||||
// Convert NodeList to Array and sort by document order
|
||||
const headings = Array.from(allHeadings).sort((a, b) => {
|
||||
// eslint-disable-next-line no-bitwise
|
||||
return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING
|
||||
? -1
|
||||
: 1;
|
||||
});
|
||||
|
||||
if (headings.length < settings.TOC_min_heading) {
|
||||
this.setOverlayVisible(false);
|
||||
|
||||
Reference in New Issue
Block a user