forked from fluid-project/infusion-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docpad.js
69 lines (60 loc) · 2.5 KB
/
docpad.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Copyright 2014 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
var URI = require('URIjs');
var path = require('path');
var ncp = require('ncp');
// The documentation root on GitHub:
// Used to build URLs for "Edit on GitHub" links
var githubDocRoot = "https://github.com/fluid-project/infusion-docs/blob/master/src/documents/";
// Helper function to rewrite *.md links to *.html:
// With this helper, we can write links to *.md files in our source files but
// generate links to *.html in the DocPad output. This arrangement gives us
// links that work both on the GitHub website and in the generated HTML.
var rewriteMdLinks = function (content) {
return content.replace(/(<a\s[^>]*href="[\w-/\.]+)\.md(["#])/gm, "$1.html$2");
};
// Helper function to build a URL for "Edit on GitHub" for the current document
var githubLocation = function () {
// in case we're on Windows, replace "\" in the path with "/"
var relativePath = this.document.relativePath.replace(/\\/g, "/");
return githubDocRoot + relativePath;
}
// Helper function to build relative URLs:
// Used for links to static resources such as CSS files. So that the generated
// DocPad output is independent of the URL that it is hosted at.
var relativeUrl = function (forUrl) {
return URI(forUrl).relativeTo(this.document.url);
}
// We locate the images within the src/documents directory so that images can
// be viewed on GitHub, as well as in the DocPad output. We need to
// instruct DocPad to treat the images specially so that they are not
// processed. We tell DocPad to ignore the images using "ignorePaths" and we
// then copy them ourselves with a "writeAfter" event handler.
var rootPath = process.cwd();
var imagesSrcDir = path.join(rootPath, "src", "documents", "images");
var imagesDestDir = "out/images";
module.exports = {
rootPath: rootPath,
ignorePaths: [ imagesSrcDir ],
renderSingleExtensions: true,
plugins: {
handlebars: {
helpers: {
rewriteMdLinks: rewriteMdLinks,
githubLocation: githubLocation,
relativeUrl: relativeUrl
}
}
},
events: {
writeAfter: function (opts, next) {
ncp.ncp(imagesSrcDir, imagesDestDir, next);
}
}
}