|
| 1 | +/** |
| 2 | + * Add anchor links to any H2 - H6 within the <main> element that: |
| 3 | + * - have an id |
| 4 | + * - are not children of a <nav> element |
| 5 | + * - do not have an ancestor with the data-anchor="no" attribute, with |
| 6 | + * the `hidden` attribute or with the .visuallyhidden class |
| 7 | + * - do not themselves have the data-anchor="no" attribute, the `hidden` |
| 8 | + * attribute or the .visuallyhidden class |
| 9 | + * |
| 10 | + * Generate the anchor using the existing heading ID value. |
| 11 | + */ |
| 12 | + |
| 13 | +import {translate} from './translations'; |
| 14 | + |
| 15 | +let headingAnchors = function () { |
| 16 | + |
| 17 | + let languageCode = document.documentElement.lang; |
| 18 | + |
| 19 | + // Only add heading anchor links on "full" sites |
| 20 | + if (languageCode === 'en' || languageCode === 'ja' || languageCode === 'zh-hans') { |
| 21 | + |
| 22 | + let headingsArray= Array.from(document.querySelectorAll('main h2[id], main h3[id], main h4[id], main h5[id], main h6[id]')); |
| 23 | + |
| 24 | + if (headingsArray.length > 0) { |
| 25 | + |
| 26 | + // Filter out headings that: |
| 27 | + // - Are not children of <nav> |
| 28 | + // - Do not have an ancestor with the data-anchor="no" attribute |
| 29 | + // - Do not have an ancestor with the `hidden` attribute |
| 30 | + // - Do not have an ancestor with the `.visuallyhidden` class |
| 31 | + // - Do not themselves have the data-anchor="no" attribute |
| 32 | + // - Do not themselves have the `hidden` attribute |
| 33 | + // - Do not themselves have the `.visuallyhidden` class |
| 34 | + let targetedHeadings = headingsArray.filter(function(heading) { |
| 35 | + let insideNav = heading.closest('nav') !== null; |
| 36 | + let parentHasDataAttribute = heading.closest('[data-anchor="no"]') !== null; |
| 37 | + let hiddenParent = heading.closest('[hidden]') !== null; |
| 38 | + let visuallyhiddenParent = heading.closest('.visuallyhidden') !== null; |
| 39 | + let hasDataAttribute = heading.getAttribute('data-anchor') === 'no'; |
| 40 | + let isHidden = heading.getAttribute('hidden'); |
| 41 | + let isVisuallyhidden = heading.classList.contains('visuallyhidden'); |
| 42 | + |
| 43 | + return !insideNav && !parentHasDataAttribute && !hiddenParent && !visuallyhiddenParent && !hasDataAttribute && !isHidden && !isVisuallyhidden; |
| 44 | + }); |
| 45 | + |
| 46 | + if (targetedHeadings.length > 0) { |
| 47 | + targetedHeadings.forEach(function(heading) { |
| 48 | + |
| 49 | + let anchor = document.createElement('a'); |
| 50 | + |
| 51 | + anchor.setAttribute('href', '#' + heading.id); |
| 52 | + anchor.setAttribute('class', 'heading-anchor'); |
| 53 | + anchor.innerHTML = '<span aria-hidden="true">§</span>' |
| 54 | + +'<span class="visuallyhidden">' |
| 55 | + + translate.translate('anchor', languageCode) + '</span>'; |
| 56 | + heading.append('\xa0'); |
| 57 | + heading.appendChild(anchor); |
| 58 | + |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +export {headingAnchors}; |
0 commit comments