mediawiki-projects-list 1.2.10 → 1.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.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const {properties: {wikiProjects: {items: {properties: wikiProjectSchema}}}} = require('./projects-schema.json');
2
+ const PROJECTS = require('./projects.json');
2
3
 
3
4
  /**
4
5
  * A MediaWiki project
@@ -20,6 +21,16 @@ const {properties: {wikiProjects: {items: {properties: wikiProjectSchema}}}} = r
20
21
  * @property {?string} note - Note about the specific project
21
22
  */
22
23
 
24
+ /**
25
+ * A frontend proxy
26
+ * @typedef {object} FrontendProxy
27
+ * @property {string} name - Hostname of the proxy
28
+ * @property {string} regex - Regex to match the proxy url
29
+ * @property {string} namePath - Name path of the proxy
30
+ * @property {string} articlePath - Article path of the proxy
31
+ * @property {string} scriptPath - Script path of the proxy
32
+ */
33
+
23
34
  /**
24
35
  * @type {{
25
36
  * inputToWikiProject: Map<string, ?{fullArticlePath: string, fullScriptPath: string, wikiProject: WikiProject}>,
@@ -30,14 +41,16 @@ const {properties: {wikiProjects: {items: {properties: wikiProjectSchema}}}} = r
30
41
  const functionCache = {
31
42
  inputToWikiProject: new Map(),
32
43
  urlToIdString: new Map(),
33
- idStringToUrl: new Map()
44
+ idStringToUrl: new Map(),
45
+ inputToFrontendProxy: new Map(),
46
+ urlToFix: new Map()
34
47
  };
35
48
 
36
49
  /**
37
50
  * List of MediaWiki projects
38
51
  * @type {WikiProject[]}
39
52
  */
