mirror of
https://github.com/EGroupware/egroupware.git
synced 2024-12-21 22:22:15 +01:00
054d124afe
Many thanks to all the other developers who made this possible, especially Shoelace
36 lines
861 B
JavaScript
36 lines
861 B
JavaScript
function normalizePathname(pathname) {
|
|
// Remove /index.html
|
|
if (pathname.endsWith('/index.html')) {
|
|
pathname = pathname.replace(/\/index\.html/, '');
|
|
}
|
|
|
|
// Remove trailing slashes
|
|
return pathname.replace(/\/$/, '');
|
|
}
|
|
|
|
/**
|
|
* Adds a class name to links that are currently active.
|
|
*/
|
|
module.exports = function (doc, options) {
|
|
options = {
|
|
className: 'active-link', // the class to add to active links
|
|
pathname: undefined, // the current pathname to compare
|
|
within: 'body', // element containing the target links
|
|
...options
|
|
};
|
|
|
|
const within = doc.querySelector(options.within);
|
|
|
|
if (!within) {
|
|
return doc;
|
|
}
|
|
|
|
within.querySelectorAll('a').forEach(link => {
|
|
if (normalizePathname(options.pathname) === normalizePathname(link.pathname)) {
|
|
link.classList.add(options.className);
|
|
}
|
|
});
|
|
|
|
return doc;
|
|
};
|