From 32bd3049bddd77f4a4211ad216631f4ec21a8b20 Mon Sep 17 00:00:00 2001 From: shmck Date: Sat, 30 May 2020 15:07:09 -0700 Subject: [PATCH 1/2] parse a step Signed-off-by: shmck --- tests/parse.test.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/parse.test.ts b/tests/parse.test.ts index 489b24c..3d622e3 100644 --- a/tests/parse.test.ts +++ b/tests/parse.test.ts @@ -19,7 +19,7 @@ describe("parse", () => { expect(result.summary).toEqual(expected.summary); }); - it("should parse a level with a summary", () => { + it("should parse a level with no steps", () => { const md = `# Title Description. @@ -49,4 +49,43 @@ levels: }; expect(result.levels).toEqual(expected.levels); }); + + it("should parse a level with a step", () => { + const md = `# Title + +Description. + +## L1 Put Level's title here + +> Level's summary: a short description of the level's content in one line. + +Some text +`; + + const yaml = `version: "0.1.0" +levels: +- id: L1 + setup: + files: [] + commits: [] + solution: + files: [] + commits: [] +`; + const result = parse(md, yaml); + const expected = { + levels: [ + { + id: "L1", + title: "Put Level's title here", + summary: + "Level's summary: a short description of the level's content in one line.", + content: "Some text", + setup: { files: [], commits: [] }, + solution: { files: [], commits: [] }, + }, + ], + }; + expect(result.levels).toEqual(expected.levels); + }); }); From 49d117ebbb0329266e48d8fbec411bbab741a39c Mon Sep 17 00:00:00 2001 From: shmck Date: Sat, 30 May 2020 15:17:02 -0700 Subject: [PATCH 2/2] truncate missing level description Signed-off-by: shmck --- src/utils/parse.ts | 18 +++++++++++---- tests/parse.test.ts | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/utils/parse.ts b/src/utils/parse.ts index e182b10..d00da9e 100644 --- a/src/utils/parse.ts +++ b/src/utils/parse.ts @@ -53,12 +53,20 @@ export function parseMdContent(md: string): TutorialFrame | never { const stepMatch = section.match(stepRegex); if (levelMatch) { + const { + levelId, + levelTitle, + levelSummary, + levelContent, + } = levelMatch.groups; const level = { - [levelMatch.groups.levelId]: { - id: levelMatch.groups.levelId, - title: levelMatch.groups.levelTitle, - summary: levelMatch.groups.levelSummary.trim(), - content: levelMatch.groups.levelContent.trim(), + [levelId]: { + id: levelId, + title: levelTitle, + summary: levelSummary + ? levelSummary.trim() + : _.truncate(levelContent, { length: 80, omission: "..." }), + content: levelContent.trim(), }, }; diff --git a/tests/parse.test.ts b/tests/parse.test.ts index 3d622e3..736f8b2 100644 --- a/tests/parse.test.ts +++ b/tests/parse.test.ts @@ -88,4 +88,60 @@ levels: }; expect(result.levels).toEqual(expected.levels); }); + + it("should parse a level with no level description", () => { + const md = `# Title + +Description. + +## L1 Put Level's title here + +Some text that becomes the summary +`; + + const yaml = `version: "0.1.0" +levels: +- id: L1 +`; + const result = parse(md, yaml); + const expected = { + levels: [ + { + id: "L1", + title: "Put Level's title here", + summary: "Some text that becomes the summary", + content: "Some text that becomes the summary", + }, + ], + }; + expect(result.levels).toEqual(expected.levels); + }); + + it("should truncate a level description", () => { + const md = `# Title + +Description. + +## L1 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 +`; + + const yaml = `version: "0.1.0" +levels: +- id: L1 +`; + const result = parse(md, yaml); + const expected = { + levels: [ + { + id: "L1", + title: "Put Level's title here", + summary: "Some text that becomes the summary", + content: "Some text that becomes the summary", + }, + ], + }; + expect(result.levels[0].summary).toMatch(/\.\.\.$/); + }); });