theme-default.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. function hashChange() {
  2. const currentItems = document.querySelectorAll('.tocify-subheader.visible, .tocify-item.tocify-focus');
  3. Array.from(currentItems).forEach((elem) => {
  4. elem.classList.remove('visible', 'tocify-focus');
  5. });
  6. const currentTag = document.querySelector(`a[href="${window.location.hash}"]`);
  7. if (currentTag) {
  8. const parent = currentTag.closest('.tocify-subheader');
  9. if (parent) {
  10. parent.classList.add('visible');
  11. }
  12. const siblings = currentTag.closest('.tocify-header');
  13. if (siblings) {
  14. Array.from(siblings.querySelectorAll('.tocify-subheader')).forEach((elem) => {
  15. elem.classList.add('visible');
  16. });
  17. }
  18. currentTag.parentElement.classList.add('tocify-focus');
  19. // wait for dom changes to be done
  20. setTimeout(() => currentTag.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center' }), 1500);
  21. }
  22. }
  23. window.addEventListener('hashchange', hashChange, false);
  24. document.addEventListener('DOMContentLoaded', function() {
  25. const updateHash = function (id) {
  26. window.location.hash = `#${id}`;
  27. };
  28. const throttledUpdateHash = _.throttle(updateHash, 200);
  29. const observer = new IntersectionObserver((entries) => {
  30. // If intersectionRatio is 0, the target is out of view
  31. // and we do not need to do anything.
  32. if (entries[0].intersectionRatio <= 0) {
  33. return;
  34. }
  35. throttledUpdateHash(entries[0].target.id);
  36. }, {
  37. rootMargin: '-8% 0px -8% 0px', // shrink the intersection viewport
  38. threshold: 1.0, // trigger at 100% visibility
  39. });
  40. function makeObserver(elem) {
  41. return observer.observe(elem);
  42. }
  43. const titles = document.querySelectorAll('.content h1, .content h2');
  44. Array.from(titles).forEach(makeObserver);
  45. window.hljs.highlightAll();
  46. // https://jets.js.org/
  47. const wrapper = document.getElementById('toc');
  48. window.jets = new window.Jets({
  49. // *OR - Selects elements whose values contains at least one part of search substring
  50. searchSelector: '*OR',
  51. searchTag: '#input-search',
  52. contentTag: '#toc li',
  53. didSearch: function(term) {
  54. wrapper.classList.toggle('jets-searching', String(term).length > 0)
  55. },
  56. // map these accent keys to plain values
  57. diacriticsMap: {
  58. a: 'ÀÁÂÃÄÅàáâãäåĀāąĄ',
  59. c: 'ÇçćĆčČ',
  60. d: 'đĐďĎ',
  61. e: 'ÈÉÊËèéêëěĚĒēęĘ',
  62. i: 'ÌÍÎÏìíîïĪī',
  63. l: 'łŁ',
  64. n: 'ÑñňŇńŃ',
  65. o: 'ÒÓÔÕÕÖØòóôõöøŌō',
  66. r: 'řŘ',
  67. s: 'ŠšśŚ',
  68. t: 'ťŤ',
  69. u: 'ÙÚÛÜùúûüůŮŪū',
  70. y: 'ŸÿýÝ',
  71. z: 'ŽžżŻźŹ'
  72. }
  73. });
  74. hashChange();
  75. });