jazzy.js 1.7 KB

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