Skip to content

Commit

Permalink
feat: omit path segments
Browse files Browse the repository at this point in the history
  • Loading branch information
openscript committed Jun 28, 2022
1 parent c01961a commit 133ac26
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Providing i18n and l10n to Gatsby. Besides translating pages and Markdown files,

## Usage

1. Make sure the peer dependencies `"gatsby": "^4.x"`, `"react-helmet": "^6.1.x"` and `"react-intl": "^5.20.x"` are dependencies of your Gatsby project.
1. Make sure the peer dependencies `"gatsby": "^4.x"`, `"react-helmet": "^6.1.x"` and `"react-intl": "^6.x"` are dependencies of your Gatsby project.
1. Install the plugin with `npm install gatsby-plugin-i18n-l10n` or `yarn add gatsby-plugin-i18n-l10n`.
1. Load and configure the plugin from your `gatsby-config.js` or `gatsby-config.ts`:

Expand Down Expand Up @@ -59,6 +59,11 @@ Providing i18n and l10n to Gatsby. Besides translating pages and Markdown files,
},
},
],
// omit certain path segments (relative directories)
// be careful not to cause path collisions
pathBlacklist: [
'/pages' // /pages/products/gummibears becomes /products/gummibears
]
},
}
```
Expand Down
1 change: 1 addition & 0 deletions gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const pluginOptionsSchema: GatsbyNode['pluginOptionsSchema'] = ({ Joi })
messages: Joi.object().required().description('Contains the translated messages.'),
}),
),
pathBlacklist: Joi.array().description('Omit certain path segments'),
});
};

Expand Down
71 changes: 71 additions & 0 deletions src/onCreateNode/translateNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,75 @@ describe('translateNode', () => {
],
});
});
it('should take the path blacklist into account when translating nodes', async () => {
const createNodeField = jest.fn();
fs.readdir = jest.fn().mockReturnValue(['page.en.md', 'page.zh-CN.md']);

const node = {
parent: '1',
internal: {
type: 'MarkdownRemark',
},
frontmatter: {
title: '',
},
};

const parentNode = {
name: 'page.de.md',
relativeDirectory: 'pages',
absolutePath: '/tmp/project/content/pages/page.de.md',
};

const args: any = {
getNode: jest.fn().mockReturnValue(parentNode),
node,
actions: {
createNodeField,
},
};

const options: PluginOptions = {
defaultLocale: `en-US`,
siteUrl: '',
locales: [
{
locale: `en-US`,
prefix: `en`,
slugs: {},
messages: {},
},
{
locale: `de-CH`,
prefix: `de`,
slugs: {},
messages: {},
},
{
locale: `zh-CN`,
prefix: `zh`,
slugs: {},
messages: {},
},
],
pathBlacklist: [`/pages`],
plugins: [],
};

await translateNode(args, options);

expect(createNodeField).toHaveBeenCalledWith({ node, name: 'locale', value: 'de-CH' });
expect(createNodeField).toHaveBeenCalledWith({ node, name: 'filename', value: 'page' });
expect(createNodeField).toHaveBeenCalledWith({ node, name: 'kind', value: 'pages' });
expect(createNodeField).toHaveBeenCalledWith({ node, name: 'slug', value: 'page' });
expect(createNodeField).toHaveBeenCalledWith({ node, name: 'path', value: '/de/page' });
expect(createNodeField).toHaveBeenCalledWith({
node,
name: 'translations',
value: [
{ locale: 'en-US', path: '/page' },
{ locale: 'zh-CN', path: '/zh/page' },
],
});
});
});
7 changes: 6 additions & 1 deletion src/onCreateNode/translateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ const translatePath = (filename: string, relativeDirectory: string, locale: stri

// 'relativeDirectory' is a synonym of 'kind'
const localeOption = options.locales.find((l) => l.locale === locale);
let filepath = addLocalePrefix(path.join('/', relativeDirectory, slug), locale, localeOption?.prefix || '', options.defaultLocale);
const currentPath = path.join('/', relativeDirectory, slug);

let filepath = addLocalePrefix(currentPath, locale, localeOption?.prefix || '', options.defaultLocale);
// remove segments which are on the path blacklist
filepath = options.pathBlacklist?.reduce((prev, curr) => prev.replace(curr, ''), filepath) || filepath;
// replace segments with slugs
if (relativeDirectory && localeOption) {
filepath = filepath.replace(`/${relativeDirectory}`, localeOption.slugs[`/${relativeDirectory}`] || `/${relativeDirectory}`);
}
Expand Down
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type PluginOptions = {
slugs: Record<string, string>;
messages: Record<string, string>;
}[];
pathBlacklist?: string[];
} & GatsbyPluginOptions;

type GatsbyNodeOnCreatePage = NonNullable<GatsbyNode['onCreatePage']>;
Expand Down

0 comments on commit 133ac26

Please sign in to comment.