-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
94 lines (78 loc) · 3.1 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
/*
Build json index used for search bar
Triggered at build-time by yarn (command in makefile)
*/
var yaml = require("yamljs");
var S = require("string");
module.exports = function(grunt) {
var CONTENT_PATH_PREFIX = grunt.option("content");
grunt.registerTask("lunr-index", function() {
grunt.log.writeln("Build pages index");
var indexPages = function() {
var pagesIndex = [];
grunt.file.recurse(CONTENT_PATH_PREFIX, function(abspath, rootdir, subdir, filename) {
if (!abspath.includes("draft")) {
var file = processFile(abspath, filename);
if (file !== undefined) {
var duplicatePages = pagesIndex.filter((page) => {
return page.href === file.href;
});
if (duplicatePages.length > 0)
duplicatePages[0].content += file.content;
else
pagesIndex.push(file);
}
}
});
return pagesIndex;
};
var processFile = function(abspath, filename) {
var pageIndex;
if (S(filename).endsWith(".md") &&
(filename === "index.md" || S(filename).startsWith("section"))) {
grunt.log.writeln("Parse file:", abspath);
pageIndex = processMDFile(abspath, filename);
}
return pageIndex;
};
var processMDFile = function(abspath, filename) {
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.parse(content[1].trim());
} catch (e) {
console.warn("This file does not contain a front-matter.");
}
href = S(abspath).chompLeft(CONTENT_PATH_PREFIX).chompRight(filename).s;
// Build Lunr index for this page
pageIndex = {
title: "",
tags: [],
href: href,
preview:'',
showcase:'',
description:'',
score:0,
content: S(content[0]).trim().stripTags().stripPunctuation().s
};
if (frontMatter !== undefined) {
pageIndex = {
title: frontMatter.title,
tags: frontMatter.tags,
href: href,
preview: frontMatter.preview,
showcase: frontMatter.showcase,
description: frontMatter.description,
score: frontMatter.score,
content: S(content[2]).trim().stripTags().stripPunctuation().s
};
}
return pageIndex;
};
grunt.file.write("static/js/lunr/PagesIndex.json", JSON.stringify(indexPages()));
grunt.log.ok("Index built");
});
};