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
update parser progress
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jun 21, 2020
commit 8af3bf6a683e08f8c2e1cc96e8a9f221aa140cf9
170 changes: 85 additions & 85 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import * as T from "../../typings/tutorial";

type TutorialFrame = {
summary: T.TutorialSummary;
levels: {
[levelKey: string]: T.Level;
};
steps: { [stepKey: string]: Partial<T.Step> };
levels: T.Level[];
};

export function parseMdContent(md: string): TutorialFrame | never {
Expand All @@ -33,8 +30,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
title: "",
description: "",
},
levels: {},
steps: {},
levels: [],
};

// Capture summary
Expand All @@ -49,23 +45,20 @@ export function parseMdContent(md: string): TutorialFrame | never {
mdContent.summary.description = summaryMatch.groups.tutorialDescription.trim();
}

let current = { level: "0", step: "0" };
let current = { level: -1, step: 0 };
// Identify each part of the content
parts.forEach((section: string) => {
// match level
const levelRegex = /^(#{2}\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 {
levelId,
levelTitle,
levelSummary,
levelContent,
} = levelMatch.groups;
current = { level: current.level + 1, step: 0 };
const { levelTitle, levelSummary, levelContent } = levelMatch.groups;

// @ts-ignore
mdContent.levels[levelId] = {
id: levelId,
mdContent.levels[current.level] = {
id: (current.level + 1).toString(),
title: levelTitle.trim(),
summary:
levelSummary && levelSummary.trim().length
Expand All @@ -75,20 +68,21 @@ export function parseMdContent(md: string): TutorialFrame | never {
omission: "...",
}),
content: levelContent.trim(),
steps: [],
};
current = { level: levelId, step: "0" };
} else {
// match step
const stepRegex = /^(#{3}\s(?<stepId>(?<levelId>L\d+)S\d+)\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.steps[stepId] = {
mdContent.levels[current.level].steps[current.step] = {
id: stepId,
content: stepContent.trim(),
setup: {},
solution: {},
};
current = { ...current, step: stepId };
current = { ...current, step: current.step + 1 };
} else {
// parse hints from stepContent
const hintDetectRegex = /^(#{4}\sHINTS[\n\r]+(\*\s(?<hintContent>[^]*))[\n\r]+)+/;
Expand All @@ -100,7 +94,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
.slice(1) // remove #### HINTS
.map((h) => h.trim());
if (hints.length) {
mdContent.steps[current.step].hints = hints;
mdContent.levels[current.level].steps[current.step].hints = hints;
}
}
}
Expand Down Expand Up @@ -135,72 +129,78 @@ export function parse(params: ParseParams): any {
};
}

// merge content and tutorial
if (params.skeleton.levels && params.skeleton.levels.length) {
parsed.levels = params.skeleton.levels
.map((level: T.Level, levelIndex: number) => {
const levelContent = mdContent.levels[level.id];
// merge content levels and tutorial

if (!levelContent) {
return null;
}

level = { ...level, ...levelContent };

// add level setup commits
const levelSetupKey = level.id;
if (params.commits[levelSetupKey]) {
level.setup = {
...(level.setup || {}),
commits: params.commits[levelSetupKey],
};
}
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 = {};
}
level.setup.commits = params.commits[levelId];
}

// add level step 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];
}

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

return level;
})
.filter((l: T.Level | null) => !!l);
}
// 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 parsed;
}
22 changes: 11 additions & 11 deletions tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Short description to be shown as a tutorial's subtitle.

Description.

## Put Level's title here
## 1. Put Level's title here

> Level's summary: a short description of the level's content in one line.

Expand Down Expand Up @@ -68,7 +68,7 @@ Some text

Description.

## Put Level's title here
## 1. Put Level's title here

> Level's summary: a short description of the level's content in one line.

Expand Down Expand Up @@ -112,7 +112,7 @@ Some text

Description.

## Put Level's title here
## 1. Put Level's title here

Some text that becomes the summary
`;
Expand Down Expand Up @@ -142,7 +142,7 @@ Some text that becomes the summary

Description.

## Put Level's title here
## 1. Put Level's title here

Some text that becomes the summary and goes beyond the maximum length of 80 so that it gets truncated at the end
`;
Expand Down Expand Up @@ -171,7 +171,7 @@ Some text that becomes the summary and goes beyond the maximum length of 80 so t

Description.

## 1 Put Level's title here
## 1. Put Level's title here

Some text.

Expand Down Expand Up @@ -203,7 +203,7 @@ But not including this line.

Description.

## Put Level's title here
## 1. Put Level's title here

>

Expand Down Expand Up @@ -239,7 +239,7 @@ Description.

Second description line

## Titles
## 1. Titles

First line

Expand Down Expand Up @@ -275,7 +275,7 @@ Third line

Description.

## Title
## 1. Title

First line

Expand Down Expand Up @@ -331,7 +331,7 @@ The first step

Description.

## Title
## 1. Title

First line

Expand Down Expand Up @@ -387,7 +387,7 @@ The first step

Description.

## Title
## 1. Title

First line

Expand Down Expand Up @@ -935,7 +935,7 @@ Description.
});
});

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

Expand Down
2 changes: 1 addition & 1 deletion typings/tutorial.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type TutorialSummary = {

export type StepActions = {
commands?: string[];
commits: string[];
commits?: string[];
files?: string[];
watchers?: string[];
filter?: string;
Expand Down