寫博客嘛,html
誰不喜歡本身倒騰一下呢。node
從自建系統到 Github Page,mysql
年輕的時候誰不喜歡多折騰折騰呢。git
年紀稍稍長了一下以後, 最後我本身仍是選了 Hexo 直接作靜態博客生成,github
結合一下 Gitlab CI 推代碼以後自動構建以後更新到本身的服務器了。golang
後來又基於「多說」直接支持了博客內容評論,web
再後來,多說倒下了,評論功能就一直沒有維護了。sql
前陣子由於某些需求,對市面上部分評論系統作了一次調研,docker
這時候發現其實你們都作了好多輪子了,近期空出來了終於能夠再玩玩了。
調研前提:
下面直接扔了上次的調研結果出來。
Isso – Ich schrei sonst – is a lightweight commenting server written in Python and JavaScript. It aims to be a drop-in replacement for Disqus.
調研搞完以後,停了一陣子,最近把項目扔給同事作進一步 demo 搭建,
同事花了點時候搭了一下 demo 和看了代碼,最後在 commento 和 remark42 中「猶豫了」
最後比較了代碼結構和二次開發成本, 選擇了 remark42
因此,我這邊最後也使用 remark42 直接搭了本身的評論系統。
因爲文檔寫得實在太好了,部署也沒遇到什麼奇怪問題,
部署服務端這一步真的跳過了,有奇怪的問題須要的朋友自行提問吧。
這一段是須要寫寫的。
看完了文檔的朋友應該知道,
在某個頁面集成評論只須要加下面這些代碼。
<script> var remark_config = { host: "REMARK_URL", // hostname of remark server, same as REMARK_URL in backend config, e.g. "https://demo.remark42.com" site_id: "YOUR_SITE_ID", components: ["embed"], // optional param; which components to load. default to ["embed"] // to load all components define components as ['embed', 'last-comments', 'counter'] // available component are: // - 'embed': basic comments widget // - 'last-comments': last comments widget, see `Last Comments` section below // - 'counter': counter widget, see `Counter` section below url: "PAGE_URL", // optional param; if it isn't defined // `window.location.origin + window.location.pathname` will be used, // // Note that if you use query parameters as significant part of url // (the one that actually changes content on page) // you will have to configure url manually to keep query params, as // `window.location.origin + window.location.pathname` doesn't contain query params and // hash. For example default url for `https://example/com/example-post?id=1#hash` // would be `https://example/com/example-post`. // // The problem with query params is that they often contain useless params added by // various trackers (utm params) and doesn't have defined order, so Remark treats differently // all this examples: // https://example.com/?postid=1&date=2007-02-11 // https://example.com/?date=2007-02-11&postid=1 // https://example.com/?date=2007-02-11&postid=1&utm_source=google // // If you deal with query parameters make sure you pass only significant part of it // in well defined order max_shown_comments: 10, // optional param; if it isn't defined default value (15) will be used theme: "dark", // optional param; if it isn't defined default value ('light') will be used page_title: "Moving to Remark42", // optional param; if it isn't defined `document.title` will be used locale: "en", // set up locale and language, if it isn't defined default value ('en') will be used }; (function (c) { for (var i = 0; i < c.length; i++) { var d = document, s = d.createElement("script"); s.src = remark_config.host + "/web/" + c[i] + ".js"; s.defer = true; (d.head || d.body).appendChild(s); } })(remark_config.components || ["embed"]); </script>
<div id="remark42"></div>
那麼問題來了,在 hexo 的 next 主題裏面怎麼作呢?
答案是: 確定是抄代碼啊!!!
首先知道(鬼知道啊),next 主題通常在項目 themes/next 路徑,
themes/next/layout 這個文件夾存放了佈局文件,其中_layout.swig 是一個重要的全局佈局文件。
因此,明顯是修改_layout.swig 引入上面的 script 代碼啦。
看了一眼_layout.swig 的代碼, 底部基本是 include 引入同級的 swig 文件。
{% include '_scripts/boostrap.swig' %} {% include 'remark42.swig' %} {% include '_third-party/comments/index.swig' %} {% include '_third-party/search/index.swig' %}
明顯,咱們也能夠加一個 swig,而後在上面引入一下。
<script> var remark_config = { host: "你部署的remark42 服務", // hostname of remark server, same as REMARK_URL in backend config, e.g. "https://demo.remark42.com" site_id: '你的站點Id,部署的時候指定的', components: ['embed'], // optional param; which components to load. default to ["embed"] // to load all components define components as ['embed', 'last-comments', 'counter'] // available component are: // - 'embed': basic comments widget // - 'last-comments': last comments widget, see `Last Comments` section below // - 'counter': counter widget, see `Counter` section below url: window.location.origin + window.location.pathname, // optional param; if it isn't defined // `window.location.origin + window.location.pathname` will be used, // // Note that if you use query parameters as significant part of url // (the one that actually changes content on page) // you will have to configure url manually to keep query params, as // `window.location.origin + window.location.pathname` doesn't contain query params and // hash. For example default url for `https://example/com/example-post?id=1#hash` // would be `https://example/com/example-post`. // // The problem with query params is that they often contain useless params added by // various trackers (utm params) and doesn't have defined order, so Remark treats differently // all this examples: // https://example.com/?postid=1&date=2007-02-11 // https://example.com/?date=2007-02-11&postid=1 // https://example.com/?date=2007-02-11&postid=1&utm_source=google // // If you deal with query parameters make sure you pass only significant part of it // in well defined order max_shown_comments: 10, // optional param; if it isn't defined default value (15) will be used theme: 'dark', // optional param; if it isn't defined default value ('light') will be used page_title: 'Moving to Remark42', // optional param; if it isn't defined `document.title` will be used locale: 'en' // set up locale and language, if it isn't defined default value ('en') will be used }; (function(c) { for(var i = 0; i < c.length; i++){ var d = document, s = d.createElement('script'); s.src = remark_config.host + '/web/' +c[i] +'.js'; s.defer = true; (d.head || d.body).appendChild(s); } })(remark_config.components || ['embed']); </script>
{% include '_scripts/boostrap.swig' %} {% include 'remark42.swig' %} {% include '_third-party/comments/index.swig' %} {% include '_third-party/search/index.swig' %}
搞完這個,全局腳本引用已經搞掂了。
下面就是每一個文章頁面須要新增 remark42 評論框了
觀察一下 themes/next/layout 目錄不難發現,
每一個文章的的樣式模板都在 post.swig,
明顯評論框也應該直接在 post.swig。
打開一看,感受應該加在
因而:
<div id="posts" class="posts-expand"> {{ post_template.render(page) }} <div class="post-spread"> {% if theme.jiathis %} {% include '_partials/share/jiathis.swig' %} {% elseif theme.baidushare %} {% include '_partials/share/baidushare.swig' %} {% elseif theme.add_this_id %} {% include '_partials/share/add-this.swig' %} {% elseif theme.duoshuo_shortname and theme.duoshuo_share %} {% include '_partials/share/duoshuo_share.swig' %} {% endif %} </div> <div id="remark42"></div> </div>
完事...