forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-step-template.ts
102 lines (84 loc) · 2.43 KB
/
get-step-template.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* eslint-disable @typescript-eslint/no-base-to-string */
import ObjectID from 'bson-objectid';
import { insertErms } from './insert-erms';
// Builds a block
function getCodeBlock(label: string, content?: string) {
return `\`\`\`${label}
${typeof content !== 'undefined' ? content : ''}
\`\`\`\n`;
}
// Builds a section
function getSeedSection(content: string, label: string) {
return content
? `
## --${label}--
${content}`
: '';
}
type StepOptions = {
challengeId: ObjectID;
challengeSeeds: Record<string, ChallengeSeed>;
stepNum: number;
challengeType: number;
isFirstChallenge?: boolean;
};
export interface ChallengeSeed {
contents: string;
ext: string;
editableRegionBoundaries: number[];
head?: string;
tail?: string;
}
// Build the base markdown for a step
function getStepTemplate({
challengeId,
challengeSeeds,
stepNum,
challengeType,
isFirstChallenge = false
}: StepOptions): string {
const seedTexts = Object.values(challengeSeeds)
.map(({ contents, ext, editableRegionBoundaries }: ChallengeSeed) => {
let fullContents = contents;
if (editableRegionBoundaries.length >= 2) {
fullContents = insertErms(contents, editableRegionBoundaries);
}
return getCodeBlock(ext, fullContents);
})
.join('\n');
const seedHeads = Object.values(challengeSeeds)
.filter(({ head }: ChallengeSeed) => head)
.map(({ ext, head }: ChallengeSeed) => getCodeBlock(ext, head))
.join('\n');
const seedTails = Object.values(challengeSeeds)
.filter(({ tail }: ChallengeSeed) => tail)
.map(({ ext, tail }: ChallengeSeed) => getCodeBlock(ext, tail))
.join('\n');
const stepDescription = `step ${stepNum} instructions`;
const seedChallengeSection = getSeedSection(seedTexts, 'seed-contents');
const seedHeadSection = getSeedSection(seedHeads, 'before-user-code');
const seedTailSection = getSeedSection(seedTails, 'after-user-code');
const demoString = isFirstChallenge
? `
# demoType can either be 'onClick' or 'onLoad'. If the project or lab doesn't have a preview, delete the property
demoType: onClick`
: '';
return (
`---
id: ${challengeId.toString()}
title: Step ${stepNum}
challengeType: ${challengeType}
dashedName: step-${stepNum}${demoString}
---
# --description--
${stepDescription}
# --hints--
Test 1
${getCodeBlock('js')}
# --seed--` +
seedChallengeSection +
seedHeadSection +
seedTailSection
);
}
export { getStepTemplate };