Skip to content

Commit a15ea7c

Browse files
fix: only convert assets static file path
1 parent ef36156 commit a15ea7c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/editors/sharedComponents/TinyMceWidget/hooks.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ describe('TinyMceEditor hooks', () => {
213213
const actual = module.replaceStaticWithAsset({ initialContent: content, learningContextId });
214214
expect(actual).toBeFalsy();
215215
});
216+
it('does not convert static URLs with subdirectories but converts direct static files', () => {
217+
const contentWithSubdirectory = '<img src="/static/images/placeholder-faculty.png"/><img src="/static/example.jpg"/>';
218+
const expected = `<img src="/static/images/placeholder-faculty.png"/><img src="/${baseAssetUrl}@example.jpg"/>`;
219+
const actual = module.replaceStaticWithAsset({
220+
initialContent: contentWithSubdirectory,
221+
learningContextId,
222+
});
223+
expect(actual).toEqual(expected);
224+
});
216225
});
217226
describe('setAssetToStaticUrl', () => {
218227
it('returns content with updated img links', () => {

src/editors/sharedComponents/TinyMceWidget/hooks.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ export const replaceStaticWithAsset = ({
121121
let staticFullUrl;
122122
const isStatic = src.startsWith('/static/');
123123
const assetSrc = src.substring(0, src.indexOf('"'));
124+
125+
// Check if this is a direct /static/filename.ext pattern (should be converted)
126+
// vs /static/images/filename.ext pattern (should NOT be converted)
127+
const isDirectStaticFile = isStatic && !assetSrc.substring(8).includes('images/');
128+
124129
const staticName = assetSrc.substring(8);
125130
const assetName = parseAssetName(src);
126131
const displayName = isStatic ? staticName : assetName;
@@ -133,16 +138,16 @@ export const replaceStaticWithAsset = ({
133138
// set the base URL to an endpoint serving the draft version of an asset by
134139
// its path.
135140
/* istanbul ignore next */
136-
if (isStatic) {
141+
if (isStatic && isDirectStaticFile) {
137142
staticFullUrl = assetSrc.substring(1);
138143
}
139144
} else if (editorType === 'expandable') {
140145
if (isCorrectAssetFormat) {
141146
staticFullUrl = `${lmsEndpointUrl}${assetSrc}`;
142-
} else {
147+
} else if (isDirectStaticFile) {
143148
staticFullUrl = `${lmsEndpointUrl}${getRelativeUrl({ courseId: learningContextId, displayName })}`;
144149
}
145-
} else if (!isCorrectAssetFormat) {
150+
} else if (!isCorrectAssetFormat && isDirectStaticFile) {
146151
staticFullUrl = getRelativeUrl({ courseId: learningContextId, displayName });
147152
}
148153
if (staticFullUrl) {

0 commit comments

Comments
 (0)