Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function initPost(post) {
var cuid = require('cuid');
var chalk = require('chalk');
var green = chalk.green;
var magenta = chalk.magenta;
var red = chalk.red;
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);
}
}
module.exports = function (model, id, update, callback, hexo) {
function removeExtname(str) {
return str.substring(0, str.length - path.extname(str).length);
}
var post = hexo.model(model).get(id)
if (!post) {
return callback('Post not found');
}
var config = hexo.config,
slug = post.slug = hfm.escape(post.slug || post.title, config.filename_case),
layout = post.layout = (post.layout || config.default_layout).toLowerCase(),
date = post.date = post.date ? moment(post.date) : moment();
var split = hfm.split(post.raw),
frontMatter = split.data
compiled = hfm.parse([frontMatter, '---', split.content].join('\n'));
var preservedKeys = ['title', 'date', 'tags', 'categories', '_content', 'author'];
Object.keys(hexo.config.metadata || {}).forEach(function (key) {
preservedKeys.push(key);
});
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]
}
module.exports = function (model, id, update, callback, hexo) {
var post = hexo.model(model).get(id)
if (!post) {
return callback('Post not found');
}
var config = hexo.config,
slug = post.slug = hfm.escape(post.slug || post.title, config.filename_case),
layout = post.layout = (post.layout || config.default_layout).toLowerCase(),
date = post.date = post.date ? moment(post.date) : moment();
var split = hfm.split(post.raw),
frontMatter = split.data
compiled = hfm.parse([frontMatter, '---', split.content].join('\n'));
var preservedKeys = ['title', 'date', 'tags', 'categories', '_content', 'keywords', 'description'];
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()
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));
}
let logic = function(data) {
var log = this.log;
if (data.layout != 'post')
return data;
if (!this.config.render_drafts && data.source.startsWith("_drafts/"))
return data;
var overwrite = true;
if (this.config.auto_category.enable && overwrite) {
let postStr;
// 1. parse front matter
var tmpPost = front.parse(data.raw);
// 2. read old categories
//
// 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
.map((post, index) => ({
// cut out front matter and surrounding whitespace
contents: post.replace(/---[\w\W]*---/, '').trim(),
frontMatter: parseFrontMatter(post),
// get filename without parent directory and without '.md'
slug: postFilenames[index].split('/').slice(-1)[0].slice(0, -3),
}))
.filter(({ frontMatter }) => {
// 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) {
module.exports = function (model, id, update, callback, hexo) {
var post = hexo.model(model).get(id)
if (!post) {
return callback('Post not found');
}
var config = hexo.config,
slug = post.slug = hfm.escape(post.slug || post.title, config.filename_case),
layout = post.layout = (post.layout || config.default_layout).toLowerCase(),
date = post.date = post.date ? moment(post.date) : moment();
var split = hfm.split(post.raw),
frontMatter = split.data
compiled = hfm.parse([frontMatter, '---', split.content].join('\n'));
var preservedKeys = ['title', 'date', 'tags', 'categories', '_content', 'keywords', 'description'];
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) {