API

  • 2018-12-03

This documentation provides more detailed information about the API and will be particularly helpful for people who want to modify the Hexo source code or write new plugins. If you are interested in more basic usage of Hexo, please refer to the docs instead.

Please note that this documentation is only valid for Hexo 3 and above.

Initialize

First, we have to create a Hexo instance. A new instance takes two arguments: the root directory of the website, base_dir, and an object containing the initialization options. Next, we initialize this instance by calling the init method on it, which will then cause Hexo to load its configuration and plugins.

1
2
3
4
5
6
var Hexo = require('hexo');
var hexo = new Hexo(process.cwd(), {});

hexo.init().then(function(){
// ...
});
Option Description Default
debug Enable debug mode. Display debug messages in the terminal and save debug.log in the root directory. false
safe Enable safe mode. Don’t load any plugins. false
silent Enable silent mode. Don’t display any messages in the terminal. false
config Specify the path of the configuration file. _config.yml

Load Files

Hexo provides two methods for loading files: load and watch. load is used for loading all files in the source folder as well as the theme data. watch does the same things load does, but will also start watching for file changes continuously.

Both methods will load the list of files and pass them to the corresponding processors. After all files have been processed, they will call upon the generators to create the routes.

1
2
3
4
5
6
7
hexo.load().then(function(){
// ...
});

hexo.watch().then(function(){
// You can call hexo.unwatch() later to stop watching.
});

Execute Commands

Any console command can be called explicitly using the call method on the Hexo instance. Such a call takes two arguments: the name of the console command, and an options argument. Different options are available for the different console commands.

1
2
3
hexo.call('generate', {}).then(function(){
// ...
});

Exit

You should call the exit method upon successful or unsuccessful completion of a console command. This allows Hexo to exit gracefully and finish up important things such as saving the database.

1
2
3
4
5
hexo.call('generate').then(function(){
return hexo.exit();
}).catch(function(err){
return hexo.exit(err);
});

扫码领红包

2018双11超级红包
PC访问链接: 2018双11超级红包 预售开启 汇聚全球潮流新品

淘口令:¥20Y0b6q4eHR¥ (复制此行再打开手机淘宝客访问)

最后更新: 2018年12月03日 09:29

原始链接: https://cyc.oy99.com/api/index.html

× 多少都行~
打赏二维码
×
  • {title}