当前位置: 首页 > 文档资料 > GitBook 中文文档 >

回调函数

优质
小牛编辑
118浏览
2023-12-01

回调函数通过自定义回调来增强或改变进程行为的方法。

回调函数列表

全局属性

名称说明参数
init在解析书之后,生成输出页面之前调用。
finish:before在生成输出页面后调用,在复制资源,生成封面之前调用
finish所有操作完成后调用。

相对于页面管道

建议使用模板来扩展页面解析。

名称说明参数
page:before在页上运行模板引擎之前调用页面对象
page在输出和索引页面之前调用。页面对象
页对象
{
    // Parser named
    "type": "markdown",

    // File Path relative to book root
    "path": "page.md",

    // Absolute file path
    "rawpath": "/usr/...",

    // Title of the page in the SUMMARY
    "title": "",

    // Content of the page
    // Markdown/Asciidoc in "page:before"
    // HTML in "page"
    "content": "# Hello"
}
添加标题的示例

page:before钩子中,page.content是markdown/asciidoc内容。

{
    "page:before": function(page) {
        page.content = "# Title\n" +page.content;
        return page;
    }
}
替换一些html的示例

page回调函数中,将page.content<b>标签替换为<strong>

{
    "page": function(page) {
        page.content = page.content.replace("<b>", "<strong>")
            .replace("</b>", "</strong>");
        return page;
    }
}

异步操作

回调函数可以异步返回。

例如:

{
    "init": function() {
        return writeSomeFile()
        .then(function() {
            return writeAnotherFile();
        });
    }
}