sbg-api
Version:
Pre-processing all source posts before rendering from hexo. Useful for low-end devices to prevent using large number of hexo plugins.
103 lines • 5.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePost = void 0;
var tslib_1 = require("tslib");
var front_matter_1 = tslib_1.__importDefault(require("front-matter"));
var fs_1 = require("fs");
var hexo_post_parser_1 = require("hexo-post-parser");
var moment_timezone_1 = tslib_1.__importDefault(require("moment-timezone"));
var sbgUtils = tslib_1.__importStar(require("sbg-utility"));
var sbg_utility_1 = require("sbg-utility");
var upath_1 = require("upath");
var processingUpdate = {};
/**
* update metadata.updated post
* @returns
*/
function updatePost(postPath, callback) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var config, parse, oriUp, oriPath, post, rBuild, rebuild, build, hasError;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
// immediately return without callback
if (processingUpdate[postPath])
return [2 /*return*/, false];
// add to index
processingUpdate[postPath] = true;
config = sbgUtils.config.getConfig();
return [4 /*yield*/, (0, hexo_post_parser_1.parsePost)(postPath, {
shortcodes: {
youtube: true,
css: true,
include: true,
link: true,
now: true,
script: true,
text: true,
codeblock: true
},
cache: false,
config: config,
formatDate: true,
fix: true,
sourceFile: postPath
})];
case 1:
parse = _a.sent();
if (!(parse && parse.metadata)) return [3 /*break*/, 4];
oriUp = parse.metadata.updated;
oriPath = postPath;
parse.metadata.updated = (0, moment_timezone_1.default)()
.tz(config.timezone || 'UTC')
.format();
post = (0, front_matter_1.default)((0, fs_1.readFileSync)(postPath, 'utf-8'));
if ('updated' in post.attributes === false) {
post.attributes.updated = parse.metadata.updated;
}
post.attributes.updated = parse.metadata.updated;
post.attributes.date = parse.metadata.date;
if ('modified' in parse.metadata) {
post.attributes.modified = parse.metadata.modified;
}
// remove meta subtitle when description is same
if (post.attributes.description &&
post.attributes.subtitle &&
post.attributes.description == post.attributes.subtitle) {
delete post.attributes.subtitle;
}
rBuild = {
metadata: post.attributes,
body: post.body,
rawbody: post.body,
content: post.body,
config: config
};
rebuild = (0, hexo_post_parser_1.buildPost)(rBuild);
//writefile(join(config.cwd, 'tmp/rebuild.md'), rebuild);
sbg_utility_1.Logger.log('write to', (0, upath_1.toUnix)(oriPath).replace((0, upath_1.toUnix)(config.cwd), ''), oriUp, '->', post.attributes.updated);
return [4 /*yield*/, (0, sbg_utility_1.writefile)(oriPath, rebuild, { async: true })];
case 2:
_a.sent(); // write original post
build = (0, hexo_post_parser_1.buildPost)(parse);
return [4 /*yield*/, (0, sbg_utility_1.writefile)(postPath, build, { async: true })];
case 3:
_a.sent();
return [3 /*break*/, 5];
case 4:
sbg_utility_1.Logger.log('cannot parse', postPath);
(0, sbg_utility_1.writefile)((0, upath_1.join)(config.cwd, 'tmp/errors', updatePost.name, 'cannot-parse.log'), postPath, { append: true });
_a.label = 5;
case 5:
hasError = typeof (parse && parse.metadata) === 'undefined';
if (typeof callback === 'function')
callback(hasError, postPath);
// remove from index
delete processingUpdate[postPath];
return [2 /*return*/, hasError];
}
});
});
}
exports.updatePost = updatePost;
//# sourceMappingURL=update.js.map