Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature/markdown as master #48

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 17 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
passing step tests
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jun 21, 2020
commit 4bb28b9b0f4a426608d973c164aaafac7e61e4c4
169 changes: 99 additions & 70 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ export function parseMdContent(md: string): TutorialFrame | never {
};
} else {
// match step
const stepRegex = /^(#{3}\s\(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*)/;
const stepRegex = /^(#{3}\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/;
const stepMatch: RegExpMatchArray | null = section.match(stepRegex);
if (stepMatch && stepMatch.groups) {
const { stepId, stepContent } = stepMatch.groups;
mdContent.levels[current.level].steps[current.step] = {
id: stepId,
id: `${current.level + 1}.${current.step + 1}`,
content: stepContent.trim(),
setup: {},
solution: {},
};
current = { ...current, step: current.step + 1 };
} else {
Expand Down Expand Up @@ -131,76 +129,107 @@ export function parse(params: ParseParams): any {

// merge content levels and tutorial

parsed.levels = mdContent.levels.map((level: T.Level, levelIndex: number) => {
// add level setup commits
const levelId = level.id;
if (params.commits[levelId]) {
if (!level.setup) {
level.setup = {};
parsed.levels = mdContent.levels.map(
(mdLevel: T.Level, mdLevelIndex: number) => {
// add level setup commits
let level: T.Level = { ...mdLevel };

const configLevel = params.skeleton.levels[mdLevelIndex];

if (configLevel) {
// add level step commits
const { steps, ...configLevelProps } = configLevel;
level = { ...configLevelProps, ...level };
if (steps) {
steps.forEach((step: T.Step, index: number) => {
console.log("step", step);
const mdStep = level.steps[index];
console.log("mdStep", mdStep);
step = {
...step,
...mdStep,
};

const stepKey = step.id;
console.log("stepKey", stepKey);
const stepSetupKey = `${stepKey}Q`;
if (params.commits[stepSetupKey]) {
if (!step.setup) {
step.setup = {
commits: [],
};
}
step.setup.commits = params.commits[stepSetupKey];
}

const stepSolutionKey = `${stepKey}A`;
if (params.commits[stepSolutionKey]) {
if (!step.solution) {
step.solution = {
commits: [],
};
}
step.solution.commits = params.commits[stepSolutionKey];
}
// update level step
level.steps[index] = step;
});
}
}
level.setup.commits = params.commits[levelId];
}

// get yaml for level
const configLevel = params.skeleton.levels.find(
(l: Partial<T.Level>) => l.id === levelId
);

let configSteps = {};
if (configLevel) {
const { steps, ...configLevelProps } = configLevel;
level = { ...configLevelProps, ...level };
if (steps) {
steps.forEach((s: T.Step) => {
configSteps[s.id] = s;
});
// try {
// level.steps = (level.steps || []).map(
// (step: T.Step, stepIndex: number) => {
// const stepKey = `${levelId}S${stepIndex + 1}`;
// const stepSetupKey = `${stepKey}Q`;
// if (params.commits[stepSetupKey]) {
// if (!step.setup) {
// step.setup = {
// commits: [],
// };
// }
// step.setup.commits = params.commits[stepSetupKey];
// }

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

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

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

console.log(params.commits);

if (params.commits[level.id]) {
if (!level.setup) {
level.setup = {};
}
level.setup.commits = params.commits[level.id];
}
}

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

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

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

// 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;
});
return level;
}
);

return parsed;
}
44 changes: 22 additions & 22 deletions tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Some text
`;

const skeleton = {
levels: [{ id: "L1" }],
levels: [{ id: "1" }],
};

const result = parse({
Expand Down Expand Up @@ -299,7 +299,7 @@ The first step
text: md,
skeleton,
commits: {
L1S1Q: ["abcdefg1"],
"1.1Q": ["abcdefg1"],
},
});
const expected = {
Expand Down Expand Up @@ -355,7 +355,7 @@ The first step
text: md,
skeleton,
commits: {
L1S1Q: ["abcdefg1", "123456789"],
"1.1Q": ["abcdefg1", "123456789"],
},
});
const expected = {
Expand Down Expand Up @@ -406,7 +406,7 @@ The first step
text: md,
skeleton,
commits: {
L1: ["abcdefg1"],
"1": ["abcdefg1"],
},
});
const expected = {
Expand Down Expand Up @@ -464,8 +464,8 @@ Another line
text: md,
skeleton,
commits: {
L1: ["abcdefg1"],
L1S1Q: ["12345678"],
"1": ["abcdefg1"],
"1.1Q": ["12345678"],
},
});
const expected = {
Expand Down Expand Up @@ -519,8 +519,8 @@ The first step
text: md,
skeleton,
commits: {
L1S1Q: ["abcdefg1", "123456789"],
L1S1A: ["1gfedcba", "987654321"],
"1.1Q": ["abcdefg1", "123456789"],
"1.1A": ["1gfedcba", "987654321"],
},
});
const expected = {
Expand Down Expand Up @@ -644,12 +644,12 @@ The third step
text: md,
skeleton,
commits: {
L1S1Q: ["abcdef1", "123456789"],
L1S1A: ["1fedcba", "987654321"],
L1S2Q: ["2abcdef"],
L1S2A: ["3abcdef"],
L2S1Q: ["4abcdef"],
L2S1A: ["5abcdef"],
"1.1Q": ["abcdef1", "123456789"],
"1.1A": ["1fedcba", "987654321"],
"1.2Q": ["2abcdef"],
"1.2A": ["3abcdef"],
"2.1Q": ["4abcdef"],
"2.1A": ["5abcdef"],
},
});
const expected = {
Expand All @@ -664,7 +664,7 @@ The third step
content: "First level content.",
steps: [
{
id: "L11.1S1",
id: "1.1",
content: "The first step",
setup: {
commits: ["abcdef1", "123456789"],
Expand Down Expand Up @@ -759,7 +759,7 @@ The first step
text: md,
skeleton,
commits: {
L1S1Q: ["abcdef1", "123456789"],
"1.1Q": ["abcdef1", "123456789"],
},
});
const expected = {
Expand Down Expand Up @@ -935,7 +935,7 @@ Description.
});
});

xdescribe("hints", () => {
describe("hints", () => {
it("should parse hints for a step", () => {
const md = `# Title

Expand Down Expand Up @@ -971,7 +971,7 @@ The first step
text: md,
skeleton,
commits: {
L1S1Q: ["abcdef1", "123456789"],
"1.1Q": ["abcdef1", "123456789"],
},
});
const expected = {
Expand Down Expand Up @@ -1040,7 +1040,7 @@ And spans multiple lines.
text: md,
skeleton,
commits: {
L1S1Q: ["abcdef1", "123456789"],
"1.1Q": ["abcdef1", "123456789"],
},
});
const expected = {
Expand Down Expand Up @@ -1119,9 +1119,9 @@ The second uninterrupted step
text: md,
skeleton,
commits: {
L1S1Q: ["abcdef1"],
L1S1A: ["123456789"],
L1S2Q: ["fedcba1"],
"1.1Q": ["abcdef1"],
"1.1A": ["123456789"],
"1.2Q": ["fedcba1"],
},
});
const expected = {
Expand Down
2 changes: 1 addition & 1 deletion tests/skeleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe("validate skeleton", () => {
const valid = validateSkeleton(json);
expect(valid).toBe(false);
});
it("should fial if level is missing id", () => {
it("should fail if level is missing id", () => {
const level1 = { ...validJson.levels[0], id: undefined };
const json = {
...validJson,
Expand Down
4 changes: 2 additions & 2 deletions typings/tutorial.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export type Level = {
export type Step = {
id: string;
content: string;
setup: StepActions;
solution: Maybe<StepActions>;
setup?: StepActions;
solution?: Maybe<StepActions>;
subtasks?: { [testName: string]: boolean };
hints?: string[];
};
Expand Down