Hello, Cactus!

jmfans 发布于 2023-11-07 258 次阅读


AI 摘要

Hello, Cactus! 是一篇关于Node.Js、Hexo和Cactus的安装与配置指南的文章。

Node.Js安装与配置

Linux

Nvm安装
# 以下操作基于Debian或Ubuntu等发行版
$ sudo apt install curl #安装curl
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash  #nvm安装脚本

$ nvm -v #查看版本号

# 若nvm无响应,执行以下操作
$ sudo vim ~/.bashrc
# 添加变量
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
# 退出并应用
$ source ~/.bashrc 
Node安装
$ nvm install 18.18.2   #指定版本v18.18.2
$ nvm use 18.18.2
$ npm -v    #查看是否安装成功
$ npm config set registry https://registry.npm.taobao.org   #指定国内源

# 若有需要,使用以下命令恢复官方源
$ npm config set registry https://registry.npmjs.org

# 或使用nrm管理源
$ npm install -g nrm    #安装nrm
$ nrm ls    #查看所有源
  npm ---------- https://registry.npmjs.org/
  yarn --------- https://registry.yarnpkg.com/
  tencent ------ https://mirrors.cloud.tencent.com/npm/
  cnpm --------- https://r.cnpmjs.org/
* taobao ------- https://registry.npm.taobao.org
  npmMirror ---- https://registry.npmmirror.com/

$ nrm use xxx   #切换源,例:nrm use taobao

Windows

待补充 To be added.

Hexo安装与配置

安装Hexo

$ npm install -g hexo-cli   #安装脚手架
$ hexo -v

# 如果使用的是宝塔面板的node管理器,需要软链接出来
$ ln -s /www/server/nodejs/{version}/lib/node_modules/hexo-cli/bin/hexo /usr/local/bin

# 新建博客
$ hexo init <项目名>
$ cd <项目名>

# 安装
$ npm install

# 编译、启动
$ hexo generate
$ hexo server

# 检查端口占用情况
$ sudo lsof -i :4000

# 清除编译内容
$ hexo clean
$ hexo new page <页面名称>
$ hexo new post "文章名"

Hexo配置

目录结构
.
├── _config.yml
├── package.json
├── scaffolds
├── source
|   ├── _drafts
|   └── _posts
└── themes
配置文件
title       网站标题
subtitle    网站副标题
description 网站描述
keywords    网站的关键词。支持多个关键词。
author  您的名字
language    网站使用的语言。对于简体中文用户来说,使用不同的主题可能需要设置成不同的值,请参考你的主题的文档自行设置,
            常见的有 zh-Hans和 zh-CN。
timezone    网站时区。Hexo 默认使用您电脑的时区。请参考 时区列表 进行设置,如 America/New_York, Japan, 和 UTC 。
            一般的,对于中国大陆地区可以使用 Asia/Shanghai。

url         网址, 必须以 http开头  
root        网站根目录
permalink   文章的 永久链接 格式 :year/:month/:day/:title/
permalink_defaults          永久链接中各部分的默认值    
pretty_urls 改写 permalink   的值来美化 URL    
pretty_urls.trailing_index  是否在永久链接中保留尾部的 index.html,设置为 false 时去除  true
pretty_urls.trailing_html   是否在永久链接中保留尾部的 .html, 设置为 false 时去除 (对尾部的 index.html无效)  true

highlight:  高亮配置替换如下
  enable: true
  auto_detect: false
  line_number: true
  line_threshold: 0
  tab_replace: ''
  exclude_languages:
    - example
  wrap: true
  hljs: false
prismjs:
  enable: false
  preprocess: true
  line_number: true
  line_threshold: 0
  tab_replace: ''

Cactus安装与配置

安装Cactus

# 确保当前目录为博客的主目录
$ git clone https://github.com/probberechts/hexo-theme-cactus.git themes/cactus

# 在_config.yml中按下述修改
# theme: landscape
theme: cactus

Cactus配置

# themes/cactus/_config.yml

# 配色 dark, light, white and classic
colorscheme: dark

# 导航栏
nav:
  home: /
  about: /about/
  articles: /archives/
  projects: http://github.com/probberechts
  LINK_NAME: URL

# 博客文章列表
posts_overview:
  show_all_posts: false
  post_count: 5

# 创建项目列表 /source/_data/projects.json
[
    {
       "name":"Hexo",
       "url":"https://hexo.io/",
       "desc":"A fast, simple & powerful blog framework"
    },
    {
       "name":"Font Awesome",
       "url":"http://fontawesome.io/",
       "desc":"The iconic font and CSS toolkit"
    }
]

# 社交链接
social_links:
  -
    icon: github
    link: your-github-url
  -
    icon: twitter
    label: "@your-twitter-handle"
    link: your-twitter-url
  -
    icon: NAME
    label: LABEL
    link: your-NAME-url

