Hexo安装和配置插件

本文最后更新于:2022年8月1日 晚上

Hexo安装和配置插件

准备工作

安装node环境

可以直接在nodejs官网下载安装包

准备路径后安装hexo

在想要安装node的路径下建立一个新的文件夹,避免装在根目录下

在新的文件夹下执行命令安装hexo

1
npm install hexo

测试hexo命令

安装各种插件

pandoc渲染latex公式

先在官网安装pandoc的包

Installing pandoc

再执行命令

1
npm install hexo-renderer-pandoc --save

安装mermaid流程图插件

1
npm install --save hexo-filter-mermaid-diagrams

安装后不能直接使用,需要修改部分文件,修改内容如下

参考文章:

Hexo博客在matery主题下插入mermaid流程图

安装支持本地图片的插件

  1. 插件安装

    1
    npm install hexo-asset-image --save

  2. 由于插件内容老旧失效,找到文件进行修改

    位置在/node_modules/hexo-asset-image/index.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    'use strict';
    var cheerio = require('cheerio');

    // http://stackoverflow.com/questions/14480345/how-to-get-the-nth-occurrence-in-a-string
    function getPosition(str, m, i) {
    return str.split(m, i).join(m).length;
    }

    var version = String(hexo.version).split('.');
    hexo.extend.filter.register('after_post_render', function(data){
    var config = hexo.config;
    if(config.post_asset_folder){
    var link = data.permalink;
    if(version.length > 0 && Number(version[0]) == 3)
    var beginPos = getPosition(link, '/', 1) + 1;
    else
    var beginPos = getPosition(link, '/', 3) + 1;
    // In hexo 3.1.1, the permalink of "about" page is like ".../about/index.html".
    var endPos = link.lastIndexOf('/') + 1;
    link = link.substring(beginPos, endPos);

    var toprocess = ['excerpt', 'more', 'content'];
    for(var i = 0; i < toprocess.length; i++){
    var key = toprocess[i];

    var $ = cheerio.load(data[key], {
    ignoreWhitespace: false,
    xmlMode: false,
    lowerCaseTags: false,
    decodeEntities: false
    });

    $('img').each(function(){
    if ($(this).attr('src')){
    // For windows style path, we replace '\' to '/'.
    var src = $(this).attr('src').replace('\\', '/');
    if(!/http[s]*.*|\/\/.*/.test(src) &&
    !/^\s*\//.test(src)) {
    // For "about" page, the first part of "src" can't be removed.
    // In addition, to support multi-level local directory.
    var linkArray = link.split('/').filter(function(elem){
    return elem != '';
    });
    var srcArray = src.split('/').filter(function(elem){
    return elem != '' && elem != '.';
    });
    if(srcArray.length > 1)
    srcArray.shift();
    src = srcArray.join('/');
    $(this).attr('src', config.root + link + src);
    console.info&&console.info("update link as:-->"+config.root + link + src);
    }
    }else{
    console.info&&console.info("no src attr, skipped...");
    console.info&&console.info($(this));
    }
    });
    data[key] = $.html();
    }
    }
    });

  3. 打开hexo配置文件_config.yml,找到这个配置项开启功能,目的是在每次使用hexo new生成新博客时,自动生成一个同名的目录存放静态文件

    1
    post_asset_folder: true

可以生成测试是否能正常加载blog中的本地图片

如果没能正确生成可以使用命令清理文件重新编译

1
hexo clean

参考链接

解决hexo引用本地图片无法显示的问题 - 掘金


Hexo安装和配置插件
https://ash-one.github.io/2022/08/01/hexo-an-zhuang-he-pei-zhi-cha-jian/
作者
灰一
发布于
2022年8月1日
许可协议