Skip to content

Commit

Permalink
Shorter code with ES6 (now that MediaWiki doesn't require ES5)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardspec committed Dec 7, 2024
1 parent beb3a7c commit 072c94f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions modules/ext.askai.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $( function () {
// List of pages isn't useful to the AI (it doesn't know what to do with it),
// we need to retrieve the text of paragraphs (e.g. [[Some page#p6-8]])
// and send this text to AI as a part of instructions (not the user-chosen Prompt).
const promises = $pages.val().split( '\n' ).map( function ( pageName ) {
const promises = $pages.val().split( '\n' ).map( ( pageName ) => {
let title;
try {
title = new mw.Title( pageName );
Expand All @@ -26,7 +26,7 @@ $( function () {

if ( fragment && fragment.match( /^p[0-9\-,]+$/ ) ) {
// Anchor is the list of paragraphs, e.g. "p4", or "p6-8", or "p3,5,7".
fragment.slice( 1 ).split( ',' ).forEach( function ( pair ) {
fragment.slice( 1 ).split( ',' ).forEach( ( pair ) => {
const range = pair.split( '-' ),
start = parseInt( range[ 0 ] ),
end = parseInt( range.length > 1 ? range[ 1 ] : start );
Expand All @@ -39,7 +39,7 @@ $( function () {

const $d = $.Deferred();

$.get( title.getUrl() ).done( function ( html ) {
$.get( title.getUrl() ).done( ( html ) => {
const $paragraphs = $( '<div>' ).append( html ).find( '.mw-parser-output > p' );

let extract;
Expand All @@ -48,7 +48,7 @@ $( function () {
extract = $paragraphs.toArray();
} else {
extract = [];
[ ...parNumbers ].sort( ( a, b ) => a - b ).forEach( function ( idx ) {
[ ...parNumbers ].sort( ( a, b ) => a - b ).forEach( ( idx ) => {
const p = $paragraphs[ idx ];
if ( p ) {
extract.push( p );
Expand All @@ -58,7 +58,7 @@ $( function () {

$d.resolve( {
title: title,
extract: extract.map( function ( p ) {
extract: extract.map( ( p ) => {
return p.innerText.trim();
} ).join( '\n\n' )
} );
Expand All @@ -68,7 +68,7 @@ $( function () {
} );

// Accumulate the results into 1 string.
return Promise.all( promises ).then( function ( pageResults ) {
return Promise.all( promises ).then( ( pageResults ) => {
return pageResults.filter( ( x ) => x.extract ).map( ( ret, idx ) => {
const fragment = ret.title.fragment;
return mw.msg( 'askai-source',
Expand All @@ -84,11 +84,11 @@ $( function () {
wpExtract: extract,
wpPrompt: $prompt.val(),
wpEditToken: token
} ).fail( function ( xhr ) {
} ).fail( ( xhr ) => {
$response.val( mw.msg( 'askai-submit-failed',
xhr.statusText + ' (' + url + ')'
) );
} ).done( function ( ret ) {
} ).done( ( ret ) => {
$response.val( $( '<div>' ).append( ret ).find( '#mw-askai-response' ).text() );
} );
}
Expand Down

0 comments on commit 072c94f

Please sign in to comment.