Reputation: 43224
I have a project which have a lot of useful docs outside of the src
directory which I'd like to render as usual DocPad documents.
Examples:
Files at the root of the project: README.md
, LICENSE
, Contributing.md
and similar, which are already there and can be used in things like GitHub. I would like to reuse the content from those files to create the corresponding readme
, license
and contributing
pages, or to include the contents from those files somewhere in layout or a document.
I have a project that have some docs inside, and I'd like to render the .md
files as DocPad documents from it by including it in package.json
, so those files would be in node_modules
at root.
In both those cases there are files outside of the src/documents
that I'd like to use as partials or documents, and it seems that the partial plugin can't help me (or I couldn't find a way to make it do what I need), and the @getCollection
can only get things from the src/documents
.
So, the question is: Is there a way I can tell DocPad to treat some of the files/folders from the outside of the src
folder? Do I miss something?
If not, then what would be the best way to do it as a plugin, which direction should I dig?
Upvotes: 1
Views: 246
Reputation: 43224
The answer would be a rather simple one: relative symbolic links. Docpad handles them perfectly.
This way, to have a symlink of README.md inside your documents, you should do this (with pwd
of src/documents):
ln -s ../../README.md readme.html.md
Or, in case of a docs from inside one of the project's modules:
ln -s ../../node_modules/foobar/docs/ docs
Both those variants work perfectly.
Note: Symlinks can be tricky. Refer to these for some common gotchas:
Upvotes: 1
Reputation: 3441
DocPad natively supports storing documents outside of the default src
folder. The way you do this is via the documentsPaths
config option in the DocPad Configuration File (e.g. docpad.coffee
). Something like this:
path = require('path')
docpadConfig = {
documentsPaths: [
'documents'
path.resolve('..','data','documents')
]
....
Of course, where this will fall down is if you want to just include arbitrary, individual files somewhere on the file system. In such cases symlinks would be the way to go.
Upvotes: 2
Reputation: 48650
Just as a comparison between the different answers:
Use the paths solution when you want to add entire extra directories to be included inside the DocPad database to be treated as normal by DocPad.
Use the sym/hard link solution when you want to include specific documents or files that you would like to be treated as a DocPad document or file, with all the intelligent file parsing and document rendering, including layout, database, and caching features.
Use the template helper solution when you want to include specific files that you do not want included in the DocPad database for whatever reason.
Made this answer a community wiki one, so it can be updated accordingly for new answers and better details.
Upvotes: 0
Reputation: 48650
Template helpers can also be used for this, as they can do whatever you want.
For instance, the Bevry Learning Centre website uses template helpers to render arbitrary files by relative paths as code examples:
If you would also like to render them, you could combine such a solution with the Text Plugin.
The combination would be like so:
Template Helper in DocPad Configuration File:
docpadConfig =
templateData:
readProjectPath: (relativePath) ->
fullPath = require('path').join(__dirname, relativePath)
return @readFullPath(fullPath)
readRelativePath: (relativePath) ->
fullPath = @getPath(relativePath)
return @readFullPath(fullPath)
readFullPath: (fullPath) ->
result = require('fs').readFileSync(fullPath)
if result instanceof Error
throw result
else
return result.toString()
Usage of Template Helper with Text Plugin using Eco as templating engine:
<t render="markdown"><%- @readProjectPath('README.md') %></t>
Upvotes: 1