-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcode.ts
More file actions
42 lines (37 loc) · 1.02 KB
/
code.ts
File metadata and controls
42 lines (37 loc) · 1.02 KB
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
import { Pattern } from 'hexo-util';
import { relative } from 'path';
import type Hexo from '../../hexo';
import type { _File } from '../../box';
export = (ctx: Hexo) => {
let codeDir = ctx.config.code_dir;
if (!codeDir.endsWith('/')) codeDir += '/';
return {
pattern: new Pattern(path => {
return path.startsWith(codeDir);
}),
process: function codeProcessor(file: _File) {
const id = relative(ctx.base_dir, file.source).replace(/\\/g, '/');
const slug = relative(ctx.config.source_dir, id).replace(/\\/g, '/');
const Code = ctx.model('Code');
const doc = Code.findById(id);
if (file.type === 'delete') {
if (doc) {
return doc.remove();
}
return;
}
if (file.type === 'skip' && doc) {
return;
}
return file.read().then(content => {
return Code.save({
_id: id,
path: file.path,
slug,
modified: file.type !== 'skip',
content
});
});
}
};
};