1
- import { CreatePageArgs , PluginOptions } from 'gatsby' ;
1
+ import { Actions , CreatePageArgs , Page , PluginOptions } from 'gatsby' ;
2
2
import { SitePageContext , UnstatefulSitePageContext } from '../../types' ;
3
3
import { createLocalePagesId } from '../utils/i18n' ;
4
4
import { generatePageContextByPath , translatePagePath , translatePagePaths } from '../utils/path' ;
5
5
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
+
6
26
export const translatePage = async ( { page, actions } : CreatePageArgs < SitePageContext > , options : PluginOptions ) => {
7
27
const { createPage, deletePage } = actions ;
8
28
@@ -11,14 +31,14 @@ export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageCo
11
31
return ;
12
32
}
13
33
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
16
36
const paths = translatePagePaths ( page . path , options ) ;
17
37
18
38
deletePage ( page ) ;
19
39
20
40
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 ) ) ;
22
42
const locale = options . locales . find ( ( l ) => l . locale === path . locale ) ;
23
43
const context = {
24
44
...page . context ,
@@ -28,12 +48,10 @@ export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageCo
28
48
prefix : locale ?. prefix ,
29
49
} ;
30
50
31
- createPage ( { ...page , path : path . path , context } ) ;
51
+ createTranslatedPage ( createPage , { ...page , path : path . path , context } , options ) ;
32
52
} ) ;
33
- }
34
-
35
- // Translate programmically generated pages
36
- if ( options && ! page . isCreatedByStatefulCreatePages ) {
53
+ } else {
54
+ // Translate programmically created pages
37
55
deletePage ( page ) ;
38
56
39
57
const { referTranslations, adjustPath, ...restContext } = ( page . context as UnstatefulSitePageContext ) || { } ;
@@ -52,7 +70,10 @@ export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageCo
52
70
// Refer translations if requested
53
71
if ( referTranslations && Array . isArray ( referTranslations ) && referTranslations . length > 0 ) {
54
72
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 ) ,
56
77
) ;
57
78
context = { ...context , translations } ;
58
79
}
@@ -62,6 +83,6 @@ export const translatePage = async ({ page, actions }: CreatePageArgs<SitePageCo
62
83
path = translatePagePath ( path , optionsLocale . slugs , context . locale , context . prefix , options . defaultLocale ) ;
63
84
}
64
85
65
- createPage ( { ...page , context, path } ) ;
86
+ createTranslatedPage ( createPage , { ...page , context, path } , options ) ;
66
87
}
67
88
} ;
0 commit comments