fix: i18n regex logic (#5136)

This commit is contained in:
Art Tosborvorn 2021-03-21 22:29:25 +07:00 committed by GitHub
parent 826d82fd80
commit b0c0ea60c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 12 deletions

View File

@ -130,23 +130,40 @@ describe('i18n', () => {
});
describe('getFilePath', () => {
const args = ['md', 'src/content/index.md', 'index', 'de'];
it('should return directory path based on locale when structure is I18N_STRUCTURE.MULTIPLE_FOLDERS', () => {
expect(i18n.getFilePath(i18n.I18N_STRUCTURE.MULTIPLE_FOLDERS, ...args)).toEqual(
'src/content/de/index.md',
);
expect(
i18n.getFilePath(
i18n.I18N_STRUCTURE.MULTIPLE_FOLDERS,
'md',
'src/content/index.md',
'index',
'de',
),
).toEqual('src/content/de/index.md');
});
it('should return file path based on locale when structure is I18N_STRUCTURE.MULTIPLE_FILES', () => {
expect(i18n.getFilePath(i18n.I18N_STRUCTURE.MULTIPLE_FILES, ...args)).toEqual(
'src/content/index.de.md',
);
expect(
i18n.getFilePath(
i18n.I18N_STRUCTURE.MULTIPLE_FILES,
'md',
'src/content/file-with-md-in-the-name.md',
'file-with-md-in-the-name',
'de',
),
).toEqual('src/content/file-with-md-in-the-name.de.md');
});
it('should not modify path when structure is I18N_STRUCTURE.SINGLE_FILE', () => {
expect(i18n.getFilePath(i18n.I18N_STRUCTURE.SINGLE_FILE, ...args)).toEqual(
'src/content/index.md',
);
expect(
i18n.getFilePath(
i18n.I18N_STRUCTURE.SINGLE_FILE,
'md',
'src/content/index.md',
'index',
'de',
),
).toEqual('src/content/index.md');
});
});

View File

@ -1,5 +1,5 @@
import { Map, List } from 'immutable';
import { set, trimEnd, groupBy } from 'lodash';
import { set, trimEnd, groupBy, escapeRegExp } from 'lodash';
import { Collection, Entry, EntryDraft, EntryField, EntryMap } from '../types/redux';
import { selectEntrySlug } from '../reducers/collections';
import { EntryValue } from '../valueObjects/Entry';
@ -79,7 +79,7 @@ export function getFilePath(
case I18N_STRUCTURE.MULTIPLE_FOLDERS:
return path.replace(`/${slug}`, `/${locale}/${slug}`);
case I18N_STRUCTURE.MULTIPLE_FILES:
return path.replace(extension, `${locale}.${extension}`);
return path.replace(new RegExp(`${escapeRegExp(extension)}$`), `${locale}.${extension}`);
case I18N_STRUCTURE.SINGLE_FILE:
default:
return path;