-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-index.js
192 lines (169 loc) · 5.26 KB
/
generate-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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict'
// Antora extension to create a new and clean search index based on ElasticSearch
const _ = require('lodash')
const cheerio = require('cheerio')
const Entities = require('html-entities')
const { Client } = require('@elastic/elasticsearch')
module.exports.register = function () {
// run when the Antora site publishing event has been fired = all done
// get all the pages created and index the result
this.on('sitePublished', ({ playbook, contentCatalog }) => {
const pages = contentCatalog.getPages()
generateIndex(playbook, pages)
})
}
async function generateIndex(playbook, pages) {
// this env is usually not set when building local - skip indexing
// but when set, we index, also when building local
// only set in .drone.star in the docs repo when doing a full build
if (process.env.UPDATE_SEARCH_INDEX !== 'true') {
console.log('ElasticSearch: index generation skipped')
return
} else {
console.log('ElasticSearch: indexing requested')
}
// note that the following envvars are required to build the index
// see the documentation at docs/README.md and docs-ui/src/partials/header-content.hbs for more information
if (!process.env.ELASTICSEARCH_NODE) {
console.log('ElasticSearch: ELASTICSEARCH_NODE envvar is missing, indexing skipped')
return
}
if (!process.env.ELASTICSEARCH_INDEX) {
console.log('ElasticSearch: ELASTICSEARCH_INDEX envvar is missing, indexing skipped')
return
}
if (!process.env.ELASTICSEARCH_WRITE_AUTH) {
console.log('ElasticSearch: ELASTICSEARCH_WRITE_AUTH envvar is missing, indexing skipped')
return
}
let siteUrl = playbook.site.url
if (!siteUrl) {
siteUrl = ''
}
const index_start = Date.now()
// index the documents available
const documents = pages.map((page) => {
// a document like '_email-config.adoc' gets excluded from the catalog
// because it has a leading '_' which will result in an undefined
// 'page.pub.url' variable breaking generating the index
if (page.pub === undefined) {
return
}
const titles = []
const html = page.contents.toString()
// for performance reasons, we use parameter xml which then uses the htmlparser2 engine.
// for more details see:
// https://cheerio.js.org/docs/api/interfaces/CheerioOptions
// https://github.com/taoqf/node-html-parser#performance
const $ = cheerio.load(html, { xml: true })
const $article = $('article')
const title = $article.find('h1').text()
$article.find('h1,h2,h3,h4,h5,h6').each(function () {
let $title = $(this)
let id = $title.attr('id')
titles.push({
text: $title.text(),
id: $title.attr('id'),
url: siteUrl + page.pub.url + '#' + $title.attr('id')
})
$title.remove()
})
let text = Entities.decode($('article').text())
.replace(/(<([^>]+)>)/gi, '')
.replace(/\n/g, ' ')
.replace(/\r/g, ' ')
.replace(/\s+/g, ' ')
.trim()
// todo, we want to do more fancy stuff with results
// also see: docs-ui/src/js/vendor/elastic.js
return {
component: page.src.component,
version: page.src.version,
name: page.src.stem,
title: title,
text: text,
url: siteUrl + page.pub.url,
titles: titles
}
})
// prepare index for uploading
let result = []
documents.forEach((document, index) => {
result.push({
index: {
_index: process.env.ELASTICSEARCH_INDEX,
_type: 'page',
_id: index
}
})
result.push(document)
})
// create a new client to connect
const client = new Client({
node: process.env.ELASTICSEARCH_NODE,
auth: {
username: process.env.ELASTICSEARCH_WRITE_AUTH.split(':')[0],
password: process.env.ELASTICSEARCH_WRITE_AUTH.split(':')[1]
}
})
// remove the old index and create/upload the new one
try {
console.log('ElasticSearch: remove old search index')
await indexDelete(client)
console.log('ElasticSearch: create empty search index')
await indexCreate(client)
console.log('ElasticSearch: upload search index')
await indexBulk(client, result)
} catch (err) {
const errObj = JSON.parse(err)
console.log('ElasticSearch: ERROR: ' + errObj.status + ' - ' + errObj.error.reason)
process.exit(1)
}
const index_end = Date.now()
const elapsedTime = (index_end - index_start) / 1000
console.log(`ElasticSearch: indexing time: ${elapsedTime}s`)
}
function indexDelete(client) {
return new Promise((resolve, reject) => {
client.indices
.delete({
index: process.env.ELASTICSEARCH_INDEX,
ignore_unavailable: true
})
.then((resp) => {
resolve(resp)
})
.catch((err) => {
console.warn(err)
resolve(true)
})
})
}
function indexCreate(client) {
return new Promise((resolve, reject) => {
client.indices
.create({
index: process.env.ELASTICSEARCH_INDEX
})
.then((resp) => {
resolve(resp)
})
.catch((err) => {
reject(err)
})
})
}
function indexBulk(client, result) {
return new Promise((resolve, reject) => {
client
.bulk({
body: result
})
.then((resp) => {
resolve(resp)
})
.catch((err) => {
reject(err)
})
})
}