2
0

index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const match = /#\/(.+)\\.md(.*)/g.exec(window.location.hash);
  2. if (match && match[1]) {
  3. // Redirect from URL format to new URL, for example:
  4. // Old: https://marked.js.org/#/USING_PRO.md#renderer
  5. // New: https://marked.js.org/using_pro#renderer
  6. const pageName = match[1].toLowerCase();
  7. const sectionName = match[2];
  8. window.location.href = '/' + pageName + sectionName;
  9. }
  10. const navLinks = document.querySelectorAll('nav a');
  11. function hashChange() {
  12. const fullUrl = window.location.href;
  13. navLinks.forEach(function(link) {
  14. link.className = link.href === fullUrl ? 'selected' : '';
  15. });
  16. }
  17. window.addEventListener('hashchange', function(e) {
  18. e.preventDefault();
  19. hashChange();
  20. });
  21. hashChange();
  22. document.addEventListener('DOMContentLoaded', function() {
  23. const div = document.createElement('div');
  24. div.innerHTML = '<div class="tooltip-copy"><img src="/img/copy-icon.svg" class="icon-copy" title="Click to Copy" /></div>';
  25. div.className = 'div-copy';
  26. const allPres = document.querySelectorAll('pre');
  27. allPres.forEach(function(pre) {
  28. let timeout = null;
  29. const copy = div.cloneNode(true);
  30. pre.appendChild(copy);
  31. pre.onmouseover = function() {
  32. copy.classList.add('active');
  33. };
  34. pre.onmouseleave = function() {
  35. clearTimeout(timeout);
  36. copy.classList.remove('active');
  37. copy.classList.remove('click');
  38. };
  39. copy.onclick = function() {
  40. navigator.clipboard.writeText(pre.textContent);
  41. copy.classList.add('click');
  42. clearTimeout(timeout);
  43. timeout = setTimeout(function() {
  44. copy.classList.remove('click');
  45. }, 3000);
  46. };
  47. });
  48. });