碎碎念

之前在用其他主題的時候 經常因為不兼容一些評論系統 所以而放棄 但是Nobelium很符合我的審美 所以我準備強制兼容Waline在Nobelium!

在Nobelium已經有了其他評論系統的框架 魔改起來是十分輕鬆的!

讓我們開始!

使用pnpm安裝waline客戶端

bash
pnpm add -D @waline/client

創建文件 components/WalineComponent.js 並且添加內容

jsx
import  { createRef, useEffect } from 'react'
import { init } from '@waline/client'
import { useRouter } from 'next/router'
import '../node_modules/@waline/client/dist/waline.css'

const path = ''
let waline = null
const WalineComponent = (props) => {
  const containerRef = createRef()
  const router = useRouter()

  const updateWaline = url => {
    if (url !== path && waline) {
      waline.update(props)
    }
  }

  useEffect(() => {
    if (!waline) {
      waline = init({
        ...props,
        el: containerRef.current,
        serverURL: '',//修改為你的Waline服務端
        lang: "zh-cn",
        dark: 'html.dark',
        emoji: [
          '//npm.elemecdn.com/@waline/emojis@1.1.0/tieba',
          '//npm.elemecdn.com/@waline/emojis@1.1.0/weibo',
          '//npm.elemecdn.com/@waline/emojis@1.1.0/bilibili'
        ]
      })
    }

  
    router.events.on('routeChangeComplete', updateWaline)
    const anchor = window.location.hash
    if (anchor) {
    
      const targetNode = document.getElementsByClassName('wl-cards')[0]

      const mutationCallback = (mutations) => {
        for (const mutation of mutations) {
          const type = mutation.type
          if (type === 'childList') {
            const anchorElement = document.getElementById(anchor.substring(1))
            if (anchorElement && anchorElement.className === 'wl-item') {
              anchorElement.scrollIntoView({ block: 'end', behavior: 'smooth' })
              setTimeout(() => {
                anchorElement.classList.add('animate__animated')
                anchorElement.classList.add('animate__bounceInRight')
                observer.disconnect()
              }, 300)
            }
          }
        }
      }

      const observer = new MutationObserver(mutationCallback)
      observer.observe(targetNode, { childList: true })
    }

    return () => {
      if (waline) {
        waline.destroy()
        waline = null
      }
      router.events.off('routeChangeComplete', updateWaline)
    }
  }, [])

  return <div ref={containerRef} />
}

export default WalineComponent

⚠️ 請修改成你的Waline服務器地址!

在 components/Comments.js的適當位置添加

text
const WalineComponent = dynamic(
  () => {
    return import('@/components/WalineComponent')
  },
  { ssr: false }
)
jsx
            <div key='Waline'>
              <WalineComponent />
            </div>

至此 部署後 你的Waline就應該生效啦!