Box 是一个用于处理指定文件夹中文件的容器。Hexo 使用两个不同的框:hexo.source 和 hexo.theme。前者用于处理 source 文件夹,后者用于处理 theme 文件夹。
¥Box is a container used for processing files in a specified folder. Hexo uses two different boxes: hexo.source and hexo.theme. The former is used to process the source folder and the latter to process the theme folder.
加载文件
¥Load Files
Box 提供了两种加载文件的方法:process 和 watch。process 加载文件夹中的所有文件。watch 执行相同的操作,但还会开始监视文件更改。
¥Box provides two methods for loading files: process and watch. process loads all files in the folder. watch does the same, but also starts watching for file changes.
box.process().then(function () { |
路径匹配
¥Path Matching
Box 提供了多种路径匹配方式。你可以使用正则表达式、函数或 Express 样式的模式字符串。例如:
¥Box provides many ways for path matching. You can use a regular expression, a function or an Express-style pattern string. For example:
posts/:id => posts/89 |
更多信息请参见 util.Pattern。
¥See util.Pattern for more info.
处理器
¥Processors
处理器是 Box 的基本元素,用于处理文件。你可以使用如上所述的路径匹配来限制处理器应该处理的内容。使用 addProcessor 方法注册新的处理器。
¥A processor is an essential element of Box and is used to process files. You can use path matching as described above to restrict what exactly the processor should process. Register a new processor with the addProcessor method.
box.addProcessor("posts/:id", function (file) { |
Box 将匹配到的文件内容传递给处理器。然后可以直接从回调中的 file 参数中读取此信息:
¥Box passes the content of matched files to processors. This information can then be read straight from the file argument in the callback:
| 属性 | 描述 |
|---|---|
source |
文件的完整路径 |
path |
文件框的相对路径 |
type |
文件类型。值可以是 create、update、skip、delete。 |
params |
来自路径匹配的信息。 |
Box 还提供了一些方法,因此你不必自己进行文件 IO。
¥Box also provides some methods so you don’t have to do file IO by yourself.
| 方法 | 描述 |
|---|---|
read |
读取文件 |
readSync |
同步读取文件 |
stat |
读取文件状态 |
statSync |
同步读取文件状态 |
render |
渲染文件 |
renderSync |
同步渲染文件 |