mediawiki-projects-list 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -35,9 +35,30 @@ declare module 'mediawiki-projects-list' {
35
35
  note: string | null;
36
36
  };
37
37
 
38
+ /** A frontend proxy */
39
+ export type FrontendProxy = {
40
+ /** Hostname of the proxy */
41
+ name: string;
42
+ /** Regex to match the proxy url */
43
+ regex: string;
44
+ /** Name path of the proxy */
45
+ namePath: string;
46
+ /** Article path of the proxy */
47
+ articlePath: string;
48
+ /** Script path of the proxy */
49
+ scriptPath: string;
50
+ /** Regex to remove from the relative url */
51
+ relativeFix: string;
52
+ /** Note about the specific proxy */
53
+ note: string | null;
54
+ };
55
+
38
56
  /** List of MediaWiki projects */
39
57
  export const wikiProjects: WikiProject[];
40
58
 
59
+ /** List of frontend proxies */
60
+ export const frontendProxies: FrontendProxy[];
61
+
41
62
  export function inputToWikiProject(input: string): {
42
63
  fullArticlePath: string;
43
64
  fullScriptPath: string;
@@ -48,4 +69,13 @@ declare module 'mediawiki-projects-list' {
48
69
 
49
70
  export function idStringToUrl(idString: string, projectName: string): URL | null;
50
71
 
72
+ export function inputToWikiProject(input: string): {
73
+ fullNamePath: string;
74
+ fullArticlePath: string;
75
+ fullScriptPath: string;
76
+ frontendProxy: FrontendProxy;
77
+ } | null;
78
+
79
+ export function urlToFix(url: string): ((href: string, pagelink: string) => string) | null;
80
+
51
81
  }
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const {properties: {wikiProjects: {items: {properties: wikiProjectSchema}}}} = require('./projects-schema.json');
1
+ const {properties: {wikiProjects: {items: {properties: wikiProjectSchema}}, frontendProxies: {items: {properties: frontendProxySchema}}}} = require('./projects-schema.json');
2
2
  const PROJECTS = require('./projects.json');
3
3
 
4
4
  /**
@@ -29,6 +29,8 @@ const PROJECTS = require('./projects.json');
29
29
  * @property {string} namePath - Name path of the proxy
30
30
  * @property {string} articlePath - Article path of the proxy
31
31
  * @property {string} scriptPath - Script path of the proxy
32
+ * @property {?string} relativeFix - Regex to remove from the relative url
33
+ * @property {?string} note - Note about the specific proxy
32
34
  */
33
35
 
34
36
  /**
@@ -69,7 +71,11 @@ const wikiProjects = PROJECTS.wikiProjects.map( wikiProject => {
69
71
  * List of frontend proxies
70
72
  * @type {FrontendProxy[]}
71
73
  */
72
- const frontendProxies = PROJECTS.frontendProxies;
74
+ const frontendProxies = PROJECTS.frontendProxies.map( frontendProxy => {
75
+ frontendProxy.relativeFix ??= frontendProxySchema.relativeFix.default;
76
+ frontendProxy.note ??= frontendProxySchema.note.default;
77
+ return frontendProxy;
78
+ } );
73
79
 
74
80
  /**
75
81
  *
@@ -188,8 +194,19 @@ function urlToFix(url) {
188
194
  let frontendProxy = frontendProxies.find( frontendProxy => hostname.endsWith( frontendProxy.name ) );
189
195
  if ( frontendProxy ) {
190
196
  let splitLength = frontendProxy.namePath.split('/').length;
191
- let querykeys = frontendProxy.namePath.split('?').slice(1).join('?').split('&').flatMap( query => query.split('=', 1) );
192
- if ( splitLength > 4 && querykeys.length ) {
197
+ let querykeys = frontendProxy.namePath.split('?').slice(1).join('?').split('&').flatMap( query => {
198
+ if ( !query ) return [];
199
+ return query.split('=', 1);
200
+ } );
201
+ if ( splitLength > 4 && querykeys.length && frontendProxy.relativeFix ) {
202
+ result = (href, pagelink) => {
203
+ let prepend = '/' + pagelink.split('/', splitLength).slice(3, -1).join('/');
204
+ let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
205
+ let append = ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
206
+ return prepend + href.replace( new RegExp( frontendProxy.relativeFix ), '' ) + append;
207
+ };
208
+ }
209
+ else if ( splitLength > 4 && querykeys.length ) {
193
210
  result = (href, pagelink) => {
194
211
  let prepend = '/' + pagelink.split('/', splitLength).slice(3, -1).join('/');
195
212
  let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
@@ -197,15 +214,30 @@ function urlToFix(url) {
197
214
  return prepend + href + append;
198
215
  };
199
216
  }
217
+ else if ( splitLength > 4 && frontendProxy.relativeFix ) {
218
+ result = (href, pagelink) => {
219
+ let prepend = '/' + pagelink.split('/', splitLength).slice(3, -1).join('/');
220
+ return prepend + href.replace( new RegExp( frontendProxy.relativeFix ), '' );
221
+ }
222
+ }
200
223
  else if ( splitLength > 4 ) {
201
224
  result = (href, pagelink) => '/' + pagelink.split('/', splitLength).slice(3, -1).join('/') + href;
202
225
  }
226
+ else if ( querykeys.length && frontendProxy.relativeFix ) {
227
+ result = (href, pagelink) => {
228
+ let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
229
+ return href.replace( new RegExp( frontendProxy.relativeFix ), '' ) + ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
230
+ }
231
+ }
203
232
  else if ( querykeys.length ) {
204
233
  result = (href, pagelink) => {
205
234
  let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
206
235
  return href + ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
207
236
  }
208
237
  }
238
+ else if ( frontendProxy.relativeFix ) {
239
+ result = (href, pagelink) => href.replace( new RegExp( frontendProxy.relativeFix ), '' );
240
+ }
209
241
  }
210
242
  functionCache.urlToFix.set(hostname, result);
211
243
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mediawiki-projects-list",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "type": "commonjs",
5
5
  "description": "List of MediaWiki projects for use in discord-wiki-bot",
6
6
  "main": "index.js",
@@ -34,7 +34,17 @@
34
34
  },
35
35
  "scriptPath": {
36
36
  "type": "string",
37
- "description": "Script path of the project"
37
+ "description": "Script path of the proxy"
38
+ },
39
+ "relativeFix": {
40
+ "type": "string",
41
+ "description": "Regex to remove from the relative url",
42
+ "default": null
43
+ },
44
+ "note": {
45
+ "type": "string",
46
+ "description": "Note about the specific proxy",
47
+ "default": null
38
48
  }
39
49
  },
40
50
  "required": [
package/projects.json CHANGED
@@ -3,31 +3,42 @@
3
3
  "frontendProxies": [
4
4
  {
5
5
  "name": "wikiless.org",
6
- "regex": "wikiless\\.org/.*?\\?(?:.*?&)?lang=([a-z\\d-]{1,50})(?:&|$)",
6
+ "regex": "wikiless\\.org/.*?\\?(?:.*?&)?lang=([a-z\\d-]{1,50})(?:&|/?$)",
7
7
  "namePath": "https://wikiless.org/?lang=$1",
8
8
  "articlePath": "https://wikiless.org/wiki/?lang=$1",
9
9
  "scriptPath": "https://$1.wikipedia.org/w/"
10
10
  },
11
+ {
12
+ "name": "wikiwand.com",
13
+ "regex": "(?:www\\.)?wikiwand\\.com/([a-z\\d-]{1,50})/?",
14
+ "namePath": "https://www.wikiwand.com/$1/",
15
+ "articlePath": "https://www.wikiwand.com/$1/",
16
+ "scriptPath": "https://$1.wikipedia.org/w/",
17
+ "relativeFix": "^/wiki(?=/)"
18
+ },
11
19
  {
12
20
  "name": ".breezewiki.com",
13
21
  "regex": "([a-z\\d-]{1,50})\\.breezewiki\\.com(?:/wiki/|/?$)",
14
22
  "namePath": "https://breezewiki.com/$1/",
15
23
  "articlePath": "https://breezewiki.com/$1/wiki/",
16
- "scriptPath": "https://$1.fandom.com/"
24
+ "scriptPath": "https://$1.fandom.com/",
25
+ "note": "Missing language support."
17
26
  },
18
27
  {
19
28
  "name": "breezewiki.com",
20
29
  "regex": "breezewiki\\.com/([a-z\\d-]{1,50})(?:/wiki/|/?$)",
21
30
  "namePath": "https://breezewiki.com/$1/",
22
31
  "articlePath": "https://breezewiki.com/$1/wiki/",
23
- "scriptPath": "https://$1.fandom.com/"
32
+ "scriptPath": "https://$1.fandom.com/",
33
+ "note": "Missing language support."
24
34
  },
25
35
  {
26
36
  "name": "breezewiki.pussthecat.org",
27
37
  "regex": "breezewiki\\.pussthecat\\.org/([a-z\\d-]{1,50})(?:/wiki/|/?$)",
28
38
  "namePath": "https://breezewiki.pussthecat.org/$1/",
29
39
  "articlePath": "https://breezewiki.pussthecat.org/$1/wiki/",
30
- "scriptPath": "https://$1.fandom.com/"
40
+ "scriptPath": "https://$1.fandom.com/",
41
+ "note": "Missing language support."
31
42
  }
32
43
  ],
33
44
  "wikiProjects": [