Skip to content

Commit

Permalink
feat: adds remark24
Browse files Browse the repository at this point in the history
  • Loading branch information
harrytang committed Aug 6, 2024
1 parent 5e198e7 commit c80d4a2
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ NEXT_PUBLIC_HOME_PAGE_SIZE: '3'
NEXT_PUBLIC_AVATAR_URL: 'https://assets.harrytang.xyz/avatar.png'
NEXT_PUBLIC_FAVICON_URL: 'https://assets.harrytang.xyz/favicon.ico'
NEXT_PUBLIC_ICON_URL: 'https://assets.harrytang.xyz/favicon.png'
NEXT_PUBLIC_COMMENTBOX_ID: '5721924772560896-proj'
NEXT_PUBLIC_REMARK42_HOST: 'https://comment.local.harrytang.xyz'
NEXT_PUBLIC_REMARK42_SITE_ID: 'harrytang.xyz'
REVALIDATE_TOKEN: 'yyy'
STRAPI_API_URL: 'http://strapi:1337'
SITEMAP_SIZE: '500'
3 changes: 2 additions & 1 deletion .github/workflows/production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ jobs:
NEXT_PUBLIC_AVATAR_URL=${{ vars.NEXT_PUBLIC_AVATAR_URL }}
NEXT_PUBLIC_FAVICON_URL=${{ vars.NEXT_PUBLIC_FAVICON_URL }}
NEXT_PUBLIC_ICON_URL=${{ vars.NEXT_PUBLIC_ICON_URL }}
NEXT_PUBLIC_COMMENTBOX_ID=${{ vars.NEXT_PUBLIC_COMMENTBOX_ID }}
NEXT_PUBLIC_REMARK42_HOST=${{ vars.NEXT_PUBLIC_REMARK42_HOST }}
NEXT_PUBLIC_REMARK42_SITE_ID=${{ vars.NEXT_PUBLIC_REMARK42_SITE_ID }}
STRAPI_API_URL=${{ vars.STRAPI_API_URL }}
SITEMAP_SIZE=${{ vars.SITEMAP_SIZE }}
platforms: linux/arm64,linux/amd64
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ jobs:
NEXT_PUBLIC_AVATAR_URL=${{ vars.NEXT_PUBLIC_AVATAR_URL }}
NEXT_PUBLIC_FAVICON_URL=${{ vars.NEXT_PUBLIC_FAVICON_URL }}
NEXT_PUBLIC_ICON_URL=${{ vars.NEXT_PUBLIC_ICON_URL }}
NEXT_PUBLIC_COMMENTBOX_ID=${{ vars.NEXT_PUBLIC_COMMENTBOX_ID }}
NEXT_PUBLIC_REMARK42_HOST=${{ vars.NEXT_PUBLIC_REMARK42_HOST }}
NEXT_PUBLIC_REMARK42_SITE_ID=${{ vars.NEXT_PUBLIC_REMARK42_SITE_ID }}
STRAPI_API_URL=${{ vars.STRAPI_API_URL }}
SITEMAP_SIZE=${{ vars.SITEMAP_SIZE }}
platforms: linux/arm64,linux/amd64
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ jobs:
NEXT_PUBLIC_AVATAR_URL: ${{ vars.NEXT_PUBLIC_AVATAR_URL }}
NEXT_PUBLIC_FAVICON_URL: ${{ vars.NEXT_PUBLIC_FAVICON_URL }}
NEXT_PUBLIC_ICON_URL: ${{ vars.NEXT_PUBLIC_ICON_URL }}
NEXT_PUBLIC_COMMENTBOX_ID: ${{ vars.NEXT_PUBLIC_COMMENTBOX_ID }}
NEXT_PUBLIC_REMARK42_HOST: ${{ vars.NEXT_PUBLIC_REMARK42_HOST }}
NEXT_PUBLIC_REMARK42_SITE_ID: ${{ vars.NEXT_PUBLIC_REMARK42_SITE_ID }}
STRAPI_API_URL: ${{ vars.STRAPI_API_URL }}
SITEMAP_SIZE: ${{ vars.SITEMAP_SIZE }}
with:
Expand Down Expand Up @@ -75,7 +76,8 @@ jobs:
NEXT_PUBLIC_AVATAR_URL: ${{ vars.NEXT_PUBLIC_AVATAR_URL }}
NEXT_PUBLIC_FAVICON_URL: ${{ vars.NEXT_PUBLIC_FAVICON_URL }}
NEXT_PUBLIC_ICON_URL: ${{ vars.NEXT_PUBLIC_ICON_URL }}
NEXT_PUBLIC_COMMENTBOX_ID: ${{ vars.NEXT_PUBLIC_COMMENTBOX_ID }}
NEXT_PUBLIC_REMARK42_HOST: ${{ vars.NEXT_PUBLIC_REMARK42_HOST }}
NEXT_PUBLIC_REMARK42_SITE_ID: ${{ vars.NEXT_PUBLIC_REMARK42_SITE_ID }}
STRAPI_API_URL: ${{ vars.STRAPI_API_URL }}
SITEMAP_SIZE: ${{ vars.SITEMAP_SIZE }}
- name: Get Playwright Version
Expand All @@ -102,7 +104,8 @@ jobs:
NEXT_PUBLIC_AVATAR_URL: ${{ vars.NEXT_PUBLIC_AVATAR_URL }}
NEXT_PUBLIC_FAVICON_URL: ${{ vars.NEXT_PUBLIC_FAVICON_URL }}
NEXT_PUBLIC_ICON_URL: ${{ vars.NEXT_PUBLIC_ICON_URL }}
NEXT_PUBLIC_COMMENTBOX_ID: ${{ vars.NEXT_PUBLIC_COMMENTBOX_ID }}
NEXT_PUBLIC_REMARK42_HOST: ${{ vars.NEXT_PUBLIC_REMARK42_HOST }}
NEXT_PUBLIC_REMARK42_SITE_ID: ${{ vars.NEXT_PUBLIC_REMARK42_SITE_ID }}
STRAPI_API_URL: ${{ vars.STRAPI_API_URL }}
SITEMAP_SIZE: ${{ vars.SITEMAP_SIZE }}
- name: Upload Playwright report
Expand Down
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ ARG NEXT_PUBLIC_HOME_PAGE_SIZE
ARG NEXT_PUBLIC_AVATAR_URL
ARG NEXT_PUBLIC_FAVICON_URL
ARG NEXT_PUBLIC_ICON_URL
ARG NEXT_PUBLIC_COMMENTBOX_ID
ARG NEXT_PUBLIC_REMARK42_SITE_ID
ARG NEXT_PUBLIC_REMARK42_HOST
ARG STRAPI_API_URL
ARG SITEMAP_SIZE

