@@ -41,21 +41,37 @@ function buildComicOutput(xkcdPayload, comicFound, additionalComics) {
4141 ]
4242 } ) ;
4343
44- if ( additionalComics && additionalComics . length > 1 ) {
45- var msgArr = [ ] ;
46- for ( var i = 0 ; i < additionalComics . length ; i ++ ) {
47- parseInt ( additionalComics [ i ] , 10 ) !== xkcdPayload . num ? msgArr . push ( additionalComics [ i ] ) : null ;
48- }
49- blockArr . push ( {
50- "type" : "context" ,
51- "elements" : [
52- {
53- "type" : "mrkdwn" ,
54- "text" : "*Other matching comics:* " + msgArr . join ( ', ' )
55- }
56- ]
57- } ) ;
58- }
44+ if ( additionalComics && additionalComics . length > 1 ) {
45+ var msgArr = [ ] ;
46+ for ( var i = 0 ; i < additionalComics . length ; i ++ ) {
47+ if ( parseInt ( additionalComics [ i ] . id , 10 ) !== xkcdPayload . num ) {
48+ msgArr . push (
49+ "- #" +
50+ additionalComics [ i ] . id +
51+ " - " +
52+ additionalComics [ i ] . title
53+ ) ;
54+
55+ if ( msgArr . length >= 3 ) {
56+ break ;
57+ }
58+ }
59+ }
60+
61+ if ( msgArr . length > 0 ) {
62+ blockArr . push ( {
63+ "type" : "context" ,
64+ "elements" : [
65+ {
66+ "type" : "mrkdwn" ,
67+ "text" :
68+ "*Other matching comics:*\n" +
69+ msgArr . join ( "\n" )
70+ }
71+ ]
72+ } ) ;
73+ }
74+ }
5975
6076 return {
6177 "text" : xkcdPayload . safe_title ,
@@ -125,9 +141,10 @@ if (terms != "" && (terms == "-random" || comicNumProvided)) {
125141
126142 if ( searchResults . hits ) {
127143 for ( var i = 0 ; i < Math . min ( searchResults . hits . length , 10 ) ; i ++ ) {
128- comicNumbers . push (
129- searchResults . hits [ i ] . document . id
130- ) ;
144+ comicNumbers . push ( {
145+ id : searchResults . hits [ i ] . document . id ,
146+ title : searchResults . hits [ i ] . document . title
147+ } ) ;
131148 }
132149 }
133150
@@ -136,15 +153,14 @@ if (terms != "" && (terms == "-random" || comicNumProvided)) {
136153 // Use the highest ranked result instead of random
137154 endPoint =
138155 "https://xkcd.com/" +
139- comicNumbers [ 0 ] +
156+ comicNumbers [ 0 ] . id +
140157 "/info.0.json" ;
141158
142159 comicFound = true ;
143160
144161 } else {
145162
146- var randomComic =
147- Math . floor ( Math . random ( ) * parseInt ( jsonBody . num ) ) ;
163+ var randomComic = Math . floor ( Math . random ( ) * parseInt ( jsonBody . num ) ) + 1 ;
148164
149165 endPoint =
150166 "https://xkcd.com/" +
@@ -161,7 +177,7 @@ comicDataMsg.setRequestHeader('User-Agent', 'servicenow');
161177response = comicDataMsg . execute ( ) ;
162178var body = JSON . parse ( response . getBody ( ) ) ;
163179
164- if ( comicNumbers && comicNumbers . length > 1 ) {
180+ if ( comicNumbers . length > 1 ) {
165181 msg = buildComicOutput ( body , comicFound , comicNumbers ) ;
166182} else {
167183 msg = buildComicOutput ( body , comicFound , null ) ;
0 commit comments