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

fix: absolute media urls leading slash being trimmed #981

Merged
merged 3 commits into from
Nov 9, 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
2 changes: 1 addition & 1 deletion cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import setupNodeEvents from "./cypress/plugins";
export default defineConfig({
projectId: "wvw3x3",
retries: {
runMode: 0,
runMode: 2,
openMode: 0,
},
chromeWebSecurity: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/dev-test/backends/bitbucket/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ backend:
branch: main
repo: staticcms/static-cms-bitbucket
site_url: 'https://example.com'
media_folder: assets/uploads
media_folder: /assets/uploads
media_library:
folder_support: true
locale: en
Expand Down
2 changes: 1 addition & 1 deletion packages/core/dev-test/backends/git-gateway/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ backend:
name: git-gateway
branch: main
site_url: 'https://example.com'
media_folder: assets/uploads
media_folder: /assets/uploads
media_library:
folder_support: true
locale: en
Expand Down
2 changes: 1 addition & 1 deletion packages/core/dev-test/backends/github/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ backend:
branch: main
repo: staticjscms/static-cms-github
site_url: 'https://example.com'
media_folder: assets/uploads
media_folder: /assets/uploads
media_library:
folder_support: true
locale: en
Expand Down
2 changes: 1 addition & 1 deletion packages/core/dev-test/backends/gitlab/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ backend:
auth_type: pkce
app_id: 589ccb30ce31da8ba08e0b1e10b681a10be108e144508c2712573c6e4d60707e
site_url: 'https://example.com'
media_folder: assets/uploads
media_folder: /assets/uploads
media_library:
folder_support: true
locale: en
Expand Down
21 changes: 0 additions & 21 deletions packages/core/dev-test/backends/gitlab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,6 @@ const PostDateFieldPreview = ({ value }) => {
);
};

const PostDraftFieldPreview = ({ value }) => {
return h(
'div',
{
style: {
backgroundColor: value === true ? 'rgb(37 99 235)' : 'rgb(22 163 74)',
color: 'white',
border: 'none',
padding: '2px 6px',
textAlign: 'center',
textDecoration: 'none',
display: 'inline-block',
cursor: 'pointer',
borderRadius: '4px',
fontSize: '14px',
},
},
value === true ? 'Draft' : 'Published',
);
};

const GeneralPreview = ({ widgetsFor, entry, collection }) => {
const title = entry.data.site_title;
const posts = entry.data.posts;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/dev-test/backends/test/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
backend:
name: test-repo
site_url: 'https://example.com'
media_folder: assets/uploads
media_folder: /assets/uploads
media_library:
folder_support: true
locale: en
Expand Down
2 changes: 1 addition & 1 deletion packages/core/dev-test/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
backend:
name: test-repo
site_url: 'https://example.com'
media_folder: assets/uploads
media_folder: /assets/uploads
media_library:
folder_support: true
locale: en
Expand Down
23 changes: 0 additions & 23 deletions packages/core/dev-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,6 @@ const PostDateFieldPreview = ({ value }) => {
);
};

const PostDraftFieldPreview = ({ value }) => {
return h(
'div',
{
style: {
backgroundColor: value === true ? 'rgb(37 99 235)' : 'rgb(22 163 74)',
color: 'white',
border: 'none',
padding: '2px 6px',
textAlign: 'center',
textDecoration: 'none',
display: 'inline-block',
cursor: 'pointer',
borderRadius: '4px',
fontSize: '14px',
lineHeight: '16px',
height: '20px',
},
},
value === true ? 'Draft' : 'Published',
);
};

const GeneralPreview = ({ widgetsFor, entry, collection }) => {
const title = entry.data.site_title;
const posts = entry.data.posts;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/common/card/CardContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface CardContentProps {
}

const CardContent: FC<CardContentProps> = ({ children }) => {
return <p className={cardClasses.content}>{children}</p>;
return <div className={cardClasses.content}>{children}</div>;
};

export default CardContent;
10 changes: 5 additions & 5 deletions packages/core/src/lib/util/__tests__/media.util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('media.util', () => {
.fields[3] as FileOrImageField;

expect(selectMediaFolder(mockConfig, mockCollection, mockBaseEntry, mockImageField)).toBe(
'path/to/some/other/media/folder',
'/path/to/some/other/media/folder',
);
});

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

expect(
selectMediaFolder(mockConfig, mockCollection, mockBaseEntry, mockImageField),
).toBe('path/to/some/other/media/i-am-a-title');
).toBe('/path/to/some/other/media/i-am-a-title');
});

it('should substitute slug', () => {
Expand Down Expand Up @@ -309,7 +309,7 @@ describe('media.util', () => {
});

expect(selectMediaFolder(mockConfig, mockCollection, mockEntry, mockImageField)).toBe(
'path/to/some/other/media/i-am-a-title-fish',
'/path/to/some/other/media/i-am-a-title-fish',
);
});

