NotionNext icon indicating copy to clipboard operation
NotionNext copied to clipboard

【开关评论】特定頁面的評論區開關

Open nagongze opened this issue 3 years ago • 3 comments

为什么提出这个新的特性改动 想在特定的頁面可以關閉評論區,但不是全關

描述一下你推荐的解决方案 模板內新增一欄Comment的select?

nagongze avatar Nov 23 '22 11:11 nagongze

我担心在模板内 新增一个 comment的select 会让使用变得复杂。

我希望更多的了解,是什么场景需要关闭页面的评论呢?

tangly1024 avatar Nov 26 '22 13:11 tangly1024

比如我有提供一些素材的下載 但本身是需要自行解開密碼後才能成功下載的 關閉那個頁面的評論能減少將密碼直接貼在評論區的可能 又或者是 文章比較多連貫性 所以我專門開一個頁面把一些系統性的文章給集中在一起 這種時候也不需要評論

nagongze avatar Nov 26 '22 14:11 nagongze

我同样有这样的需求,我自己修改的方案是这样的: notion里添加comment_flag字段,类型select,值域['true','false']:

blog.config.js中声明有这样的字段comment_flag:

// 自定义配置notion数据库字段名
  NOTION_PROPERTY_NAME: {
    // ...
    // added by zfhxi
    comment_flag: process.env.NEXT_PUBLIC_NOTION_PROPERTY_COMMENT_FLAG || 'comment_flag'
    ////
  },

然后lib/notion/getPageProperties.js中解析并处理字段comment_flag的值:

  // ...
  delete properties.content
  // added by zfhxi
  properties.comment_flag = (properties.comment_flag || ['true'])[0]
  ////
  return properties

然后components/Comment.js中使用字段comment_flag的值:

// ...
const Comment = ({ frontMatter }) => {
  const router = useRouter()

  React.useEffect(() => {
    // 跳转到评论区
    setTimeout(() => {
      if (window.location.href.indexOf('target=comment') > -1) {
        const url = router.asPath.replace('?target=comment', '')
        history.replaceState({}, '', url)
        const commentNode = document.getElementById('comment')
        commentNode.scrollIntoView({ block: 'start', behavior: 'smooth' })
      }
    }, 200)
  }, [])

  //moded by zfhxi
  if (!frontMatter) {
    return <>Loading...</>
  } else if (frontMatter.comment_flag === 'false') {
    return <></>
  }
  ////
  return (
    <div id='comment' className='comment mt-5 text-gray-800 dark:text-gray-300'>
      <Tabs>

         { BLOG.COMMENT_TWIKOO_ENV_ID && (<div key='Twikoo'>
            <TwikooCompenent/>
         </div>)}
         // ...

最后,将你希望关闭评论的文章或页面的属性中,把comment_flag设置为false即可,要显示评论的文章可留默认值

zfhxi avatar Feb 22 '23 02:02 zfhxi

+1 一些页面,只需要查看,不需要评论(比如关于)。

hiyaabai avatar Sep 17 '23 14:09 hiyaabai