mermaid-init.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // This Source Code Form is subject to the terms of the Mozilla Public
  2. // License, v. 2.0. If a copy of the MPL was not distributed with this
  3. // file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. (() => {
  5. const darkThemes = ['ayu', 'navy', 'coal'];
  6. const lightThemes = ['light', 'rust'];
  7. const classList = document.getElementsByTagName('html')[0].classList;
  8. let lastThemeWasLight = true;
  9. for (const cssClass of classList) {
  10. if (darkThemes.includes(cssClass)) {
  11. lastThemeWasLight = false;
  12. break;
  13. }
  14. }
  15. const theme = lastThemeWasLight ? 'default' : 'dark';
  16. mermaid.initialize({ startOnLoad: true, theme });
  17. // Simplest way to make mermaid re-render the diagrams in the new theme is via refreshing the page
  18. for (const darkTheme of darkThemes) {
  19. document.getElementById('mdbook-theme-' + darkTheme).addEventListener('click', () => {
  20. if (lastThemeWasLight) {
  21. window.location.reload();
  22. }
  23. });
  24. }
  25. for (const lightTheme of lightThemes) {
  26. document.getElementById('mdbook-theme-' + lightTheme).addEventListener('click', () => {
  27. if (!lastThemeWasLight) {
  28. window.location.reload();
  29. }
  30. });
  31. }
  32. })();