Skip to content

Commit d816792

Browse files
committed
removed news summary html, return pages
1 parent 590780a commit d816792

File tree

4 files changed

+126
-18
lines changed

4 files changed

+126
-18
lines changed

package-lock.json

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"json-socket": "^0.3.0",
2020
"sleep-promise": "^8.0.1",
2121
"snakecase-keys": "^3.2.0",
22+
"string-strip-html": "^7.0.3",
2223
"yargs": "^15.4.1"
2324
},
2425
"devDependencies": {

src/handlers/news.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async function getNews({
99
}) {
1010
const queryHandler = this.server.queryHandler;
1111

12-
const articles = queryHandler.getNews({
12+
const { pages, articles } = queryHandler.getNews({
1313
id,
1414
terms,
1515
page,
@@ -18,7 +18,7 @@ async function getNews({
1818
after
1919
});
2020

21-
this.socket.sendMessage({ token, articles });
21+
this.socket.sendMessage({ token, articles, pages });
2222
}
2323

2424
async function addNews({ token, title, category, body }) {}

src/query-handler.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const camelCaseKeys = require('camelcase-keys');
22
const fs = require('fs');
33
const skills = require('./skills.json');
44
const snakeCaseKeys = require('snakecase-keys');
5+
const stripHTML = require('string-strip-html');
56
const { experienceToLevel } = require('./skills');
67

78
const CREATE_TABLES = fs
@@ -145,8 +146,8 @@ class QueryHandler {
145146
'FROM `players` WHERE `username` = ?',
146147
searchNews:
147148
'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 ' +
150151
'CASE WHEN :category > -1 THEN `category` = :category ELSE ' +
151152
'true END AND ' +
152153
'CASE WHEN LENGTH(:terms) > 2 THEN `title` LIKE :terms OR ' +
@@ -454,7 +455,7 @@ class QueryHandler {
454455

455456
getNews(query) {
456457
if (query.id !== -1) {
457-
return this.statements.getNews.get(query.id);
458+
return { articles: this.statements.getNews.get(query.id) };
458459
}
459460

460461
delete query.id;
@@ -468,22 +469,39 @@ class QueryHandler {
468469
query.category = -1;
469470
}
470471

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;
474473

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+
}
479480

480-
article.summary = article.summary.replace(
481-
/[^a-z0-9\-=/$@!.,'"? :]/gi,
482-
''
483-
).trim();
481+
const summary = article.summary.trim();
482+
const newLineAt = summary.indexOf('\n');
484483

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-z0-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 };
487505
}
488506

489507
getFile(name) {

0 commit comments

Comments
 (0)