# 版权年份
copyright:
  start_year: 2016
  end_year:

# 访问分析
google_analytics:
  enabled: true
  id: 'UA-49627206-1'

baidu_analytics:
  enabled: true
  id: 2e6da3c375c8a87f5b664cea6d4cb29c

cloudflare_analytics:
  enabled: true
  id: 0dabb6e21848405483a237e6a7b6ed04

umami_analytics:
  enabled: false
  id: e77e68be-f6e4-4br3-9365-2b76b57cd571
  host: https://analytics.domain.com
  script_name: umami.js

# cdn
cdn:
  enable: true
  jquery: 'url'
  clipboard: 'url'
  font_awesome: 'url'
  justified_gallery_css: 'url'
  justified_gallery_js: 'url'

# 评论
disqus:
  enabled: true
  shortname: SITENAME
utterances:
  enabled: true
  repo: owner/githubrepo
  issue_term: pathname
  label: utteranc
  theme: themename

highlight: theme

# 启用MathJax
mathjax:
  enabled: true
可选配置
# 添加标签和类别
hexo new page tags
hexo new page categories

# themes/cactus/_config.yml
# 导航栏添加
nav:
  tag: /tags/
  category: /categories/

# 添加搜索页面
npm install hexo-generator-search --save
hexo new page search

# 在search中index.md添加
title: Search
type: search
---

# 导航栏添加
nav:
  search: /search/
进阶配置
# 下列配置有可能损坏文件!

# 页尾添加访问人数
# themes/cactus/_config.yml
busuanzi:
  enable: true

# 修改/themes/cactus/layout/_partial
<footer id="footer">

    <div class="footer-left">
      <%= __('footer.copyright') %> ©
      <% var endYear = (theme.copyright && theme.copyright.end_year) ? theme.copyright.end_year : new Date().getFullYear() %>
      <% var startYear = (theme.copyright && theme.copyright.start_year) ? theme.copyright.start_year : new Date().getFullYear() %>
      <%= startYear >= endYear ? endYear : startYear + "-" + endYear %>
      <%= config.author || config.title %>
      <br>
      <a href="https://beian.miit.gov.cn/" target="_blank">xICP备xxxxxxxx号-x</a>
    #如果在中国需要添加备案信息
    </div>

      <div class="footer-right">

        <nav>

          <ul>

            <% for (var i in theme.nav) { %><!--

          --><li><a href="<%- url_for(theme.nav[i]) %>"><%= __('nav.'+i).replace("nav.", "") %></a></li><!--

        --><% } %>

          </ul>

          <ul>

            <% if (theme.busuanzi && theme.busuanzi.enable){ %>

              <!-- 不蒜子统计 -->

              <span id="busuanzi_container_site_pv">

                  本站总访问量<span id="busuanzi_value_site_pv"></span>次

              </span>

              <span class="post-meta-divider">|</span>

              <span id="busuanzi_container_site_uv" style='display:none'>

                      本站访客数<span id="busuanzi_value_site_uv"></span>人

              </span>

            <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

            <% } %>

          </ul>
        </nav>
      </div>
</footer>

# 添加复制版权提示
# 修改themes/cactus/layout/post.ejs
# 末尾添加
<% if (page.copyright) { %>
      <script>
          function setClipboardText(event){
              // clipboardData 对象是为通过编辑菜单、快捷菜单和快捷键执行的编辑操作所保留的,也就是你复制或者剪切内容
              let clipboardData = event.clipboardData || window.clipboardData;
              // 如果未复制或者未剪切,则return出去
              if (!clipboardData) { return; }
              event.preventDefault();
              // Selection 对象,表示用户选择的文本范围或光标的当前位置。
              //     声明一个变量接收 -- 用户输入的剪切或者复制的文本转化为字符串
              let text = window.getSelection().toString();

              if (text) {
                // 如果文本存在则先取消文本默认事件
                event.preventDefault();
                // 通过调用常clipboardData对象的 setData(format, data) 方法;来设置相关文本
                // format: 一个DOMString 表示要添加到 drag object的拖动数据的类型。
                // data: 一个 DOMString表示要添加到 drag object的数据。
                var copyright = '\n --------------- \n'
                                + '\n著作权归作者所有。'
                                + '\n商业转载请联系作者获得授权,非商业转载请注明出处。'
                                + '\n作者: '
                                + '\n邮箱:'
                                + '\n原文地址: <%= page.permalink %>'

                clipboardData.setData('text/plain', text + copyright);

              }
          };
          var contents = document.getElementsByClassName("content");
          // 监听文章内容的copy事件
          contents[0].addEventListener('copy',function(e){
              setClipboardText(e);
          });
      </script>
<% } %>