33const _ = require ( 'lodash' )
44const cheerio = require ( 'cheerio' )
55const Entities = require ( 'html-entities' )
6- const Elasticsearch = require ( 'elasticsearch' )
6+ const { Client } = require ( '@elastic/ elasticsearch' )
77
88const aggregateContent = require ( '@antora/content-aggregator' )
99const buildNavigation = require ( '@antora/navigation-builder' )
@@ -17,11 +17,13 @@ const produceRedirects = require('@antora/redirect-producer')
1717const publishSite = require ( '@antora/site-publisher' )
1818const { resolveConfig : resolveAsciiDocConfig } = require ( '@antora/asciidoc-loader' )
1919
20- async function generateSite ( args , env ) {
20+ async function generateSite ( args , env ) {
2121 const playbook = buildPlaybook ( args , env )
2222 const [ contentCatalog , uiCatalog ] = await Promise . all ( [
23- aggregateContent ( playbook ) . then ( ( contentAggregate ) => classifyContent ( playbook , enforceEditurl ( contentAggregate ) ) ) ,
24- loadUi ( playbook ) ,
23+ aggregateContent ( playbook ) . then ( ( contentAggregate ) =>
24+ classifyContent ( playbook , enforceEditurl ( contentAggregate ) )
25+ ) ,
26+ loadUi ( playbook )
2527 ] )
2628 const asciidocConfig = resolveAsciiDocConfig ( playbook )
2729 const pages = convertDocuments ( contentCatalog , asciidocConfig )
@@ -35,7 +37,7 @@ async function generateSite (args, env) {
3537 return publishSite ( playbook , [ contentCatalog , uiCatalog , siteCatalog ] )
3638}
3739
38- async function generateIndex ( playbook , pages ) {
40+ async function generateIndex ( playbook , pages ) {
3941 if ( ( process . env . BUILD_SEARCH_INDEX || 'true' ) !== 'true' ) {
4042 console . log ( 'elastic: search index generation skipped' )
4143 return
@@ -67,7 +69,7 @@ async function generateIndex (playbook, pages) {
6769 } )
6870
6971 let text = Entities . decode ( $ ( 'article' ) . text ( ) )
70- . replace ( / ( < ( [ ^ > ] + ) > ) / ig , '' )
72+ . replace ( / ( < ( [ ^ > ] + ) > ) / gi , '' )
7173 . replace ( / \n / g, ' ' )
7274 . replace ( / \r / g, ' ' )
7375 . replace ( / \s + / g, ' ' )
@@ -84,13 +86,18 @@ async function generateIndex (playbook, pages) {
8486 }
8587 } )
8688
87- if ( process . env . UPDATE_SEARCH_INDEX == 'true' && process . env . ELASTICSEARCH_HOST && process . env . ELASTICSEARCH_INDEX ) {
89+ if (
90+ process . env . UPDATE_SEARCH_INDEX == 'true' &&
91+ process . env . ELASTICSEARCH_NODE &&
92+ process . env . ELASTICSEARCH_INDEX &&
93+ process . env . ELASTICSEARCH_WRITE_AUTH
94+ ) {
8895 console . log ( 'elastic: rebuild search index' )
8996 let result = [ ]
9097
9198 documents . forEach ( ( document , index ) => {
9299 result . push ( {
93- index : {
100+ index : {
94101 _index : process . env . ELASTICSEARCH_INDEX ,
95102 _type : 'page' ,
96103 _id : index
@@ -100,35 +107,36 @@ async function generateIndex (playbook, pages) {
100107 result . push ( document )
101108 } )
102109
103- const client = new Elasticsearch . Client ( {
104- host : [ {
105- host : process . env . ELASTICSEARCH_HOST ,
106- port : process . env . ELASTICSEARCH_PORT || 443 ,
107- protocol : process . env . ELASTICSEARCH_PROTOCOL || 'https' ,
108- auth : process . env . ELASTICSEARCH_WRITE_AUTH ,
109- } ]
110+ const client = new Client ( {
111+ node : process . env . ELASTICSEARCH_NODE ,
112+ auth : {
113+ username : process . env . ELASTICSEARCH_WRITE_AUTH . split ( ':' ) [ 0 ] ,
114+ password : process . env . ELASTICSEARCH_WRITE_AUTH . split ( ':' ) [ 1 ]
115+ }
110116 } )
111117
112118 try {
113- console . log ( " elastic: remove old search index" ) ;
119+ console . log ( ' elastic: remove old search index' )
114120 await indexDelete ( client )
115- console . log ( " elastic: create empty search index" ) ;
121+ console . log ( ' elastic: create empty search index' )
116122 await indexCreate ( client )
117123 console . log ( 'elastic: upload search index' )
118124 await indexBulk ( client , result )
119125 } catch ( err ) {
120- console . log ( "elastic: ERROR: " + err . status + " - " + err . displayName ) ;
121- process . exit ( 1 ) ;
126+ const errObj = JSON . parse ( err )
127+ console . log ( 'elastic: ERROR: ' + errObj . status + ' - ' + errObj . error . reason )
128+ process . exit ( 1 )
122129 }
123130 }
124131}
125132
126- function enforceEditurl ( contentAggregate ) {
133+ function enforceEditurl ( contentAggregate ) {
127134 _ . map ( contentAggregate , ( source ) => {
128135 _ . map ( source . files , ( file ) => {
129136 if ( _ . startsWith ( file . src . editUrl , 'file://' ) ) {
130137 if ( source . name === 'server' ) {
131- file . src . editUrl = 'https://github.com/owncloud/docs/edit/' + source . version + '/' + file . src . path
138+ file . src . editUrl =
139+ 'https://github.com/owncloud/docs/edit/' + source . version + '/' + file . src . path
132140 }
133141 }
134142
@@ -146,53 +154,54 @@ function indexDelete(client) {
146154 client . indices
147155 . delete ( {
148156 index : process . env . ELASTICSEARCH_INDEX ,
149- ignore_unavailable : true ,
157+ ignore_unavailable : true
150158 } )
151159 . then ( ( resp ) => {
152- resolve ( resp ) ;
160+ resolve ( resp )
153161 } )
154162 . catch ( ( err ) => {
155- reject ( err ) ;
156- } ) ;
157- } ) ;
163+ reject ( err )
164+ } )
165+ } )
158166}
159167
160168function indexCreate ( client ) {
161169 return new Promise ( ( resolve , reject ) => {
162170 client . indices
163171 . create ( {
164- index : process . env . ELASTICSEARCH_INDEX ,
172+ index : process . env . ELASTICSEARCH_INDEX
165173 } )
166174 . then ( ( resp ) => {
167- resolve ( resp ) ;
175+ resolve ( resp )
168176 } )
169177 . catch ( ( err ) => {
170- reject ( err ) ;
171- } ) ;
172- } ) ;
178+ reject ( err )
179+ } )
180+ } )
173181}
174182
175183function indexBulk ( client , result ) {
176184 return new Promise ( ( resolve , reject ) => {
177- client . bulk ( {
178- body : result
179- } )
185+ client
186+ . bulk ( {
187+ body : result
188+ } )
180189 . then ( ( resp ) => {
181- resolve ( resp ) ;
190+ resolve ( resp )
182191 } )
183192 . catch ( ( err ) => {
184- reject ( err ) ;
185- } ) ;
186- } ) ;
193+ reject ( err )
194+ } )
195+ } )
187196}
188197
189- function create404Page ( ) {
198+ function create404Page ( ) {
190199 return {
191200 title : 'Page Not Found' ,
192201 mediaType : 'text/html' ,
193202 src : { stem : '404' } ,
194203 out : { path : '404.html' } ,
195- pub : { url : '/404.html' , rootPath : '' } ,
204+ pub : { url : '/404.html' , rootPath : '' }
196205 }
197206}
198207
0 commit comments