Skip to content

Commit 6d6a156

Browse files
committed
CiteHighlighter: highlight citations in edit previews & diffs
- highlight citations in edit previews more accurately - highlight citations in Special:Undelete previews - highlight citations in diffs - highlight citations in old revisions fixes #252 (I think it was this code change, not sure though) fixes #108
1 parent d690044 commit 6d6a156

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

CiteHighlighter/CiteHighlighter.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ class CiteHighlighter {
1313
}
1414

1515
async execute() {
16+
// Things like deleted pages, special pages, etc. don't have parser output.
17+
// Things like diffs, previews, and a regular page do have parser output.
18+
// Let's exit early if there's no parser output.
19+
const hasParserOutput = this.$( '.mw-parser-output' ).length > 0;
20+
if ( !hasParserOutput ) {
21+
return;
22+
}
23+
this.wikicode = await this.getWikicodeVariable();
1624
this.sources = await this.getListOfSourcesAndRatings();
1725
this.unreliableWordsForOrangeHighlighting = this.getUnreliableWords();
1826
this.setConfigVariableDefaultsIfNeeded();
@@ -25,13 +33,22 @@ class CiteHighlighter {
2533
this.preventWikipediaFalsePositives();
2634
this.colors = this.getColors();
2735
this.writeCSS();
28-
this.wikicode = await this.getWikicode( this.articleTitle );
2936
// Note: Any wikicode containing a lot of domain names included in CiteHighlighter will be slow, unless added to isSlowPage(). This is because addHTMLClassesToRefs() checks the wikicode before trying to add classes to CSS.
3037
this.addHTMLClassesToRefs();
3138
this.addHTMLClassesForUnreliableWords();
3239
this.observeAndAddClassesToTooltips();
3340
}
3441

42+
async getWikicodeVariable() {
43+
// Is it a preview page or a regular page? This will determine where we get our wikicode from. A preview page will contain both parser output and a textarea.
44+
const isPreviewPage = this.$( '#mw-content-text textarea' ).length > 0;
45+
if ( isPreviewPage ) {
46+
return this.$( '#mw-content-text textarea' ).val();
47+
} else {
48+
return await this.getWikicodeFromApi( this.mw.config.get( 'wgRevisionId' ) );
49+
}
50+
}
51+
3552
getUnreliableWords() {
3653
return [
3754
'/comment',
@@ -130,9 +147,7 @@ class CiteHighlighter {
130147
'Wikipedia:Redirects for discussion',
131148
'User:Novem Linguae/Scripts/CiteHighlighter/SourcesJSON.js'
132149
].map( ( title ) => title.replace( / /g, '_' ) );
133-
const isHistory = this.mw.config.get( 'wgAction' ) === 'history';
134-
const isDiff = this.mw.config.get( 'wgDiffNewId' );
135-
return isHistory || isDiff || slowPages.includes( this.articleTitle );
150+
return slowPages.includes( this.articleTitle );
136151
}
137152

138153
/**
@@ -372,21 +387,17 @@ class CiteHighlighter {
372387
return haystack;
373388
}
374389

375-
async getWikicode( title ) {
376-
const pageIsDeleted = !this.mw.config.get( 'wgCurRevisionId' );
377-
if ( pageIsDeleted ) {
378-
return '';
379-
}
380-
390+
async getWikicodeFromApi( revisionId ) {
381391
const api = new this.mw.Api();
382392
const response = await api.get( {
383-
action: 'parse',
384-
page: title,
385-
prop: 'wikitext',
393+
action: 'query',
394+
format: 'json',
395+
prop: 'revisions',
396+
revids: revisionId,
386397
formatversion: '2',
387-
format: 'json'
398+
rvprop: 'content'
388399
} );
389-
return response.parse.wikitext;
400+
return response.query.pages[ 0 ].revisions[ 0 ].content;
390401
}
391402

392403
async getWikicodeFromCache( title ) {
@@ -408,24 +419,19 @@ class CiteHighlighter {
408419
}
409420
}
410421

411-
// Fire after wiki content is added to the DOM, such as when first loading a page, or when a gadget such as the XTools gadget loads.
412-
mw.hook( 'wikipage.content' ).add( async () => {
422+
async function executeCiteHighlighter() {
413423
await mw.loader.using(
414424
[ 'mediawiki.util', 'mediawiki.api' ],
415425
async () => {
416426
await ( new CiteHighlighter( window, $, mw ) ).execute();
417427
}
418428
);
419-
} );
429+
}
430+
431+
// Fire after wiki content is added to the DOM, such as when first loading a page, when loading a diff, or when a gadget such as the XTools gadget loads.
432+
mw.hook( 'wikipage.content' ).add( executeCiteHighlighter );
420433

421434
// Fire after an edit is successfully saved via JavaScript, such as edits by the Visual Editor and HotCat.
422-
mw.hook( 'postEdit' ).add( async () => {
423-
await mw.loader.using(
424-
[ 'mediawiki.util', 'mediawiki.api' ],
425-
async () => {
426-
await ( new CiteHighlighter( window, $, mw ) ).execute();
427-
}
428-
);
429-
} );
435+
mw.hook( 'postEdit' ).add( executeCiteHighlighter );
430436

431437
// </nowiki>

0 commit comments

Comments
 (0)