From 0232eaf48e7d38bf5dd971b3394cc88d29d0c9c0 Mon Sep 17 00:00:00 2001 From: shmck Date: Wed, 24 Jun 2020 21:45:43 -0700 Subject: [PATCH 1/3] release v0.4.3 Signed-off-by: shmck --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index f2acb7b..843fe40 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@coderoad/cli", - "version": "0.4.2", + "version": "0.4.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7d1f14d..8bfc00a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@coderoad/cli", - "version": "0.4.2", + "version": "0.4.3", "description": "A CLI to build the configuration file for Coderoad Tutorials", "keywords": [ "coderoad", From 14418938307450c263be482ade312cc56ff920fe Mon Sep 17 00:00:00 2001 From: shmck Date: Thu, 25 Jun 2020 08:19:59 -0700 Subject: [PATCH 2/3] add out of order id test Signed-off-by: shmck --- tests/parse.test.ts | 102 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/tests/parse.test.ts b/tests/parse.test.ts index 6139dbc..cc79bea 100644 --- a/tests/parse.test.ts +++ b/tests/parse.test.ts @@ -837,6 +837,108 @@ The first step }); }); + it("should load when commits are not in direct order (100, 200, 201)", () => { + const md = `# Title + +Description. + +## 100. Title + +First line + +### 100.1 + +The first step + +## 200. Title + +First line + +### 200.1 + +The first step + +## 201. Title + +First line + +### 201.1 + +The first step +`; + const skeleton = { + levels: [ + { + id: "100", + steps: [{ id: "100.1" }], + }, + { + id: "200", + steps: [{ id: "200.1" }], + }, + { + id: "201", + steps: [{ id: "201.1" }], + }, + ], + }; + const result = parse({ + text: md, + skeleton, + commits: {}, + }); + const expected = { + summary: { + description: "Description.", + }, + levels: [ + { + id: "100", + summary: "First line", + content: "First line", + steps: [ + { + id: "100.1", + content: "The first step", + setup: { + commits: [], + }, + }, + ], + }, + { + id: "200", + summary: "Second line", + content: "Second line", + steps: [ + { + id: "200.1", + content: "The second step", + setup: { + commits: [], + }, + }, + ], + }, + { + id: "201", + summary: "Third line", + content: "Third line", + steps: [ + { + id: "201.1", + content: "The third step", + setup: { + commits: [], + }, + }, + ], + }, + ], + }; + expect(result.levels).toEqual(expected.levels); + }); + describe("config", () => { it("should parse the tutorial config", () => { const md = `# Title From ab93ee538a5ded4e0c9d6689813723c18d9309b4 Mon Sep 17 00:00:00 2001 From: shmck Date: Thu, 25 Jun 2020 08:36:50 -0700 Subject: [PATCH 3/3] handle levels out of order Signed-off-by: shmck --- src/utils/parse.ts | 27 +++++++++++++++++++-------- tests/parse.test.ts | 17 ++++++++++------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/utils/parse.ts b/src/utils/parse.ts index c53bf73..7c4fb04 100644 --- a/src/utils/parse.ts +++ b/src/utils/parse.ts @@ -45,7 +45,7 @@ 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 @@ -53,12 +53,17 @@ export function parseMdContent(md: string): TutorialFrame | never { 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 @@ -75,10 +80,14 @@ export function parseMdContent(md: string): TutorialFrame | never { const stepRegex = /^(#{3}\s(?.*)[\n\r]+(?[^]*))/; 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 { @@ -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; } } } diff --git a/tests/parse.test.ts b/tests/parse.test.ts index cc79bea..d5b98bb 100644 --- a/tests/parse.test.ts +++ b/tests/parse.test.ts @@ -842,7 +842,7 @@ The first step Description. -## 100. Title +## 100. First Title First line @@ -850,21 +850,21 @@ First line 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: [ @@ -894,6 +894,7 @@ The first step levels: [ { id: "100", + title: "First Title", summary: "First line", content: "First line", steps: [ @@ -908,6 +909,7 @@ The first step }, { id: "200", + title: "Second Title", summary: "Second line", content: "Second line", steps: [ @@ -922,6 +924,7 @@ The first step }, { id: "201", + title: "Third Title", summary: "Third line", content: "Third line", steps: [