mirror of
https://github.com/EGroupware/egroupware.git
synced 2024-12-22 23:00:56 +01:00
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;
|
||
|
};
|