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) returnconsole.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 optionalvar 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 belowdocpadInstance.action('generate', generateOpts,function(err,result){if (err) returnconsole.log(err.stack);console.log('OK');});
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 files, returns a cached live collectionresultCollection =docpadInstance.getFiles(query, sorting, paging)// Get a fileresultModel =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 URLresultModel =docpadInstance.getFileAtPath(path, sorting, paging)// Get a file by its idresultModel =docpadInstance.getFileById(id, {collection:null})// Get a file by a route, useful when doing server requestsdocpadInstance.getFileByRoute(url,function(err, resultModel){if ( err ) console.log(err.stack)})
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 Applicationvar express =require('express');var http =require('http');var app =express();var server =http.createServer(app).listen(8080);// Add our Application Middlewaresapp.use(app.router);// Add DocPad to our Applicationvar 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) returnconsole.log(err.stack);// Tell DocPad to perform a generation, extend our server with its routes, and watch for changesdocpadInstance.action('generate server watch',function(err){if (err) returnconsole.log(err.stack); });});// Continue with your application// ...
Here is some code for manually rendering a document (inside src/render) with a custom route: