it('2 JSON overwrite', () => { var config = fs.readFileSync(mcp(base, 'test1.json,test2.json')); config = yml.safeLoad(config); config.author.should.eql('waldo'); config.favorites.food.should.eql('ice cream'); config.type.should.eql('elephant'); config = fs.readFileSync(mcp(base, 'test2.json,test1.json')); config = yml.safeLoad(config); config.author.should.eql('dinosaur'); config.favorites.food.should.eql('burgers'); config.type.should.eql('elephant'); });
it('JSON & YAML overwrite', () => { var config = fs.readFileSync(mcp(base, 'test1.yml,test1.json')); config = yml.safeLoad(config); config.author.should.eql('dinosaur'); config.favorites.food.should.eql('burgers'); config.type.should.eql('elephant'); config = fs.readFileSync(mcp(base, 'test1.json,test1.yml')); config = yml.safeLoad(config); config.author.should.eql('foo'); config.favorites.food.should.eql('sushi'); config.type.should.eql('dinosaur'); });
return criticalPromise.then(function (criticalCss) { var contents = fs.readFileSync(destFilename); var toReplace = new RegExp(options.htmlTagToReplace); var replacement = options.replacementHtmlHeader + criticalCss + options.replacementHtmlTrailer; var STRING_IS_NOT_PRESENT = -1; if (!toReplace.test(replacement)) { log.warn('The HTML tag hexo-critical-css is replacing is not put back by the replacement'); } if (criticalCss === '') { log.warn('critical did not return any CSS. hexo-critical-css will not inject an empty style string into the file', htmlFile); return; } if (toReplace.test(contents)) { if (contents.indexOf(replacement) === STRING_IS_NOT_PRESENT) { contents = contents.replace(toReplace, replacement); fs.writeFileSync(destFilename, contents); log.log('Generated critical CSS for', htmlFile); return; } log.log('critical CSS was already present in the file', htmlFile); return; } log.error('The HTML expression hexo-critical-css is attempting to replace is not present in the HTML for file', htmlFile, ': the match was', options.htmlTagToReplace); return; })
Render.prototype.renderSync = function(data, options){ if (!data) throw new TypeError('No input file or string!'); options = options || {}; var ctx = this.context; if (data.text == null){ if (!data.path) throw new TypeError('No input file or string!'); data.text = fs.readFileSync(data.path); } if (data.text == null) throw new TypeError('No input file or string!'); var ext = data.engine || getExtname(data.path); var result; if (ext && this.isRenderableSync(ext)){ var renderer = this.renderer.get(ext, true); result = renderer.call(ctx, data, options); } else { result = data.text; } var output = this.getOutput(ext) || ext; result = toString(result, data); return ctx.execFilterSync('after_render:' + output, result, { context: ctx, args: [data] }); };
it('2 YAML overwrite', () => { var configFile = mcp(base, 'test1.yml,test2.yml'); var config = fs.readFileSync(configFile); config = yml.safeLoad(config); config.author.should.eql('bar'); config.favorites.food.should.eql('candy'); config.type.should.eql('dinosaur'); config = fs.readFileSync(mcp(base, 'test2.yml,test1.yml')); config = yml.safeLoad(config); config.author.should.eql('foo'); config.favorites.food.should.eql('sushi'); config.type.should.eql('dinosaur'); });
hexo.extend.helper.register("getFileContent", function(fileName) { var result = fs.readFileSync("source/" + fileName); result = result.replace(/&/g, "&"); result = result.replace(/</g, "<"); result = result.replace(/>/g, ">"); return result; });
hexo.extend.tag.register('aplayer', function(args) { var title = args[0], author = args[1], url = args[2], lrcPath = '', narrow = false, autoplay = false, lrcOpt = false, width = '', pic = args[3] && args[3] !== 'narrow' && args[3] !== 'autoplay' && args[3].indexOf('lrc:') < 0 && args[3].indexOf('width:') < 0 ? args[3] : '', id = 'aplayer' + (counter++), raw = '', content = ''; // Parse optional arguments if (args.length > 3) { var options = args.slice(3); narrow = options.indexOf('narrow') < 0 ? false : true; autoplay = options.indexOf('autoplay') < 0 ? false : true; for (var i = 0; i < options.length; i++) { var option = options[i]; lrcOpt = option.indexOf('lrc:') == 0 ? true : lrcOpt; lrcPath = option.indexOf('lrc:') == 0 ? option.slice(option.indexOf(':') + 1) : lrcPath; width = option.indexOf('width:') == 0 ? option + ';' : width; } } width = narrow ? '' : width; raw = '<div id="'+ id + '" class="aplayer" style="margin-bottom: 20px;'+ width + '">'; if (lrcOpt) { // Generate lyric texts if (lrcPath.indexOf('http:') ==0 || lrcPath.indexOf('https:') ==0) { content = request('GET', lrcPath).getBody(); } else { var PostAsset = hexo.database._models.PostAsset; var _path = path.join(hexo.base_dir, PostAsset.findOne({post: this._id, slug: lrcPath})._id); content = fs.readFileSync(_path); } raw += '<pre class="aplayer-lrc-content">' + content + '</pre>'; } raw += '</div><script>var '+ id + ' = new APlayer({'+ 'element: document.getElementById("'+ id +'"),' + 'narrow: ' + (narrow ? 'true' : 'false') + ',' + 'autoplay: ' + (autoplay ? 'true' : 'false') + ',' + 'showlrc: ' + (lrcOpt ? '2' : '0') + ',' + 'music : {' + 'title: "'+ title +'",' + 'author: "'+ author +'",' + 'url: "'+ url + '",' + 'pic: "'+ pic + '"' + '}' + '});' + id + '.init();</script>'; return raw; });
module.exports.getCacheHash = function(hexoConfig){ var result = null; if( existsOption(hexoConfig) && fs.existsSync(getCachePath(hexoConfig)) ){ if(!read_cacheData){ read_cacheData = fs.readFileSync( getCachePath(hexoConfig) ); read_cacheData = JSON.parse(read_cacheData); } if(!read_cacheData.hash){ return ""; }else{ return read_cacheData.hash; } }else{ return ""; } };
File.prototype.readSync = function(options) { var content = this.content; options = wrapReadOptions(options); if (!options.cache || !content) { return fs.readFileSync(this.source, options); } var encoding = options.encoding; if (!encoding) return content; var result = content.toString(encoding); if (options.escape) return escapeContent(result); return result; };
module.exports.getCache = function(post, hexoConfig){ var result = null; if( existsOption(hexoConfig) && fs.existsSync(getCachePath(hexoConfig)) && !hexoConfig.generator_amp.isCacheUpdate ){ if(!read_cacheData){ // console.log("\u001b[31mrefreshed cache: \u001b[0m" + post.path); read_cacheData = fs.readFileSync( getCachePath(hexoConfig) ); read_cacheData = JSON.parse(read_cacheData); } for( var i=0; i<read_cacheData.ampData.length; i++){ if( read_cacheData.ampData[i].path == post.path ){ if( read_cacheData.ampData[i].date_eyeCatch == String(post.updated._i) && read_cacheData.ampData[i].date_amp == String(post.updated._i) ){ result = read_cacheData.ampData[i]; // console.log("use cached: "+post.path); break; } } } } return result; };
Render.prototype.renderSync = function(data, options) { if (!data) throw new TypeError('No input file or string!'); options = options || {}; const ctx = this.context; if (data.text == null) { if (!data.path) throw new TypeError('No input file or string!'); data.text = fs.readFileSync(data.path); } if (data.text == null) throw new TypeError('No input file or string!'); const ext = data.engine || getExtname(data.path); let result; if (ext && this.isRenderableSync(ext)) { const renderer = this.getRendererSync(ext); result = renderer.call(ctx, data, options); } else { result = data.text; } const output = this.getOutput(ext) || ext; result = toString(result, data); if (data.onRenderEnd) { result = data.onRenderEnd(result); } return ctx.execFilterSync(`after_render:${output}`, result, { context: ctx, args: [data] }); };
var path = require('path'); var ejs = require('ejs'); var Hexo = require('hexo'); var hexo = new Hexo(process.cwd(), {}); var fs = require('hexo-fs'); var layout = 'layout.ejs'; var bodyTag = '</body>'; var mathjaxScript = fs.readFileSync(path.join(__dirname, 'mathjax.html')); hexo.extend.renderer.register('ejs', 'html', function(data, options, callback) { var path = options.filename = data.path; var content = data.text; if (layout === path.substring(path.length - layout.length)) content = content.replace(bodyTag, mathjaxScript + '\n' + bodyTag); callback(null, ejs.render(content, options)); });
//--------------------------------------- // Writing cache file //--------------------------------------- function createCacheFile(inPath, hexoConfig, inPostPath , inDate , inEyeCatchImage , inTitleImageForAmp, inEyeCatchImageProperty , inXml , callback){ var cacheData; var newObj = { "path" : "" , "date_eyeCatch" : "" , "date_amp" : "" , "eyeCatchImage" : "" , "titleImageForAmp" : "" , "eyeCatchImageProperty" : "" , "xml" : "" }; var addObj = getAddObj(inPath, inPostPath , inDate , inEyeCatchImage , inTitleImageForAmp, inEyeCatchImageProperty , inXml); if( !fs.existsSync(getCachePath(hexoConfig)) || hexoConfig.generator_amp.isCacheClear ){ cacheData = { "hash": hexoConfig.generator_amp.hash , "ampData":[ assign(newObj,addObj) ] }; hexoConfig.generator_amp.isCacheClear = false; }else{ var isAddedData = false; var cacheData = fs.readFileSync(inPath); cacheData = JSON.parse(cacheData); for( var i=0; i<cacheData.ampData.length; i++){ if( cacheData.ampData[i].path == inPostPath ){ if( addObj.xml ){ if( cacheData.ampData[i].date_amp != inDate ){ cacheData.ampData[i] = assign(cacheData.ampData[i],addObj); } }else{ if( cacheData.ampData[i].date_eyeCatch != inDate ){ cacheData.ampData[i] = assign(cacheData.ampData[i],addObj); } } isAddedData = true; break; } } if(!isAddedData){ cacheData.ampData.push( addObj ); } } cacheData.hash = hexoConfig.generator_amp.hash mkdirp.sync( pathFn.dirname(inPath) ); fs.writeFileSync(inPath , JSON.stringify(cacheData)); read_cacheData = cacheData; // console.log("cached: "+inPostPath); newObj = null; addObj = null; if(callback)callback(); }
module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { const log = ctx.log; const defaultPath = pathFn.join(base, '_config.yml'); let paths; if (!configPaths) { log.w('No config file entered.'); return pathFn.join(base, '_config.yml'); } // determine if comma or space separated if (configPaths.includes(',')) { paths = configPaths.replace(' ', '').split(','); } else { // only one config let configPath = pathFn.isAbsolute(configPaths) ? configPaths : pathFn.resolve(base, configPaths); if (!fs.existsSync(configPath)) { log.w(`Config file ${configPaths} not found, using default.`); configPath = defaultPath; } return configPath; } const numPaths = paths.length; // combine files const combinedConfig = {}; let count = 0; for (let i = 0; i < numPaths; i++) { let configPath = ''; if (pathFn.isAbsolute(paths[i])) { configPath = paths[i]; } else { configPath = pathFn.join(base, paths[i]); } if (!fs.existsSync(configPath)) { log.w(`Config file ${paths[i]} not found.`); continue; } // files read synchronously to ensure proper overwrite order const file = fs.readFileSync(configPath); const ext = pathFn.extname(paths[i]).toLowerCase(); if (ext === '.yml') { merge(combinedConfig, yml.load(file)); count++; } else if (ext === '.json') { merge(combinedConfig, yml.safeLoad(file, {json: true})); count++; } else { log.w(`Config file ${paths[i]} not supported type.`); } } if (count === 0) { log.e('No config files found. Using _config.yml.'); return defaultPath; } log.i('Config based on', count, 'files'); const multiconfigRoot = outputDir || base; const outputPath = pathFn.join(multiconfigRoot, '_multiconfig.yml'); log.d(`Writing _multiconfig.yml to ${outputPath}`); fs.writeFileSync(outputPath, yml.dump(combinedConfig)); // write file and return path return outputPath; };
hexo.extend.helper.register("getFileContentRaw", function(fileName) { return fs.readFileSync("source/" + fileName); });
return function multiConfigPath(base, configPaths) { var log = ctx.log; var defaultPath = pathFn.join(base, '_config.yml'); var paths; if (!configPaths) { log.w('No config file entered.'); return pathFn.join(base, '_config.yml'); } // determine if comma or space separated if (configPaths.indexOf(',') > -1) { paths = configPaths.replace(' ', '').split(','); } else { // only one config if (!fs.existsSync(pathFn.join(base, configPaths))) { log.w('Config file ' + configPaths + ' not found, using default.'); return defaultPath; } return pathFn.resolve(base, configPaths); } var numPaths = paths.length; // combine files var combinedConfig = {}; var count = 0; for (var i = 0; i < numPaths; i++) { if (!fs.existsSync(pathFn.join(base, paths[i]))) { log.w('Config file ' + paths[i] + ' not found.'); continue; } // files read synchronously to ensure proper overwrite order var file = fs.readFileSync(pathFn.join(base, paths[i])); var ext = pathFn.extname(paths[i]).toLowerCase(); if (ext === '.yml') { _.merge(combinedConfig, yml.load(file)); count++; } else if (ext === '.json') { _.merge(combinedConfig, yml.safeLoad(file, {json: true})); count++; } else { log.w('Config file ' + paths[i] + ' not supported type.'); } } if (count === 0) { log.e('No config files found. Using _config.yml.'); return defaultPath; } log.i('Config based on', count, 'files'); var outputPath = pathFn.join(base, '_multiconfig.yml'); fs.writeFileSync(outputPath, yml.dump(combinedConfig)); // write file and return path return outputPath; };