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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| "use strict"; const ob = hexo.config.obsidian_link;
;
/%20匹配%20[](xx) 形式 const reg_posts = /\[\s*([^\x00-\x1f\x7f]*?)\s*\]\(\s*(?!\S+:\/\/)(.+?)\.md\s*\)/g;
,%20/^%20各种空格替换成%20 ];
/** %20*%20文章渲染之前批处理%20替换链接,修复路径 %20*/ hexo.extend.filter.register("before_post_render",%20(data)%20=>%20{ %20/%20加%20url_fix%20以避免在首页预览时链接错误 %20const%20url_fix%20=%20ob.url_fix_enable %20?%20new%20URL(data.permalink).pathname%20+%20"../" %20:%20"";
%20/** %20*%20为[wiki%20link](wiki%20link)做处理,wiki%20link%20to%20md%20link, %20*%20wiki%20link可以是.md%20.png%20.pdf%20.cn%20.com等等形式,所以先转为普通md%20link, %20*%20保留.md%20.png等后缀不做处理,在后面再做处理 %20*%20^%20[label](path%20)%20[%20label](wikilink%20)%20后面的%20label可有可无 %20*%20^%20split_label%20-->%20`|%20label` %20*/ %20data.content%20=%20data.content.replace( %20reg_wiki, %20(_match_str,%20path,%20_split_label,%20label)%20=>%20{ %20return%20result_format("",%20url_fix,%20chooseLabel(label,%20path),%20path,%20""); %20} %20);
%20/** %20*%20为[](xxx)做处理去除.md后缀
%20*/ %20data.content%20=%20data.content.replace(reg_posts,%20(_match_str,%20label,%20path)%20=>%20{ %20return%20result_format( %20ob.posts_prefix, %20url_fix, %20chooseLabel(label,%20path), %20path, %20ob.posts_extend %20); %20});
%20/** %20*%20为附件assets:%20pdf/json等做处理,修改为cdn %20*%20不处理md%20link%20/wiki%20link %20*%20^%20[](xxx.pdf) [label](path)
data.content = data.content.replace(reg_assets, (_match_str, label, path) => { return result_format( ob.assets_prefix, url_fix, chooseLabel(label, path), path, ob.assets_extend ); });
data.content = data.content.replace(reg_img, (_match_str, label, path) => { return ( "!" + result_format( ob.img_prefix, url_fix, chooseLabel(label, path), path, ob.img_extend ) ); });
return data; });
function chooseLabel(label, path) { return label ? label : path; }
function result_format(prefix, url_fix, label, path, extend) { var fianl_link = (prefix + url_fix + path + extend) .replace(reg_url_format[0], "/") .replace(reg_url_format[1], "/") .replace(reg_url_format[2], "%20"); var result_str = "[" + label + "](" + fianl_link + ")"; return result_str; }
|