@jsuites/indexing
Advanced tools
Comparing version 1.4.0 to 1.4.1
106
indexing.js
@@ -1,11 +0,1 @@ | ||
/** | ||
* (c) jSuites Javascript Plugins and Web Components | ||
* | ||
* Website: https://jsuites.net | ||
* Description: Create amazing web based applications. | ||
* Plugin: Indexing | ||
* | ||
* MIT License | ||
*/ | ||
;(function (global, factory) { | ||
@@ -34,11 +24,17 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
let elements = options.source.querySelectorAll('h2,h3'); | ||
// Create elements | ||
let ul = document.createElement('ul'); | ||
let lastUl; | ||
ul.classList.add('indexing'); | ||
elements.forEach(function (v, k) { | ||
if (v.tagName && v.textContent) { | ||
// Create root ul | ||
let rootUl = document.createElement('ul'); | ||
rootUl.classList.add('indexing'); | ||
let currentH2Li = null; | ||
let currentH2Ul = null; | ||
elements.forEach(function(element) { | ||
if (element.tagName && element.textContent) { | ||
let a = document.createElement('a'); | ||
a.textContent = v.textContent.replace('¶', ''); | ||
a.href = '#content-' + v.textContent.toLowerCase().replace('-', ' ').replace(/[^\w\s]/g, '').replace(/\s+/g, '-'); | ||
a.textContent = element.textContent.replace('¶', ''); | ||
a.href = '#content-' + element.textContent.toLowerCase() | ||
.replace('-', ' ') | ||
.replace(/[^\w\s]/g, '') | ||
.replace(/\s+/g, '-'); | ||
@@ -48,11 +44,15 @@ let li = document.createElement('li'); | ||
if (v.tagName === 'H2') { | ||
lastUl = document.createElement('ul'); | ||
ul.appendChild(li); | ||
ul.appendChild(lastUl); | ||
} else { | ||
if (lastUl) { | ||
lastUl.appendChild(li); | ||
if (element.tagName === 'H2') { | ||
// For H2, create a new ul for potential children | ||
currentH2Li = li; | ||
currentH2Ul = document.createElement('ul'); | ||
li.appendChild(currentH2Ul); | ||
rootUl.appendChild(li); | ||
} else if (element.tagName === 'H3') { | ||
// For H3, append to the current H2's ul if it exists | ||
if (currentH2Ul) { | ||
currentH2Ul.appendChild(li); | ||
} else { | ||
ul.appendChild(li); | ||
// If no parent H2 exists, append to root | ||
rootUl.appendChild(li); | ||
} | ||
@@ -63,4 +63,5 @@ } | ||
if (ul.children.length) { | ||
el.appendChild(ul); | ||
// Only append if we have content | ||
if (rootUl.children.length) { | ||
el.appendChild(rootUl); | ||
} | ||
@@ -70,26 +71,27 @@ } | ||
// Items | ||
// Items for scroll tracking | ||
let items = []; | ||
// Get all elements h2 and h3 in the page | ||
let elements = el.querySelectorAll('a'); | ||
// Adding events | ||
elements.forEach(function (v) { | ||
if (v.tagName && v.textContent) { | ||
let link = v.href.split('#'); | ||
let element = document.querySelector('[href="#'+link[1]+'"]'); | ||
let top = element.offsetTop; | ||
let item = { | ||
top: top - offset, | ||
behavior: 'smooth', | ||
element: v, | ||
} | ||
items.push(item); | ||
v.addEventListener('click', function(e) { | ||
window.scrollTo(item); | ||
e.preventDefault(); | ||
e.stopImmediatePropagation(); | ||
return false; | ||
}); | ||
v.setAttribute('href', window.location.pathname + '#' + link[1]); | ||
} | ||
elements.forEach(function(v) { | ||
if (v.tagName && v.textContent) { | ||
let link = v.href.split('#'); | ||
let element = document.querySelector('[href="#' + link[1] + '"]'); | ||
let top = element.offsetTop; | ||
let item = { | ||
top: top - offset, | ||
behavior: 'smooth', | ||
element: v, | ||
} | ||
items.push(item); | ||
v.addEventListener('click', function(e) { | ||
window.scrollTo(item); | ||
e.preventDefault(); | ||
e.stopImmediatePropagation(); | ||
return false; | ||
}); | ||
v.setAttribute('href', window.location.pathname + '#' + link[1]); | ||
} | ||
}); | ||
@@ -110,3 +112,3 @@ | ||
let item = null; | ||
items.forEach(function (v) { | ||
items.forEach(function(v) { | ||
v.element.classList.remove('selected'); | ||
@@ -123,2 +125,2 @@ if (document.documentElement.scrollTop >= v.top - offset) { | ||
} | ||
}))); | ||
}))); |
@@ -22,3 +22,3 @@ { | ||
"main": "indexing.js", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"bugs": "https://github.com/jsuites/jsuites/issues", | ||
@@ -25,0 +25,0 @@ "homepage": "https://github.com/jsuites/jsuites", |
6317
137