40
- const wikiProjects = require('./projects.json').wikiProjects.map( wikiProject => {
53
+ const wikiProjects = PROJECTS.wikiProjects.map( wikiProject => {
41
54
  if ( wikiProject.idString ) {
42
55
  wikiProject.idString.separator ??= wikiProjectSchema.idString.properties.separator.default;
43
56
  wikiProject.idString.direction ??= wikiProjectSchema.idString.properties.direction.default;
@@ -50,6 +63,12 @@ const wikiProjects = require('./projects.json').wikiProjects.map( wikiProject =>
50
63
  return wikiProject;
51
64
  } );
52
65
 
66
+ /**
67
+ * List of frontend proxies
68
+ * @type {FrontendProxy[]}
69
+ */
70
+ const frontendProxies = PROJECTS.frontendProxies;
71
+
53
72
  /**
54
73
  *
55
74
  * @param {string} input
@@ -123,9 +142,54 @@ function idStringToUrl(idString, projectName) {
123
142
  return ( result ? new URL(result) : result );
124
143
  }
125
144
 
145
+ /**
146
+ *
147
+ * @param {string} input
148
+ * @returns {?{fullNamePath: string, fullArticlePath: string, fullScriptPath: string, frontendProxy: FrontendProxy}}
149
+ */
150
+ function inputToFrontendProxy(input) {
151
+ if ( functionCache.inputToFrontendProxy.has(input) ) return structuredClone(functionCache.inputToFrontendProxy.get(input));
152
+ let result = null;
153
+ let frontendProxy = frontendProxies.find( frontendProxy => input.split('/').slice(0, 3).some( part => part.endsWith( frontendProxy.name ) ) );
154
+ if ( frontendProxy ) {
155
+ let regex = input.match( new RegExp( frontendProxy.regex ) );
156
+ if ( regex ) {
157
+ result = {
158
+ fullNamePath: frontendProxy.namePath.replace( /\$(\d)/g, (match, n) => regex[n] ),
159
+ fullArticlePath: frontendProxy.articlePath.replace( /\$(\d)/g, (match, n) => regex[n] ),
160
+ fullScriptPath: frontendProxy.scriptPath.replace( /\$(\d)/g, (match, n) => regex[n] ),
161
+ frontendProxy: frontendProxy
162
+ };
163
+ }
164
+ }
165
+ functionCache.inputToFrontendProxy.set(input, result);
166
+ return structuredClone(result);
167
+ }
168
+
169
+ /**
170
+ *
171
+ * @param {string} url
172
+ * @returns {?((href:String,pagelink:String)=>String)}
173
+ */
174
+ function urlToFix(url) {
175
+ let hostname = url.split('/')[2];
176
+ if ( functionCache.urlToFix.has(hostname) ) return functionCache.urlToFix.get(hostname);
177
+ let result = null;
178
+ let frontendProxy = frontendProxies.find( frontendProxy => hostname.endsWith( frontendProxy.name ) );
179
+ if ( frontendProxy?.namePath.split('/').length > 4 ) {
180
+ let splitLength = frontendProxy.namePath.split('/').length;
181
+ result = (href, pagelink) => '/' + pagelink.split('/', splitLength).slice(3, -1).join('/') + href;
182
+ }
183
+ functionCache.urlToFix.set(hostname, result);
184
+ return result;
185
+ }
186
+
126
187
  module.exports = {
127
188
  wikiProjects,
189
+ frontendProxies,
128
190
  inputToWikiProject,
129
191
  urlToIdString,
130
- idStringToUrl
192
+ idStringToUrl,
193
+ inputToFrontendProxy,
194
+ urlToFix
131
195
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mediawiki-projects-list",
3
- "version": "1.2.10",
3
+ "version": "1.3.0",
4
4
  "type": "commonjs",
5
5
  "description": "List of MediaWiki projects for use in discord-wiki-bot",
6
6
  "main": "index.js",
@@ -9,6 +9,46 @@
9
9
  "type": "string",
10
10
  "format": "path"
11
11
  },
12
+ "frontendProxies": {
13
+ "type": "array",
14
+ "description": "List of frontend proxies",
15
+ "items": {
16
+ "type": "object",
17
+ "description": "A frontend proxy",
18
+ "properties": {
19
+ "name": {
20
+ "type": "string",
21
+ "description": "Hostname of the proxy"
22
+ },
23
+ "regex": {
24
+ "type": "string",
25
+ "description": "Regex to match the proxy url"
26
+ },
27
+ "namePath": {
28
+ "type": "string",
29
+ "description": "Name path of the proxy"
30
+ },
31
+ "articlePath": {
32
+ "type": "string",
33
+ "description": "Article path of the proxy"
34
+ },
35
+ "scriptPath": {
36
+ "type": "string",
37
+ "description": "Script path of the project"
38
+ }
39
+ },
40
+ "required": [
41
+ "name",
42
+ "regex",
43
+ "namePath",
44
+ "articlePath",
45
+ "scriptPath"
46
+ ],
47
+ "additionalProperties": false
48
+ },
49
+ "uniqueItemProperties": ["name"],
50
+ "additionalItems": false
51
+ },
12
52
  "wikiProjects": {
13
53
  "type": "array",
14
54
  "description": "List of MediaWiki projects",
package/projects.json CHANGED
@@ -1,5 +1,28 @@
1
1
  {
2
2
  "$schema": "./projects-schema.json",
3
+ "frontendProxies": [
4
+ {
5
+ "name": ".breezewiki.com",
6
+ "regex": "([a-z\\d-]{1,50})\\.breezewiki\\.com(?:/wiki/|/?$)",
7
+ "namePath": "https://breezewiki.com/$1/",
8
+ "articlePath": "https://breezewiki.com/$1/wiki/",
9
+ "scriptPath": "https://$1.fandom.com/"
10
+ },
11
+ {
12
+ "name": "breezewiki.com",
13
+ "regex": "breezewiki\\.com/([a-z\\d-]{1,50})(?:/wiki/|/?$)",
14
+ "namePath": "https://breezewiki.com/$1/",
15
+ "articlePath": "https://breezewiki.com/$1/wiki/",
16
+ "scriptPath": "https://$1.fandom.com/"
17
+ },
18
+ {
19
+ "name": "breezewiki.pussthecat.org",
20
+ "regex": "breezewiki\\.pussthecat\\.org/([a-z\\d-]{1,50})(?:/wiki/|/?$)",
21
+ "namePath": "https://breezewiki.pussthecat.org/$1/",
22
+ "articlePath": "https://breezewiki.pussthecat.org/$1/wiki/",
23
+ "scriptPath": "https://$1.fandom.com/"
24
+ }
25
+ ],
3
26
  "wikiProjects": [
4
27
  {
5
28
  "name": "fandom.com",