Skip to content

Fix/id order #52

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 3 commits into from
Jun 25, 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
handle levels out of order
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Jun 25, 2020
commit ab93ee538a5ded4e0c9d6689813723c18d9309b4
27 changes: 19 additions & 8 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,25 @@ export function parseMdContent(md: string): TutorialFrame | never {
mdContent.summary.description = summaryMatch.groups.tutorialDescription.trim();
}

let current = { level: -1, step: -1 };
let current = { levelId: "", levelIndex: -1, stepIndex: -1 };
// 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 levelMatch: RegExpMatchArray | null = section.match(levelRegex);

if (levelMatch && levelMatch.groups) {
current = { level: current.level + 1, step: -1 };
const levelId = levelMatch.groups.levelId.replace(".", "");
current = {
levelId: levelId,
levelIndex: current.levelIndex + 1,
stepIndex: -1,
};
const { levelTitle, levelSummary, levelContent } = levelMatch.groups;

// @ts-ignore
mdContent.levels[current.level] = {
id: (current.level + 1).toString(),
mdContent.levels[current.levelIndex] = {
id: levelId,
title: levelTitle.trim(),
summary:
levelSummary && levelSummary.trim().length
Expand All @@ -75,10 +80,14 @@ export function parseMdContent(md: string): TutorialFrame | never {
const stepRegex = /^(#{3}\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/;
const stepMatch: RegExpMatchArray | null = section.match(stepRegex);
if (stepMatch && stepMatch.groups) {
current = { level: current.level, step: current.step + 1 };
current = {
levelId: current.levelId,
levelIndex: current.levelIndex,
stepIndex: current.stepIndex + 1,
};
const { stepId, stepContent } = stepMatch.groups;
mdContent.levels[current.level].steps[current.step] = {
id: `${current.level + 1}.${current.step + 1}`,
mdContent.levels[current.levelIndex].steps[current.stepIndex] = {
id: `${current.levelId}.${current.stepIndex + 1}`,
content: stepContent.trim(),
};
} else {
Expand All @@ -92,7 +101,9 @@ export function parseMdContent(md: string): TutorialFrame | never {
.slice(1) // remove #### HINTS
.map((h) => h.trim());
if (hints.length) {
mdContent.levels[current.level].steps[current.step].hints = hints;
mdContent.levels[current.levelIndex].steps[
current.stepIndex
].hints = hints;
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,29 +842,29 @@ The first step
Description.
## 100. Title
## 100. First Title
First line
### 100.1
The first step
## 200. Title
## 200. Second Title
First line
Second line
### 200.1
The first step
The second step
## 201. Title
## 201. Third Title
First line
Third line
### 201.1
The first step
The third step
`;
const skeleton = {
levels: [
Expand Down Expand Up @@ -894,6 +894,7 @@ The first step
levels: [
{
id: "100",
title: "First Title",
summary: "First line",
content: "First line",
steps: [
Expand All @@ -908,6 +909,7 @@ The first step
},
{
id: "200",
title: "Second Title",
summary: "Second line",
content: "Second line",
steps: [
Expand All @@ -922,6 +924,7 @@ The first step
},
{
id: "201",
title: "Third Title",
summary: "Third line",
content: "Third line",
steps: [
Expand Down