Skip to content

Commit

Permalink
fix: slug translation
Browse files Browse the repository at this point in the history
  • Loading branch information
openscript committed Jun 30, 2022
1 parent ad97956 commit 355c0d7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"build": "tsc",
"check:format": "eslint . --max-warnings 0",
"check:types": "tsc --noEmit",
"dev:test": "jest --watch",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
"postbuild": "node ./postbuild.js",
"release:major": "standard-version --release-as major && git push --follow-tags",
Expand Down
69 changes: 68 additions & 1 deletion src/onCreateNode/translateNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('translateNode', () => {
],
});
});

it('should create nodes with accurate kind description', async () => {
fs.readdir = jest.fn().mockReturnValue(['section.en.md', 'section.zh-CN.md']);

Expand Down Expand Up @@ -115,7 +116,8 @@ describe('translateNode', () => {
],
});
});
it('should take the path blacklist into account when translating nodes', async () => {

it('should remote blacklisted path segments when translating nodes', async () => {
fs.readdir = jest.fn().mockReturnValue(['page.en.md', 'page.zh-CN.md']);

const parentNode = {
Expand Down Expand Up @@ -154,4 +156,69 @@ describe('translateNode', () => {
],
});
});

it('should translate slugs', async () => {
fs.readdir = jest.fn().mockReturnValue(['imprint.en.md', 'imprint.fr.md']);

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

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

const currentOptions: PluginOptions = {
defaultLocale: `en-US`,
siteUrl: '',
locales: [
{
locale: `en-US`,
prefix: `en`,
slugs: {},
messages: {},
},
{
locale: `de-CH`,
prefix: `de`,
slugs: {
'/imprint': '/impressum',
},
messages: {},
},
{
locale: `fr-FR`,
prefix: `fr`,
slugs: {
'/imprint': '/imprimer',
},
messages: {},
},
],
plugins: [],
};

await translateNode(args, currentOptions);

expect(createNodeField).toHaveBeenNthCalledWith(1, { node, name: 'locale', value: 'de-CH' });
expect(createNodeField).toHaveBeenNthCalledWith(2, { node, name: 'filename', value: 'imprint' });
expect(createNodeField).toHaveBeenNthCalledWith(3, { node, name: 'kind', value: 'pages' });
expect(createNodeField).toHaveBeenNthCalledWith(4, { node, name: 'slug', value: 'imprint' });
expect(createNodeField).toHaveBeenNthCalledWith(5, { node, name: 'path', value: '/de/pages/impressum' });
expect(createNodeField).toHaveBeenNthCalledWith(6, { node, name: 'pathPrefix', value: 'de' });
expect(createNodeField).toHaveBeenNthCalledWith(7, {
node,
name: 'translations',
value: [
{ locale: 'en-US', path: '/pages/imprint' },
{ locale: 'fr-FR', path: '/fr/pages/imprimer' },
],
});
});
});
15 changes: 11 additions & 4 deletions src/onCreateNode/translateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ const translatePath = (filename: string, relativeDirectory: string, locale: stri
const localeOption = options.locales.find((l) => l.locale === locale);
const currentPath = path.join('/', relativeDirectory, slug);

// add locale prefix to path
let filepath = addLocalePrefix(currentPath, locale, localeOption?.prefix || '', options.defaultLocale);
// remove segments which are on the path blacklist

// remove path 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}`);

// replace path segments with slugs
if (localeOption) {
const keys = Object.keys(localeOption.slugs);
if (keys.length > 0) {
const exp = new RegExp(keys.join('|'), 'g');
filepath = filepath.replace(exp, (match) => localeOption.slugs[match]);
}
}

return { slug, kind: relativeDirectory, filepath };
Expand Down

0 comments on commit 355c0d7

Please sign in to comment.