This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbuild.js
107 lines (100 loc) · 2.5 KB
/
build.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* # DEPENDENCIES #
*/
var Metalsmith = require('metalsmith')
var layouts = require('metalsmith-layouts')
var collections = require('metalsmith-collections')
var setMetadata = require('metalsmith-filemetadata')
var filepath = require('metalsmith-filepath')
var ignore = require('metalsmith-ignore')
var relative = require('metalsmith-relative')
var define = require('metalsmith-define')
var _ = require('lodash')
var changed = require('metalsmith-changed')
var paths = require('metalsmith-paths')
var md = require('./markdown.js')
// get configuration variables
var config = require('./config.js')
var tools = require('./tools.js')
var lessonReadme = require('./lesson-readme.js')
/*
* # SETUP OBJECTS #
*/
// metadata
var metadataOptions = [
// scratch lesson layout
{
pattern: '**/*.md',
metadata: { layout: 'lesson.jade' }
},
// scratch lesson layout
{
pattern: 'scratch/**/*.md',
metadata: { layout: 'scratch.jade' }
}
]
// ignores
var ignoreOptions = [
'index.md',
'*/index.md'
]
// collections
var collectionOptions = {}
config.collections.forEach(function (collection) {
// options for collections
collectionOptions[collection] = {
pattern: collection + '/**/*.md'
}
})
// defines available in layout
var defineOptions = {
markdown: md.parser,
_: _,
config: config,
isFile: tools.isFile,
matter: tools.frontmatter
}
// layout
var layoutOptions = {
engine: 'jade',
directory: config.builderRoot + '/layouts'
}
/*
* # EXPORT #
* build-function which takes a callback
*/
module.exports = function build (callback, options) {
options = options || {}
var forceBuild = options.force || false
// do the building
Metalsmith(config.lessonRoot)
.source(config.sourceFolder)
.clean(false) // do not delete files, allow gulp tasks in parallel
.use(changed({
force: forceBuild
}))
.use(lessonReadme())
.use(ignore(ignoreOptions))
.use(paths())
// set layout for exercises
.use(setMetadata(metadataOptions))
// add relative(path) for use in layouts
.use(relative())
// create collections
.use(collections(collectionOptions))
// convert markdown to html
.use(md)
// add file.link metadata (now files are .html)
.use(filepath())
// globals for use in layouts
.use(define(defineOptions))
// apply layouts
.use(layouts(layoutOptions))
// build
.destination('build')
.build(function (err) {
if (err) console.log(err)
// callback when build is done
callback(err)
})
}