-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (46 loc) · 1.29 KB
/
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
/**
* Author Xuefei Chen
* Email [email protected]
* Created on 2017/8/18 10:58
*/
'use strict';
const url = require('url');
const pathFn = require('path');
// eslint-disable-next-line no-undef
const ctx = hexo;
const oss_config = ctx.config.asset_oss;
const enable =
oss_config.enable &&
oss_config.oss_url.length &&
oss_config.oss_acid.length &&
oss_config.oss_ackey.length &&
oss_config.oss_region.length &&
oss_config.oss_bucket.length;
// eslint-disable-next-line no-undef
hexo.extend.tag.register('ossimg', function(args) {
const PostAsset = ctx.model('PostAsset');
let imageRoot = ctx.config.root;
if (enable && oss_config.oss_root) {
imageRoot = pathFn.join(oss_config.oss_root, 'post-assets/');
imageRoot = url.resolve(oss_config.oss_url, imageRoot);
}
const slug = args.shift();
if (!slug) return;
const asset = PostAsset.findOne({ post: this._id, slug: slug });
if (!asset) return;
const title = args.length ? args.join(' ') : '';
const alt = title || asset.slug;
return (
'<img src="' +
url.resolve(imageRoot, asset.path) +
'" alt="' +
alt +
'" title="' +
title +
'">'
);
});
if (enable) {
// eslint-disable-next-line no-undef
hexo.extend.filter.register('after_generate', require('./lib/process')(ctx));
}