Expand Down Expand Up @@ -345,7 +345,7 @@ describe('media.util', () => {
});

expect(selectMediaFolder(mockConfig, mockCollection, mockEntry, mockImageField)).toBe(
'path/to/media/folder/i-am-a-title-fish',
'/path/to/media/folder/i-am-a-title-fish',
);
});

Expand Down Expand Up @@ -383,7 +383,7 @@ describe('media.util', () => {
});

expect(selectMediaFolder(mockConfig, mockCollection, mockEntry, mockImageField)).toBe(
'path/to/some/other/media',
'/path/to/some/other/media/',
);
});
});
Expand Down
16 changes: 6 additions & 10 deletions packages/core/src/lib/util/media.util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import trim from 'lodash/trim';
import { dirname } from 'path';

import { basename, isAbsolutePath } from '.';
Expand Down Expand Up @@ -246,7 +245,7 @@
} else if (hasCustomFolder('media_folder', collection, entryMap?.slug, field)) {
const folder = evaluateFolder('media_folder', config, collection!, entryMap, field);
if (folder.startsWith('/')) {
mediaFolder = folder.replace(/^[/]*/g, '');
mediaFolder = folder;
} else {
const entryPath = entryMap?.path;
mediaFolder = entryPath
Expand All @@ -259,7 +258,7 @@
}
}

return trim(mediaFolder, '/');
return mediaFolder;
}

export function selectMediaFilePublicPath<EF extends BaseField>(
Expand Down Expand Up @@ -297,7 +296,7 @@
const mediaFolder = customMediaFolder
? evaluateFolder('media_folder', config, collection!, entryMap, field)
: config['media_folder'];
selectedPublicFolder = trim(currentFolder, '/').replace(trim(mediaFolder!, '/'), publicFolder);
selectedPublicFolder = currentFolder.replace(mediaFolder!, publicFolder);

Check warning on line 299 in packages/core/src/lib/util/media.util.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/util/media.util.ts#L299

Added line #L299 was not covered by tests
}

const finalPublicPath = joinUrlPath(selectedPublicFolder, basename(mediaPath));
Expand All @@ -322,17 +321,14 @@

let mediaFolder = selectMediaFolder(config, collection, entryMap, field, currentFolder);
if (!currentFolder) {
let publicFolder = trim(config['public_folder'] ?? mediaFolder, '/');
let mediaPathDir = trim(dirname(mediaPath), '/');
let publicFolder = config['public_folder'] ?? mediaFolder;
let mediaPathDir = dirname(mediaPath);

Check warning on line 325 in packages/core/src/lib/util/media.util.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/util/media.util.ts#L325

Added line #L325 was not covered by tests
if (mediaPathDir === '.') {
mediaPathDir = '';
}

if (hasCustomFolder('public_folder', collection, entryMap?.slug, field)) {
publicFolder = trim(
evaluateFolder('public_folder', config, collection!, entryMap, field),
'/',
);
publicFolder = evaluateFolder('public_folder', config, collection!, entryMap, field);

Check warning on line 331 in packages/core/src/lib/util/media.util.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/util/media.util.ts#L331

Added line #L331 was not covered by tests
}

if (mediaPathDir.includes(publicFolder) && mediaPathDir != mediaFolder) {
Expand Down
2 changes: 1 addition & 1 deletion packages/demo/public/editorial/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
backend:
name: test-repo
site_url: 'https://example.com'
media_folder: assets/uploads
media_folder: /assets/uploads
media_library:
folder_support: true
locale: en
Expand Down
2 changes: 1 addition & 1 deletion packages/demo/public/simple/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
backend:
name: test-repo
site_url: 'https://example.com'
media_folder: assets/uploads
media_folder: /assets/uploads
media_library:
folder_support: true
locale: en
Expand Down
23 changes: 0 additions & 23 deletions packages/demo/src/cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,6 @@ const PostDateFieldPreview = ({ value }) => {
);
};

const PostDraftFieldPreview = ({ value }) => {
return h(
'div',
{
style: {
backgroundColor: value === true ? 'rgb(37 99 235)' : 'rgb(22 163 74)',
color: 'white',
border: 'none',
padding: '2px 6px',
textAlign: 'center',
textDecoration: 'none',
display: 'inline-block',
cursor: 'pointer',
borderRadius: '4px',
fontSize: '14px',
lineHeight: '16px',
height: '20px',
},
},
value === true ? 'Draft' : 'Published',
);
};

const GeneralPreview = ({ widgetsFor, entry, collection }) => {
const title = entry.data.site_title;
const posts = entry.data.posts;
Expand Down