API

This page will go over using DocPad as a module, and the API available to you.

Technical API

DocPad has auto-generated Technical API from its source code, check it out!

Install DocPad

Besides having Node.js installed, you'll want to install DocPad locally to your project, you can do this by running npm install --save docpad in your command line. This will install DocPad into ./node_modules/docpad and make it accessible via Node.js's require function (e.g., require('docpad'))

If you are wanting to utilise DocPad for rendering, you'll also want to install some rendering Plugins.

Create your DocPad Instance

Firstly, you need to create your DocPad instance, you can do this like so:

var docpadInstanceConfiguration = {};
require('docpad').createInstance(docpadInstanceConfiguration, function(err,docpadInstance){
    if (err)  return console.log(err.stack);
    // ...
});

Rendering individual files

You can use DocPad as a module to render individual files very easily. This allows you to utilise DocPad for all the rendering inside your application, instead of having to write and maintain specific wrappers for each rendering engine yourself.

Render some text with DocPad

var renderOpts = {
    text: 'here is some **markdown**',
    filename:'markdown',
    renderSingleExtensions:true
};
docpadInstance.action('render', renderOpts, function(err,result){
    console.log(result);
});

Render a file path with DocPad

var renderOpts = {
    path: '/some/file.html.md',
    renderSingleExtensions:true
};
docpadInstance.action('render', renderOpts, function(err,result){
    console.log(result);
});

DocPad CLI Actions

Here is how you would normalise common tasks you would typically achieve with the DocPad command line interface.

Performing a generation

// `generateOpts` is optional
var generateOpts = {
    collection: docpad.getCollection("myChangedPages"),  // only regenerate a subset
    reset:true                                           // default
};
// Which means it doesn't have to be included in the function call below
docpadInstance.action('generate', generateOpts, function(err,result){
    if (err)  return console.log(err.stack);
    console.log('OK');
});

Start the DocPad server

docpadInstance.action('server', function(err,result){
    if (err)  return console.log(err.stack);
    console.log('OK');
});

Generate and Start the DocPad Server

You can combine actions by separating them with a space, like so:

docpadInstance.action('generate server', function(err,result){
    if (err)  return console.log(err.stack);
    console.log('OK');
});

Perform an initial generation, then watch files and regenerate when a change occurs

docpadInstance.action('generate watch', function(err,result){
    if (err)  return console.log(err.stack);
    console.log('OK');
});

Using the Database

DocPad using Backbone.js for its Models, and QueryEngine for its Collections. Providing a powerful database that you can query in a noSQL type fashion.

Get the database

database = docpadInstance.getDatabase()

Create a Document and File

document = docpadInstance.createDocument(data, options)
file = docpadInstance.createFile(data, options)

Create a Document and File, then add it to the Database

database = docpadInstance.getDatabase()
document = docpadInstance.createDocument(data, options)
file = docpadInstance.createFile(data, options)
database.add(document)
database.add(file)

Parse a Document and File Directory

// options = {path}
// next(err, files)
docpadInstance.parseDocumentDirectory(options, next)
docpadInstance.parseFileDirectory(options, next)

Querying

// Get files, returns a cached live collection
resultCollection = docpadInstance.getFiles(query, sorting, paging)

// Get a file
resultModel = docpadInstance.getFile(query, sorting, paging)

// Get files at path (forwards onto getFiles)
resultCollection = docpadInstance.getFilesAtPath(path, sorting, paging)

// Get a file at a relative or absolute path or URL
resultModel = docpadInstance.getFileAtPath(path, sorting, paging)

// Get a file by its id
resultModel = docpadInstance.getFileById(id, {collection:null})

// Get a file by a route, useful when doing server requests
docpadInstance.getFileByRoute(url, function(err, resultModel){
    if ( err )  console.log(err.stack)
})

For more information about Querying, check out this FAQ Entry.

Using with Express

If you already have an Express.js application, you can do the following to just stick DocPad straight ontop of it:

// Create Server and Express Application
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app).listen(8080);

// Add our Application Middlewares
app.use(app.router);

// Add DocPad to our Application
var docpadInstanceConfiguration = {
    // Give it our express application and HTTP server
    serverExpress: app,
    serverHttp: server,

    // Tell it not to load the standard middlewares (as we handled that above)
    middlewareStandard: false
};
var docpadInstance = require('docpad').createInstance(docpadInstanceConfiguration, function(err){
    if (err)  return console.log(err.stack);

    // Tell DocPad to perform a generation, extend our server with its routes, and watch for changes
    docpadInstance.action('generate server watch', function(err){
        if (err)  return console.log(err.stack);
    });
});

// Continue with your application
// ...

Here is some code for manually rendering a document (inside src/render) with a custom route:

app.get '/alias-for-home', (req,res,next) ->
    req.templateData = {
        weDidSomeCustomRendering: true
    };
    var document = docpadInstance.getFile({relativePath:'home.html.md'});
    docpadInstance.serveDocument({document, req, res, next});

Last updated