2020-07-22 12:57:41 +03:00
|
|
|
# Adding a comment system
|
|
|
|
|
2021-11-13 18:07:06 +03:00
|
|
|
Material for MkDocs allows to easily add the third-party comment system of your
|
2022-01-16 14:11:24 +03:00
|
|
|
choice to the footer of any page by using [theme extension]. As an example,
|
|
|
|
we'll be integrating [Giscus], which is Open Source, free, and uses GitHub
|
|
|
|
discussions as a backend.
|
2020-07-22 12:57:41 +03:00
|
|
|
|
2022-01-16 14:11:24 +03:00
|
|
|
[Giscus]: https://giscus.app/
|
2020-07-22 12:57:41 +03:00
|
|
|
|
2021-11-13 18:07:06 +03:00
|
|
|
## Customization
|
2020-07-22 12:57:41 +03:00
|
|
|
|
2022-01-16 14:11:24 +03:00
|
|
|
### Giscus integration
|
2021-11-13 18:07:06 +03:00
|
|
|
|
2022-01-16 14:11:24 +03:00
|
|
|
Before you can use [Giscus], you need to complete the following steps:
|
2021-11-13 18:07:06 +03:00
|
|
|
|
2022-01-16 14:11:24 +03:00
|
|
|
1. __Install the [Giscus GitHub App]__ and grant access to the repository
|
|
|
|
that should host comments as GitHub discussions. Note that this can be a
|
|
|
|
repository different from your documentation.
|
|
|
|
2. __Visit [Giscus] and generate the snippet__ through their configuration tool
|
|
|
|
to load the comment system. Copy the snippet for the next step. The
|
|
|
|
resulting snippet should look similar to this:
|
2021-11-13 18:07:06 +03:00
|
|
|
|
|
|
|
``` html
|
2022-01-16 14:11:24 +03:00
|
|
|
<script
|
|
|
|
src="https://giscus.app/client.js"
|
|
|
|
data-repo="<username>/<repository>"
|
|
|
|
data-repo-id="..."
|
|
|
|
data-category="..."
|
|
|
|
data-category-id="..."
|
|
|
|
data-mapping="pathname"
|
|
|
|
data-reactions-enabled="1"
|
|
|
|
data-emit-metadata="1"
|
|
|
|
data-theme="light"
|
|
|
|
data-lang="en"
|
|
|
|
crossorigin="anonymous"
|
|
|
|
async
|
|
|
|
>
|
|
|
|
</script>
|
2021-11-13 18:07:06 +03:00
|
|
|
```
|
2020-07-22 14:37:14 +03:00
|
|
|
|
2022-10-02 17:36:47 +03:00
|
|
|
The [`comments.html`][comments] partial (empty by default) is the best place to
|
|
|
|
add the snippet generated by [Giscus]. Follow the guide on [theme extension]
|
2022-09-11 20:25:40 +03:00
|
|
|
and [override the `comments.html` partial][overriding partials] with:
|
2022-01-16 14:11:24 +03:00
|
|
|
|
2022-09-11 20:25:40 +03:00
|
|
|
``` html hl_lines="3"
|
|
|
|
{% if page.meta.comments %}
|
2022-01-16 14:11:24 +03:00
|
|
|
<h2 id="__comments">{{ lang.t("meta.comments") }}</h2>
|
2022-10-02 17:36:47 +03:00
|
|
|
<!-- Insert generated snippet here -->
|
2022-01-16 14:11:24 +03:00
|
|
|
|
2022-06-26 12:19:57 +03:00
|
|
|
<!-- Synchronize Giscus theme with palette -->
|
2022-01-16 14:11:24 +03:00
|
|
|
<script>
|
2022-06-26 12:19:57 +03:00
|
|
|
var giscus = document.querySelector("script[src*=giscus]")
|
|
|
|
|
|
|
|
/* Set palette on initial load */
|
2022-01-16 14:11:24 +03:00
|
|
|
var palette = __md_get("__palette")
|
2022-06-26 11:43:33 +03:00
|
|
|
if (palette && typeof palette.color === "object") {
|
2022-06-26 12:19:57 +03:00
|
|
|
var theme = palette.color.scheme === "slate" ? "dark" : "light"
|
|
|
|
giscus.setAttribute("data-theme", theme) // (1)!
|
2022-06-26 11:43:33 +03:00
|
|
|
}
|
2022-09-11 20:25:40 +03:00
|
|
|
|
2022-01-16 14:11:24 +03:00
|
|
|
/* Register event handlers after documented loaded */
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
|
|
var ref = document.querySelector("[data-md-component=palette]")
|
2022-01-16 14:34:51 +03:00
|
|
|
ref.addEventListener("change", function() {
|
2022-01-23 14:33:35 +03:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
}
|
2022-01-16 14:11:24 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
</script>
|
2022-09-11 20:25:40 +03:00
|
|
|
{% endif %}
|
2022-01-16 14:11:24 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
1. This code block ensures that [Giscus] renders with a dark theme when the
|
|
|
|
palette is set to `slate`. Note that multiple dark themes are available,
|
|
|
|
so you can change it to your liking.
|
|
|
|
|
|
|
|
Replace the highlighted line with the snippet you generated with the [Giscus]
|
2022-09-11 20:25:40 +03:00
|
|
|
configuration tool in the previous step. If you copied the snippet from above,
|
|
|
|
you can enable comments on a page by setting the `comments` front matter
|
|
|
|
property to `true`:
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
---
|
|
|
|
comments: true
|
|
|
|
---
|
|
|
|
|
2023-09-14 20:09:18 +03:00
|
|
|
# Page title
|
2022-09-11 20:25:40 +03:00
|
|
|
...
|
|
|
|
```
|
|
|
|
|
|
|
|
If you wish to enable comments for an entire folder, you can use the
|
|
|
|
[built-in meta plugin].
|
2022-01-16 14:11:24 +03:00
|
|
|
|
|
|
|
[Giscus GitHub App]: https://github.com/apps/giscus
|
2021-11-13 18:07:06 +03:00
|
|
|
[theme extension]: ../customization.md#extending-the-theme
|
2022-09-11 20:25:40 +03:00
|
|
|
[comments]: https://github.com/squidfunk/mkdocs-material/blob/master/src/partials/comments.html
|
|
|
|
[overriding partials]: ../customization.md#overriding-partials
|
|
|
|
[built-in meta plugin]: ../reference/index.md#built-in-meta-plugin
|