-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathonCreatePage.ts
110 lines (100 loc) · 2.98 KB
/
onCreatePage.ts
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
import {CreatePageArgs, Page} from 'gatsby';
import BP from 'bluebird';
import {match} from 'path-to-regexp';
import {PageContext, PageOptions, PluginOptions} from '../types';
export const onCreatePage = async (
{page, actions}: CreatePageArgs<PageContext>,
pluginOptions: PluginOptions
) => {
//Exit if the page has already been processed.
if (typeof page.context?.i18n === 'object') {
return;
}
const {createPage, deletePage} = actions;
const {
defaultLanguage = 'en',
generateDefaultLanguagePage = false,
languages = ['en'],
pages = []
} = pluginOptions;
type GeneratePageParams = {
language: string;
path?: string;
originalPath?: string;
routed?: boolean;
matchPath?: string;
pageOptions?: PageOptions;
};
const generatePage = async ({
language,
path = page.path,
originalPath = page.path,
routed = false,
matchPath = page.matchPath,
pageOptions
}: GeneratePageParams): Promise<Page<PageContext>> => {
return {
...page,
matchPath,
path,
context: {
...page.context,
language,
i18n: {
language,
languages: pageOptions?.languages || languages,
defaultLanguage,
generateDefaultLanguagePage,
routed,
originalPath,
path
}
}
};
};
const pageOptions = pages.find((opt) => match(opt.matchPath)(page.path));
let newPage;
let alternativeLanguages = generateDefaultLanguagePage
? languages
: languages.filter((lng) => lng !== defaultLanguage);
if (pageOptions?.excludeLanguages) {
alternativeLanguages = alternativeLanguages.filter(
(lng) => !pageOptions?.excludeLanguages?.includes(lng)
);
}
if (pageOptions?.languages) {
alternativeLanguages = generateDefaultLanguagePage
? pageOptions.languages
: pageOptions.languages.filter((lng) => lng !== defaultLanguage);
}
if (pageOptions?.getLanguageFromPath) {
const result = match<{lang: string}>(pageOptions.matchPath)(page.path);
if (!result) return;
const language = languages.find((lng) => lng === result.params.lang) || defaultLanguage;
const originalPath = page.path.replace(`/${language}`, '');
const routed = Boolean(result.params.lang);
newPage = await generatePage({language, originalPath, routed, pageOptions});
if (routed || !pageOptions.excludeLanguages) {
alternativeLanguages = [];
}
} else {
newPage = await generatePage({language: defaultLanguage, pageOptions});
}
try {
deletePage(page);
} catch {}
createPage(newPage);
await BP.map(alternativeLanguages, async (lng) => {
const localePage = await generatePage({
language: lng,
path: `${lng}${page.path}`,
matchPath: page.matchPath ? `/${lng}${page.matchPath}` : undefined,
routed: true
});
const regexp = new RegExp('/404/?$');
if (regexp.test(localePage.path)) {
localePage.matchPath = `/${lng}/*`;
}
createPage(localePage);
});
};