Skip to content

Commit b088a8f

Browse files
authored
fix: asset name parsing in static converter (#501)
1 parent b68257e commit b088a8f

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/editors/sharedComponents/TinyMceWidget/hooks.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { StrictDict } from '../../utils';
1212
import pluginConfig from './pluginConfig';
1313
import * as module from './hooks';
1414
import tinyMCE from '../../data/constants/tinyMCE';
15-
import { getRelativeUrl, getStaticUrl } from './utils';
15+
import { getRelativeUrl, getStaticUrl, parseAssetName } from './utils';
1616

1717
export const state = StrictDict({
1818
// eslint-disable-next-line react-hooks/rules-of-hooks
@@ -89,9 +89,9 @@ export const replaceStaticWithAsset = ({
8989
const isStatic = src.startsWith('/static/');
9090
const assetSrc = src.substring(0, src.indexOf('"'));
9191
const staticName = assetSrc.substring(8);
92-
const assetName = assetSrc.replace(/\/assets\/.+[^/]\//g, '');
92+
const assetName = parseAssetName(src);
9393
const displayName = isStatic ? staticName : assetName;
94-
const isCorrectAssetFormat = assetSrc.match(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+[@]/g)?.length >= 1;
94+
const isCorrectAssetFormat = assetSrc.startsWith('/asset') && assetSrc.match(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+[@]/g)?.length >= 1;
9595
// assets in expandable text areas so not support relative urls so all assets must have the lms
9696
// endpoint prepended to the relative url
9797
if (editorType === 'expandable') {
@@ -407,15 +407,7 @@ export const setAssetToStaticUrl = ({ editorValue, lmsEndpointUrl }) => {
407407

408408
const assetSrcs = typeof content === 'string' ? content.split(/(src="|src="|href="|href=&quot)/g) : [];
409409
assetSrcs.filter(src => src.startsWith('/asset')).forEach(src => {
410-
let nameFromEditorSrc;
411-
if (src.match(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+\/\w/)?.length >= 1) {
412-
const assetBlockName = src.substring(0, src.search(/("|")/));
413-
const dividedSrc = assetBlockName.split(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+\//);
414-
[, nameFromEditorSrc] = dividedSrc;
415-
} else {
416-
const assetBlockName = src.substring(src.indexOf('@') + 1, src.search(/("|")/));
417-
nameFromEditorSrc = assetBlockName.substring(assetBlockName.indexOf('@') + 1);
418-
}
410+
const nameFromEditorSrc = parseAssetName(src);
419411
const portableUrl = getStaticUrl({ displayName: nameFromEditorSrc });
420412
const currentSrc = src.substring(0, src.search(/("|")/));
421413
const updatedContent = content.replace(currentSrc, portableUrl);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,17 @@ describe('TinyMceEditor hooks', () => {
183183
});
184184

185185
describe('replaceStaticWithAsset', () => {
186-
const initialContent = `<img src="/static/soMEImagEURl1.jpeg"/><a href="/assets/v1/${baseAssetUrl}/test.pdf">test</a><img src="/${baseAssetUrl}@correct.png" />`;
186+
const initialContent = `<img src="/static/soMEImagEURl1.jpeg"/><a href="/assets/v1/${baseAssetUrl}/test.pdf">test</a><img src="/${baseAssetUrl}@correct.png" /><img src="/${baseAssetUrl}/correct.png" />`;
187187
const learningContextId = 'course-v1:org+test+run';
188188
const lmsEndpointUrl = 'sOmEvaLue.cOm';
189189
it('returns updated src for text editor to update content', () => {
190-
const expected = `<img src="/${baseAssetUrl}@soMEImagEURl1.jpeg"/><a href="/${baseAssetUrl}@test.pdf">test</a><img src="/${baseAssetUrl}@correct.png" />`;
190+
const expected = `<img src="/${baseAssetUrl}@soMEImagEURl1.jpeg"/><a href="/${baseAssetUrl}@test.pdf">test</a><img src="/${baseAssetUrl}@correct.png" /><img src="/${baseAssetUrl}@correct.png" />`;
191191
const actual = module.replaceStaticWithAsset({ initialContent, learningContextId });
192192
expect(actual).toEqual(expected);
193193
});
194194
it('returns updated src with absolute url for expandable editor to update content', () => {
195195
const editorType = 'expandable';
196-
const expected = `<img src="${lmsEndpointUrl}/${baseAssetUrl}@soMEImagEURl1.jpeg"/><a href="${lmsEndpointUrl}/${baseAssetUrl}@test.pdf">test</a><img src="${lmsEndpointUrl}/${baseAssetUrl}@correct.png" />`;
196+
const expected = `<img src="${lmsEndpointUrl}/${baseAssetUrl}@soMEImagEURl1.jpeg"/><a href="${lmsEndpointUrl}/${baseAssetUrl}@test.pdf">test</a><img src="${lmsEndpointUrl}/${baseAssetUrl}@correct.png" /><img src="${lmsEndpointUrl}/${baseAssetUrl}@correct.png" />`;
197197
const actual = module.replaceStaticWithAsset({
198198
initialContent,
199199
editorType,

src/editors/sharedComponents/TinyMceWidget/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,16 @@ export const getRelativeUrl = ({ courseId, displayName }) => {
1313
}
1414
return '';
1515
};
16+
17+
export const parseAssetName = (relativeUrl) => {
18+
let assetName = '';
19+
if (relativeUrl.match(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+\/\w/)?.length >= 1) {
20+
const assetBlockName = relativeUrl.substring(0, relativeUrl.search(/("|&quot;)/));
21+
const dividedSrc = assetBlockName.split(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+\//);
22+
[, assetName] = dividedSrc;
23+
} else {
24+
const assetBlockName = relativeUrl.substring(relativeUrl.indexOf('@') + 1, relativeUrl.search(/("|&quot;)/));
25+
assetName = assetBlockName.substring(assetBlockName.indexOf('@') + 1);
26+
}
27+
return assetName;
28+
};

0 commit comments

Comments
 (0)