Skip to content

Commit 9c17bd5

Browse files
authored
Merge pull request #52 from coderoad/fix/id-order
Fix/id order
2 parents 9c787da + ab93ee5 commit 9c17bd5

File tree

4 files changed

+126
-10
lines changed

4 files changed

+126
-10
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coderoad/cli",
3-
"version": "0.4.2",
3+
"version": "0.4.3",
44
"description": "A CLI to build the configuration file for Coderoad Tutorials",
55
"keywords": [
66
"coderoad",

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

+105
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,111 @@ The first step
837837
});
838838
});
839839

840+
it("should load when commits are not in direct order (100, 200, 201)", () => {
841+
const md = `# Title
842+
843+
Description.
844+
845+
## 100. First Title
846+
847+
First line
848+
849+
### 100.1
850+
851+
The first step
852+
853+
## 200. Second Title
854+
855+
Second line
856+
857+
### 200.1
858+
859+
The second step
860+
861+
## 201. Third Title
862+
863+
Third line
864+
865+
### 201.1
866+
867+
The third step
868+
`;
869+
const skeleton = {
870+
levels: [
871+
{
872+
id: "100",
873+
steps: [{ id: "100.1" }],
874+
},
875+
{
876+
id: "200",
877+
steps: [{ id: "200.1" }],
878+
},
879+
{
880+
id: "201",
881+
steps: [{ id: "201.1" }],
882+
},
883+
],
884+
};
885+
const result = parse({
886+
text: md,
887+
skeleton,
888+
commits: {},
889+
});
890+
const expected = {
891+
summary: {
892+
description: "Description.",
893+
},
894+
levels: [
895+
{
896+
id: "100",
897+
title: "First Title",
898+
summary: "First line",
899+
content: "First line",
900+
steps: [
901+
{
902+
id: "100.1",
903+
content: "The first step",
904+
setup: {
905+
commits: [],
906+
},
907+
},
908+
],
909+
},
910+
{
911+
id: "200",
912+
title: "Second Title",
913+
summary: "Second line",
914+
content: "Second line",
915+
steps: [
916+
{
917+
id: "200.1",
918+
content: "The second step",
919+
setup: {
920+
commits: [],
921+
},
922+
},
923+
],
924+
},
925+
{
926+
id: "201",
927+
title: "Third Title",
928+
summary: "Third line",
929+
content: "Third line",
930+
steps: [
931+
{
932+
id: "201.1",
933+
content: "The third step",
934+
setup: {
935+
commits: [],
936+
},
937+
},
938+
],
939+
},
940+
],
941+
};
942+
expect(result.levels).toEqual(expected.levels);
943+
});
944+
840945
describe("config", () => {
841946
it("should parse the tutorial config", () => {
842947
const md = `# Title

0 commit comments

Comments
 (0)