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-indexes.js
138 lines (131 loc) · 3.29 KB
/
build-indexes.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* build index files
*/
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 paths = require('metalsmith-paths')
var md = require('./markdown.js')
// get configuration variables
var config = require('./config.js')
var tools = require('./tools.js')
var playlists = require('./playlists.js')
var lessonReadme = require('./lesson-readme.js')
/**
* # SETUP OBJECTS #
*/
// metadata
var metadataOptions = [
// front page layout
{
pattern: 'index.md',
metadata: { layout: 'index.jade' }
}
]
// ignore everything except index files
var ignoreOptions = [
'**',
'!**/*.md',
'**/README.md'
]
// collections
var collectionOptions = {}
config.collections.forEach(function (collection) {
// options for collections
collectionOptions[collection] = {
pattern: collection + '/**/*.md'
}
})
function sortCollections (courses) {
var out = []
for (var name in courses) {
var course = {}
course.lessons = courses[name]
course.name = name
out.push(course)
}
out.sort(function (a, b) {
if (a.lessons.length > b.lessons.length) {
return -1
}
if (a.lessons.length < b.lessons.length) {
return 1
}
return 0
})
return out
}
// defines available in layout
var defineOptions = {
markdown: md.parser,
_: _,
config: config,
isFile: tools.isFile,
matter: tools.frontmatter,
sort: sortCollections
}
// layout
var layoutOptions = {
engine: 'jade',
directory: config.builderRoot + '/layouts',
default: 'lesson-index.jade'
}
/**
* # EXPORT #
* build-function, calls callback when done
*/
module.exports = function build (callback) {
// do the building
Metalsmith(config.lessonRoot)
.source(config.sourceFolder)
.clean(false) // do not delete files, allow gulp tasks in parallel
.use(lessonReadme()) // before ignore
.use(playlists({ // before ignore
collections: Object.keys(collectionOptions)
}))
.use(ignore(ignoreOptions))
// add file.link metadata (files are .md here)
.use(filepath())
.use(ignoreIndexedFalse)
.use(paths())
// set layout for exercises
.use(setMetadata(metadataOptions))
// add relative(path) for use in layouts
.use(relative())
// create collections for index
.use(collections(collectionOptions))
// remove lessons *after* we have necessary metadata
.use(ignore(['**', '!**/index.md']))
.use(tools.removeExternal)
// convert markdown to html
.use(md)
// 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)
})
}
/**
* remove files from build which have set indexed: false in frontmatter
*/
function ignoreIndexedFalse (files, _, done) {
Object.keys(files).forEach(function (key) {
var file = files[key]
if (file.indexed === false) {
delete files[key]
}
})
done()
}