-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (131 loc) · 3.22 KB
/
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
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
'use strict'
const ware = require('ware')
const assign = Object.assign
const useCache = require('./lib/helpers/use_cache')
const buildJs = require('./lib/build_js')
const memoize = require('./lib/memoize')
module.exports = function search (options) {
const ctx = {}
var app = ware()
.use(updateIndex.bind(ctx))
.use(addMeta.bind(ctx))
.use(addJs.bind(ctx))
return function (files, ms, done) {
app.run(files, ms, done)
}
}
/**
* Internal: actual indexing happens here
*/
function updateIndex (files, ms, done) {
const idx = {}
Object.keys(files).forEach((fname) => {
const file = files[fname]
// memoize based on file contents
if (file.$) {
const cacheKey = [ ms.directory(), fname, file.contents ]
const newIndices = memoize(['index', cacheKey], () => {
return index(fname, file)
})
assign(idx, newIndices)
}
})
extendIndex(files['_docpress.json'], (file) => {
file.searchIndex = idx
file.lunrIndex = memoize([ 'lunr', ms.directory(), idx ], () => {
return JSON.stringify(lunrize(idx))
})
})
done()
}
/**
Internal: adds the search.js file
*/
function addJs (files, ms, done) {
const callback = (err, contents) => {
if (err) return done(err)
contents =
'window.__searchindex=(' +
JSON.stringify(files['_docpress.json'].searchIndex, null, 2) +
');\n' +
'window.__lunrindex=(' +
files['_docpress.json'].lunrIndex +
');\n' + contents
files['assets/search.js'] = { contents }
done()
}
useCache('cache/search.js', callback) ||
buildJs({}, callback)
}
/**
* Internal: tells docpress-base to add search.js as a script
*/
function addMeta (files, ms, done) {
const meta = ms.metadata()
if (!meta.js) meta.js = []
if (meta.js.indexOf('assets/search.js') === -1) {
meta.js.push('assets/search.js')
}
done()
}
/*
* Internal: turns the index into a lunr object
*/
function lunrize (idx) {
const lunr = require('lunr')
const index = lunr(function () {
this.field('title', { boost: 10 })
// todo: boost if heading
this.field('pagetitle', { boost: 3 })
this.field('body')
this.ref('slug')
})
Object.keys(idx).forEach((slug) => {
index.add(idx[slug])
})
return index
}
/**
* Internal: extends `docpress.json` with more metadata. It will rewrite
* `contents` afterwards.
*/
function extendIndex (file, block) {
delete file.contents
block(file)
file.contents = JSON.stringify(file)
}
/**
* Internal: adds new indexes to `idx` from the file `file`/`fname`.
*/
function index (fname, file) {
let slug = fname
let title = file.title
let idx = {}
file.$(':root').each(function () {
const $this = file.$(this)
if ($this.is('h2, h3, h4, h5, h6')) {
slug = fname + '#' + $this.attr('id')
title = $this.text()
idx[slug] = initial()
} else {
if (!idx[slug]) {
idx[slug] = initial()
} else {
if (idx[slug].body.length) {
idx[slug].body += '\n' + $this.text()
} else {
idx[slug].body = $this.text()
}
}
}
})
return idx
function initial () {
return {
title: title,
pagetitle: file.title,
slug: slug,
body: ''
}
}
}