hexo 图片路径错误/.com//

问题描述

hexo发博客的时候想插入一张图片,路径莫名奇妙的多了/.com//
请问一下这个路径是怎么产生的?找不到原因哇
下面贴几张图

配置:
clipboard.png

服务:
clipboard.png

控制台:
clipboard.png

阅读 5.8k
1 个回答
✓ 已被采纳新手上路,请多包涵

这个错误是插件的bug
一、先卸载 hexo-asset-image
二、重新下载 npm install https://github.com/CodeFallin... --save
三、修改js,一定要改,不然这个bug解决了,还会有别的bug出现,所以打开文件/node_modules/hexo-asset-image/index.js,把下面这段复制进去就ok

'use strict';
var cheerio = require('cheerio');

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();
    }
  }
});


撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题