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-search-index.js
63 lines (59 loc) · 1.51 KB
/
build-search-index.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
/**
* build searchIndex.json
*/
var Metalsmith = require('metalsmith')
var ignore = require('metalsmith-ignore')
var setMetadata = require('metalsmith-filemetadata')
var lunr = require('lunr')
require('lunr-no/lunr.stemmer.support')(lunr)
require('lunr-no')(lunr)
var metlunr = require('metalsmith-lunr')
var config = require('./config.js')
var tools = require('./tools.js')
// lunr: true on all .md files
var metadataOptions = [
{ pattern: '**/*.md',
metadata: { lunr: true } }
]
// for is not a stopword in this context
var words = lunr.no.stopWordFilter.stopWords.elements
words.splice(words.indexOf('for'), 1)
// ignore everything except .md files
var ignoreOptions = [
'**',
'!**/*.md',
'**/README.md'
]
/**
* build-function, calls callback when done
*/
module.exports = function build (callback) {
// do the building
Metalsmith(config.lessonRoot)
.source(config.sourceFolder)
.use(ignore(ignoreOptions))
.use(tools.removeExternal)
.clean(false) // do not delete files, allow gulp tasks in parallel
.use(setMetadata(metadataOptions))
.use(metlunr({
fields: {
contents: 1,
title: 10,
tags: 20
},
pipelineFunctions: [
lunr.no.trimmer,
lunr.no.stopWordFilter,
lunr.no.stemmer
]
}))
// remove lessons *after* we have created searchIndex.json
.use(ignore(['**', '!searchIndex.json']))
// put in build folder
.destination('build')
.build(function (err) {
if (err) console.log(err)
// callback when build is done
callback(err)
})
}