Skip to content

Commit 49d117e

Browse files
committed
truncate missing level description
Signed-off-by: shmck <[email protected]>
1 parent 32bd304 commit 49d117e

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/utils/parse.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,20 @@ export function parseMdContent(md: string): TutorialFrame | never {
5353
const stepMatch = section.match(stepRegex);
5454

5555
if (levelMatch) {
56+
const {
57+
levelId,
58+
levelTitle,
59+
levelSummary,
60+
levelContent,
61+
} = levelMatch.groups;
5662
const level = {
57-
[levelMatch.groups.levelId]: {
58-
id: levelMatch.groups.levelId,
59-
title: levelMatch.groups.levelTitle,
60-
summary: levelMatch.groups.levelSummary.trim(),
61-
content: levelMatch.groups.levelContent.trim(),
63+
[levelId]: {
64+
id: levelId,
65+
title: levelTitle,
66+
summary: levelSummary
67+
? levelSummary.trim()
68+
: _.truncate(levelContent, { length: 80, omission: "..." }),
69+
content: levelContent.trim(),
6270
},
6371
};
6472

tests/parse.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,60 @@ levels:
8888
};
8989
expect(result.levels).toEqual(expected.levels);
9090
});
91+
92+
it("should parse a level with no level description", () => {
93+
const md = `# Title
94+
95+
Description.
96+
97+
## L1 Put Level's title here
98+
99+
Some text that becomes the summary
100+
`;
101+
102+
const yaml = `version: "0.1.0"
103+
levels:
104+
- id: L1
105+
`;
106+
const result = parse(md, yaml);
107+
const expected = {
108+
levels: [
109+
{
110+
id: "L1",
111+
title: "Put Level's title here",
112+
summary: "Some text that becomes the summary",
113+
content: "Some text that becomes the summary",
114+
},
115+
],
116+
};
117+
expect(result.levels).toEqual(expected.levels);
118+
});
119+
120+
it("should truncate a level description", () => {
121+
const md = `# Title
122+
123+
Description.
124+
125+
## L1 Put Level's title here
126+
127+
Some text that becomes the summary and goes beyond the maximum length of 80 so that it gets truncated at the end
128+
`;
129+
130+
const yaml = `version: "0.1.0"
131+
levels:
132+
- id: L1
133+
`;
134+
const result = parse(md, yaml);
135+
const expected = {
136+
levels: [
137+
{
138+
id: "L1",
139+
title: "Put Level's title here",
140+
summary: "Some text that becomes the summary",
141+
content: "Some text that becomes the summary",
142+
},
143+
],
144+
};
145+
expect(result.levels[0].summary).toMatch(/\.\.\.$/);
146+
});
91147
});

0 commit comments

Comments
 (0)