mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-09-24 23:01:18 +08:00
fix(client): cert project help post sans editable region (#49157)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
co-authored by
Oliver Eyton-Williams
parent
033707d18c
commit
9771e0ad70
@@ -38,6 +38,31 @@ function filesToMarkdown(challengeFiles = {}) {
|
||||
}, '\n');
|
||||
}
|
||||
|
||||
export function insertEditableRegions(challengeFiles = []) {
|
||||
if (challengeFiles?.some(file => file.editableRegionBoundaries?.length > 0)) {
|
||||
const editableRegionStrings = fileExtension => {
|
||||
const startComment = fileExtension === 'html' ? '<!--' : '/*';
|
||||
const endComment = fileExtension === 'html' ? '-->' : '*/';
|
||||
return `\n${startComment} User Editable Region ${endComment}\n`;
|
||||
};
|
||||
|
||||
const filesWithEditableRegions = challengeFiles.map(file => {
|
||||
const { contents, editableRegionBoundaries, ext } = file;
|
||||
if (editableRegionBoundaries.length > 0) {
|
||||
const comment = editableRegionStrings(ext);
|
||||
const [start, end] = editableRegionBoundaries;
|
||||
const lines = contents.split('\n');
|
||||
lines.splice(start, 0, comment);
|
||||
lines.splice(end, 0, comment);
|
||||
return { ...file, contents: lines.join('\n') };
|
||||
}
|
||||
return file;
|
||||
});
|
||||
return filesWithEditableRegions;
|
||||
}
|
||||
return challengeFiles;
|
||||
}
|
||||
|
||||
function createQuestionEpic(action$, state$, { window }) {
|
||||
return action$.pipe(
|
||||
ofType(actionTypes.createQuestion),
|
||||
@@ -50,29 +75,9 @@ function createQuestionEpic(action$, state$, { window }) {
|
||||
block,
|
||||
helpCategory
|
||||
} = challengeMetaSelector(state);
|
||||
if (
|
||||
challengeFiles?.some(file => file.editableRegionBoundaries.length > 0)
|
||||
) {
|
||||
const editableRegionStrings = fileExtension => {
|
||||
const startComment = fileExtension === 'html' ? '<!--' : '/*';
|
||||
const endComment = fileExtension === 'html' ? '-->' : '*/';
|
||||
return `\n${startComment} User Editable Region ${endComment}\n`;
|
||||
};
|
||||
|
||||
const filesWithEditableRegions = challengeFiles.map(file => {
|
||||
const { contents, editableRegionBoundaries, ext } = file;
|
||||
if (editableRegionBoundaries.length > 0) {
|
||||
const comment = editableRegionStrings(ext);
|
||||
const [start, end] = editableRegionBoundaries;
|
||||
const lines = contents.split('\n');
|
||||
lines.splice(start, 0, comment);
|
||||
lines.splice(end, 0, comment);
|
||||
return { ...file, contents: lines.join('\n') };
|
||||
}
|
||||
return file;
|
||||
});
|
||||
challengeFiles = filesWithEditableRegions;
|
||||
}
|
||||
challengeFiles = insertEditableRegions(challengeFiles);
|
||||
|
||||
const {
|
||||
navigator: { userAgent },
|
||||
location: { pathname, origin }
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { transformEditorLink } from '../utils';
|
||||
import { insertEditableRegions } from './create-question-epic';
|
||||
|
||||
describe('create-question-epic', () => {
|
||||
describe('transformEditorLink', () => {
|
||||
@@ -27,4 +28,65 @@ describe('create-question-epic', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('insertEditableRegions', () => {
|
||||
const multiCertChallengeFiles = [
|
||||
{
|
||||
contents: '<h1>Hello World</h1>',
|
||||
ext: 'html',
|
||||
fileKey: 'indexhtml',
|
||||
history: ['index.html'],
|
||||
head: '',
|
||||
id: '',
|
||||
name: 'index',
|
||||
path: 'index.html',
|
||||
seed: '',
|
||||
tail: ''
|
||||
},
|
||||
{
|
||||
contents: '',
|
||||
ext: 'css',
|
||||
fileKey: 'stylescss',
|
||||
history: ['styles.css'],
|
||||
head: '',
|
||||
id: '',
|
||||
name: 'styles',
|
||||
path: 'styles.css',
|
||||
seed: '',
|
||||
tail: ''
|
||||
}
|
||||
];
|
||||
const multiPracticeChallengeFiles = [
|
||||
{
|
||||
contents:
|
||||
'<html>\n <body>\n <h1>Hello World</h1>\n </body>\n</html>',
|
||||
editableRegionBoundaries: [2, 4],
|
||||
ext: 'html',
|
||||
fileKey: 'indexhtml',
|
||||
history: ['index.html'],
|
||||
head: '',
|
||||
id: '',
|
||||
name: 'index',
|
||||
path: 'index.html',
|
||||
seed: '<html>\n <body>\n <h1>Hello World</h1>\n </body>\n</html>',
|
||||
tail: ''
|
||||
}
|
||||
];
|
||||
it('should not insert editable regions for certification projects', () => {
|
||||
const challengeFiles = insertEditableRegions(multiCertChallengeFiles);
|
||||
challengeFiles.forEach(({ contents }) => {
|
||||
expect(contents).not.toContain('User Editable Region');
|
||||
});
|
||||
});
|
||||
it('should insert editable regions for multifile practice projects', () => {
|
||||
const challengeFiles = insertEditableRegions(multiPracticeChallengeFiles);
|
||||
challengeFiles.forEach(({ contents }) => {
|
||||
expect(contents).toContain('User Editable Region');
|
||||
});
|
||||
});
|
||||
it('should not throw if editableRegionBoundaries is undefined', () => {
|
||||
expect(() =>
|
||||
insertEditableRegions(multiCertChallengeFiles)
|
||||
).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user