Skip to content

Commit cd15b11

Browse files
committed
+blog tutorial engagement part
1 parent 876e7c6 commit cd15b11

File tree

1 file changed

+116
-43
lines changed

1 file changed

+116
-43
lines changed

docs/tutorials/blogs/engage.md

Lines changed: 116 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,17 @@ feedback, learning something, as well as giving readers the opportunity to
194194
discuss the content and the topic it is about.
195195

196196
There are plenty of discussion system out there and you will need to consider
197-
your audience when choosing one appropriate for your blog. In this tutorial,
198-
we will be using [Giscus] because it is free, open source, and uses [GitHub
199-
Discussions] as a backend. Because a lot of users of Material for MkDocs use
200-
GitHub, this seems like an obvious choice.
197+
your audience when choosing one appropriate for your blog. Likewise, you will
198+
also need to consider existing commitments to communication channels. If you
199+
are a heavy user Slack, for example, you may have a string preference for this
200+
system. Consider that when you add a communication channel, you will need to
201+
be prepared to use it regularly and to moderate discussions.
202+
203+
### Giscus integration
204+
205+
In this tutorial, we will be using [Giscus] because it is free, open source,
206+
and uses [GitHub Discussions] as a backend. Because a lot of users of Material
207+
for MkDocs use GitHub, this seems like an obvious choice.
201208

202209
[Giscus]: https://giscus.app/
203210
[GitHub Discussions]: https://docs.github.com/en/discussions
@@ -209,30 +216,30 @@ To add Giscuss to your blog you will need to go through a number of steps:
209216
3. Configure the code needed to embed Giscus into your blog
210217
4. Add the code to your MkDocs project
211218

212-
!!! example "Create a GitHub repository"
219+
[Giscus app]: https://github.com/apps/giscus
213220

214-
You may want to create a test repository for this tutorial that you an
215-
scap later on. The instructions below assume that you are user "example"
216-
and that you create a repository "giscus-test". The repository will need
217-
to be public for people to be able to use the discussions.
221+
You may want to create a test repository for this tutorial that you can
222+
scrap later on. The instructions below assume that you are user "example"
223+
and that you create a repository "giscus-test." The repository will need
224+
to be public for people to be able to use the discussions.
218225

219-
In the instructions given, you will need to replace at least the username
220-
but also the repository name if you chose another name such as when you
221-
want to work directly on an existing repository.
226+
In the instructions given below, you will need to replace at least the username
227+
but also the repository name if you chose another name such as when you
228+
want to work directly on an existing repository.
222229

223230
!!! example "Turn on discussions and install the Giscus app"
224231

225-
Once the repository is set up, go to its settings page and find the
226-
`Features` section. Tick the checkbox for `Discussions`. You will see that
227-
`Discussions` appears in the top navigation for the repository. If you
228-
are using a live repository then you may want to add some minimal content
229-
to the dicussions section at this point and come back to the tutorial.
232+
Once the repository is set up, go to its settings page and find
233+
`Features` in the `General` section. Tick the checkbox for `Discussions`.
234+
You will see that `Discussions` appears in the top navigation for the
235+
repository. If you are using a live repository then you may want to add some
236+
minimal content to the dicussions section at this point and come back to the
237+
tutorial.
230238

231-
Next, you need to [install the Giscus app] by following the link in this
239+
Next, you need to install the [Giscus app] by following the link in this
232240
sentence, and choosing `Install`, then following the instructions to choose
233241
where the Giscus app is to be installed:
234242

235-
236243
1. Choose the account or organization for the repository you want to use.
237244
2. Choose to install only on select repositories and select the one you
238245
want to use. Note that you can choose more than one repository here.
@@ -241,43 +248,109 @@ To add Giscuss to your blog you will need to go through a number of steps:
241248
4. You will end up on the `Applications` page in your settings, where you
242249
can control the Gicsus app and uninstall it if so desired.
243250

251+
That is all the preparation you will need for the repository. Next, it is time
252+
to generate a piece of code that embeds Giscuss in your site. The resulting code
253+
snippet will look something like this:
254+
255+
```html
256+
<script
257+
src="https://giscus.app/client.js"
258+
data-repo="<username>/<repository>"
259+
data-repo-id="..."
260+
data-category="..."
261+
data-category-id="..."
262+
data-mapping="pathname"
263+
data-reactions-enabled="1"
264+
data-emit-metadata="1"
265+
data-theme="preferred_color_scheme"
266+
data-lang="en"
267+
crossorigin="anonymous"
268+
async
269+
>
270+
</script>
271+
```
272+
244273
!!! example "Configure the code needed to embed Giscus into your blog"
245274

