diff --git a/package-lock.json b/package-lock.json index d62581a..f2acb7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@coderoad/cli", - "version": "0.4.1", + "version": "0.4.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index c58f867..7d1f14d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@coderoad/cli", - "version": "0.4.1", + "version": "0.4.2", "description": "A CLI to build the configuration file for Coderoad Tutorials", "keywords": [ "coderoad", diff --git a/src/utils/parse.ts b/src/utils/parse.ts index 0b6ec1f..c53bf73 100644 --- a/src/utils/parse.ts +++ b/src/utils/parse.ts @@ -140,6 +140,7 @@ export function parse(params: ParseParams): any { // add level step commits const { steps, ...configLevelProps } = configLevel; level = { ...configLevelProps, ...level }; + if (steps) { steps.forEach((step: T.Step, index: number) => { try { @@ -151,12 +152,14 @@ export function parse(params: ParseParams): any { }; const stepSetupKey = `${step.id}:T`; + + if (!step?.setup) { + step.setup = {}; + } + if (!step.setup.commits) { + step.setup.commits = []; + } if (params.commits[stepSetupKey]) { - if (!step.setup) { - step.setup = { - commits: [], - }; - } step.setup.commits = params.commits[stepSetupKey]; } diff --git a/src/utils/validateMarkdown.ts b/src/utils/validateMarkdown.ts index 9933c67..6703786 100644 --- a/src/utils/validateMarkdown.ts +++ b/src/utils/validateMarkdown.ts @@ -53,16 +53,17 @@ const codeBlockRegex = /```[a-z]*\n[\s\S]*?\n```/gm; export function validateMarkdown(md: string): boolean { // remove codeblocks which might contain any valid combinations - const text = md.replace(codeBlockRegex, ""); + // trim white space + const text = md.replace(codeBlockRegex, "").trim(); let valid = true; for (const v of validations) { if (!v.validate(text)) { valid = false; - // if (process.env.NODE_ENV !== "test") { - console.warn(v.message); - // } + if (process.env.NODE_ENV !== "test") { + console.warn(v.message); + } } } diff --git a/tests/markdown.test.ts b/tests/markdown.test.ts index 0ab15bf..4606551 100644 --- a/tests/markdown.test.ts +++ b/tests/markdown.test.ts @@ -132,4 +132,26 @@ Should not be an issue First Step`; expect(validateMarkdown(md)).toBe(true); }); + it("should ignore empty space at the top", () => { + const md = ` + +# Title + +Description.`; + expect(validateMarkdown(md)).toBe(true); + }); + it("should ignore empty space at the bottom", () => { + const md = ` + +# Title + +Description. + + + + + +`; + expect(validateMarkdown(md)).toBe(true); + }); }); diff --git a/tests/parse.test.ts b/tests/parse.test.ts index a24bfc7..6139dbc 100644 --- a/tests/parse.test.ts +++ b/tests/parse.test.ts @@ -786,6 +786,55 @@ The first step }; expect(result.levels).toEqual(expected.levels); }); + it("should load no commits if none found for a step", () => { + const md = `# Title + +Description. + +## 1. Title + +First line + +### 1.1 + +The first step +`; + const skeleton = { + levels: [ + { + id: "1", + steps: [{ id: "1" }], + }, + ], + }; + const result = parse({ + text: md, + skeleton, + commits: {}, + }); + const expected = { + summary: { + description: "Description.", + }, + levels: [ + { + id: "1", + summary: "First line", + content: "First line", + steps: [ + { + id: "1.1", + content: "The first step", + setup: { + commits: [], + }, + }, + ], + }, + ], + }; + expect(result.levels[0].steps[0]).toEqual(expected.levels[0].steps[0]); + }); }); describe("config", () => {