File size: 3,533 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { capitalize } from 'lodash';
import type { ImporterPlatform } from 'calypso/lib/importer/types';

export const CAPTURE_URL_RGX =
	/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([-.][a-z0-9]+)*\.[a-z]{2,63}(:[0-9]{1,5})?(\/.*)?$/i;

/**
 * This regex is a bit more lenient than CAPTURE_URL_RGX, and is used for receiving any redirects like:
 * - http://www.example.com
 * - https://www.example.com
 * - http://localhost:9090
 * - http://somethingelse
 * - http://somecampaing#withhash?andquery
 */
export const CAPTURE_URL_RGX_SOFT =
	/^(https?:)?(?:[a-zA-Z0-9-.]+\.)?[a-zA-Z]{0,}(?:\/[^\s]*){0,}(:[0-9/a-z-#]*)?$/i;

const platformMap: { [ key in ImporterPlatform ]: string } = {
	wordpress: 'WordPress',
	wix: 'Wix',
	blogger: 'Blogger',
	medium: 'Medium',
	'godaddy-central': 'GoDaddy Central',
	tumblr: 'Tumblr',
	squarespace: 'Squarespace',
	ghost: 'Ghost',
	livejournal: 'LiveJournal',
	movabletype: 'Movable Type & TypePad',
	xanga: 'Xanga',
	substack: 'Substack',
	unknown: 'Unknown',
};

export const platformImporterNameMap: { [ key: string ]: string } = {
	xanga: 'xanga-wxr',
	ghost: 'ghost_import',
	movabletype: 'mt',
};

export const orgImporters: ImporterPlatform[] = [
	'xanga',
	'tumblr',
	'movabletype',
	'livejournal',
	'ghost',
];

/**
 * Platform name helpers
 */
export function getPlatformImporterName( platform: ImporterPlatform ): string {
	return platformImporterNameMap[ platform ] ? platformImporterNameMap[ platform ] : platform;
}

export function convertPlatformName( platform: ImporterPlatform ): string {
	return platformMap[ platform ] ?? 'Unknown';
}

export function convertToFriendlyWebsiteName( website: string ): string {
	const { hostname, pathname } = new URL(
		website.startsWith( 'http' ) ? website : `http://${ website }`
	);
	return ( hostname + ( pathname === '/' ? '' : pathname ) ).replace( 'www.', '' );
}

/**
 * Importer URL helpers
 */
export function getWpComMigrateUrl( siteSlug: string, fromSite?: string ): string {
	return '/migrate/{siteSlug}?from-site={fromSite}'
		.replace( '{siteSlug}', siteSlug )
		.replace( '{fromSite}', fromSite || '' );
}

export function getWpComOnboardingUrl(
	siteSlug: string,
	platform: ImporterPlatform,
	fromSite?: string
): string {
	const route = 'importer{importer}?siteSlug={siteSlug}&from={fromSite}';

	return route
		.replace( '{siteSlug}', siteSlug )
		.replace( '{importer}', capitalize( getPlatformImporterName( platform ) ) )
		.replace( '{fromSite}', fromSite || '' );
}

export function getWpComImporterUrl(
	siteSlug: string,
	platform: ImporterPlatform,
	fromSite?: string
): string {
	const wpComBase = '/import/{siteSlug}?engine={importer}&from-site={fromSite}';

	return wpComBase
		.replace( '{siteSlug}', siteSlug )
		.replace( '{importer}', getPlatformImporterName( platform ) )
		.replace( '{fromSite}', fromSite || '' );
}

export function getWpOrgImporterUrl( siteSlug: string, platform: ImporterPlatform ): string {
	const wpAdminBase = 'https://{siteSlug}/wp-admin/admin.php?import={importer}';

	return wpAdminBase
		.replace( '{siteSlug}', siteSlug )
		.replace( '{importer}', getPlatformImporterName( platform ) );
}

export function getImporterUrl(
	siteSlug: string,
	platform: ImporterPlatform,
	fromSite?: string
): string {
	if ( platform === 'wordpress' ) {
		return getWpComMigrateUrl( siteSlug, fromSite );
	} else if ( orgImporters.includes( platform ) ) {
		return getWpOrgImporterUrl( siteSlug, platform );
	}

	return getWpComImporterUrl( siteSlug, platform, fromSite );
}