Skip to content

Commit 5ddeb6c

Browse files
committed
optimization
1 parent d7c26f9 commit 5ddeb6c

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

cr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"project":{"title":"Test","description":"This is a test,"},"chapters":[{"title":"Chapter one","description":"Some chapter one content.,","pages":[{"title":"Page one","description":"page one description","explanation":"page one explanation,","tasks":[]},{"title":"Page two","description":"page two description","explanation":"page two explanation,","tasks":[{"title":" Task One","description":"task one description","tests":[],"actions":[]},{"title":" Task Two","description":"task two description","tests":["test1.js"],"actions":["open(file.js)"]}]}]},{"title":"Chapter two","description":"","pages":[]}]}
1+
{"project":{"title":"Test","description":"This is a test,"},"chapters":[{"title":"Chapter one","description":"Some chapter one content.","pages":[{"title":"Page one","description":"page one descriptionWith another line","explanation":"page one explanationAnd more explanation","tasks":[]},{"title":"Page two","description":"page two description","explanation":"page two explanation","tasks":[{"title":" Task One","description":"task one description","tests":[],"actions":[]},{"title":" Task Two","description":"task two description","tests":["test1.js"],"actions":["open(file.js)"]}]}]},{"title":"Chapter two","description":"some other chapter","pages":[]}]}

src/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ Some chapter one content.
66

77
### Page one
88
page one description
9+
With another line
910

1011
page one explanation
12+
And more explanation
1113

1214
### Page two
1315
page two description

src/build.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ function parseWithCode(code, content) {
2626
}
2727
function build(filePath) {
2828
var result = {
29-
project: {},
29+
project: {
30+
title: '',
31+
description: ''
32+
},
3033
chapters: []
3134
}, index = {
3235
chapter: -1,
@@ -69,13 +72,14 @@ function chapter(result, lines, index) {
6972
});
7073
}
7174
else if (pageStart) {
72-
result.chapters[index.chapter].description = lines.slice(matchedAt + 1, i).toString();
7375
return page(result, lines.slice(i), index);
7476
}
7577
else if (chapterTitleMatch) {
76-
result.chapters[index.chapter].description = lines.slice(matchedAt + 1, i).toString();
7778
return chapter(result, lines.slice(i), index);
7879
}
80+
else {
81+
result.chapters[index.chapter].description += lines[i];
82+
}
7983
}
8084
return result;
8185
}
@@ -96,26 +100,25 @@ function page(result, lines, index) {
96100
if (!hasBreak && isEmpty(lines[i])) {
97101
hasBreak = i;
98102
}
99-
else if (!!pageTitleMatch || !!nextChapter || !!nextTask) {
100-
if (hasBreak) {
101-
result.chapters[index.chapter].pages[index.page].description = lines.slice(1, hasBreak).toString();
102-
result.chapters[index.chapter].pages[index.page].explanation = lines.slice(hasBreak + 1, i).toString();
103+
else if (!!nextChapter) {
104+
return chapter(result, lines.slice(i), index);
105+
}
106+
else if (!!pageTitleMatch) {
107+
return page(result, lines.slice(i), index);
108+
}
109+
else if (!!nextTask) {
110+
return task(result, lines.slice(i), index);
111+
}
112+
else {
113+
if (!hasBreak) {
114+
result.chapters[index.chapter].pages[index.page].description += lines[i];
103115
}
104116
else {
105-
result.chapters[index.chapter].pages[index.page].description = lines.slice(1, i).toString();
106-
}
107-
if (!!nextChapter) {
108-
return chapter(result, lines.slice(i), index);
117+
result.chapters[index.chapter].pages[index.page].explanation += lines[i];
109118
}
110-
else if (!!pageTitleMatch) {
111-
return page(result, lines.slice(i), index);
112-
}
113-
else if (!!nextTask) {
114-
return task(result, lines.slice(i), index);
115-
}
116-
return result;
117119
}
118120
}
121+
return result;
119122
}
120123
function task(result, lines, index) {
121124
result.chapters[index.chapter].pages[index.page].tasks.push({

src/build.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ function parseWithCode(code: string, content: string) {
3131

3232
function build(filePath: string) {
3333
var result = {
34-
project: {},
34+
project: {
35+
title: '',
36+
description: ''
37+
},
3538
chapters: []
3639
},
3740
index = {
@@ -95,12 +98,13 @@ function chapter(result: Result, lines: string[], index: Index): Result {
9598
});
9699
// next page
97100
} else if (pageStart) {
98-
result.chapters[index.chapter].description = lines.slice(matchedAt + 1, i).toString();
99101
return page(result, lines.slice(i), index);
100102
// next chapter
101103
} else if (chapterTitleMatch) {
102-
result.chapters[index.chapter].description = lines.slice(matchedAt + 1, i).toString();
103104
return chapter(result, lines.slice(i), index);
105+
// add to description
106+
} else {
107+
result.chapters[index.chapter].description += lines[i];
104108
}
105109
}
106110
return result;
@@ -134,27 +138,23 @@ function page(result: Result, lines: string[], index: Index) {
134138
if (!hasBreak && isEmpty(lines[i])) {
135139
hasBreak = i;
136140
// 3. exit on page title match again or next chapter
137-
} else if (!!pageTitleMatch || !!nextChapter || !!nextTask) {
138-
139-
// add to result
140-
if (hasBreak) {
141-
result.chapters[index.chapter].pages[index.page].description = lines.slice(1, hasBreak).toString();
142-
result.chapters[index.chapter].pages[index.page].explanation = lines.slice(hasBreak + 1, i).toString();
141+
} else if (!!nextChapter) {
142+
return chapter(result, lines.slice(i), index);
143+
// next page
144+
} else if (!!pageTitleMatch) {
145+
return page(result, lines.slice(i), index);
146+
} else if (!!nextTask) {
147+
return task(result, lines.slice(i), index);
148+
} else {
149+
// description || explanation
150+
if (!hasBreak) {
151+
result.chapters[index.chapter].pages[index.page].description += lines[i];
143152
} else {
144-
result.chapters[index.chapter].pages[index.page].description = lines.slice(1, i).toString();
153+
result.chapters[index.chapter].pages[index.page].explanation += lines[i];
145154
}
146-
// next chapter
147-
if (!!nextChapter) {
148-
return chapter(result, lines.slice(i), index);
149-
// next page
150-
} else if (!!pageTitleMatch) {
151-
return page(result, lines.slice(i), index);
152-
} else if (!!nextTask) {
153-
return task(result, lines.slice(i), index);
154-
}
155-
return result;
156155
}
157156
}
157+
return result;
158158
}
159159

160160
// task
@@ -203,7 +203,7 @@ function task(result: Result, lines: string[], index: Index) {
203203
// exit on chapter
204204
} else if (!!nextChapter) {
205205
return chapter(result, lines.slice(i), index);
206-
// test, action
206+
// task description +
207207
} else {
208208
result.chapters[index.chapter].pages[index.page].tasks[index.task].description += lines[i];
209209
}

0 commit comments

Comments
 (0)