mediawiki-projects-list 2.1.0 → 2.3.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
@@ -47,6 +47,19 @@ declare module 'mediawiki-projects-list' {
47
47
  articlePath: string;
48
48
  /** Script path of the proxy */
49
49
  scriptPath: string;
50
+ /** Regex to remove from the relative url */
51
+ relativeFix: string;
52
+ /** Only exists when the hostname contains multiple wikis: How to handle the id string */
53
+ idString?: {
54
+ /** Separator to join or split the id string on */
55
+ separator: string;
56
+ /** Order in which the project regex additional group matches should be chained to gain the id string */
57
+ direction: "asc" | "desc";
58
+ /** Regex to match the id string */
59
+ regex: string;
60
+ /** How to turn the group matches of the id string regex into an URL to the script path, index based on group matches */
61
+ scriptPaths: string[];
62
+ };
50
63
  /** Note about the specific proxy */
51
64
  note: string | null;
52
65
  };
package/index.js CHANGED
@@ -29,6 +29,12 @@ 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 {object} [idString] - Only exists when the hostname contains multiple wikis: How to handle the id string
34
+ * @property {string} idString.separator - Separator to join or split the id string on
35
+ * @property {"asc"|"desc"} idString.direction - Order in which the project regex additional group matches should be chained to gain the id string
36
+ * @property {string} idString.regex - Regex to match the id string
37
+ * @property {string[]} idString.scriptPaths - How to turn the group matches of the id string regex into an URL to the script path, index based on group matches
32
38
  * @property {?string} note - Note about the specific proxy
33
39
  */
34
40
 
