Bluesky for comments on mkdocs blog
Background¶
I have recently migrated this blog from Jekyll to Mkdocs using material theme. I was also planning to move away from Disqus commenting system given all the known issues and just when I had finished the migration to Mkdocs, there was this post from Emily Liu - which showcases how easily the bluesky replies to a post can be added in comments section of a blog page. Followed closely by this, was a post from Cory Zue who created NPM packaging.
I initially was looking to use that on my blog and while searching for that package on jsdelivr, I came across the bluesky-comments-tag: a package created by Matt Kane. I understood the code behind this one much better and that was my starting point. There were two problems with the solution thus far that I wanted to solve.
Problem Description¶
For the comments to appear on the blog, the bluesky post url needs to be added to the frontmatter. Now that is a cyclic process because you can't post the url of your blog post on bluesky until you have published it.
This, I have solved to an acceptable flow as shown below and the "How?" is explained in this post.
Second part of the problem is that while the 'acceptable flow' removes need to republish the blogpost, it still means that unless the author has created a bluesky post, comments are not open for that blog post. I am working on a way to automate that using blusky API but it is still very much work in progress.
Obtain your DID¶
DID is short for Decentralized identifier.
Javascript¶
The required javascript for this code is available in the repo of my blog here. Copy all the content which will be following:
What does this script do?¶
- The function
loadComments
is where bulk of the magic happens. - This function first obtains the URL of the webpage and extracts
pathname
and discards the#
part. - It then creates an assembled URL which concatenates my blog domain with the pathname.
- It then searches on bluesky URL against my DID for the oldest post that has the URL for this blog post on it.
- If it finds one, it passes it further to then get the thread and replies etc.
- If it does not find any post, it checks if the bsky value from frontmatter is a valid bluesky URL and if so it tries using that to obtain thread.
- If neither value was available, it displays
No Comments thread found for this post
under comments section until such time that a post is created on bluesky by the account being used to host comments.
Modify javascript¶
Now, in order to achieve step 2 of algorithm Search on bluesky for the oldest post created by the author containing this URL
I have done some bit of hardcoding. I can do away with it in due course but for the moment, anyone planning to reuse the code, must modify it, specifically the highlighted lines below:
- Change this to your own domain
- Change this to your own
did
.
CSS¶
Make sure you update your extra.css
so the comments match your theme colours. The css rules associated with bluesky comment are ,clearly marked in docs/assets/stylesheets/extra.css
on my repo. However, specifically following rules must be added / adjusted to ensure light and dark theme work well:
There are also some css rules specified in BlueskyComments.js
that can be seen by clicking on this link and will need to be modified depending on the theme in use.
Comments Template¶
Now, my comments template currently has some code that ensures the comments from disqus are displayed if needed but assuming that a typical deployment would only need bsky comments, following code should suffice.
Important
Please do make sure you remove space after first curly bracket in each of the highlighted lines so { %
becomes {%
and { {
becomes {{
. Refer to code in my repo for clarity.
Tip
This ofcourse means that in your frontmatter of the post, there must be an entry for bsky
.
For example, frontmatter of this post is:
Update mkdocs.yml¶
Finally, update the mkdocs.yml
to include the javascript and css files, like so:
extra_css:
- assets/stylesheets/extra.css
extra_javascript:
- assets/js/bluesky-comments.mjs
This should then get the comments thread from bluesky on your mkdocs blog or documentation site. Do let me know if something is not working and I will try to help troubleshoot.