246-
Now it is time to go back to the [Giscus homepage] and configure the
247-
embedding code. There are a number of settings:
275+
Go to the [Giscus homepage] and configure the embedding code. There are a
276+
number of settings:
248277

249278
1. Choose the language
250279
2. Enter the username / organization name and repository name
251280
3. Choose how the discussions are to be mapped to the page on your blog.
252281
Because for a blog post the title is the basis of the URL, it makes
253282
sense to use the `Discussion title contains page <title>` option.
254283
4. Under `Discussion Category` choose `Announcements` to limit the creation
255-
of new
256-
284+
of new discussions to Giscuss and people with maintainer or admin
285+
permissions.
257286
5. Under `Features`, select the following:
258-
1. Enable reactions for the main post
259-
2. Emit discussion metadata
260-
3.
287+
1. Enable reactions for the main post
288+
2. Emit discussion metadata
289+
3. Place the comment box above the comments
290+
6. Under `Theme`, select `Preferred color scheme` so that Giscus matches
291+
the color scheme selected by the user for your site.
292+
293+
[Giscus homepage]: https://giscus.app/
261294

295+
With these settings in place, you now need to integrate the code into your
296+
site. There is a partial `partials/comments.html` that exists for this purpose
297+
and is empty be default. It is included by the `content.html` partial, so will
298+
be included for every page on your site. You may or may not want this. In this
299+
tutorial, you will limit the Giscus integration to only blog posts but it is
300+
easy enough to leave out the code that achieves this if you want to have Giscus
301+
discussions active for every page.
302+
303+
!!! example "Add Giscus integration code"
304+
305+
First, you need to create an `overrides` directory that will contain the
306+
templates and partials you want to override.
307+
308+
```
309+
mkdir -p overrides/partials
310+
```
262311

263-
The resulting code snippet should look like this:
312+
You need to declare it in your `mkdocs.yaml`:
313+
314+
```yaml hl_lines="3"
315+
theme:
316+
name: material
317+
custom_dir: overrides
318+
```
319+
320+
Now add a file `overrides/partials/comments.html` and paste in the code
321+
snippet you obtained from the Giscus homepage. Look at the result locally
322+
and you will see that the integration is active on all pages of the site.
323+
If you want to restrict it to your blog posts, you need to add a conditional
324+
around the Giscus script that tests if comments should be included. A simple
325+
way of doing this is to test for a metadata flag:
326+
327+
```html
328+
{% if page.meta.comments %}
329+
<script>...</script>
330+
{% endif %}
331+
```
332+
333+
The disadvantage is that you now need to manually turn on comments for each
334+
blog post - unless you want to turn them off on some. To get the comments
335+
section on all blog posts, use code like this:
264336

265337
```html
266-
<script
267-
src="https://giscus.app/client.js"
268-
data-repo="<username>/<repository>"
269-
data-repo-id="..."
270-
data-category="..."
271-
data-category-id="..."
272-
data-mapping="pathname"
273-
data-reactions-enabled="1"
274-
data-emit-metadata="1"
275-
data-theme="preferred_color_scheme"
276-
data-lang="en"
277-
crossorigin="anonymous"
278-
async
279-
>
280-
</script>
338+
{% if page.file.src_uri.startswith('blog/posts') %}
339+
<script>...</script>
340+
{% endif %}
281341
```
282342

283-
[Giscus homepage]: https://giscus.app/
343+
You should see now that the Giscus comments are added at the bottom of your
344+
blog posts but not on other pages.
345+
346+
## What's next?
347+
348+
This is the end of the blog tutorial. We hope you have enjoyed it and manage to
349+
set up your blog the way you like it. There are numerous other features and
350+
options that we have not been able to cover here. The [blog plugin reference]
351+
provides comprehensive documentation for the plugin. You may also want to
352+
look at the [social plugin tutorial] to generate social cards for your blog
353+
posts that get displayed when you post links to social media systems.
354+
355+
[blog plugin reference]: https://squidfunk.github.io/mkdocs-material/plugins/blog/
356+
[social plugin tutorial]: ../social.md

0 commit comments

Comments
 (0)