Skip to content

Commit beb4813

Browse files
authored
fix: setAssetToStaticUrl regex matcher (#497)
1 parent ce87037 commit beb4813

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/editors/sharedComponents/TinyMceWidget/hooks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,9 @@ export const setAssetToStaticUrl = ({ editorValue, lmsEndpointUrl }) => {
408408
const assetSrcs = typeof content === 'string' ? content.split(/(src="|src="|href="|href=&quot)/g) : [];
409409
assetSrcs.filter(src => src.startsWith('/asset')).forEach(src => {
410410
let nameFromEditorSrc;
411-
if (src.match(/\/assets\/.+\/asset-v1:\S+[+]\S+[@]\S+[+]\S+\//)?.length >= 1) {
411+
if (src.match(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+\/\w/)?.length >= 1) {
412412
const assetBlockName = src.substring(0, src.search(/("|")/));
413-
const dividedSrc = assetBlockName.split(/\/assets\/.+\/asset-v1:\S+[+]\S+[@]\S+[+]\S+\//);
413+
const dividedSrc = assetBlockName.split(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+\//);
414414
[, nameFromEditorSrc] = dividedSrc;
415415
} else {
416416
const assetBlockName = src.substring(src.indexOf('@') + 1, src.search(/("|")/));

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const mockEditorContentHtml = `
6363
</img>
6464
</p>
6565
`;
66+
const baseAssetUrl = 'asset-v1:org+test+run+type@asset+block';
6667

6768
const mockImagesRef = { current: [mockImage] };
6869

@@ -182,17 +183,17 @@ describe('TinyMceEditor hooks', () => {
182183
});
183184

184185
describe('replaceStaticWithAsset', () => {
185-
const initialContent = '<img src="/static/soMEImagEURl1.jpeg"/><a href="/assets/v1/some-key/test.pdf">test</a><img src="/asset-v1:org+test+run+type@asset+block@correct.png" />';
186+
const initialContent = `<img src="/static/soMEImagEURl1.jpeg"/><a href="/assets/v1/${baseAssetUrl}/test.pdf">test</a><img src="/${baseAssetUrl}@correct.png" />`;
186187
const learningContextId = 'course-v1:org+test+run';
187188
const lmsEndpointUrl = 'sOmEvaLue.cOm';
188189
it('returns updated src for text editor to update content', () => {
189-
const expected = '<img src="/asset-v1:org+test+run+type@asset+block@soMEImagEURl1.jpeg"/><a href="/asset-v1:org+test+run+type@asset+block@test.pdf">test</a><img src="/asset-v1:org+test+run+type@asset+block@correct.png" />';
190+
const expected = `<img src="/${baseAssetUrl}@soMEImagEURl1.jpeg"/><a href="/${baseAssetUrl}@test.pdf">test</a><img src="/${baseAssetUrl}@correct.png" />`;
190191
const actual = module.replaceStaticWithAsset({ initialContent, learningContextId });
191192
expect(actual).toEqual(expected);
192193
});
193194
it('returns updated src with absolute url for expandable editor to update content', () => {
194195
const editorType = 'expandable';
195-
const expected = `<img src="${lmsEndpointUrl}/asset-v1:org+test+run+type@asset+block@soMEImagEURl1.jpeg"/><a href="${lmsEndpointUrl}/asset-v1:org+test+run+type@asset+block@test.pdf">test</a><img src="${lmsEndpointUrl}/asset-v1:org+test+run+type@asset+block@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" />`;
196197
const actual = module.replaceStaticWithAsset({
197198
initialContent,
198199
editorType,
@@ -209,7 +210,7 @@ describe('TinyMceEditor hooks', () => {
209210
});
210211
describe('setAssetToStaticUrl', () => {
211212
it('returns content with updated img links', () => {
212-
const editorValue = '<img src="/asset@/soME_ImagE_URl1"/> <a href="/asset@soMEImagEURl">testing link</a>';
213+
const editorValue = `<img src="/${baseAssetUrl}/soME_ImagE_URl1"/> <a href="/${baseAssetUrl}@soMEImagEURl">testing link</a>`;
213214
const lmsEndpointUrl = 'sOmEvaLue.cOm';
214215
const content = module.setAssetToStaticUrl({ editorValue, lmsEndpointUrl });
215216
expect(content).toEqual('<img src="/static/soME_ImagE_URl1"/> <a href="/static/soMEImagEURl">testing link</a>');

0 commit comments

Comments
 (0)