@@ -2,6 +2,7 @@ const camelCaseKeys = require('camelcase-keys');
2
2
const fs = require ( 'fs' ) ;
3
3
const skills = require ( './skills.json' ) ;
4
4
const snakeCaseKeys = require ( 'snakecase-keys' ) ;
5
+ const stripHTML = require ( 'string-strip-html' ) ;
5
6
const { experienceToLevel } = require ( './skills' ) ;
6
7
7
8
const CREATE_TABLES = fs
@@ -145,8 +146,8 @@ class QueryHandler {
145
146
'FROM `players` WHERE `username` = ?' ,
146
147
searchNews :
147
148
'SELECT `id`, `date`, `category`, `title`, ' +
148
- `substr(\`body\`, 0, ${ SUMMARY_LENGTH } ) AS \`summary\` ` +
149
- 'FROM `news` WHERE ' +
149
+ `substr(\`body\`, 0, ${ SUMMARY_LENGTH } ) AS \`summary\`, ` +
150
+ 'count(1) over() AS `total` FROM `news` WHERE ' +
150
151
'CASE WHEN :category > -1 THEN `category` = :category ELSE ' +
151
152
'true END AND ' +
152
153
'CASE WHEN LENGTH(:terms) > 2 THEN `title` LIKE :terms OR ' +
@@ -454,7 +455,7 @@ class QueryHandler {
454
455
455
456
getNews ( query ) {
456
457
if ( query . id !== - 1 ) {
457
- return this . statements . getNews . get ( query . id ) ;
458
+ return { articles : this . statements . getNews . get ( query . id ) } ;
458
459
}
459
460
460
461
delete query . id ;
@@ -468,22 +469,39 @@ class QueryHandler {
468
469
query . category = - 1 ;
469
470
}
470
471
471
- return this . statements . searchNews . all ( query ) . map ( ( article ) => {
472
- const summary = article . summary . trim ( ) ;
473
- const newLineAt = summary . indexOf ( '\n' ) ;
472
+ let pages = 0 ;
474
473
475
- article . summary =
476
- newLineAt > - 1
477
- ? summary . slice ( 0 , newLineAt ) . replace ( / \. | ! | \? $ / , '' )
478
- : summary ;
474
+ const articles = this . statements . searchNews
475
+ . all ( query )
476
+ . map ( ( article ) => {
477
+ if ( ! pages ) {
478
+ pages = Math . ceil ( article . total / NEWS_PER_PAGE ) ;
479
+ }
479
480
480
- article . summary = article . summary . replace (
481
- / [ ^ a - z 0 - 9 \- = / $ @ ! . , ' " ? : ] / gi,
482
- ''
483
- ) . trim ( ) ;
481
+ const summary = article . summary . trim ( ) ;
482
+ const newLineAt = summary . indexOf ( '\n' ) ;
484
483
485
- return article ;
486
- } ) ;
484
+ article . summary =
485
+ newLineAt > - 1
486
+ ? summary . slice ( 0 , newLineAt ) . replace ( / \. | ! | \? $ / , '' )
487
+ : summary ;
488
+
489
+ article . summary = stripHTML ( article . summary , {
490
+ stripTogetherWithTheirContents : [ '*' ]
491
+ } ) . result ;
492
+
493
+ article . summary = article . summary
494
+ . replace ( / [ ^ a - z 0 - 9 \- = / $ @ ! . , ' " ? : ] / gi, '' )
495
+ . trim ( ) ;
496
+
497
+ if ( ! article . summary . length ) {
498
+ article . summary = 'No summary available' ;
499
+ }
500
+
501
+ return article ;
502
+ } ) ;
503
+
504
+ return { articles, pages } ;
487
505
}
488
506
489
507
getFile ( name ) {
0 commit comments