Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 3. generate categories from directory
// var categories = data.slug.split('/');
var categories = data.source.split('/');
// 3.1 handle depth
var depth = this.config.auto_category.depth || categories.length-2;
if (depth==0) { // Uncategorized
//tmpPost.categories = ["Uncategorized"];
return data;
}
var newCategories = categories.slice(1, 1+Math.min(depth, categories.length-2));
// 3.2 prevents duplicate file changes
if (Array.isArray(tmpPost.categories) && (tmpPost.categories.join("_") == newCategories.join("_"))) return data;
tmpPost.categories = newCategories
// 4. process post
postStr = front.stringify(tmpPost);
postStr = '---\n' + postStr;
fs.writeFile(data.full_source, postStr, 'utf-8');
log.i("Generated: categories [%s] for post [%s]", tmpPost.categories, categories[categories.length-1]);
}
return data
}
var fs = require('fs');
var frontMatter = require('hexo-front-matter');
var id = cuid();
/* eslint-disable camelcase */
var filepath = post.full_source || post.path;
/* eslint-enable camelcase */
var file = fs.readFileSync(filepath, 'utf-8');
var data = frontMatter.parse(file);
/* eslint-disable camelcase */
data.algolia_object_id = id;
post.algolia_object_id = id;
/* eslint-enable camelcase */
try {
fs.writeFileSync(filepath, frontMatter.stringify(data), 'utf-8');
console.log(green('INFO ') + 'Initialized: ' + magenta(post.path));
return post;
}
catch (err) {
console.log(red('ERROR ') + 'Can\'t initialize ' + post.path + ' - ' + err);
}
}
async.forEach(filenames, function(filename, cb) {
try {
file = fs.readFileSync(filename, 'utf-8');
data = frontMatter.parse(file);
if (data.algolia_object_id) {
cb();
}
else {
/* eslint-disable camelcase */
data.algolia_object_id = cuid();
/* eslint-enable camelcase */
fs.writeFileSync(filename, frontMatter.stringify(data));
console.log(chalk.green('Initialized: ') + chalk.magenta(path.basename(filename)));
modifiedFilesCount++;
cb();
}
}
catch (err) {
console.log(chalk.red('Can\'t process ' + filename + ' file : ' + err));
}
}, function(err) {
if (err) {
full_source = prev_full;
if (update.source && update.source !== post.source) {
// post.full_source only readable ~ see: /hexo/lib/models/post.js
full_source = hexo.source_dir + update.source
}
preservedKeys.forEach(function (attr) {
if (attr in update) {
compiled[attr] = update[attr]
}
});
compiled.date = moment(compiled.date).toDate()
delete update._content
var raw = hfm.stringify(compiled, {prefixSeparator: true}); // md 开头添加 ---
update.raw = raw
update.updated = moment()
// tags and cats are only getters now. ~ see: /hexo/lib/models/post.js
if ( typeof update.tags !== 'undefined' ) {
post.setTags(update.tags)
delete update.tags
}
if ( typeof update.categories !== 'undefined' ) {
post.setCategories(update.categories)
delete update.categories
}
extend(post, update)
post.save(function () {
var prev_full = post.full_source,
full_source = prev_full;
if (update.source && update.source !== post.source) {
// post.full_source only readable ~ see: /hexo/lib/models/post.js
full_source = hexo.source_dir + update.source
}
preservedKeys.forEach(function (attr) {
if (attr in update) {
compiled[attr] = update[attr]
}
});
compiled.date = moment(compiled.date).toDate()
delete update._content
var raw = hfm.stringify(compiled);
update.raw = raw
update.updated = moment()
// tags and cats are only getters now. ~ see: /hexo/lib/models/post.js
if ( typeof update.tags !== 'undefined' ) {
post.setTags(update.tags)
delete update.tags
}
if ( typeof update.categories !== 'undefined' ) {
post.setCategories(update.categories)
delete update.categories
}
extend(post, update)
post.save(function () {
// Parse front-matter
const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : yaml.load(frontMatter);
// Add data which are not in the front-matter
for (const key of Object.keys(data)) {
if (!preservedKeys.includes(key) && obj[key] == null) {
obj[key] = data[key];
}
}
let content = '';
// Prepend the separator
if (yfmSplit.prefixSeparator) content += `${separator}\n`;
content += yfm.stringify(obj, {
mode: jsonMode ? 'json' : ''
});
// Concat content
content += yfmSplit.content;
if (data.content) {
content += `\n${data.content}`;
}
return content;
});
};
var opt_rep = ((this.config.abbrlink && this.config.abbrlink.rep) ? this.config.abbrlink.rep : 'dec')
let res = (opt_alg == 'crc32' ? crc32.str(data.title) >>> 0 : crc16(data.title) >>> 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;
let postStr;
if (!/.*\.org/.test(data.source)){
//re parse front matter
var tmpPost = front.parse(data.raw);
//add new generated link
tmpPost.abbrlink = abbrlink;
//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');
}
if(data.title.length==0)
log.i("No title found for post [%s]", data.slug);
log.i("Generate link %s for post [%s]", abbrlink, data.title);
}
model.add(abbrlink);
}
return data
update(id, { meta, content }) {
let post = this.detail(id);
let compiled = hfm.parse(['---', meta, '---', content].join('\n'));
compiled.updated = compiled.updated || new Date();
compiled.date = compiled.date || new Date(post.date.valueOf());
compiled.author = compiled.author || post.author || this.hexo.config.author;
if (this.type === "Post") {
compiled.categories = compiled.categories || [this.hexo.config.default_category]
}
return fs.writeFile(post.full_source, hfm.stringify(compiled))
.then(() => this.updateDB())
.then(() => this.getSource(post.full_source));
}