Skip to content

Feature/hints #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ This level has two steps...

The first step with id L1S1. The Step id should start with the level id.

#### HINTS

- The first hint available when a user requests a hint
- The second hint that will show
- The third and final hint, as it is last in order

### L1S2 The second step

The second step...
Expand Down
10 changes: 10 additions & 0 deletions src/schema/tutorial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ export default {
],
},
},
hints: {
type: "array",
description:
"An optional array of hints to provide helpful feedback to users",
items: {
type: "string",
description: "A hint to provide to the user",
examples: ["Have you tried doing X?"],
},
},
required: ["content", "setup", "solution"],
},
},
Expand Down
6 changes: 6 additions & 0 deletions src/templates/TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ Short description of the step's purpose. Should be short and fit in one line
### L1S2 Another step

Step's short description.

#### Hints

- If a hint exists, the user can request a hint
- Hints will show up in the order they are written
- When all hints are added, the hint option will become disabled
84 changes: 54 additions & 30 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ export function parseMdContent(md: string): TutorialFrame | never {
mdContent.summary.description = summaryMatch.groups.tutorialDescription.trim();
}

let current = { level: "0", step: "0" };
// Identify each part of the content
parts.forEach((section: string) => {
// match level
const levelRegex = /^(##\s(?<levelId>L\d+)\s(?<levelTitle>.*)[\n\r]*(>\s*(?<levelSummary>.*))?[\n\r]+(?<levelContent>[^]*))/;
const levelRegex = /^(#{2}\s(?<levelId>L\d+)\s(?<levelTitle>.*)[\n\r]*(>\s*(?<levelSummary>.*))?[\n\r]+(?<levelContent>[^]*))/;
const levelMatch: RegExpMatchArray | null = section.match(levelRegex);
if (levelMatch && levelMatch.groups) {
const {
Expand All @@ -71,16 +72,33 @@ export function parseMdContent(md: string): TutorialFrame | never {
: truncate(levelContent.trim(), { length: 80, omission: "..." }),
content: levelContent.trim(),
};
current = { level: levelId, step: "0" };
} else {
// match step
const stepRegex = /^(###\s(?<stepId>(?<levelId>L\d+)S\d+)\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/;
const stepRegex = /^(#{3}\s(?<stepId>(?<levelId>L\d+)S\d+)\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/;
const stepMatch: RegExpMatchArray | null = section.match(stepRegex);
if (stepMatch && stepMatch.groups) {
const { stepId, stepContent } = stepMatch.groups;

mdContent.steps[stepId] = {
id: stepId,
content: stepContent.trim(),
};
current = { ...current, step: stepId };
} else {
// parse hints from stepContent
const hintDetectRegex = /^(#{4}\sHINTS[\n\r]+(\*\s(?<hintContent>[^]*))[\n\r]+)+/;
const hintMatch = section.match(hintDetectRegex);
if (!!hintMatch) {
const hintItemRegex = /[\n\r]+\*\s/;
const hints = section
.split(hintItemRegex)
.slice(1) // remove #### HINTS
.map((h) => h.trim());
if (hints.length) {
mdContent.steps[current.step].hints = hints;
}
}
}
}
});
Expand Down Expand Up @@ -135,39 +153,45 @@ export function parse(params: ParseParams): any {
}

// add level step commits
level.steps = (level.steps || []).map(
(step: T.Step, stepIndex: number) => {
const stepKey = `${levelSetupKey}S${stepIndex + 1}`;
const stepSetupKey = `${stepKey}Q`;
if (params.commits[stepSetupKey]) {
if (!step.setup) {
step.setup = {
commits: [],
};
try {
level.steps = (level.steps || []).map(
(step: T.Step, stepIndex: number) => {
const stepKey = `${levelSetupKey}S${stepIndex + 1}`;
const stepSetupKey = `${stepKey}Q`;
if (params.commits[stepSetupKey]) {
if (!step.setup) {
step.setup = {
commits: [],
};
}
step.setup.commits = params.commits[stepSetupKey];
}
step.setup.commits = params.commits[stepSetupKey];
}

const stepSolutionKey = `${stepKey}A`;
if (params.commits[stepSolutionKey]) {
if (!step.solution) {
step.solution = {
commits: [],
};
const stepSolutionKey = `${stepKey}A`;
if (params.commits[stepSolutionKey]) {
if (!step.solution) {
step.solution = {
commits: [],
};
}
step.solution.commits = params.commits[stepSolutionKey];
}
step.solution.commits = params.commits[stepSolutionKey];
}

// add markdown
const stepMarkdown: Partial<T.Step> = mdContent.steps[step.id];
if (stepMarkdown) {
step = { ...step, ...stepMarkdown };
}
// add markdown
const stepMarkdown: Partial<T.Step> = mdContent.steps[step.id];
if (stepMarkdown) {
step = { ...step, ...stepMarkdown };
}

step.id = `${stepKey}`;
return step;
}
);
step.id = `${stepKey}`;
return step;
}
);
} catch (error) {
console.log(JSON.stringify(level.steps));
console.error("Error parsing level steps");
console.error(error.message);
}

return level;
})
Expand Down
Loading