@@ -71,6 +77,11 @@ const wikiProjects = PROJECTS.wikiProjects.map( wikiProject => {
71
77
  * @type {FrontendProxy[]}
72
78
  */
73
79
  const frontendProxies = PROJECTS.frontendProxies.map( frontendProxy => {
80
+ if ( frontendProxy.idString ) {
81
+ frontendProxy.idString.separator ??= frontendProxySchema.idString.properties.separator.default;
82
+ frontendProxy.idString.direction ??= frontendProxySchema.idString.properties.direction.default;
83
+ }
84
+ frontendProxy.relativeFix ??= frontendProxySchema.relativeFix.default;
74
85
  frontendProxy.note ??= frontendProxySchema.note.default;
75
86
  return frontendProxy;
76
87
  } );
@@ -141,11 +152,12 @@ function idStringToUrl(idString, projectName) {
141
152
  return ( result ? new URL(result) : result );
142
153
  }
143
154
  let result = null;
144
- let wikiProject = wikiProjects.find( wikiProject => wikiProject.idString && wikiProject.name === projectName )?.idString;
145
- if ( wikiProject ) {
146
- let regex = idString.match( new RegExp( '^' + wikiProject.regex + '$' ) )?.[1].split(wikiProject.separator);
147
- if ( regex && regex.length <= wikiProject.scriptPaths.length ) {
148
- result = wikiProject.scriptPaths[regex.length - 1].replace( /\$(\d)/g, (match, n) => regex[n - 1] );
155
+ let idString = wikiProjects.find( wikiProject => wikiProject.idString && wikiProject.name === projectName )?.idString;
156
+ if ( !idString ) idString = frontendProxies.find( frontendProxy => frontendProxy.idString && frontendProxy.name === projectName )?.idString;
157
+ if ( idString ) {
158
+ let regex = idString.match( new RegExp( '^' + idString.regex + '$' ) )?.[1].split(idString.separator);
159
+ if ( regex && regex.length <= idString.scriptPaths.length ) {
160
+ result = idString.scriptPaths[regex.length - 1].replace( /\$(\d)/g, (match, n) => regex[n - 1] );
149
161
  }
150
162
  }
151
163
  functionCache.idStringToUrl.set(cacheKey, result);
@@ -192,8 +204,19 @@ function urlToFix(url) {
192
204
  let frontendProxy = frontendProxies.find( frontendProxy => hostname.endsWith( frontendProxy.name ) );
193
205
  if ( frontendProxy ) {
194
206
  let splitLength = frontendProxy.namePath.split('/').length;
195
- let querykeys = frontendProxy.namePath.split('?').slice(1).join('?').split('&').flatMap( query => query.split('=', 1) );
196
- if ( splitLength > 4 && querykeys.length ) {
207
+ let querykeys = frontendProxy.namePath.split('?').slice(1).join('?').split('&').flatMap( query => {
208
+ if ( !query ) return [];
209
+ return query.split('=', 1);
210
+ } );
211
+ if ( splitLength > 4 && querykeys.length && frontendProxy.relativeFix ) {
212
+ result = (href, pagelink) => {
213
+ let prepend = '/' + pagelink.split('/', splitLength).slice(3, -1).join('/');
214
+ let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
215
+ let append = ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
216
+ return prepend + href.replace( new RegExp( frontendProxy.relativeFix ), '' ) + append;
217
+ };
218
+ }
219
+ else if ( splitLength > 4 && querykeys.length ) {
197
220
  result = (href, pagelink) => {
198
221
  let prepend = '/' + pagelink.split('/', splitLength).slice(3, -1).join('/');
199
222
  let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
@@ -201,15 +224,30 @@ function urlToFix(url) {
201
224
  return prepend + href + append;
202
225
  };
203
226
  }
227
+ else if ( splitLength > 4 && frontendProxy.relativeFix ) {
228
+ result = (href, pagelink) => {
229
+ let prepend = '/' + pagelink.split('/', splitLength).slice(3, -1).join('/');
230
+ return prepend + href.replace( new RegExp( frontendProxy.relativeFix ), '' );
231
+ }
232
+ }
204
233
  else if ( splitLength > 4 ) {
205
234
  result = (href, pagelink) => '/' + pagelink.split('/', splitLength).slice(3, -1).join('/') + href;
206
235
  }
236
+ else if ( querykeys.length && frontendProxy.relativeFix ) {
237
+ result = (href, pagelink) => {
238
+ let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
239
+ return href.replace( new RegExp( frontendProxy.relativeFix ), '' ) + ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
240
+ }
241
+ }
207
242
  else if ( querykeys.length ) {
208
243
  result = (href, pagelink) => {
209
244
  let querystring = pagelink.split('?').slice(1).join('?').split('&').filter( query => querykeys.includes( query.split('=', 1)[0] ) );
210
245
  return href + ( href.includes('?') ? '&' : '?' ) + querystring.join('&');
211
246
  }
212
247
  }
248
+ else if ( frontendProxy.relativeFix ) {
249
+ result = (href, pagelink) => href.replace( new RegExp( frontendProxy.relativeFix ), '' );
250
+ }
213
251
  }
214
252
  functionCache.urlToFix.set(hostname, result);
215
253
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mediawiki-projects-list",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "type": "commonjs",
5
5
  "description": "List of MediaWiki projects for use in discord-wiki-bot",
6
6
  "main": "index.js",
@@ -36,6 +36,50 @@
36
36
  "type": "string",
37
37
  "description": "Script path of the proxy"
38
38
  },
39
+ "relativeFix": {
40
+ "type": "string",
41
+ "description": "Regex to remove from the relative url",
42
+ "default": null
43
+ },
44
+ "idString": {
45
+ "type": "object",
46
+ "description": "Only exists when the hostname contains multiple wikis: How to handle the id string",
47
+ "properties": {
48
+ "separator": {
49
+ "type": "string",
50
+ "description": "Separator to join or split the id string on",
51
+ "default": "."
52
+ },
53
+ "direction": {
54
+ "type": "string",
55
+ "description": "Order in which the project regex additional group matches should be chained to gain the id string",
56
+ "enum": [
57
+ "asc",
58
+ "desc"
59
+ ],
60
+ "default": "desc"
61
+ },
62
+ "regex": {
63
+ "type": "string",
64
+ "description": "Regex to match the id string"
65
+ },
66
+ "scriptPaths": {
67
+ "type": "array",
68
+ "description": "How to turn the group matches of the id string regex into an URL to the script path, index based on group matches",
69
+ "items": {
70
+ "type": "string",
71
+ "description": "Replacement for the id string regex match."
72
+ },
73
+ "contains": true,
74
+ "additionalItems": false
75
+ }
76
+ },
77
+ "required": [
78
+ "regex",
79
+ "scriptPaths"
80
+ ],
81
+ "additionalProperties": false
82
+ },
39
83
  "note": {
40
84
  "type": "string",
41
85
  "description": "Note about the specific proxy",
package/projects.json CHANGED
@@ -3,18 +3,30 @@
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
- "scriptPath": "https://$1.wikipedia.org/w/"
9
+ "scriptPath": "https://$1.wikipedia.org/w/",
10
+ "idString": {
11
+ "regex": "([a-z\\d-]{1,50})",
12
+ "scriptPaths": [
13
+ "https://wikiless.org/?lang=$1"
14
+ ]
15
+ }
10
16
  },
11
17
  {
12
- "name": ".breezewiki.com",
13
- "regex": "([a-z\\d-]{1,50})\\.breezewiki\\.com(?:/wiki/|/?$)",
14
- "namePath": "https://breezewiki.com/$1/",
15
- "articlePath": "https://breezewiki.com/$1/wiki/",
16
- "scriptPath": "https://$1.fandom.com/",
17
- "note": "Missing language support."
18
+ "name": "wikiwand.com",
19
+ "regex": "(?:www\\.)?wikiwand\\.com/([a-z\\d-]{1,50})/?",
20
+ "namePath": "https://www.wikiwand.com/$1/",
21
+ "articlePath": "https://www.wikiwand.com/$1/",
22
+ "scriptPath": "https://$1.wikipedia.org/w/",
23
+ "relativeFix": "^/wiki(?=/)",
24
+ "idString": {
25
+ "regex": "([a-z\\d-]{1,50})",
26
+ "scriptPaths": [
27
+ "https://www.wikiwand.com/$1/"
28
+ ]
29
+ }
18
30
  },
19
31
  {
20
32
  "name": "breezewiki.com",