Expand All @@ -44,7 +45,8 @@ ENV NEXT_PUBLIC_HOME_PAGE_SIZE=${NEXT_PUBLIC_HOME_PAGE_SIZE}
ENV NEXT_PUBLIC_AVATAR_URL=${NEXT_PUBLIC_AVATAR_URL}
ENV NEXT_PUBLIC_FAVICON_URL=${NEXT_PUBLIC_FAVICON_URL}
ENV NEXT_PUBLIC_ICON_URL=${NEXT_PUBLIC_ICON_URL}
ENV NEXT_PUBLIC_COMMENTBOX_ID=${NEXT_PUBLIC_COMMENTBOX_ID}
ENV NEXT_PUBLIC_REMARK42_SITE_ID=${NEXT_PUBLIC_REMARK42_SITE_ID}
ENV NEXT_PUBLIC_REMARK42_HOST=${NEXT_PUBLIC_REMARK42_HOST}
ENV STRAPI_API_URL=${STRAPI_API_URL}
ENV SITEMAP_SIZE=${SITEMAP_SIZE}
ENV NEXT_TELEMETRY_DISABLED=1
Expand Down
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ docker build \
--build-arg NEXT_PUBLIC_AVATAR_URL=${NEXT_PUBLIC_AVATAR_URL} \
--build-arg NEXT_PUBLIC_FAVICON_URL=${NEXT_PUBLIC_FAVICON_URL} \
--build-arg NEXT_PUBLIC_ICON_URL=${NEXT_PUBLIC_ICON_URL} \
--build-arg NEXT_PUBLIC_COMMENTBOX_ID=${NEXT_PUBLIC_COMMENTBOX_ID} \
--build-arg NEXT_PUBLIC_REMARK42_HOST=${NEXT_PUBLIC_REMARK42_HOST} \
--build-arg NEXT_PUBLIC_REMARK42_SITE_ID=${NEXT_PUBLIC_REMARK42_SITE_ID} \
--build-arg STRAPI_API_URL=${NEXT_PUBLIC_STRAPI_API_URL} \
--build-arg SITEMAP_SIZE=${SITEMAP_SIZE} \
-t ghcr.io/harrytang/web:dev .
4 changes: 3 additions & 1 deletion src/components/BlogLayout/BlogLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ const BlogLayout: React.FC<BlogLayoutProps> = ({ blog }) => {
</figure>

<ReactMarkdown>{blog.attributes.content}</ReactMarkdown>
<CommentBox projectId={process.env.NEXT_PUBLIC_COMMENTBOX_ID!} />
<CommentBox
location={`${process.env.NEXT_PUBLIC_SITE_URL}/blog/${blog.attributes.slug}`}
/>
</Prose>
</article>
</div>
Expand Down
131 changes: 103 additions & 28 deletions src/components/CommentBox/CommentBox.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,117 @@
import React, { useEffect } from 'react'
import { useTheme } from 'next-themes'
import { Fragment, useEffect, useState } from 'react'

interface CommentBoxProps {
projectId: string
}

// Extend the window interface directly in this file
declare global {
interface Window {
commentBox: (projectId: string) => void
REMARK42: any
remark_config: any
}
}

const insertScript = (
id: string,
parentElement: HTMLElement,
theme: string,
) => {
const script = window.document.createElement('script')
script.type = 'text/javascript'
script.async = true
script.id = id
let url = window.location.origin + window.location.pathname
if (url.endsWith('/')) {
url = url.slice(0, -1)
}
script.innerHTML = `
var remark_config = {
host: "${process.env.NEXT_PUBLIC_REMARK42_HOST}",
site_id: "${process.env.NEXT_PUBLIC_REMARK42_SITE_ID}",
url: "${url}",
theme: "${theme}",
no_footer: true,
};
!function(e,n){for(var o=0;o<e.length;o++){var r=n.createElement("script"),c=".js",d=n.head||n.body;"noModule"in r?(r.type="module",c=".mjs"):r.async=!0,r.defer=!0,r.src=remark_config.host+"/web/"+e[o]+c,d.appendChild(r)}}(remark_config.components||["embed"],document);`
parentElement.appendChild(script)
}

const removeScript = (id: string, parentElement: HTMLElement) => {
const script = window.document.getElementById(id)
if (script) {
parentElement.removeChild(script)
}
}

const CommentBox: React.FC<CommentBoxProps> = ({ projectId }) => {
const manageScript = (theme: string) => {
if (!window) {
return () => {}
}
const { document } = window
if (document.getElementById('remark42')) {
insertScript('comments-script', document.body, theme)
}
return () => removeScript('comments-script', document.body)
}

const recreateRemark42Instance = () => {
if (!window) {
return
}
const remark42 = window.REMARK42
if (remark42) {
remark42.destroy()
remark42.createInstance(window.remark_config)
}
}

const getPreferredTheme = (theme: string) => {
if (theme === 'system') {
const prefersDark =
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
return prefersDark ? 'dark' : 'light'
}
return theme
}

type CommentBoxProps = {
location: string
}

export default function CommentBox({ location }: CommentBoxProps) {
const { theme } = useTheme()
const [preferredTheme, setPreferredTheme] = useState(
getPreferredTheme(theme ?? 'system'),
)

useEffect(() => {
// Create a script element
const script = document.createElement('script')
script.src = 'https://unpkg.com/commentbox.io/dist/commentBox.min.js'
script.async = true

// Append the script to the document body
document.body.appendChild(script)

// Initialize CommentBox.io after the script has loaded
script.onload = () => {
if (window.commentBox) {
window.commentBox(projectId)
const handleSystemThemeChange = (e: MediaQueryListEvent) => {
if (theme === 'system') {
setPreferredTheme(e.matches ? 'dark' : 'light')
}
}

// Cleanup function to remove the script when the component unmounts
return () => {
if (script.parentNode) {
script.parentNode.removeChild(script)
if (theme === 'system') {
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)')
mediaQueryList.addEventListener('change', handleSystemThemeChange)
setPreferredTheme(mediaQueryList.matches ? 'dark' : 'light')

return () => {
mediaQueryList.removeEventListener('change', handleSystemThemeChange)
}
} else {
setPreferredTheme(theme ?? 'system')
}
}, [projectId])
}, [theme])

return <div className="commentbox"></div>
}
useEffect(() => {
return manageScript(preferredTheme)
}, [location, preferredTheme])

useEffect(recreateRemark42Instance, [location, preferredTheme])

export default CommentBox
return (
<Fragment>
<h2>Comments</h2>
<div id="remark42" />
</Fragment>
)
}

0 comments on commit c80d4a2

Please sign in to comment.