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.

133 lines 6.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.taskSafelink = void 0; var tslib_1 = require("tslib"); var ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors")); var fs_1 = require("fs"); var gulp_1 = tslib_1.__importDefault(require("gulp")); var path_1 = tslib_1.__importDefault(require("path")); var safelinkify_1 = tslib_1.__importDefault(require("safelinkify")); var sbg_utility_1 = require("sbg-utility"); var through2_1 = tslib_1.__importDefault(require("through2")); /** * Process Safelink on Deploy Dir * @param _done callback function * @param cwd working directory to scan html's * @returns */ function taskSafelink(_done, cwd) { var _this = this; var _a, _b, _c; var config = (0, sbg_utility_1.getConfig)(); var workingDir = typeof cwd === 'string' ? cwd : config.deploy.deployDir; var logname = ansi_colors_1.default.greenBright('safelink'); // skip process safelink var hasError = false; if (!config.external_link.safelink) { hasError = true; sbg_utility_1.Logger.log(logname, 'config safelink', ansi_colors_1.default.red('not configured')); } if (!config.external_link.safelink.redirect) { hasError = true; sbg_utility_1.Logger.log(logname, 'safelink redirector', ansi_colors_1.default.red('not configured')); } if (!config.external_link.safelink.enable) { hasError = true; sbg_utility_1.Logger.log(logname, ansi_colors_1.default.red('disabled')); } if ((0, fs_1.existsSync)(workingDir) && !hasError) { var defaultConfigSafelink = { enable: false, exclude: [], redirect: 'https://www.webmanajemen.com/page/safelink.html?url=', password: 'root', type: 'base64' }; var configSafelink = Object.assign(defaultConfigSafelink, config.external_link.safelink || {}); var baseURL = ''; try { baseURL = new URL(config.url).host; } catch (_d) { // } var opt = { // exclude patterns (dont anonymize these patterns) exclude: tslib_1.__spreadArray(tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read((((_a = config.external_link) === null || _a === void 0 ? void 0 : _a.exclude) || [])), false), [ /https?:\/\/?(?:([^*]+)\.)?webmanajemen\.com/, /([a-z0-9](?:[a-z0-9-]{1,61}[a-z0-9])?[.])*webmanajemen\.com/, baseURL, 'www.webmanajemen.com', 'https://github.com/dimaslanjaka', 'https://facebook.com/dimaslanjaka1', 'dimaslanjaka.github.io' ], false), tslib_1.__read(configSafelink.exclude), false).filter(function (x, i, a) { // remove duplicate and empties return a.indexOf(x) === i && x.toString().trim().length !== 0; }), redirect: [], //redirect: [, configSafelink.redirect], password: configSafelink.password || config.external_link.safelink.password, type: configSafelink.type || config.external_link.safelink.type }; if (configSafelink.redirect) { opt.redirect = configSafelink.redirect; } var safelink_1 = new safelinkify_1.default.safelink(opt); var gulpopt = { cwd: workingDir, ignore: [] }; if (Array.isArray(config.external_link.exclude)) { (_b = gulpopt.ignore) === null || _b === void 0 ? void 0 : _b.concat.apply(_b, tslib_1.__spreadArray([], tslib_1.__read(config.external_link.exclude), false)); } if (Array.isArray(configSafelink.exclude)) { var ignore = configSafelink.exclude.filter(function (str) { if (typeof str === 'string') { return !/^(https?:\/|www.)/.test(str); } return false; }); (_c = gulpopt.ignore) === null || _c === void 0 ? void 0 : _c.concat.apply(_c, tslib_1.__spreadArray([], tslib_1.__read(ignore), false)); } return gulp_1.default .src(['**/*.{html,htm}'], gulpopt) .pipe((0, sbg_utility_1.gulpCached)({ name: 'safelink' })) .pipe(through2_1.default.obj(function (file, _enc, next) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var content, parsed; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: // drops if (file.isNull() || file.isDirectory() || !file || file.isStream()) return [2 /*return*/, next()]; if (!(file.isBuffer() && Buffer.isBuffer(file.contents))) return [3 /*break*/, 2]; content = file.contents.toString('utf-8'); return [4 /*yield*/, safelink_1.parse(content)]; case 1: parsed = _a.sent(); if (typeof parsed === 'string') { // Logger.log(parsed); file.contents = Buffer.from(parsed); return [2 /*return*/, next(null, file)]; } _a.label = 2; case 2: sbg_utility_1.Logger.log('cannot parse', file.path); // drop fails next(); return [2 /*return*/]; } }); }); })) .pipe(gulp_1.default.dest(workingDir)); } else { var wstream = (0, sbg_utility_1.createWriteStream)(path_1.default.join(config.cwd, 'tmp/errors/safelink.log')); return wstream; } } exports.taskSafelink = taskSafelink; // safelinkify the deploy folder gulp_1.default.task('safelink', taskSafelink); //# sourceMappingURL=gulp.safelink.js.map