This repository was archived by the owner on May 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiki.js
More file actions
79 lines (76 loc) · 2.16 KB
/
wiki.js
File metadata and controls
79 lines (76 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* wiki.js
*
* Module for parsing wiki URLs.
*/
/**
* Importing modules.
*/
// eslint-disable-next-line node/no-extraneous-import
import 'babel-polyfill';
import {validateLanguageCode} from 'locale-code';
/**
* Constants.
*/
const WIKI_VALIDATION_REGEX = /^\s*https?:\/\/([a-z0-9-.]+)\.(fandom\.com|wikia\.(?:com|org)|(?:wikia|fandom)-dev\.(?:com|us|pl))\/?([a-z-]*)\/?/u;
/**
* Fandom wiki URL validator.
*/
export default class Wiki {
/**
* Class constructor.
* @param {string} url URL to be validated and parsed
*/
constructor(url) {
this.valid = false;
const result = Wiki.validate(url);
const subdomain = result.shift(),
domain = result.shift(),
language = result.shift(),
splitSubdomain = subdomain.split('.');
this.domain = domain === 'wikia.com' ? 'fandom.com' : domain;
if (validateLanguageCode(`${language}-US`)) {
this.language = language;
this.subdomain = subdomain;
} else if (
splitSubdomain.length > 1 &&
validateLanguageCode(`${splitSubdomain[0]}-US`)
) {
this.language = splitSubdomain[0];
this.subdomain = splitSubdomain.slice(1).join('.');
} else {
this.subdomain = subdomain;
}
}
/**
* Validates a wiki URL.
* @param {string} url URL to be validated
* @returns {boolean|Array} Regular expression result
*/
static validate(url) {
const result = WIKI_VALIDATION_REGEX.exec(url);
if (!result) {
// Invalid URL, return.
return false;
}
result.shift();
return result;
}
/**
* Gets the wiki URL.
* @returns {string} The wiki URL
*/
get url() {
if (this.language) {
return `https://${this.subdomain}.${this.domain}/${this.language}`;
}
return `https://${this.subdomain}.${this.domain}`;
}
/**
* Gets the URL to the wiki's MediaWiki API endpoint.
* @returns {string} The URL to the wiki's MediaWiki API endpoint
*/
get apiUrl() {
return `${this.url}/api.php`;
}
}