From e6bcb8100f269f5e2aa0d19dd82a40947ab29b8d Mon Sep 17 00:00:00 2001 From: squidfunk Date: Sun, 23 Jan 2022 12:33:35 +0100 Subject: [PATCH] Updated Giscus example to support theme change without reload --- docs/setup/adding-a-comment-system.md | 12 +++++++++++- material/overrides/blog.html | 2 +- src/overrides/blog.html | 12 +++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/docs/setup/adding-a-comment-system.md b/docs/setup/adding-a-comment-system.md index a86ade118..3bfa77838 100644 --- a/docs/setup/adding-a-comment-system.md +++ b/docs/setup/adding-a-comment-system.md @@ -74,7 +74,17 @@ calling the `super()` function at the beginning of the block: document.addEventListener("DOMContentLoaded", function() { var ref = document.querySelector("[data-md-component=palette]") ref.addEventListener("change", function() { - location.reload() + var palette = __md_get("__palette") + if (palette && typeof palette.color === "object") { + var theme = palette.color.scheme === "slate" ? "dark" : "light" + + /* Instruct Giscus to change theme */ + var frame = document.querySelector(".giscus-frame") + frame.contentWindow.postMessage( + { giscus: { setConfig: { theme } } }, + "https://giscus.app" + ) + } }) }) diff --git a/material/overrides/blog.html b/material/overrides/blog.html index 4b177e7e0..ae59b4cb7 100644 --- a/material/overrides/blog.html +++ b/material/overrides/blog.html @@ -6,5 +6,5 @@ {{ super() }}

{{ lang.t("meta.comments") }}

- + {% endblock %} diff --git a/src/overrides/blog.html b/src/overrides/blog.html index 32987668f..240090520 100644 --- a/src/overrides/blog.html +++ b/src/overrides/blog.html @@ -57,7 +57,17 @@ document.addEventListener("DOMContentLoaded", function() { var ref = document.querySelector("[data-md-component=palette]") ref.addEventListener("change", function() { - location.reload() + var palette = __md_get("__palette") + if (palette && typeof palette.color === "object") { + var theme = palette.color.scheme === "slate" ? "dark" : "light" + + /* Instruct Giscus to change theme */ + var frame = document.querySelector(".giscus-frame") + frame.contentWindow.postMessage( + { giscus: { setConfig: { theme } } }, + "https://giscus.app" + ) + } }) })