Skip to content

Commit ab93ee5

Browse files
committed
handle levels out of order
Signed-off-by: shmck <[email protected]>
1 parent 1441893 commit ab93ee5

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/utils/parse.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,25 @@ export function parseMdContent(md: string): TutorialFrame | never {
4545
mdContent.summary.description = summaryMatch.groups.tutorialDescription.trim();
4646
}
4747

48-
let current = { level: -1, step: -1 };
48+
let current = { levelId: "", levelIndex: -1, stepIndex: -1 };
4949
// Identify each part of the content
5050
parts.forEach((section: string) => {
5151
// match level
5252
const levelRegex = /^(#{2}\s(?<levelId>L?\d+\.?)\s(?<levelTitle>.*)[\n\r]*(>\s(?<levelSummary>.*))?[\n\r]+(?<levelContent>[^]*))/;
5353
const levelMatch: RegExpMatchArray | null = section.match(levelRegex);
5454

5555
if (levelMatch && levelMatch.groups) {
56-
current = { level: current.level + 1, step: -1 };
56+
const levelId = levelMatch.groups.levelId.replace(".", "");
57+
current = {
58+
levelId: levelId,
59+
levelIndex: current.levelIndex + 1,
60+
stepIndex: -1,
61+
};
5762
const { levelTitle, levelSummary, levelContent } = levelMatch.groups;
5863

5964
// @ts-ignore
60-
mdContent.levels[current.level] = {
61-
id: (current.level + 1).toString(),
65+
mdContent.levels[current.levelIndex] = {
66+
id: levelId,
6267
title: levelTitle.trim(),
6368
summary:
6469
levelSummary && levelSummary.trim().length
@@ -75,10 +80,14 @@ export function parseMdContent(md: string): TutorialFrame | never {
7580
const stepRegex = /^(#{3}\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/;
7681
const stepMatch: RegExpMatchArray | null = section.match(stepRegex);
7782
if (stepMatch && stepMatch.groups) {
78-
current = { level: current.level, step: current.step + 1 };
83+
current = {
84+
levelId: current.levelId,
85+
levelIndex: current.levelIndex,
86+
stepIndex: current.stepIndex + 1,
87+
};
7988
const { stepId, stepContent } = stepMatch.groups;
80-
mdContent.levels[current.level].steps[current.step] = {
81-
id: `${current.level + 1}.${current.step + 1}`,
89+
mdContent.levels[current.levelIndex].steps[current.stepIndex] = {
90+
id: `${current.levelId}.${current.stepIndex + 1}`,
8291
content: stepContent.trim(),
8392
};
8493
} else {
@@ -92,7 +101,9 @@ export function parseMdContent(md: string): TutorialFrame | never {
92101
.slice(1) // remove #### HINTS
93102
.map((h) => h.trim());
94103
if (hints.length) {
95-
mdContent.levels[current.level].steps[current.step].hints = hints;
104+
mdContent.levels[current.levelIndex].steps[
105+
current.stepIndex
106+
].hints = hints;
96107
}
97108
}
98109
}

tests/parse.test.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -842,29 +842,29 @@ The first step
842842
843843
Description.
844844
845-
## 100. Title
845+
## 100. First Title
846846
847847
First line
848848
849849
### 100.1
850850
851851
The first step
852852
853-
## 200. Title
853+
## 200. Second Title
854854
855-
First line
855+
Second line
856856
857857
### 200.1
858858
859-
The first step
859+
The second step
860860
861-
## 201. Title
861+
## 201. Third Title
862862
863-
First line
863+
Third line
864864
865865
### 201.1
866866
867-
The first step
867+
The third step
868868
`;
869869
const skeleton = {
870870
levels: [
@@ -894,6 +894,7 @@ The first step
894894
levels: [
895895
{
896896
id: "100",
897+
title: "First Title",
897898
summary: "First line",
898899
content: "First line",
899900
steps: [
@@ -908,6 +909,7 @@ The first step
908909
},
909910
{
910911
id: "200",
912+
title: "Second Title",
911913
summary: "Second line",
912914
content: "Second line",
913915
steps: [
@@ -922,6 +924,7 @@ The first step
922924
},
923925
{
924926
id: "201",
927+
title: "Third Title",
925928
summary: "Third line",
926929
content: "Third line",
927930
steps: [

0 commit comments

Comments
 (0)