mirror of
https://github.com/jackyzha0/quartz.git
synced 2026-03-24 15:05:42 -05:00
feat: add disqus and commento comments providers
This commit is contained in:
parent
c5d97db000
commit
1d58d69a7a
@ -52,6 +52,9 @@ const config: QuartzConfig = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
comments: {
|
||||||
|
provider: "commento",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
plugins: {
|
plugins: {
|
||||||
transformers: [
|
transformers: [
|
||||||
|
|||||||
@ -43,6 +43,31 @@ export type Analytics =
|
|||||||
projectId?: string
|
projectId?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Comments =
|
||||||
|
| null
|
||||||
|
| {
|
||||||
|
provider: "giscus"
|
||||||
|
repo: `${string}/${string}`
|
||||||
|
repoId: string
|
||||||
|
category: string
|
||||||
|
categoryId: string
|
||||||
|
mapping?: "url" | "title" | "og:title" | "specific" | "number" | "pathname"
|
||||||
|
strict?: boolean
|
||||||
|
reactionsEnabled?: boolean
|
||||||
|
inputPosition?: "top" | "bottom"
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
provider: "commento"
|
||||||
|
host?: string
|
||||||
|
cssOverride?: string
|
||||||
|
noFonts?: boolean
|
||||||
|
hideDeleted?: boolean
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
provider: "disqus"
|
||||||
|
shortName: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface GlobalConfiguration {
|
export interface GlobalConfiguration {
|
||||||
pageTitle: string
|
pageTitle: string
|
||||||
pageTitleSuffix?: string
|
pageTitleSuffix?: string
|
||||||
@ -56,6 +81,8 @@ export interface GlobalConfiguration {
|
|||||||
ignorePatterns: string[]
|
ignorePatterns: string[]
|
||||||
/** Whether to use created, modified, or published as the default type of date */
|
/** Whether to use created, modified, or published as the default type of date */
|
||||||
defaultDateType: ValidDateType
|
defaultDateType: ValidDateType
|
||||||
|
/** Comments for page */
|
||||||
|
comments: Comments
|
||||||
/** Base URL to use for CNAME files, sitemaps, and RSS feeds that require an absolute URL.
|
/** Base URL to use for CNAME files, sitemaps, and RSS feeds that require an absolute URL.
|
||||||
* Quartz will avoid using this as much as possible and use relative URLs most of the time
|
* Quartz will avoid using this as much as possible and use relative URLs most of the time
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -3,42 +3,53 @@ import { classNames } from "../util/lang"
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import script from "./scripts/comments.inline"
|
import script from "./scripts/comments.inline"
|
||||||
|
|
||||||
type Options = {
|
|
||||||
provider: "giscus"
|
|
||||||
options: {
|
|
||||||
repo: `${string}/${string}`
|
|
||||||
repoId: string
|
|
||||||
category: string
|
|
||||||
categoryId: string
|
|
||||||
mapping?: "url" | "title" | "og:title" | "specific" | "number" | "pathname"
|
|
||||||
strict?: boolean
|
|
||||||
reactionsEnabled?: boolean
|
|
||||||
inputPosition?: "top" | "bottom"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function boolToStringBool(b: boolean): string {
|
function boolToStringBool(b: boolean): string {
|
||||||
return b ? "1" : "0"
|
return b ? "1" : "0"
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ((opts: Options) => {
|
export default (() => {
|
||||||
const Comments: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps) => {
|
const Comments: QuartzComponent = ({ displayClass, cfg }: QuartzComponentProps) => {
|
||||||
return (
|
switch (cfg.comments?.provider) {
|
||||||
<div
|
case "giscus":
|
||||||
class={classNames(displayClass, "giscus")}
|
return (
|
||||||
data-repo={opts.options.repo}
|
<div id="comment" data-provider={cfg.comments.provider}>
|
||||||
data-repo-id={opts.options.repoId}
|
<div
|
||||||
data-category={opts.options.category}
|
class={classNames(displayClass, "giscus")}
|
||||||
data-category-id={opts.options.categoryId}
|
data-repo={cfg.comments?.repo}
|
||||||
data-mapping={opts.options.mapping ?? "url"}
|
data-repo-id={cfg.comments?.repoId}
|
||||||
data-strict={boolToStringBool(opts.options.strict ?? true)}
|
data-category={cfg.comments?.category}
|
||||||
data-reactions-enabled={boolToStringBool(opts.options.reactionsEnabled ?? true)}
|
data-category-id={cfg.comments?.categoryId}
|
||||||
data-input-position={opts.options.inputPosition ?? "bottom"}
|
data-mapping={cfg.comments?.mapping ?? "url"}
|
||||||
></div>
|
data-strict={boolToStringBool(cfg.comments?.strict ?? true)}
|
||||||
)
|
data-reactions-enabled={boolToStringBool(cfg.comments?.reactionsEnabled ?? true)}
|
||||||
|
data-input-position={cfg.comments?.inputPosition ?? "bottom"}
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
case "commento":
|
||||||
|
return (
|
||||||
|
<div id="comment" data-provider={cfg.comments.provider}>
|
||||||
|
<div
|
||||||
|
id="commento"
|
||||||
|
data-host={cfg.comments?.host ?? "cdn.commento.io"}
|
||||||
|
data-css-override={cfg.comments?.cssOverride ?? ""}
|
||||||
|
data-no-fonts={cfg.comments?.noFonts ?? true}
|
||||||
|
data-hide-deleted={cfg.comments?.hideDeleted ?? false}
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
case "disqus":
|
||||||
|
return (
|
||||||
|
<div id="comment" data-provider={cfg.comments.provider}>
|
||||||
|
<div id="disqus_thread" data-short-name={cfg.comments?.shortName}></div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
default:
|
||||||
|
return <></>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Comments.afterDOMLoaded = script
|
Comments.afterDOMLoaded = script
|
||||||
|
|
||||||
return Comments
|
return Comments
|
||||||
}) satisfies QuartzComponentConstructor<Options>
|
}) satisfies QuartzComponentConstructor
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
const changeTheme = (e: CustomEventMap["themechange"]) => {
|
const changeGiscusTheme = (e: CustomEventMap["themechange"]) => {
|
||||||
const theme = e.detail.theme
|
const theme = e.detail.theme
|
||||||
const iframe = document.querySelector("iframe.giscus-frame") as HTMLIFrameElement
|
const iframe = document.querySelector("iframe.giscus-frame") as HTMLIFrameElement
|
||||||
if (!iframe) {
|
if (!iframe) {
|
||||||
@ -34,7 +34,7 @@ type GiscusElement = Omit<HTMLElement, "dataset"> & {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener("nav", () => {
|
function setupGiscus() {
|
||||||
const giscusContainer = document.querySelector(".giscus") as GiscusElement
|
const giscusContainer = document.querySelector(".giscus") as GiscusElement
|
||||||
if (!giscusContainer) {
|
if (!giscusContainer) {
|
||||||
return
|
return
|
||||||
@ -62,6 +62,66 @@ document.addEventListener("nav", () => {
|
|||||||
|
|
||||||
giscusContainer.appendChild(giscusScript)
|
giscusContainer.appendChild(giscusScript)
|
||||||
|
|
||||||
document.addEventListener("themechange", changeTheme)
|
document.addEventListener("themechange", changeGiscusTheme)
|
||||||
window.addCleanup(() => document.removeEventListener("themechange", changeTheme))
|
window.addCleanup(() => document.removeEventListener("themechange", changeGiscusTheme))
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommentoElement = Omit<HTMLElement, "dataset"> & {
|
||||||
|
dataset: DOMStringMap & {
|
||||||
|
host: string
|
||||||
|
cssOverride: string
|
||||||
|
noFonts: string
|
||||||
|
hideDeleted: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupCommento() {
|
||||||
|
const commentoContainer = document.querySelector("#commento") as CommentoElement
|
||||||
|
if (!commentoContainer) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const commentoScript = document.createElement("script")
|
||||||
|
commentoScript.src = "https://" + commentoContainer.dataset.host + "/js/commento.js"
|
||||||
|
commentoScript.defer = true
|
||||||
|
commentoScript.setAttribute("data-css-override", commentoContainer.dataset.cssOverride)
|
||||||
|
commentoScript.setAttribute("data-no-fonts", commentoContainer.dataset.noFonts)
|
||||||
|
commentoScript.setAttribute("data-hide-deleted", commentoContainer.dataset.hideDeleted)
|
||||||
|
|
||||||
|
commentoContainer.appendChild(commentoScript)
|
||||||
|
}
|
||||||
|
|
||||||
|
type DisqusElement = Omit<HTMLElement, "dataset"> & {
|
||||||
|
dataset: DOMStringMap & {
|
||||||
|
shortName: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupDisqus() {
|
||||||
|
const disqusContainer = document.querySelector("#disqus_thread") as DisqusElement
|
||||||
|
if (!disqusContainer) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const disqusScript = document.createElement("script")
|
||||||
|
disqusScript.src = "https://" + disqusContainer.dataset.shortName + ".disqus.com/embed.js"
|
||||||
|
disqusScript.setAttribute("data-timestamp", "" + +new Date())
|
||||||
|
|
||||||
|
disqusContainer.appendChild(disqusScript)
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener("nav", () => {
|
||||||
|
const commentContainer = document.querySelector("#comment")
|
||||||
|
if (!commentContainer) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const provider = commentContainer.getAttribute("data-provider")
|
||||||
|
switch (provider) {
|
||||||
|
case "giscus":
|
||||||
|
setupGiscus()
|
||||||
|
case "commento":
|
||||||
|
setupCommento()
|
||||||
|
case "disqus":
|
||||||
|
setupDisqus()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user