<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>字数统计 - 标签 - 菠菜眾長</title><link>https://lruihao.cn/tags/%E5%AD%97%E6%95%B0%E7%BB%9F%E8%AE%A1/</link><description>字数统计 - 标签 - 菠菜眾長</description><generator>Hugo -- gohugo.io</generator><language>zh-CN</language><managingEditor>1024@lruihao.cn (Lruihao)</managingEditor><webMaster>1024@lruihao.cn (Lruihao)</webMaster><lastBuildDate>Thu, 30 Aug 2018 13:15:50 +0000</lastBuildDate><atom:link href="https://lruihao.cn/tags/%E5%AD%97%E6%95%B0%E7%BB%9F%E8%AE%A1/" rel="self" type="application/rss+xml"/><item><title>hexo next 主题添加字数统计（2018）</title><link>https://lruihao.cn/posts/hexo-wordcount/</link><pubDate>Thu, 30 Aug 2018 13:15:50 +0000</pubDate><author>Lruihao</author><guid>https://lruihao.cn/posts/hexo-wordcount/</guid><description><![CDATA[<div class="details admonition warning open">
    <div class="details-summary admonition-title">
      <i class="icon fa-solid fa-exclamation-triangle fa-fw" aria-hidden="true"></i>警告<i class="details-icon fa-solid fa-angle-right fa-fw" aria-hidden="true"></i>
    </div>
    <div class="details-content">
      <div class="admonition-content"><p><strong>2018/11/1 说明
next 主题好像更新了，现在自带的可以用了，如果可以用了，以下教程可以忽略！！！</strong></p>
<p>最近有几个小伙伴问我博客的字数统计怎么实现的，怎么网上的教程不管用啊？一开始我搭建博客的时候也遇到了类似的问题，按照 github 上 wordcount 的 readme 操作后，并没有什么用，我打开 post 相关配置文件并没有发现发现 wordcount 这个关键词，next 本身似乎也并没有在主题配置文件提供选项（或许是个人原因），所以只好自己动手加一个了。为了不重复回答问题，先做个原创记录。转载请注明出处。在此抛转引玉，如果有更好的方法请在留言区提出，我会及时更改。同时也希望小伙伴多发扬折腾精神，多专研，少提问，毕竟还是 RTFSC 大法好！(Read the fucking source code)</p>
</div>
    </div>
  </div>
<h2 id="安装">安装</h2>
<p><a href="https://github.com/willin/hexo-wordcount"target="_blank" rel="external nofollow noopener noreferrer">wordcount github<i class="fa-solid fa-external-link-alt fa-fw fa-xs ms-1 text-secondary" aria-hidden="true"></i></a>
如果没有安装 hexo-wordcount 插件，先安装该插件：</p>
<div class="highlight" id="id-1"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">npm i --save hexo-wordcount
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"># Node 版本 7.6.0 之前，请安装 2.x 版本 (Node.js v7.6.0 and previous)
</span></span><span class="line"><span class="cl">npm install hexo-wordcount@2 --save</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="post-添加">post 添加</h2>
<p>打开<code>hexo\themes\hexo-theme-next\layout\_macro</code>路径下的 post.swig 文件，既然没有字数统计那么我们就加一个，简单暴力地直接在阅读数后面加上一条就好了，在文件类搜索关键词<code>busuanzi</code>, 我用的是不蒜子，如果用的了 leancloud 的搜 leancloud 就好了，其他类似。找到这段代码后</p>
<div class="highlight" id="id-2"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">{% if not is_index and theme.busuanzi_count.enable and theme.busuanzi_count.post_views %}
</span></span><span class="line"><span class="cl">    &lt;span class=&#34;post-meta-divider&#34;&gt;|&lt;/span&gt;
</span></span><span class="line"><span class="cl">    &lt;span class=&#34;post-meta-item-icon&#34;
</span></span><span class="line"><span class="cl">    {% if not theme.post_meta.item_text %} title=&#34;{{ __(&#39;post.views&#39;) }}&#34; {% endif %}&gt;
</span></span><span class="line"><span class="cl">    &lt;i class=&#34;fa fa-{{ theme.busuanzi_count.post_views_icon }}&#34;&gt;&lt;/i&gt;
</span></span><span class="line"><span class="cl">    {% if theme.post_meta.item_text %} {{__(&#39;post.views&#39;) + __(&#39;symbol.colon&#39;) }} {% endif %}
</span></span><span class="line"><span class="cl">    &lt;span class=&#34;busuanzi-value&#34; id=&#34;busuanzi_value_page_pv&#34; &gt;&lt;/span&gt;
</span></span><span class="line"><span class="cl">    &lt;/span&gt;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">{% endif %}</span></span></code></pre></td></tr></table>
</div>
</div><p>在<code>endif</code>上面，即本文代码块那个空行处添加以下代码</p>
<div class="highlight" id="id-3"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">&lt;span class=&#34;post-meta-divider&#34;&gt;|&lt;/span&gt;
</span></span><span class="line"><span class="cl">&lt;span title=&#34;{{ __(&#39;post.wordcount&#39;) }}&#34;&gt;&lt;span class=&#34;post-meta-item-icon&#34;&gt;&lt;i class=&#34;fa fa-file-word-o&#34;&gt;&lt;/i&gt;&lt;/span&gt;字数： {{ wordcount(post.content) }}&lt;/span&gt;</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="全站添加">全站添加</h2>
<p>打开<code>hexo\themes\hexo-theme-next\layout\_partials</code>路径下 footer.swig 文件，在你喜欢的位置添加以下代码</p>
<div class="highlight" id="id-4"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">&lt;div class=&#34;theme-info&#34;&gt;
</span></span><span class="line"><span class="cl">  &lt;div class=&#34;powered-by&#34;&gt;&lt;/div&gt;
</span></span><span class="line"><span class="cl">  &lt;span class=&#34;post-count&#34;&gt;全站共 {{ totalcount(site) }} 字&lt;/span&gt;
</span></span><span class="line"><span class="cl">&lt;/div&gt;</span></span></code></pre></td></tr></table>
</div>
</div>]]></description></item></channel></rss>