hexo博文包含图片的坑

hexo博文包含图片的坑

网上有很多关于这个的教程,主要的总结如下

  • ①修改博客目录下的_config_yml的post_asset_folder为true
post_asset_folder: true
  • ②安装hexo-asset-image插件
npm install hexo-asset-image --save
  • ③hexo new file_name 时会在source/_post/下生成file_name的文件夹,将需要使用的图片放置在里面,然后使用相对路径引入
![用于图片加载失败时显示的内容](/file_name/image_name)
  • 如此博客中的图片最后会和.md文件一起生成到public\2019\12\27\file_name中,这样在hexe g 时,可以看到命令窗口会打印修改后的路径,如下
Start processing
update link as:-->/2019/12/27/first/1577523021175.png
update link as:-->/2019/12/27/first/1577523021175.png

我遇到的问题

Start processing
update link as:-->.io//2019/12/27/first/1577523021175.png
update link as:-->.io//2019/12/27/first/1577523021175.png
  • 经过一番搜寻,发现hexo-asset-image会将图片的地址修改,具体的源码信息可见\node_modules\hexo-asset-image\index.js,打开后内容如下:
'use strict';
var cheerio = require('cheerio');
function getPosition(str, m, i) {
  return str.split(m, i).join(m).length;
}

hexo.extend.filter.register('after_post_render', function(data){
  var config = hexo.config;
  if(config.post_asset_folder){
    var link = data.permalink;
    var beginPos = getPosition(link, '/', 3) + 1;
    var appendLink = '';
    // In hexo 3.1.1, the permalink of "about" page is like ".../about/index.html".
    // if not with index.html endpos = link.lastIndexOf('.') + 1 support hexo-abbrlink
    if(/.*\/index\.html$/.test(link)) {
      // when permalink is end with index.html, for example 2019/02/20/xxtitle/index.html
      // image in xxtitle/ will go to xxtitle/index/
      appendLink = 'index/';
      var endPos = link.lastIndexOf('/');
    }
    else {
      var endPos = link.lastIndexOf('.') ;
    }
    link = link.substring(beginPos, endPos) + '/' + appendLink;

    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)
            || /^\s*\/uploads|images\//.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();
    }
  }
});
  • 通过查看源码发现里面有对生成博客图片的地址修改:
link = link.substring(beginPos, endPos) + '/' + appendLink;
  • 通过排查发现图片的路径的endPos为:
var endPos = link.lastIndexOf('.') ;
  • 我打印data.permalink得到
http://tigerLuHai.github.io/2019/12/27/first/

如此在截取字符串的时候就会多出四个字符 .io/

最后发现这段代码的作用就是要将data.permalink中路径的https://tigerLuhai.gituhub.io/去掉,因为在后面部署到github时使用相对路径访问会重新加上这个前缀,如果这里有就会重复,导致地址为https://tigerLuhai.gituhub.io/http://tigerLuHai.github.io/2019/12/27/first/的现象.

明白了需求就可以修改代码为

var endPos = link.lastIndexOf('/') ;

这样就可以正常部署了.


 上一篇
使用VMware安装linux虚拟机 使用VMware安装linux虚拟机
使用VMware安装linux虚拟机使用背景Linux是一种自由和开放源码的操作系统,存在着许多不同的Linux发行版本,但它们都使用了Linux内核。现在的服务器基本都是使用linux,其中CentOS使用广泛,还有ubuntu也是l
2019-12-29
下一篇 
FastDFS分布式文件系统安装使用教程 FastDFS分布式文件系统安装使用教程
FastDFS分布式文件系统安装使用教程使用场景分布式文件系统用于海量文件存储及传输访问的瓶颈问题,对海量视频的管理、对海量图片的管理等,FastDFS与其他分布式文件系统相比的一个显著优点就是特别适合大量小文件(图片等)的存储,因为它
2019-12-28
  目录