Skip to content

Commit 5025a21

Browse files
committed
feat: add page blacklist
1 parent db3bd73 commit 5025a21

File tree

5 files changed

+102
-11
lines changed

5 files changed

+102
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Providing i18n and l10n to Gatsby. Besides translating pages and Markdown files,
5656
'/products#donut-filled-with-jam': '/produkte#berliner',
5757
'/services/software-development': '/dienstleistungen/softwareentwicklung'
5858
},
59+
pageBlacklist: ['/do-not-translate-to-german'], // If there is a page with the a given path it won't be translated
5960
messages: {
6061
"language": "Deutsch"
6162
},
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`translatePage should obey page blacklists 1`] = `
4+
[MockFunction] {
5+
"calls": [
6+
[
7+
{
8+
"component": {},
9+
"context": {
10+
"locale": "en-US",
11+
"localePagesId": "do-not-translate-to-german",
12+
"prefix": "en",
13+
"translations": [
14+
{
15+
"locale": "zh-CN",
16+
"path": "/zh/do-not-translate-to-german",
17+
},
18+
],
19+
},
20+
"isCreatedByStatefulCreatePages": true,
21+
"path": "/do-not-translate-to-german",
22+
},
23+
],
24+
[
25+
{
26+
"component": {},
27+
"context": {
28+
"locale": "zh-CN",
29+
"localePagesId": "do-not-translate-to-german",
30+
"prefix": "zh",
31+
"translations": [
32+
{
33+
"locale": "en-US",
34+
"path": "/do-not-translate-to-german",
35+
},
36+
],
37+
},
38+
"isCreatedByStatefulCreatePages": true,
39+
"path": "/zh/do-not-translate-to-german",
40+
},
41+
],
42+
],
43+
"results": [
44+
{
45+
"type": "return",
46+
"value": undefined,
47+
},
48+
{
49+
"type": "return",
50+
"value": undefined,
51+
},
52+
],
53+
}
54+
`;

src/onCreatePage/translatePage.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const options: PluginOptions = {
1919
'/pages/imprint': '/seiten/impressum',
2020
},
2121
messages: {},
22+
pageBlacklist: ['/do-not-translate-to-german'],
2223
},
2324
{
2425
locale: `zh-CN`,
@@ -254,4 +255,17 @@ describe('translatePage', () => {
254255
expect(actions.deletePage).not.toBeCalled();
255256
expect(actions.createPage).not.toBeCalled();
256257
});
258+
259+
it('should obey page blacklists', () => {
260+
const page: Page = {
261+
path: '/do-not-translate-to-german',
262+
component: {} as any,
263+
context: {},
264+
isCreatedByStatefulCreatePages: true,
265+
};
266+
267+
translatePage({ page, actions } as any, options);
268+
269+
expect(actions.createPage).toMatchSnapshot();
270+
});
257271
});

src/onCreatePage/translatePage.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
import { CreatePageArgs, PluginOptions } from 'gatsby';
1+
import { Actions, CreatePageArgs, Page, PluginOptions } from 'gatsby';
22
import { SitePageContext, UnstatefulSitePageContext } from '../../types';
33
import { createLocalePagesId } from '../utils/i18n';
44
import { generatePageContextByPath, translatePagePath, translatePagePaths } from '../utils/path';
55

6+
const isPagePathBlacklisted = (path: string, options: PluginOptions, locale?: string) => {
7+
const localeOptions = options.locales.find((l) => l.locale === locale);
8+
9+
if (!localeOptions?.pageBlacklist) {
10+
return false;
11+
}
12+
13+
if (localeOptions.pageBlacklist.find((pbl) => path.includes(pbl))) {
14+
return true;
15+
}
16+
17+
return false;
18+
};
19+
20+
const createTranslatedPage = (createPage: Actions['createPage'], page: Page<SitePageContext>, options: PluginOptions) => {
21+
if (!isPagePathBlacklisted(page.path, options, page.context?.locale)) {
22+
createPage(page);
23+
}
24+
};
25+
626
export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageContext>, options: PluginOptions) => {
727
const { createPage, deletePage } = actions;
828

@@ -11,14 +31,14 @@ export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageCo
1131
return;
1232
}
1333

14-
// Translate statefully created pages from `/src/pages` or gatsby-plugin-page-creator
15-
if (options && page.isCreatedByStatefulCreatePages) {
34+
if (page.isCreatedByStatefulCreatePages) {
35+
// Translate statefully created pages from `/src/pages` or gatsby-plugin-page-creator
1636
const paths = translatePagePaths(page.path, options);
1737

1838
deletePage(page);
1939

2040
paths.forEach((path) => {
21-
const translations = paths.filter((p) => p.locale !== path.locale);
41+
const translations = paths.filter((p) => p.locale !== path.locale && !isPagePathBlacklisted(p.path, options, p.locale));
2242
const locale = options.locales.find((l) => l.locale === path.locale);
2343
const context = {
2444
...page.context,
@@ -28,12 +48,10 @@ export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageCo
2848
prefix: locale?.prefix,
2949
};
3050

31-
createPage({ ...page, path: path.path, context });
51+
createTranslatedPage(createPage, { ...page, path: path.path, context }, options);
3252
});
33-
}
34-
35-
// Translate programmically generated pages
36-
if (options && !page.isCreatedByStatefulCreatePages) {
53+
} else {
54+
// Translate programmically created pages
3755
deletePage(page);
3856

3957
const { referTranslations, adjustPath, ...restContext } = (page.context as UnstatefulSitePageContext) || {};
@@ -52,7 +70,10 @@ export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageCo
5270
// Refer translations if requested
5371
if (referTranslations && Array.isArray(referTranslations) && referTranslations.length > 0) {
5472
const translations = translatePagePaths(path, options).filter(
55-
(p) => p.locale !== localeAndPrefixContext.locale && referTranslations.includes(p.locale),
73+
(p) =>
74+
p.locale !== localeAndPrefixContext.locale &&
75+
referTranslations.includes(p.locale) &&
76+
!isPagePathBlacklisted(p.path, options, p.locale),
5677
);
5778
context = { ...context, translations };
5879
}
@@ -62,6 +83,6 @@ export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageCo
6283
path = translatePagePath(path, optionsLocale.slugs, context.locale, context.prefix, options.defaultLocale);
6384
}
6485

65-
createPage({ ...page, context, path });
86+
createTranslatedPage(createPage, { ...page, context, path }, options);
6687
}
6788
};

types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ declare module 'gatsby' {
3636
prefix: string;
3737
slugs: Record<string, string>;
3838
messages: Record<string, string>;
39+
pageBlacklist?: string[];
3940
}[];
4041
pathBlacklist?: string[];
4142
}

0 commit comments

Comments
 (0)