// 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; } function getLocation(str, n) { var index=str.lastIndexOf('/'); for (vari=0; i < n-1; i++) { index=str.lastIndexOf('/',index-1); } var location=str.substring(index+1); return location; }
hexo.extend.filter.register('after_post_render', function(data){ varconfig= hexo.config; if(config.post_asset_folder){ varlink= data.permalink; varbeginPos= getPosition(link, '/', 4); // In hexo 3.1.1, the permalink of "about" page is like ".../about/index.html". varendPos= link.lastIndexOf('/');
$('img').each(function(){ if ($(this).attr('src')){ // For windows style path, we replace '\' to '/'. varsrc= $(this).attr('src').replace('\\', '/'); varsrc_temp= getLocation(src, 3); console.info&&console.info("yuanshi src:" + src+" del src: "+src_temp);
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. varlinkArray= link.split('/').filter(function(elem){ return elem != ''; }); varsrcArray= src.split('/').filter(function(elem){ return elem != '' && elem != '.'; }); if(srcArray.length > 1) srcArray.shift(); src = srcArray.join('/');
//$(this).attr('src', config.root + link + src); //notes: src is image name,and config.root is localhost:port,the middle cahr string is my local image Repository if(src_temp.split('/')[1].substring(0,5)=="image"){ link = src_temp.split('/')[1]+'/'; }else{ varlink_temp= getLocation(data.permalink, 3); link = link_temp.split('/')[1]+'/'; // link = '' // src = src_temp } if(src_temp.substring(0,5) =="img/a"){ link="diary/img/"; console.info&&console.info("judement the seventh capture!"); } $(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)); console.info&&console.info('config file = porterpan.github.io/node_modules/hexo-asset-image/index.js') } }); data[key] = $.html(); } } });