Skip to content

Commit

Permalink
Add English API doc
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Mar 12, 2015
1 parent 51cbdbb commit ecfabaa
Show file tree
Hide file tree
Showing 18 changed files with 1,017 additions and 0 deletions.
60 changes: 60 additions & 0 deletions source/api/box.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
title: Box
---
A box is a container used to processing files in a specified folder. There're two boxes in Hexo: `hexo.source` and `hexo.theme`. The former is used to process the `source` folder and the letter is used to process the theme folder.

## Load Files

Box provides two methods for loading files: `process`, `watch`. The former is used to load all files in the folder; the letter do what `process` do and watch file changes continuously.

``` js
box.process().then(function(){
// ...
});

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

## Path Matching

Box provides many ways for path matching. You can use a regular expression, a function or a pattern string like Express. For example:

``` plain
posts/:id => posts/89
posts/*path => posts/2015/title
```

See [util.Pattern] for more info.

## Processors

A processor is a very important element in Box. It is used to process files. You can use the path matching above to restrict what should be processed by the processor. Use `addProcessor` to register a processor.

``` js
box.addProcessor('posts/:id', function(file){
//
});
```

Box passes the content of processing files (`file`) to processors. You can read the information from this argument.

Attribute | Description
--- | ---
`source` | Full path of the file
`path` | Relative path to the box of the file
`type` | File type. The value can be `create`, `update`, `skip`, `delete`.
`params` | The information from path matching.

Box also provides some methods so you don't have to do file IO by yourself.

Method | Description
--- | ---
`read` | Read a file
`readSync` | Read a file synchronously
`stat` | Read the status of a file
`statSync` | Read the status of a file synchronously
`render` | Render a file
`renderSync` | Render a file synchronously

[util.Pattern]: https://github.com/hexojs/hexo-util#patternrule
69 changes: 69 additions & 0 deletions source/api/console.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
title: Console
---
A console is the bridge between Hexo and users.

## Synopsis

``` js
hexo.extend.console.register(name, desc, options, function(args){
// ...
});
```

Argument | Description
--- | ---
`name` | Name
`desc` | Description
`options`| Options

An argument `args` will be passed into the functoin. This is the argument that users input in the terminal. It's parsed by [Minimist].

## Options

### usage

The usage of a console. For example:

``` js
{usage: '[layout] <title>'}
// hexo new [layout] <title>
```

### arguments

The description of each argument of a console. For example:

``` js
{
arguments: [
{name: 'layout', desc: 'Post layout'},
{name: 'title', desc: 'Post title'}
]
}
```

### options

The description of each option of a console. For example:

``` js
{
options: [
{name: '-r, --replace', desc: 'Replace existing files'}
]
}
```

### desc

More detailed information about a console.

## Example

``` js
hexo.extend.console.register('config', 'Display configuration', function(args){
console.log(hexo.config);
});
```

[Minimist]: https://github.com/substack/minimist
13 changes: 13 additions & 0 deletions source/api/deployer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
title: Deployer
---
A deployer helps users deploy their site to a remote server fast without complicated commands.

## Synopsis

``` js
hexo.extend.deployer.register(name, function(args){
// ...
});
```

An argument `args` will be passed into the function. It contains `deploy` setting in `_config.yml` and the input of users in the terminal.
52 changes: 52 additions & 0 deletions source/api/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
title: Events
---
Hexo inherits [EventEmitter]. You can use `on` method to listen to the events emitted by Hexo, or use `emit` method to emit events to Hexo. For more info, see the API documentation of Node.js.

### deployBefore

Emitted before deployment started.

### deployAfter

Emitted after deployment finished.

### exit

Emitted before Hexo exiting.

### generateBefore

Emitted before generation started.

### generateAfter

Emitted after generation finished.

### new

Emitted after a new post is created. This event will returns a data.

``` js
hexo.on('new', function(post){
//
});
```

Data | Description
--- | ---
`post.path` | Full path of the post file
`post.content` | Content of the post file

### processBefore

Emitted before processing started. This event will returns a path representing the root directory of the box.

### processAfter

Emitted after processing finished. This event will returns a path representing the root directory of the box.

### ready

Emitted after initialization finished.

[EventEmitter]: http://nodejs.org/api/events.html
182 changes: 182 additions & 0 deletions source/api/filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
title: Filter
---
A filter is used to modify some specified data. Hexo passes data to filter in sequence and filters can modify the data. This concept is stolen from [WordPress](http://codex.wordpress.org/Plugin_API#Filters).

## Synopsis

``` js
hexo.extend.filter.register(type, function(){
// ...
}, priority);
```

You can define the `priority`. Lower `priority` executes first. The default `priority` is 10.

## Execute Filters

``` js
hexo.extend.filter.exec(type, data, options);
hexo.extend.filter.execSync(type, data, options);
```

Option | Description
--- | ---
`context` | Context
`args` | Arguments. This must be an array.

The first argument passed into each filter is `data`. You can change `data` in the next filter by returning a new value. If nothing is returned then it won't be changed. You can even use `args` to specify other arguments in filters. For example:

``` js
hexo.extend.filter.register('test', function(data, arg1, arg2){
// data === 'some data'
// arg1 === 'foo'
// arg2 === 'bar'

return 'something';
});

hexo.extend.filter.register('test', function(data, arg1, arg2){
// data === 'something'
});

hexo.extend.filter.exec('test', 'some data', {
args: ['foo', 'bar']
});
```

You can also use the following methods to execute filters:

``` js
hexo.execFilter(type, data, options);
hexo.execFilterSync(type, data, options);
```

## Unregister Filters

``` js
hexo.extend.filter.unregister(type, filter);
```

## Filter List

The following is the filters used in Hexo.

### before_post_render

Executed before a post is rendered. You can see [post rendering](posts.html#Render) to learn the execution steps.

For example, transform the title to lower case.

``` js
hexo.extend.filter.register('before_post_render', function(data){
data.title = data.title.toLowerCase();
return data;
});
```

### after_post_render

Executed after a post is rendered. You can see [post rendering](posts.html#Render) to learn the execution steps.

For example, replace `@username` to the profile link on Twitter.

``` js
hexo.extend.filter.register('after_post_render', function(data){
data.content = data.content.replace(/@(\d+)/, '<a href="http://twitter.com/$1">#$1</a>');
return data;
});
```

### before_exit

Executed before Hexo is about to exit, which means executed after `hexo.exit` get called.

``` js
hexo.extend.filter.register('before_exit', function(){
// ...
});
```

### before_generate

Executed before generation started.

``` js
hexo.extend.filter.register('before_generate', function(){
// ...
});
```

### after_generate

Executed after generation done.

``` js
hexo.extend.filter.register('after_generate', function(){
// ...
});
```

### template_locals

Modify [local variables](../docs/variables.html) in templates.

For example, add the current time to the local variables of templates.

``` js
hexo.extend.filter.register('template_locals', function(locals){
locals.now = Date.now();
return locals;
});
```

### after_init

Executed after Hexo is initialized, which means executed after `hexo.init` is done.

``` js
hexo.extend.filter.register('after_init', function(){
// ...
});
```

### new_post_path

Executed when creating a post to determine the path of new posts.

``` js
hexo.extend.filter.register('new_post_path', function(data, replace){
// ...
});
```

### post_permalink

Used to determine the permalink of posts.

``` js
hexo.extend.filter.register('post_permalink', function(data){
// ...
});
```

### after_render

Executed after rendering. You can see [rendering](rendering.html#after_render_Filters) for more info.

### server_middleware

Add middlewares to the server. `app` is a [Connect] instance.

For example, add `X-Powered-By: Hexo` to the response header.

``` js
hexo.extend.filter.register('server_middleware', function(app){
app.use(function(req, res, next){
res.setHeader('X-Powered-By', 'Hexo');
next();
});
});
```

[Connect]: https://github.com/senchalabs/connect
Loading

0 comments on commit ecfabaa

Please sign in to comment.