UNPKG

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.

70 lines 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSourcePosts = void 0; var tslib_1 = require("tslib"); var fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); var glob = tslib_1.__importStar(require("glob")); var sbg_utility_1 = require("sbg-utility"); var upath_1 = tslib_1.__importDefault(require("upath")); var copy_1 = require("./copy"); /** * get all source markdown posts (_configyml.post_dir) * @returns */ function getSourcePosts(config) { return tslib_1.__awaiter(this, void 0, void 0, function () { var cachePath, _a, _b, _c, sourcePostDir, results, matches, promises; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: if (!config) config = (0, sbg_utility_1.getConfig)(); if (!config.cache) config.cache = true; if (!config.cwd) throw new Error('config.cwd is required'); if (!config.post_dir) throw new Error('config.post_dir is required'); // default cache directory if (!config.cacheDirectory) config.cacheDirectory = upath_1.default.join(config.cwd, 'tmp'); cachePath = upath_1.default.join(config.cacheDirectory, 'source-posts.json'); _a = config.cache; if (!_a) return [3 /*break*/, 2]; return [4 /*yield*/, fs_extra_1.default.exists(cachePath)]; case 1: _a = (_d.sent()); _d.label = 2; case 2: if (!_a) return [3 /*break*/, 4]; _c = (_b = JSON).parse; return [4 /*yield*/, fs_extra_1.default.readFile(cachePath, 'utf-8')]; case 3: return [2 /*return*/, _c.apply(_b, [_d.sent()])]; case 4: sourcePostDir = upath_1.default.join(config.cwd, config.post_dir); results = []; if (!(results.length === 0)) return [3 /*break*/, 7]; return [4 /*yield*/, glob.glob('**/*.md', { cwd: sourcePostDir, realpath: true, absolute: true })]; case 5: matches = _d.sent(); promises = matches.map(function (p) { return (0, copy_1.processSinglePost)(p, function (parsed) { results.push(Object.assign(parsed, { full_source: p })); }); }); // wait all promises to be resolved return [4 /*yield*/, Promise.all(promises)]; case 6: // wait all promises to be resolved _d.sent(); // write cache (0, sbg_utility_1.writefile)(cachePath, (0, sbg_utility_1.jsonStringifyWithCircularRefs)(results)); _d.label = 7; case 7: return [2 /*return*/, results]; } }); }); } exports.getSourcePosts = getSourcePosts; exports.default = getSourcePosts; //# sourceMappingURL=get-source-posts.js.map