Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

fix: only add public folder if path matches media folder #991

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ backend:
name: test-repo
site_url: 'https://example.com'
media_folder: assets/uploads
public_folder: /assets/uploads
media_library:
folder_support: true
locale: en
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/lib/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,9 @@ export function folderFormatter<EF extends BaseField>(
'folder' in collection ? collection.folder : '',
);

const slug = slugFormatter(collection, entry.data, slugConfig);

const date = parseDateFromEntry(entry, selectInferredField(collection, 'date')) || null;

const processSegment = getProcessSegment(slugConfig, [defaultFolder, fields?.dirname as string]);

const mediaFolder = compileStringTemplate(folderTemplate, date, slug, fields, processSegment);

return mediaFolder;
return compileStringTemplate(folderTemplate, date, entry.slug, fields, processSegment);
}
98 changes: 78 additions & 20 deletions packages/core/src/lib/util/__tests__/media.util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,50 @@ describe('media.util', () => {
},
});

it('does not transform path if it does not start with media_folder', () => {
const mockConfig = createMockConfig({
collections: [
createMockCollection<UnknownField>({
folder: 'base/folder',
slug: '{{fields.title}}-{{fields.name}}',
fields: [
{
name: 'title',
widget: 'string',
},
{
name: 'name',
widget: 'string',
},
mockBaseImageField,
],
}),
],
media_folder: '/path/to/media/folder/{{slug}}',
public_folder: '/path/to/public/folder/{{slug}}',
});

const mockCollection = mockConfig.collections[0];
const mockImageField = (mockConfig.collections[0] as FolderCollection)
.fields[3] as FileOrImageField;

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

expect(
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'/some/other/path/image.png',
mockEntry,
mockImageField,
),
).toBe('/some/other/path/image.png');
});

describe('top level', () => {
it('should default to top level config media_folder', () => {
const mockConfig = createMockConfig({
Expand Down Expand Up @@ -138,6 +182,7 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

Expand Down Expand Up @@ -174,6 +219,7 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

Expand Down Expand Up @@ -211,6 +257,7 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: '',
data: {},
newRecord: true,
});
Expand Down Expand Up @@ -305,6 +352,7 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

Expand Down Expand Up @@ -341,6 +389,7 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

Expand Down Expand Up @@ -378,6 +427,7 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: '',
data: {},
newRecord: true,
});
Expand Down Expand Up @@ -420,7 +470,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
undefined,
'image.png',
'path/to/media/folder/image.png',
undefined,
undefined,
undefined,
Expand All @@ -438,7 +488,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
undefined,
'image.png',
'path/to/media/folder/image.png',
undefined,
undefined,
undefined,
Expand All @@ -463,7 +513,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'path/to/media/folder/image.png',
mockBaseEntry,
mockImageField,
),
Expand All @@ -484,7 +534,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'path/to/media/folder/image.png',
mockBaseEntry,
mockImageField,
),
Expand Down Expand Up @@ -518,7 +568,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'path/to/collection/media/folder/image.png',
mockBaseEntry,
mockImageField,
),
Expand Down Expand Up @@ -550,7 +600,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'path/to/collection/media/folder/image.png',
mockBaseEntry,
mockImageField,
),
Expand Down Expand Up @@ -583,7 +633,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'path/to/some/other/media/i-am-a-title/image.png',
mockBaseEntry,
mockImageField,
),
Expand Down Expand Up @@ -619,14 +669,15 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

expect(
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'path/to/some/other/media/i-am-a-title-fish/image.png',
mockEntry,
mockImageField,
),
Expand Down Expand Up @@ -661,14 +712,15 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

expect(
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'/path/to/media/folder/i-am-a-title-fish/image.png',
mockEntry,
mockImageField,
),
Expand Down Expand Up @@ -704,14 +756,15 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

expect(
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'path/to/media/folder/i-am-a-title-fish/image.png',
mockEntry,
mockImageField,
),
Expand Down Expand Up @@ -748,6 +801,7 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: '',
data: {},
newRecord: true,
});
Expand All @@ -756,7 +810,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'path/to/collection/media/folder/image.png',
mockEntry,
mockImageField,
),
Expand Down Expand Up @@ -792,7 +846,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'path/to/collection/media/folder/image.png',
mockBaseEntry,
mockImageField,
),
Expand Down Expand Up @@ -824,7 +878,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'/path/to/collection/media/folder/image.png',
mockBaseEntry,
mockImageField,
),
Expand Down Expand Up @@ -857,7 +911,7 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'/path/to/some/other/media/i-am-a-title/image.png',
mockBaseEntry,
mockImageField,
),
Expand Down Expand Up @@ -893,14 +947,15 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

expect(
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'/path/to/some/other/media/i-am-a-title-fish/image.png',
mockEntry,
mockImageField,
),
Expand Down Expand Up @@ -935,14 +990,15 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

expect(
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'/path/to/media/folder/i-am-a-title-fish/image.png',
mockEntry,
mockImageField,
),
Expand Down Expand Up @@ -978,14 +1034,15 @@ describe('media.util', () => {

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
slug: 'i-am-a-title-fish',
data: { title: 'i am a title', name: 'fish' },
});

expect(
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'/path/to/media/folder/i-am-a-title-fish/image.png',
mockEntry,
mockImageField,
),
Expand Down Expand Up @@ -1021,7 +1078,8 @@ describe('media.util', () => {
.fields[3] as FileOrImageField;

const mockEntry = createMockEntry({
path: 'path/to/entry/index.md',
path: 'path/to/entry/DRAFT_MEDIA_FILES/index.md',
slug: '',
data: {},
newRecord: true,
});
Expand All @@ -1030,11 +1088,11 @@ describe('media.util', () => {
selectMediaFilePublicPath(
mockConfig,
mockCollection,
'image.png',
'/path/to/collection/media/folder/DRAFT_MEDIA_FILES/image.png',
mockEntry,
mockImageField,
),
).toBe('/path/to/collection/public/folder/image.png');
).toBe('/path/to/collection/public/folder/DRAFT_MEDIA_FILES/image.png');
});
});
});
Expand Down
Loading