Hexo_blog_issue

Hexo 部署、配置、修改等常见问题记录

1. 写文章、发布文章

hexo new post article_title    # 新建一篇文章
# 编辑 [root]\source\_posts 下的markdown文件
hexo g   # 生成静态网页
hexo s   # 本地预览效果
hexo d   # 上传到github

文章开头的配置如下所示

title: 标题
catalog: 是否显示段落目录
date: 文章日期
subtitle: 子标题
header-img: 顶部背景图片
top: 是否置顶
tags: 标签
categories: 分类

2. 添加图片

  1. 修改配置文件,把主页配置文件 _config.yml 里的 post_asset_folder:这个选项设置为true

  2. 安装可以上传本地图片的插件, npm install hexo-asset-image --save。此时运行hexo n "xxxx"来生成md博文时,/source/_posts 文件夹内除了 xxxx.md 文件还有一个同名的文件夹。

  3. 将图片放在 xxxx 这个文件夹中,在xxxx.md中使用markdown的格式引入图片:

    ![title](xxxx/img_name.jpg)
  4. 修改 /node_modules/hexo-asset-image/index.js , 替换为如下代码:

    '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();
        }
      }
    });
  5. 检查 _config.yml 文件, 是否修改了对应的 URL:

    # URL
    ## If your site is put in a subdirectory, set url as 'http://example.com/child' and root as '/child/'
    url: https://polariszhao.github.io/  # 修改为对应的 URL

test_img


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!