/
logic.js
147 lines (133 loc) · 5.89 KB
/
logic.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
var crc16 = require('./crc16');
var crc32 = require('./crc32');
var model = require('./model');
var front = require('hexo-front-matter');
var fs = require('hexo-fs');
function org_get_abbrlink(data) {
var r = data.content.match(/#\+ABBRLINK:.*\n/);
if (r) {
data.abbrlink = r[0].split(':')[1].trim();
} else {
data.abbrlink = '';
}
return data;
}
let logic = function (data) {
var log = this.log;
const config = this.config.abbrlink || {};
//+++ Draft processing
var opt_drafts = this.config.abbrlink && this.config.abbrlink.drafts ? this.config.abbrlink.drafts : false;
if (opt_drafts == false) {
if (!this.config.render_drafts && data.source.startsWith('_drafts/')) return data;
}
//+++
if (data.layout == 'post') {
let abbrlink;
if (!/.*\.org/.test(data.source)) {
abbrlink = data.abbrlink;
} else {
abbrlink = org_get_abbrlink(data).abbrlink;
}
// if (!abbrlink || abbrlink == '0' || config.force) {
let root_path = data.source.startsWith('_drafts/') ? 'source/_drafts' : 'source/_posts'
//re parse front matter
var tmpPost = front.parse(data.raw);
// ------ auto title ?
if (this.config.abbrlink && this.config.abbrlink.auto_title && !tmpPost.title) {
// maybe the title is path/to/something.md
// so we split / first and split . again
const pathParts = data.source.split('/');
let last = pathParts[pathParts.length - 1];
let last2 = pathParts[pathParts.length - 2];
const endPort2 = last2.lastIndexOf('.');
var last2tail = ''
var last2front = ''
if (endPort2 > -1) {
last2tail = last2.substring(endPort2 + 1);
last2front = last2.substring(0, endPort2);
}
if (last2tail == 'textbundle'){
tmpPost.title = last2front;
} else {
const endPort = last.indexOf('.');
if (endPort > -1) {
last = last.substring(0, endPort);
}
tmpPost.title = last;
}
if (data.title.length == 0)
log.i('No title [%s] in post [ %s ]', data.title, data.full_source);
log.i('Generated: title [%s] for post [ %s ]', tmpPost.title, data.full_source);
}
// ----- auto date ? easy
if (this.config.abbrlink && this.config.abbrlink.auto_date && !tmpPost.date) {
tmpPost.date = data.date.format('YYYY-MM-DD HH:mm:ss');
log.i('Generated: date [%s] for post [ %s ]', tmpPost.date, data.full_source);
}
//From: hexo-auto-category
//see:https://github.com/xu-song/hexo-auto-category
//File: hexo-auto-category\lib\logic.js
var opt_AutoCategoryEnable = config.auto_category && config.auto_category.enable;
var overwrite = config.auto_category && config.auto_category.over_write;
if (opt_AutoCategoryEnable && overwrite) {
var categories = data.source.split('/');
if (categories.length - 2 >= 0) {
let last2 = categories[categories.length - 2];
const endPort2 = last2.lastIndexOf('.');
var last2tail = ''
var last2front = ''
if (endPort2 > -1) {
last2tail = last2.substring(endPort2 + 1);
last2front = last2.substring(0, endPort2);
}
if (last2tail == 'textbundle'){
categories.pop();
}
}
var opt_AutoCategoryDepth = config.auto_category.depth || 3;
var depth = opt_AutoCategoryDepth || categories.length - 2;
if (categories.length - 2 == 0 || depth == 0) {
tmpPost.categories = this.config.default_category;
} else {
var newCategories = categories.slice(1, 1 + Math.min(depth, categories.length - 2));
//prevents duplicate file changes
if (
!Array.isArray(tmpPost.categories) ||
tmpPost.categories.join('_') != newCategories.join('_')
) {
tmpPost.categories = newCategories;
log.i('Generated: categories [%s] for post [ %s ]', tmpPost.categories, data.full_source);
}
}
}
//add new generated link
if (!abbrlink || abbrlink == '0' || config.force) {
var opt_alg = this.config.abbrlink && this.config.abbrlink.alg ? this.config.abbrlink.alg : 'crc16';
var opt_rep = this.config.abbrlink && this.config.abbrlink.rep ? this.config.abbrlink.rep : 'dec';
let res = opt_alg == 'crc32' ? crc32.str(tmpPost.title + tmpPost.date) >>> 0 : crc16(tmpPost.title + tmpPost.date) >>> 0;
//check this abbrlink is already exist then get a different one
abbrlink = model.check(res);
//set abbrlink to hex or dec
abbrlink = opt_rep == 'hex' ? abbrlink.toString(16) : abbrlink;
data.abbrlink = abbrlink;
tmpPost.abbrlink = abbrlink;
log.i('Generated: link [%s] for post [ %s ]', tmpPost.abbrlink, data.full_source);
}
let abbrlink_int = opt_rep == 'hex' ? parseInt('0x'+abbrlink) : abbrlink
model.add(abbrlink_int);
let postStr;
if (!/.*\.org/.test(data.source)) {
//process post
postStr = front.stringify(tmpPost);
postStr = '---\n' + postStr;
fs.writeFileSync(data.full_source, postStr, 'utf-8');
} else {
postStr = data.raw.split('\n');
postStr.splice(2, 0, '#+ABBRLINK: ' + abbrlink);
fs.writeFileSync(data.full_source, postStr.join('\n'), 'utf-8');
}
}
return data;
};
module.exports = logic;