Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/components/Comments.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
// Comments + reactions via giscus, backed by GitHub Discussions. No database,
// no build dependency — data lives in this repo's Discussions.
//
// One-time setup (repo owner): install the giscus GitHub App and grant it
// access to this repo -> https://github.com/apps/giscus
// (Discussions is already enabled and the IDs below are this repo's.)
const GISCUS = {
repo: "codemug/codemug.github.io",
repoId: "R_kgDOSqw0-w",
category: "Announcements",
categoryId: "DIC_kwDOSqw0-84DCd5X",
};
---

<section class="comments" aria-label="Comments and reactions">
<h2 class="comments-h">Comments &amp; reactions</h2>
<p class="comments-note">
React or leave a comment below — sign in with GitHub. It all lives in this
site's <a
href="https://github.com/codemug/codemug.github.io/discussions"
target="_blank"
rel="noopener noreferrer">GitHub Discussions</a
>.
</p>
<div id="giscus-mount"></div>
</section>

<script is:inline define:vars={{ GISCUS }}>
(function () {
var mount = document.getElementById("giscus-mount");
if (!mount) return;
var theme = function () {
return document.documentElement.getAttribute("data-theme") === "light"
? "light"
: "dark";
};
var s = document.createElement("script");
s.src = "https://giscus.app/client.js";
var attrs = {
"data-repo": GISCUS.repo,
"data-repo-id": GISCUS.repoId,
"data-category": GISCUS.category,
"data-category-id": GISCUS.categoryId,
"data-mapping": "pathname",
"data-strict": "0",
"data-reactions-enabled": "1",
"data-emit-metadata": "0",
"data-input-position": "top",
"data-theme": theme(),
"data-lang": "en",
"data-loading": "lazy",
};
Object.keys(attrs).forEach(function (k) {
s.setAttribute(k, attrs[k]);
});
s.crossOrigin = "anonymous";
s.async = true;
mount.appendChild(s);

// Keep giscus's theme in sync with the site's light/dark toggle.
new MutationObserver(function () {
var f = document.querySelector("iframe.giscus-frame");
if (!f || !f.contentWindow) return;
f.contentWindow.postMessage(
{ giscus: { setConfig: { theme: theme() } } },
"https://giscus.app",
);
}).observe(document.documentElement, {
attributes: true,
attributeFilter: ["data-theme"],
});
})();
</script>

<style>
.comments {
margin-top: 3.5rem;
padding-top: 2rem;
border-top: 1px solid var(--color-hairline);
}
.comments-h {
font-size: 15px;
font-weight: 600;
letter-spacing: -0.01em;
color: var(--color-text);
margin: 0 0 0.35rem;
}
.comments-note {
font-family: var(--font-mono);
font-size: 12.5px;
line-height: 1.6;
color: var(--color-text-dim);
margin: 0 0 1.5rem;
}
.comments-note a {
color: var(--color-text-muted);
text-decoration: underline;
text-underline-offset: 2px;
}
.comments-note a:hover {
color: var(--color-text);
}
</style>
8 changes: 8 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Public site configuration.

// Cloudflare Web Analytics beacon token. Free, privacy-first, no cookies.
// Get it at: dash.cloudflare.com -> Analytics & Logs -> Web Analytics ->
// "Add a site" (use codemug.github.io), then copy the token out of the JS
// snippet it shows (the value of "token"). It is a PUBLIC token (it appears in
// the page HTML), so committing it is fine. Leave empty to disable analytics.
export const CF_BEACON_TOKEN = "28b4ca2200ce4333938cc73bd96e9af2";
2 changes: 2 additions & 0 deletions src/content.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const blog = defineCollection({
// Optional per-post social-card image (absolute path from /public, e.g.
// "/og/my-post.png"). Falls back to the site default.
ogImage: z.string().optional(),
// Show the giscus comments + reactions section. Set false to opt a post out.
comments: z.boolean().default(true),
}),
});

Expand Down
14 changes: 14 additions & 0 deletions src/layouts/Base.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import "../styles/global.css";
import { CF_BEACON_TOKEN } from "../consts";

interface Props {
title: string;
Expand Down Expand Up @@ -199,6 +200,19 @@ const articleJsonLd =
/>
)
}

{/* Cloudflare Web Analytics — privacy-first, no cookies. Renders only when
a beacon token is configured in src/consts.ts. */}
{
CF_BEACON_TOKEN && (
<script
type="module"
is:inline
src="https://static.cloudflareinsights.com/beacon.min.js"
data-cf-beacon={`{"token": "${CF_BEACON_TOKEN}"}`}
/>
)
}
</head>
<body class="min-h-screen flex flex-col">
<!-- fixed ambient field -->
Expand Down
15 changes: 13 additions & 2 deletions src/pages/blog/[...slug].astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import { getCollection, render } from "astro:content";
import Base from "../../layouts/Base.astro";
import Comments from "../../components/Comments.astro";

export async function getStaticPaths() {
// Drafts are hidden in production builds, visible in `astro dev`.
Expand All @@ -15,8 +16,16 @@ export async function getStaticPaths() {

const { post } = Astro.props;
const { Content } = await render(post);
const { title, description, pubDate, updatedDate, tags, canonicalUrl, ogImage } =
post.data;
const {
title,
description,
pubDate,
updatedDate,
tags,
canonicalUrl,
ogImage,
comments,
} = post.data;

const path = `/blog/${post.id}/`;
const fmt = (d: Date) =>
Expand Down Expand Up @@ -88,5 +97,7 @@ const fmt = (d: Date) =>
</p>
)
}

{comments && <Comments />}
</article>
</Base>