forked from sensu/sensu-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
169 lines (149 loc) · 5.77 KB
/
Gruntfile.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var yaml = require("js-yaml");
var S = require("string");
var CONTENT_PATH_PREFIX = "content/";
// define the grunt function for cli
module.exports = function(grunt) {
grunt.initConfig({
env : {
options : {
add : {
HUGO_VERSION : '0.34.0'
}
},
dev : {}
}
})
grunt.loadNpmTasks('grunt-env');
grunt.registerTask("hugo-build", function() {
const done = this.async();
grunt.log.writeln("Running hugo build");
grunt.util.spawn({
cmd: "hugo",
},
function(error, result, code) {
if (code == 0) {
grunt.log.ok("Successfully built site");
} else {
grunt.fail.fatal(error);
}
done();
});
});
grunt.registerTask("hugo-server", function() {
const done = this.async();
const args = process.argv.slice(3); // fetch given arguments
grunt.log.writeln("Running Hugo server");
grunt.util.spawn({
cmd: "hugo",
args: ["server", ...args] , // pass arguments down
opts: {stdio: 'inherit'}
},
function(error, result, code) {
if (code == 0) {
grunt.log.ok("Thanks for using Hugo!");
} else {
grunt.fail.fatal(error);
}
done();
});
});
grunt.registerTask("print-hugo-version", function() {
const done = this.async();
grunt.util.spawn({
cmd: "hugo",
args: ["version"],
opts: {stdio: 'inherit'}
},
function(error, result, code) {
if (code == 0) {
} else {
grunt.fail.fatal(error);
}
done();
});
});
// define the actual lunr-index task for cli
grunt.registerTask("lunr-index", function() {
grunt.log.writeln("Build pages index");
// makes an array of the names of all the files
var indexPages = function() {
let pagesIndex = [];
// go through the folders recursively
grunt.file.recurse(CONTENT_PATH_PREFIX, function(abspath, rootdir, subdir, filename) {
grunt.verbose.writeln("Parse file:", abspath);
// push adds the processed file to the array of pages
const entry = processFile(abspath, filename);
if (entry !== null && entry !== undefined) {
pagesIndex.push(entry);
}
});
return pagesIndex;
};
// call the appropriate process for if it's a content file (md) or html page
var processFile = function(abspath, filename) {
var pageIndex;
if (S(filename).endsWith(".html")) {
pageIndex = processHTMLFile(abspath, filename);
} else if (S(filename).endsWith(".md")) {
pageIndex = processMDFile(abspath, filename);
}
return pageIndex;
};
// process html
var processHTMLFile = function(abspath, filename) {
// read the file contents
var content = grunt.file.read(abspath);
// the page name will be the filename, minus html
var pageName = S(filename).chompRight(".html").s;
// create the path to the file, minus everything before the path prefix
var href = "/" + S(abspath)
.chompLeft(CONTENT_PATH_PREFIX).s;
grunt.log.writeln("PageName (html)" + pageName);
return {
// return an object containing these values
title: pageName,
href: href,
// remove any tags and punctuation from the page
content: S(content).trim().stripTags().stripPunctuation().s
};
};
// process md
var processMDFile = function(abspath, filename) {
// read the file contents
var content = grunt.file.read(abspath);
var pageIndex;
// first separate the Front Matter from the content and parse it
content = content.split("---");
var frontMatter;
try {
frontMatter = yaml.load(content[1].trim());
} catch (e) {
grunt.log.writeln("Front matter failed: " + e.message);
}
// create the path to the file, minus everything before the path prefix
var href = S(abspath).chompLeft(CONTENT_PATH_PREFIX).chompRight(".md").s;
// href for index.md files stops at the folder name
if (filename === "index.md") {
href = S(abspath).chompLeft(CONTENT_PATH_PREFIX).chompRight(filename).s;
}
href = "/" + href
grunt.log.writeln("PageName (html)" + frontMatter.title);
// build Lunr index for this page
pageIndex = {
title: frontMatter.title,
tags: frontMatter.tags,
product: frontMatter.product,
version: frontMatter.version,
location: href,
display_name: frontMatter.product + " " + frontMatter.version + ": " + frontMatter.title,
content: S(content[2]).trim().stripTags().stripPunctuation().s
};
return pageIndex;
};
grunt.file.write("static/js/lunr/PagesIndex.json", JSON.stringify(indexPages()));
grunt.log.ok("Lunr index built");
});
grunt.registerTask("default", ["env", "lunr-index", "hugo-version", "hugo-build",]);
grunt.registerTask("server", ["env", "lunr-index", "hugo-version", "hugo-server",]);
grunt.registerTask("hugo-version", ["env", "print-hugo-version",]);
};