如何使用docpad创建物理文件?

时间:2013-10-19 15:16:13

标签: node.js docpad

创建文件时docpad API page中的信息太少。

这是我尝试过的:

docpad.action("render", {
    text: content,
    filename: "random.html.md",
    path: "./src/documents/posts",
    attributes: {
        id: "some-random-id,
        title: "some-random-title",
        layout: "default"
    }
}, function(error, outContent, doc) {
    res.json({
        id: doc.get("id")
    });
});

它为我提供了文档实例,但是没有创建物理文件。

1 个答案:

答案 0 :(得分:1)

这是我用来创建虚拟文档并将其写入文件的方法

outDirPath = docpad.config.outPath 

docAttr = {
    fullPath: null
    body: 'some content html to be rendered'
    outPath: outDirPath + '/index.html' # this where it will write the file
    extension: 'md'      
    extensions: ['html', 'md']
    relativePath: 'index.html'
    filename: 'index.html'
    write: true 
    render: true
    layout: 'somelayout'
}

# create the document
virtualDocument = docpad.createDocument(docAttr)
docpad.renderDocument virtualDocument, { templateData: this, someCustomData: extraData}, (err, content, file) ->
    # renderDocument complete callback
    file.write ()->
        # file.write complete callback
        return

    return