Skip to content

Conversation

@idencosmos
Copy link

Summary

  • Require frontmatter commentId when Giscus uses mapping="specific" so each page persists its own thread
  • Clear prior Giscus embeds on SPA navigations before re-injecting; skip injection when the term data is missing
  • Keep quartz.layout.ts wrapping Component.Comments behind a commentId guard so only eligible pages render the widget

Layout Example

afterBody: [
  Component.ConditionalRender({
    component: Component.Comments({
      provider: "giscus",
      options: {
        repo: "",
        repoId: "",
        category: "",
        categoryId: "",
        mapping: "specific",
        strict: true,
        reactionsEnabled: true,
        inputPosition: "bottom",
        lang: "ko",
      },
    }),
    condition: ({ fileData }) => Boolean(fileData.frontmatter?.commentId),
  }),
]

In Quartz, afterBody lets a theme append components after the main content. Wrapping Component.Comments in Component.ConditionalRender ensures the Giscus iframe is mounted only when the Markdown frontmatter declares a commentId, which prevents mis-mapping and matches Giscus’ mapping: "specific" requirement.

Why Guard commentId Everywhere

  • quartz/components/Comments.tsx blocks the initial component render when the term is missing, preventing Giscus from booting with stale props.
  • quartz/components/scripts/comments.inline.ts re-checks during SPA navigations so client-side transitions never resurrect an iframe tied to a different page.
  • quartz.layout.ts keeps the widget out of the static HTML build (SSG) entirely unless the page frontmatter opts in, avoiding accidental mounts.
  • These layered guards also cover future direct imports of Component.Comments, so no caller can accidentally instantiate the widget without a matching commentId.

- require frontmatter commentId when giscus uses mapping "specific" so each page keeps its discussion
- clear prior giscus embeds on SPA navigation before reinjecting and skip when term is missing
- keep layout.ts wrapping Comments with a commentId guard
@github-actions
Copy link

github-actions bot commented Sep 17, 2025

built with Refined Cloudflare Pages Action

⚡ Cloudflare Pages Deployment

Name Status Preview Last Commit
quartz ✅ Ready (View Log) Visit Preview 545d65e

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances Giscus comments integration by adding support for persistent discussions through commentId mapping and improving SPA navigation handling.

  • Adds commentId validation to ensure proper Giscus thread persistence when using mapping="specific"
  • Clears previous Giscus embeds during SPA navigation and skips injection when required term data is missing
  • Implements layered guards across components to prevent accidental widget instantiation without proper configuration

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
quartz/components/Comments.tsx Adds commentId validation and term data attribute handling for specific mapping
quartz/components/scripts/comments.inline.ts Implements cleanup of previous embeds and term validation for SPA navigation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

data-category-id={opts.options.categoryId}
data-mapping={opts.options.mapping ?? "url"}
data-mapping={mapping}
data-term={mapping === "specific" ? commentId : undefined}
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting data-term to undefined will result in the string 'undefined' being set as the attribute value. Use conditional rendering or commentId || undefined to properly omit the attribute when not needed.

Suggested change
data-term={mapping === "specific" ? commentId : undefined}
{...(mapping === "specific" && commentId ? { "data-term": commentId } : {})}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant