Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

giscus.js 2.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  1. // Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. // Giscus functionality
  3. function loadGiscus() {
  4. const giscusContainer = document.getElementById("giscus-container");
  5. if (!giscusContainer || giscusContainer.querySelector("script")) {
  6. return;
  7. }
  8. const script = document.createElement("script");
  9. script.src = "https://giscus.app/client.js";
  10. script.setAttribute("data-repo", "ultralytics/ultralytics");
  11. script.setAttribute("data-repo-id", "R_kgDOH-jzvQ");
  12. script.setAttribute("data-category", "Docs");
  13. script.setAttribute("data-category-id", "DIC_kwDOH-jzvc4CWLkL");
  14. script.setAttribute("data-mapping", "pathname");
  15. script.setAttribute("data-strict", "1");
  16. script.setAttribute("data-reactions-enabled", "1");
  17. script.setAttribute("data-emit-metadata", "0");
  18. script.setAttribute("data-input-position", "top");
  19. script.setAttribute("data-theme", "preferred_color_scheme");
  20. script.setAttribute("data-lang", "en");
  21. script.setAttribute("data-loading", "lazy");
  22. script.setAttribute("crossorigin", "anonymous");
  23. script.setAttribute("async", "");
  24. giscusContainer.appendChild(script);
  25. // Synchronize Giscus theme with palette
  26. var palette = __md_get("__palette");
  27. if (palette && typeof palette.color === "object") {
  28. var theme = palette.color.scheme === "slate" ? "dark" : "light";
  29. script.setAttribute("data-theme", theme);
  30. }
  31. // Register event handlers for theme changes
  32. var ref = document.querySelector("[data-md-component=palette]");
  33. if (ref) {
  34. ref.addEventListener("change", function () {
  35. var palette = __md_get("__palette");
  36. if (palette && typeof palette.color === "object") {
  37. var theme = palette.color.scheme === "slate" ? "dark" : "light";
  38. // Instruct Giscus to change theme
  39. var frame = document.querySelector(".giscus-frame");
  40. if (frame) {
  41. frame.contentWindow.postMessage(
  42. { giscus: { setConfig: { theme } } },
  43. "https://giscus.app",
  44. );
  45. }
  46. }
  47. });
  48. }
  49. }
  50. // Use Intersection Observer to load Giscus when the container is visible
  51. function setupGiscusLoader() {
  52. const giscusContainer = document.getElementById("giscus-container");
  53. if (giscusContainer) {
  54. const observer = new IntersectionObserver(
  55. (entries) => {
  56. entries.forEach((entry) => {
  57. if (entry.isIntersecting) {
  58. loadGiscus();
  59. observer.unobserve(entry.target);
  60. }
  61. });
  62. },
  63. { threshold: 0.1 },
  64. ); // Trigger when 10% of the element is visible
  65. observer.observe(giscusContainer);
  66. }
  67. }
  68. // Hook into MkDocs' navigation system
  69. if (typeof document$ !== "undefined") {
  70. document$.subscribe(() => {
  71. // This function is called on every page load/change
  72. setupGiscusLoader();
  73. });
  74. } else {
  75. console.warn("MkDocs document$ not found. Falling back to DOMContentLoaded.");
  76. document.addEventListener("DOMContentLoaded", setupGiscusLoader);
  77. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...