imsun / gitment

A comment system based on GitHub Issues.
https://imsun.github.io/gitment/
MIT License
4.06k stars 347 forks source link

关于hexo博客单篇文章初始化两次的问题 #68

Open xiaoshenmao opened 7 years ago

xiaoshenmao commented 7 years ago

文章 标示符会自动生成带 #more的url,导致一篇文章初始化了两次,有没有好的解决办法呢?

GeekaholicLin commented 7 years ago

46

为什么大家提问之前不会搜索一下issue中有没有?已经有好多次了2333(:зゝ∠)

xiaoshenmao commented 7 years ago

@GeekaholicLin 问题提了很多次,然而还是没有彻底解决

GeekaholicLin commented 7 years ago

“标示符会自动生成带 #more的url” 这是因为你的Hexo主题有一个阅读更多,点进去,自然就有一个"#more"的hash。

“导致一篇文章初始化了两次” 这是因为你的gitment配置问题,相关内容我已经发给你了啊,将你gitment配置中的id改为id:window.location.pathname

xiaoshenmao commented 7 years ago

@GeekaholicLin 感谢

mosdeo commented 7 years ago

@GeekaholicLin 請問在你的主題裏面,gitment 配置中的 id 要在哪一個檔案修改呢?我用了你最新的 theme 還是會發生重複初始化的狀況

yanzi1225627 commented 7 years ago

@mosdeo 在gitment.swig这个文件里。

isecret commented 6 years ago

今天遇到了同样的问题,@GeekaholicLin 所说的方法能搞定绝大部分情况。 但是,如果 URL 是一个页面呢? 比如 https://exp.com/about/ 呢?而这种又可能是 https://exp.com/about ,这就会导致一篇文章可能会出现初始化两次的情况。

我的最开始解决方案是这样的。id: window.location.pathname.replace(/(\/$)/g, '') + '/' ,不管末尾有没有 / 我都给去掉后再加上,当然,如果 URL 是以 .html 或者其他有尾缀的均会加上,这将会导致之前的文章可能需要重新初始化。

后来就索性直接删除最后的 / 了,不管有没有,先删了再说。

id: window.location.pathname.replace(/(\/$)/g, '')

如果之前文章的评论不重要,或者刚刚准备配置使用 GItment 的,我认为用这种比较合适。

huangjj27 commented 6 years ago

@isecret 我在使用material主题, 主题提供了gitment的配置, 但是还是有些不理想, 所以我最后改成了这样:

// themes/material/layout/_widget/comment/gitcomment/enter.ejs

<script>
    var gitment = new Gitment({
        id: <%= theme.comment.gitment_post_id %>, // 可选。默认为 location.href
       // some other settings
    })
    gitment.render('container')
</script>

配置文件:

// themes/material/_config.yml

comment:
    # useable settings
    # gitment_post_id: window.location.pathname

    # this will break!
    gitment_post_id: window.location.pathname.replace(/(\/$)/g, '')
    # other configs

如果像上面配置的话, ejs会无法识别replace而失效, 请问有什么解决方案吗?

isecret commented 6 years ago

@huangjj27 你试试这样。脑洞解析,没测试过。

<script>
    var gitment = new Gitment({
        id: <%= theme.comment.gitment_post_id %>.replace(/(\/$)/g, ''), // 可选。默认为 location.href
       // some other settings
    })
    gitment.render('container')
</script>

不行的话直接粗暴点,写在 script 标签里吧。

<script>
    var gitment = new Gitment({
        id: window.location.pathname.replace(/(\/$)/g, ''), // 可选。默认为 location.href
       // some other settings
    })
    gitment.render('container')
</script>
huangjj27 commented 6 years ago

@isecret 那就不折腾了, 目前用得还可以

JimmyZhang commented 6 years ago

噶阿三