jazzy.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Jazzy - https://github.com/realm/jazzy
  2. // Copyright Realm Inc.
  3. // SPDX-License-Identifier: MIT
  4. window.jazzy = {'docset': false}
  5. if (typeof window.dash != 'undefined') {
  6. document.documentElement.className += ' dash'
  7. window.jazzy.docset = true
  8. }
  9. if (navigator.userAgent.match(/xcode/i)) {
  10. document.documentElement.className += ' xcode'
  11. window.jazzy.docset = true
  12. }
  13. function toggleItem($link, $content) {
  14. var animationDuration = 300;
  15. $link.toggleClass('token-open');
  16. $content.slideToggle(animationDuration);
  17. }
  18. function itemLinkToContent($link) {
  19. return $link.parent().parent().next();
  20. }
  21. // On doc load + hash-change, open any targeted item
  22. function openCurrentItemIfClosed() {
  23. if (window.jazzy.docset) {
  24. return;
  25. }
  26. var $link = $(`a[name="${location.hash.substring(1)}"]`).nextAll('.token');
  27. $content = itemLinkToContent($link);
  28. if ($content.is(':hidden')) {
  29. toggleItem($link, $content);
  30. }
  31. }
  32. $(openCurrentItemIfClosed);
  33. $(window).on('hashchange', openCurrentItemIfClosed);
  34. // On item link ('token') click, toggle its discussion
  35. $('.token').on('click', function(event) {
  36. if (window.jazzy.docset) {
  37. return;
  38. }
  39. var $link = $(this);
  40. toggleItem($link, itemLinkToContent($link));
  41. // Keeps the document from jumping to the hash.
  42. var href = $link.attr('href');
  43. if (history.pushState) {
  44. history.pushState({}, '', href);
  45. } else {
  46. location.hash = href;
  47. }
  48. event.preventDefault();
  49. });
  50. // Clicks on links to the current, closed, item need to open the item
  51. $("a:not('.token')").on('click', function() {
  52. if (location == this.href) {
  53. openCurrentItemIfClosed();
  54. }
  55. });
  56. // KaTeX rendering
  57. if ("katex" in window) {
  58. $($('.math').each( (_, element) => {
  59. katex.render(element.textContent, element, {
  60. displayMode: $(element).hasClass('m-block'),
  61. throwOnError: false,
  62. trust: true
  63. });
  64. }))
  65. }