github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/doc/themes/docsy/assets/js/anchor.js (about)

     1  /*
     2   * Copyright 2018 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  (function ($) {
    18      'use strict';
    19  
    20      // Headers' anchor link that shows on hover
    21      $(function () {
    22          // append anchor links to headings in markdown.
    23          var article = document.getElementsByTagName('main')[0];
    24          if (!article) {
    25              return;
    26          }
    27          var headings = article.querySelectorAll('h1, h2, h3, h4, h5, h6');
    28          headings.forEach(function (heading) {
    29              if (heading.id) {
    30                  var a = document.createElement('a');
    31                  // set visibility: hidden, not display: none to avoid layout change
    32                  a.style.visibility = 'hidden';
    33                  // [a11y] hide this from screen readers, etc..
    34                  a.setAttribute('aria-hidden', 'true');
    35                  // material insert_link icon in svg format
    36                  a.innerHTML = ' <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/></svg>';
    37                  a.href = '#' + heading.id;
    38                  heading.insertAdjacentElement('beforeend', a);
    39                  heading.addEventListener('mouseenter', function () {
    40                      a.style.visibility = 'initial';
    41                  });
    42                  heading.addEventListener('mouseleave', function () {
    43                      a.style.visibility = 'hidden';
    44                  });
    45              }
    46          });
    47      });
    48  
    49  }(jQuery));