Pārlūkot izejas kodu

support wikiHow

Markus-Rost 3 gadi atpakaļ
vecāks
revīzija
b99f70b5d5
2 mainītis faili ar 16 papildinājumiem un 7 dzēšanām
  1. 6 0
      util/default.json
  2. 10 7
      util/wiki.js

+ 6 - 0
util/default.json

@@ -220,6 +220,12 @@
 			"articlePath": "/w/",
 			"scriptPath": "/"
 		},
+		{
+			"name": "wikihow.com",
+			"regex": "((?:www\\.)?wikihow\\.com)",
+			"articlePath": "/",
+			"scriptPath": "/"
+		},
 		{
 			"name": "librewiki.net",
 			"regex": "(librewiki\\.net)",

+ 10 - 7
util/wiki.js

@@ -170,7 +170,7 @@ class Wiki extends URL {
 	toLink(title = '', querystring = '', fragment = '', isMarkdown = false) {
 		querystring = new URLSearchParams(querystring);
 		if ( !querystring.toString().length ) title = ( title || this.mainpage );
-		title = title.replace( / /g, '_' ).replace( /%/g, '%2525' );
+		title = title.replace( / /g, this.articleURL.space ).replace( /%/g, '%2525' );
 		let link = new URL(this.articleURL);
 		link.username = '';
 		link.password = '';
@@ -184,7 +184,7 @@ class Wiki extends URL {
 		querystring.forEach( (value, name) => {
 			link.searchParams.append(name, value);
 		} );
-		let output = decodeURI( link ).replace( /\\/g, '%5C' ).replace( /@(here|everyone)/g, '%40$1' ) + Wiki.toSection(fragment);
+		let output = decodeURI( link ).replace( /\\/g, '%5C' ).replace( /@(here|everyone)/g, '%40$1' ) + Wiki.toSection(fragment, true, this.articleURL.space);
 		if ( isMarkdown ) return output.replace( /\(/g, '%28' ).replace( /\)/g, '%29' );
 		else return output;
 	}
@@ -192,11 +192,12 @@ class Wiki extends URL {
 	/**
 	 * Encode a page title.
 	 * @param {String} [title] - Title of the page.
+	 * @param {String} [space] - The url replacement for spaces.
 	 * @returns {String}
 	 * @static
 	 */
-	static toTitle(title = '') {
-		return title.replace( / /g, '_' ).replace( /[?&%\\]/g, (match) => {
+	static toTitle(title = '', space = '_') {
+		return title.replace( / /g, space ).replace( /[?&%\\]/g, (match) => {
 			return '%' + match.charCodeAt().toString(16).toUpperCase();
 		} ).replace( /@(here|everyone)/g, '%40$1' ).replace( /[()]/g, '\\$&' );
 	};
@@ -205,12 +206,13 @@ class Wiki extends URL {
 	 * Encode a link section.
 	 * @param {String} [fragment] - Fragment of the page.
 	 * @param {Boolean} [simpleEncoding] - Don't fully encode the anchor.
+	 * @param {String} [space] - The url replacement for spaces.
 	 * @returns {String}
 	 * @static
 	 */
-	static toSection(fragment = '', simpleEncoding = true) {
+	static toSection(fragment = '', simpleEncoding = true, space = '_') {
 		if ( !fragment ) return '';
-		fragment = fragment.replace( / /g, '_' );
+		fragment = fragment.replace( / /g, space );
 		if ( simpleEncoding && !/['"`^{}<>|\\]|@(everyone|here)/.test(fragment) ) return '#' + fragment;
 		return '#' + encodeURIComponent( fragment ).replace( /[!'()*~]/g, (match) => {
 			return '%' + match.charCodeAt().toString(16).toUpperCase();
@@ -288,13 +290,14 @@ class articleURL extends URL {
 		super(articlepath, wiki);
 		this.protocol = 'https';
 		this.mainpage = '';
+		this.space = ( this.hostname === 'www.wikihow.com' ? '-' : '_' );
 	}
 
 	[util.inspect.custom](depth, opts) {
 		if ( typeof depth === 'number' && depth < 0 ) return this;
 		if ( typeof depth === 'number' && depth < 2 ) {
 			var link = this.href;
-			var mainpage = link.replace( '$1', ( this.mainpage || 'Main Page' ).replace( / /g, '_' ) );
+			var mainpage = link.replace( '$1', ( this.mainpage || 'Main Page' ).replace( / /g, this.space ) );
 			return 'articleURL { ' + util.inspect(link, opts) + ' => ' + util.inspect(mainpage, opts) + ' }';
 		}
 		return super[util.inspect.custom](depth, opts);