Skip to content

Commit 12a0639

Browse files
committed
chapters & pages
1 parent b662952 commit 12a0639

File tree

4 files changed

+73
-74
lines changed

4 files changed

+73
-74
lines changed

src/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ page one description
1010
page one explanation
1111

1212
### Page two
13-
thing
13+
page two description
14+
15+
page two explanation
16+
17+
## Chapter two
18+
some other chapter

src/build.js

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ var regex = {
1010
'+': line('\\+', 1),
1111
'@': line('@', 1)
1212
};
13+
function isEmpty(line) {
14+
return !line.length || !!line.match(/^\s+?[\n\r]/);
15+
}
1316
function parseWithCode(code, content) {
1417
if (!content) {
1518
return false;
@@ -40,7 +43,7 @@ function project(result, lines, index) {
4043
var chapterStart = parseWithCode('##', lines[i]);
4144
if (projectTitleMatch) {
4245
matchedAt = i;
43-
result.project.title = projectTitleMatch;
46+
result.project.title = projectTitleMatch.trim();
4447
}
4548
else if (chapterStart) {
4649
result.project.description = lines.slice(matchedAt + 1, i).toString();
@@ -56,10 +59,10 @@ function chapter(result, lines, index) {
5659
let pageStart = parseWithCode('###', lines[i]);
5760
if (chapterTitleMatch && !matchedAt) {
5861
matchedAt = i;
59-
index.page = 0;
62+
index.page = -1;
6063
index.chapter += 1;
6164
result.chapters.push({
62-
title: chapterTitleMatch,
65+
title: chapterTitleMatch.trim(),
6366
description: '',
6467
pages: []
6568
});
@@ -78,49 +81,42 @@ function chapter(result, lines, index) {
7881
function page(result, lines, index) {
7982
let matchedAt = null;
8083
let hasBreak = null;
81-
for (let i = 0; i < lines.length; i++) {
84+
index.page += 1;
85+
result.chapters[index.chapter].pages.push({
86+
title: parseWithCode('###', lines[0]).trim(),
87+
description: '',
88+
explanation: '',
89+
tasks: []
90+
});
91+
for (let i = 1; i < lines.length; i++) {
8292
let pageTitleMatch = parseWithCode('###', lines[i]);
83-
let nextChapterStart = parseWithCode('##', lines[i]);
84-
if (pageTitleMatch && !matchedAt) {
85-
matchedAt = i;
86-
result.chapters[index.chapter].pages.push({
87-
title: pageTitleMatch,
88-
description: '',
89-
explanation: '',
90-
tasks: []
91-
});
92-
index.page += 1;
93-
}
94-
else if (!hasBreak && !lines[i].match(/\S/)) {
93+
let nextChapter = parseWithCode('##', lines[i]);
94+
let nextTask = parseWithCode('+', lines[i]);
95+
if (!hasBreak && isEmpty(lines[i])) {
9596
hasBreak = i;
9697
}
97-
else if (pageTitleMatch || nextChapterStart) {
98+
else if (pageTitleMatch || nextChapter) {
9899
if (hasBreak) {
99-
console.log('HERE!!!', hasBreak);
100-
console.log(lines.slice(matchedAt, hasBreak).toString());
101-
console.log(lines.slice(hasBreak, i).toString());
102-
result.chapters[index.chapter].pages[index.page - 1].description = lines.slice(matchedAt + 1, hasBreak).toString();
103-
result.chapters[index.chapter].pages[index.page - 1].explanation = lines.slice(hasBreak + 1, i).toString();
100+
result.chapters[index.chapter].pages[index.page].description = lines.slice(1, hasBreak).toString();
101+
result.chapters[index.chapter].pages[index.page].explanation = lines.slice(hasBreak + 1, i).toString();
104102
}
105103
else {
106-
console.log('DOWN HERE');
107-
console.log(lines.slice(matchedAt + 1, i).toString());
108-
result.chapters[index.chapter].pages[index.page - 1].description = lines.slice(matchedAt + 1, i).toString();
104+
result.chapters[index.chapter].pages[index.page].description = lines.slice(1, i).toString();
109105
}
110-
if (nextChapterStart) {
106+
if (!!nextChapter) {
111107
return chapter(result, lines.slice(i), index);
112108
}
113-
else {
109+
else if (!!pageTitleMatch) {
114110
return page(result, lines.slice(i), index);
115111
}
116112
}
113+
else if (!!nextTask) {
114+
return task(result, lines.slice(i), index);
115+
}
117116
}
118-
console.log('*** Pages ***');
119-
console.log(result.chapters[0].pages[0]);
120-
console.log('** Result ***');
121117
return result;
122118
}
123119
function task(result, lines, index) {
124120
return result;
125121
}
126-
console.log(build('./src/README.md'));
122+
console.log(build('./src/README.md').chapters[0].pages[1]);

src/build.ts

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ var regex = {
1414
};
1515

1616

17+
function isEmpty(line: string): boolean {
18+
return !line.length || !!line.match(/^\s+?[\n\r]/);
19+
}
20+
1721
function parseWithCode(code: string, content: string) {
1822
if (!content) {
1923
return false;
@@ -25,7 +29,7 @@ function parseWithCode(code: string, content: string) {
2529
}
2630
}
2731

28-
function build(filePath: string): Result {
32+
function build(filePath: string) {
2933
var result = {
3034
project: {},
3135
chapters: []
@@ -54,7 +58,7 @@ function project(result: Result, lines: string[], index: Index) {
5458
if (projectTitleMatch) {
5559
// project.title
5660
matchedAt = i;
57-
result.project.title = projectTitleMatch;
61+
result.project.title = projectTitleMatch.trim();
5862
} else if (chapterStart) {
5963
result.project.description = lines.slice(matchedAt + 1, i).toString();
6064
return chapter(result, lines.slice(i), index);
@@ -81,10 +85,10 @@ function chapter(result: Result, lines: string[], index: Index): Result {
8185
// chapter title
8286
if (chapterTitleMatch && !matchedAt) {
8387
matchedAt = i;
84-
index.page = 0;
88+
index.page = -1;
8589
index.chapter += 1;
8690
result.chapters.push({
87-
title: chapterTitleMatch,
91+
title: chapterTitleMatch.trim(),
8892
description: '',
8993
pages: []
9094
});
@@ -111,57 +115,45 @@ function chapter(result: Result, lines: string[], index: Index): Result {
111115
// - ##
112116
// - +
113117
function page(result: Result, lines: string[], index: Index) {
114-
let matchedAt = null;
118+
let matchedAt: number = null;
115119
let hasBreak: number = null;
120+
index.page += 1;
121+
result.chapters[index.chapter].pages.push({
122+
title: parseWithCode('###', lines[0]).trim(),
123+
description: '',
124+
explanation: '',
125+
tasks: []
126+
});
127+
for (let i = 1; i < lines.length; i++) {
116128

117-
for (let i = 0; i < lines.length; i++) {
118129
// matches
119130
let pageTitleMatch = parseWithCode('###', lines[i]);
120-
let nextChapterStart = parseWithCode('##', lines[i]);
121-
122-
// page title
123-
if (pageTitleMatch && !matchedAt) {
124-
matchedAt = i;
125-
result.chapters[index.chapter].pages.push({
126-
title: pageTitleMatch,
127-
description: '',
128-
explanation: '',
129-
tasks: []
130-
});
131-
index.page += 1;
132-
133-
// empty line
134-
} else if (!hasBreak && !lines[i].match(/\S/)) {
131+
let nextChapter = parseWithCode('##', lines[i]);
132+
let nextTask = parseWithCode('+', lines[i]);
133+
// 1. page title
134+
if (!hasBreak && isEmpty(lines[i])) {
135135
hasBreak = i;
136+
// 3. exit on page title match again or next chapter
137+
} else if (pageTitleMatch || nextChapter) {
136138

137-
// description / break / explanation
138-
} else if (pageTitleMatch || nextChapterStart) {
139+
// add to result
139140
if (hasBreak) {
140-
console.log('HERE!!!', hasBreak);
141-
console.log(lines.slice(matchedAt, hasBreak).toString());
142-
console.log(lines.slice(hasBreak, i).toString());
143-
144-
145-
result.chapters[index.chapter].pages[index.page - 1].description = lines.slice(matchedAt + 1, hasBreak).toString();
146-
result.chapters[index.chapter].pages[index.page - 1].explanation = lines.slice(hasBreak + 1, i).toString();
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();
147143
} else {
148-
console.log('DOWN HERE');
149-
console.log(lines.slice(matchedAt + 1, i).toString());
150-
151-
result.chapters[index.chapter].pages[index.page - 1].description = lines.slice(matchedAt + 1, i).toString();
144+
result.chapters[index.chapter].pages[index.page].description = lines.slice(1, i).toString();
152145
}
153-
154146
// next chapter
155-
if (nextChapterStart) {
147+
if (!!nextChapter) {
156148
return chapter(result, lines.slice(i), index);
157-
} else {
149+
// next page
150+
} else if (!!pageTitleMatch) {
158151
return page(result, lines.slice(i), index);
159152
}
153+
} else if (!!nextTask) {
154+
return task(result, lines.slice(i), index);
160155
}
161156
}
162-
console.log('*** Pages ***');
163-
console.log(result.chapters[0].pages[0]);
164-
console.log('** Result ***');
165157
return result;
166158
}
167159

@@ -179,4 +171,5 @@ function task(result: Result, lines: string[], index: Index) {
179171
return result;
180172
}
181173

182-
console.log(build('./src/README.md'));
174+
console.log(build('./src/README.md').chapters[0].pages[1]);
175+
// build('./src/README.md');

src/typings.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ interface Index {
55
}
66

77
interface Result {
8-
project: Object;
8+
project: Project;
99
chapters: Chapter[];
1010
}
1111

12+
interface Project {
13+
title: string;
14+
description: string;
15+
}
16+
1217
interface Chapter {
1318
title: string;
1419
description: string;

0 commit comments

Comments
 (0)