@@ -136,22 +136,29 @@ async function getWikipediaData(language, topic) {
136136 } ;
137137} ;
138138
139+ // Adapt Wikipedia's HTML to our needs
139140const convertToWikidocumentariesHTML = function ( origHTML , topic , language ) {
140141 const $ = cheerio . load ( origHTML ) ;
141142
142- $ ( "a" ) . each ( function ( index ) {
143+ // Convert links appropriately
144+ $ ( "a" ) . each ( function ( ) {
143145 const href = $ ( this ) . attr ( 'href' ) ;
144- if ( ! href ) {
145- return ;
146- } else if ( href . indexOf ( '/wiki' ) == 0 && href . indexOf ( '/wiki/Special:' ) == - 1 ) {
147- //$(this).attr('href', '#' + href + "?language=" + language);
148- var noHashPart = href . split ( '#' ) [ 0 ] ;
149- $ ( this ) . attr ( 'href' , noHashPart . replace ( "/wiki/" , "/wikipedia/" + language + "/" ) + "?language=" + language ) ;
150- }
151- else if ( href . indexOf ( '/wiki' ) == 0 && href . indexOf ( '/wiki/Special:' ) != - 1 ) {
152- $ ( this ) . attr ( 'href' , 'https://' + language + '.wikipedia.org' + href ) ;
153- $ ( this ) . attr ( 'target' , '_blank' ) ;
154- $ ( this ) . attr ( 'class' , 'extlink;' ) ;
146+ if ( ! href ) return ;
147+
148+ if ( href . startsWith ( '/wiki' ) ) {
149+ // A link to another page on the wiki
150+ const isFileLink = $ ( this ) . hasClass ( 'mw-file-description' ) ;
151+ if ( isFileLink || href . startsWith ( '/wiki/Special:' ) ) {
152+ // Point special pages to the original wiki
153+ $ ( this ) . attr ( 'href' , 'https://' + language + '.wikipedia.org' + href ) ;
154+ $ ( this ) . attr ( 'target' , '_blank' ) ;
155+ $ ( this ) . attr ( 'class' , 'extlink;' ) ;
156+ } else {
157+ // Point normal pages internally
158+ var noHashPart = href . split ( '#' ) [ 0 ] ;
159+ var internalPage = noHashPart . replace ( "/wiki/" , "/wikipedia/" + language + "/" ) ;
160+ $ ( this ) . attr ( 'href' , internalPage + "?language=" + language ) ;
161+ }
155162 }
156163 else if ( href . indexOf ( '#cite_' ) == 0 ) {
157164 $ ( this ) . attr ( 'href' , 'https://' + language + '.wikipedia.org/wiki/' + topic + href ) ;
0 commit comments