0%

hexo markdown 一些 trick

hexo 写 blog 的一些很难在网上找到好办法的问题及答案。

Q: 如何在文章中实现对于本地文章的超链接

A: 在普通的 markdown 文档中,我们可以直接使用类似超链接的的方式,
使用 [目标文章](目标文章的本地相对路劲) 来实现。但是在 hexo 中 markdown 文档被转换为 html 之后,这样的信息肯定是不行的。因此,需要按下面的方式来。

1
{% post_link 目标文章的title %}

注意这里 title 必须是文章内 title ,而不能是 markdown 文档的名字。由于文章 title 可能会有空格,因此直接用 hexo 自动生成的 xx.md 的文件名即可。
这个语法在 markdown 中是没有预览的,只有在最终生成的 html 中有效果。

for example :

1
{% post_link 我的少年时代 %}
我的少年时代

Q: 使用 hexo-asset-image 插件,图片无法正常显示

A: 通过修改hexo-asset-image这个插件的源代码来实现的,找到路径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
62

'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();
}
}
});
加个鸡腿呗.