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

Find all subheaders between headers regardless if they are siblings or children #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 25 additions & 38 deletions src/javascripts/jquery.tocify.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@

self.element.addClass(tocClassName);

// find all elements regardless of sibling relationship
var allElem = $(this.options.context).find(this.options.selectors);

// Index of top element in allElem
var topIndex = 0;

// Loops through each top level selector
firstElem.each(function(index) {

Expand All @@ -259,45 +265,26 @@
// Add the created unordered list element to the HTML element calling the plugin
self.element.append(ul);

// Finds all of the HTML tags between the header and subheader elements
$(this).nextUntil(this.nodeName.toLowerCase()).each(function() {

// If there are no nested subheader elemements
if($(this).find(self.options.selectors).length === 0) {

// Loops through all of the subheader elements
$(this).filter(self.options.selectors).each(function() {

//If the element matches the ignoreSelector then we skip it
if($(this).is(ignoreSelector)) {
return;
}

self._appendSubheaders.call(this, self, ul);

});

}

// If there are nested subheader elements
else {

// Loops through all of the subheader elements
$(this).find(self.options.selectors).each(function() {

//If the element matches the ignoreSelector then we skip it
if($(this).is(ignoreSelector)) {
return;
}

self._appendSubheaders.call(this, self, ul);

});

// find the index of current top element in allElem
while (allElem[topIndex] !== this) {
topIndex += 1;
}
// find the index of next top element in allElem (or the end)
var nextIndex = topIndex + 1;
while (nextIndex < allElem.length && allElem[nextIndex].nodeName.toLowerCase() !== allElem[topIndex].nodeName.toLowerCase()) {
nextIndex += 1;
}
// Finds all headers between the two top elements, regardless if they are sibling or children etc
topIndex += 1;
while (topIndex < nextIndex ) {
var elem = allElem[topIndex];

//If the element matches the ignoreSelector then we skip it
if( !$(elem).is(ignoreSelector)) {
self._appendSubheaders.call(elem, self, ul);
}

});

topIndex += 1;
}
});

},
Expand Down