Skip to content

Commit

Permalink
fix: allow absolute paths for collection media folder (#3160)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Jan 28, 2020
1 parent 32854de commit a215cfb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
59 changes: 47 additions & 12 deletions packages/netlify-cms-core/src/reducers/__tests__/entries.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('entries', () => {
).toEqual('posts/title');
});

it('should resolve relative media folder', () => {
it('should resolve collection relative media folder', () => {
expect(
selectMediaFolder(
Map({ media_folder: 'static/media' }),
Expand All @@ -108,7 +108,21 @@ describe('entries', () => {
).toEqual('posts/');
});

it('should resolve media folder template', () => {
it('should return collection absolute media folder as is', () => {
expect(
selectMediaFolder(
Map({ media_folder: '/static/Images' }),
Map({
name: 'getting-started',
folder: 'src/docs/getting-started',
media_folder: '/static/images/docs/getting-started',
}),
Map({ path: 'src/docs/getting-started/with-github.md' }),
),
).toEqual('/static/images/docs/getting-started');
});

it('should compile relative media folder template', () => {
const slugConfig = {
encoding: 'unicode',
clean_accents: false,
Expand All @@ -134,6 +148,33 @@ describe('entries', () => {
),
).toEqual('static/media/hosting-and-deployment/deployment-with-nanobox');
});

it('should compile absolute media folder template', () => {
const slugConfig = {
encoding: 'unicode',
clean_accents: false,
sanitize_replacement: '-',
};

const entry = fromJS({
path: 'src/docs/extending/overview.md',
data: { title: 'Overview' },
});
const collection = fromJS({
name: 'extending',
folder: 'src/docs/extending',
media_folder: '{{media_folder}}/docs/extending',
fields: [{ name: 'title', widget: 'string' }],
});

expect(
selectMediaFolder(
fromJS({ media_folder: '/static/images', slug: slugConfig }),
collection,
entry,
),
).toEqual('/static/images/docs/extending');
});
});

describe('selectMediaFilePath', () => {
Expand All @@ -143,13 +184,7 @@ describe('entries', () => {
);
});

it('should resolve path from global media folder when absolute path', () => {
expect(
selectMediaFilePath(Map({ media_folder: 'static/media' }), null, null, '/media/image.png'),
).toBe('static/media/image.png');
});

it('should resolve path from global media folder when relative path for collection with no media folder', () => {
it('should resolve path from global media folder for collection with no media folder', () => {
expect(
selectMediaFilePath(
Map({ media_folder: 'static/media' }),
Expand All @@ -160,7 +195,7 @@ describe('entries', () => {
).toBe('static/media/image.png');
});

it('should resolve path from collection media folder when relative path for collection with media folder', () => {
it('should resolve path from collection media folder for collection with media folder', () => {
expect(
selectMediaFilePath(
Map({ media_folder: 'static/media' }),
Expand Down Expand Up @@ -196,7 +231,7 @@ describe('entries', () => {
).toBe('/media/image.png');
});

it('should resolve path from collection media folder for collection with public folder', () => {
it('should resolve path from collection public folder for collection with public folder', () => {
expect(
selectMediaFilePublicPath(
Map({ public_folder: '/media' }),
Expand All @@ -216,7 +251,7 @@ describe('entries', () => {
).toBe('../../static/media/image.png');
});

it('should resolve public folder template', () => {
it('should compile public folder template', () => {
const slugConfig = {
encoding: 'unicode',
clean_accents: false,
Expand Down
12 changes: 5 additions & 7 deletions packages/netlify-cms-core/src/reducers/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ export const selectMediaFolder = (
'media_folder',
config.get('slug'),
);
// return absolute paths as is
if (folder.startsWith('/')) {
return folder;
}
mediaFolder = join(entryDir, folder as string);
} else {
mediaFolder = join(collection.get('folder') as string, DRAFT_MEDIA_FILES);
Expand All @@ -176,13 +180,7 @@ export const selectMediaFilePath = (
return mediaPath;
}

let mediaFolder;
if (mediaPath.startsWith('/')) {
// absolute media paths are not bound to a collection
mediaFolder = selectMediaFolder(config, null, entryMap);
} else {
mediaFolder = selectMediaFolder(config, collection, entryMap);
}
const mediaFolder = selectMediaFolder(config, collection, entryMap);

return join(mediaFolder, basename(mediaPath));
};
Expand Down

0 comments on commit a215cfb

Please sign in to comment.