@@ -34,28 +34,46 @@ function formatDate(timestamp)
34
34
}
35
35
36
36
// format a massege for code blocks
37
- function formatMessage ( string )
37
+ function formatMessage ( string , keywords )
38
38
{
39
- const regex = / \` \` \` ( ( [ a - z ] + ) \n ) ? \n * ( [ \s \S ] * ?) \n * \` \` \` / g;
40
- const s = string . replace ( regex , ( match , p1 , p2 , p3 , offset , string ) =>
39
+ const regex = / \` \` \` ( ( [ a - z A - Z ] + ) \n ) ? \n * ( [ \s \S ] * ?) \n * \` \` \` / g;
40
+ var s = string . replace ( regex , ( match , p1 , p2 , p3 , offset , string ) =>
41
41
`<div class="code">${ p2 ? `<span class="lang">#${ p2 } </span><br />` : "" } ${ p3 . replace ( / \n / g, "<br />" ) } </div>` ) ;
42
- // also parse inline `code` and urls
42
+
43
+ const inlineRegex = / \` ( [ ^ \` ] + ) \` / g;
44
+ s = s . replace ( inlineRegex , ( match , p1 ) => `<span class="inlineCode">${ p1 } </span>` ) ;
45
+
46
+ const urlRegex = / ( h t t p s ? : \/ \/ ( w w w \. ) ? [ - a - z A - Z 0 - 9 @ : % . _ \+ ~ # = ] { 2 , 256 } \. [ a - z ] { 2 , 6 } \b ( [ - a - z A - Z 0 - 9 @ : % _ \+ . ~ # ? & / / = ] * ) ) / g;
47
+ s = s . replace ( urlRegex , ( match , p1 ) => `<a href="${ p1 } ">${ p1 } </a>` ) ;
48
+
49
+ keywords . filter ( word => word . length ) . forEach ( word => s = s . replace ( new RegExp ( word , "g" ) , `<span class="keyword">${ word } </span>` ) ) ;
43
50
return s ;
44
51
}
45
52
46
53
// make html output for a message
47
- function makeMessagePage ( rows )
54
+ function makeMessagePage ( rows , keywords )
48
55
{
49
56
const body = rows . map ( row => `
50
57
<a class="perma" href="/code?msgId=${ row . msgId } ">permalink</a><br />
51
58
Author: ${ row . author } <br />
52
59
Channel: ${ row . channel } <br />` + row . revisions . map ( revision => `
53
60
<span class="timestamp">${ formatDate ( revision . date ) } </span>
54
- <div class="messageBody">${ formatMessage ( revision . fullMessage ) } </div>
61
+ <div class="messageBody">${ formatMessage ( revision . fullMessage , keywords ) } </div>
55
62
` ) . join ( "<br /><br />" ) ) . join ( "<br /><br />" ) ;
56
63
57
64
return `
58
65
<style>
66
+ .inlineCode
67
+ {
68
+ font-family: monospace;
69
+ color: #EEE;
70
+ background-color: #555;
71
+ }
72
+ .keyword
73
+ {
74
+ background-color: yellow;
75
+ color: black;
76
+ }
59
77
.perma
60
78
{
61
79
font-size: small;
@@ -119,6 +137,8 @@ frontend.get("/", (request, response) => {
119
137
{
120
138
response . send ( `
121
139
<form action="/code/">
140
+ <label for="keywords">Keywords</label>
141
+ <input id="keywords" name="keywords"><br />
122
142
<label for="author">Author</label>
123
143
<select id="author" name="author">
124
144
<option value="">*</option>
@@ -152,6 +172,11 @@ function strtotime(string)
152
172
return new Date ( string ) . getTime ( ) ;
153
173
}
154
174
175
+ function containsKeywords ( string , keywords )
176
+ {
177
+ return keywords . reduce ( ( acc , word ) => word === "" ? acc : acc && ( string . indexOf ( word ) != - 1 ) , true ) ;
178
+ }
179
+
155
180
// filter messages query
156
181
frontend . get ( "/code/" , ( request , response ) => {
157
182
var sql = `SELECT * FROM Message` ;
@@ -186,6 +211,7 @@ frontend.get("/code/", (request, response) => {
186
211
if ( e ) exit ( `Error querying database: ${ e } ` ) ;
187
212
else
188
213
{
214
+ const keywords = request . query . keywords ? request . query . keywords . split ( " " ) : [ ] ;
189
215
var rows = messageRows ;
190
216
rows = rows . map ( row => {
191
217
row . revisions = contentRows
@@ -197,8 +223,10 @@ frontend.get("/code/", (request, response) => {
197
223
rows = rows . filter ( row => row . revisions [ 0 ] . date >= strtotime ( request . query . from ) ) ;
198
224
if ( request . query . to )
199
225
rows = rows . filter ( row => row . revisions [ 0 ] . date < strtotime ( request . query . to ) ) ;
226
+ if ( request . query . keywords )
227
+ rows = rows . filter ( row => row . revisions . reduce ( ( acc , revision ) => containsKeywords ( revision . fullMessage , keywords ) , true ) ) ;
200
228
if ( request . query . as === "json" ) response . send ( rows ) ;
201
- else response . send ( makeMessagePage ( rows ) ) ;
229
+ else response . send ( makeMessagePage ( rows , keywords ) ) ;
202
230
} ;
203
231
} ) ;
204
232
} ;
0 commit comments