-
Notifications
You must be signed in to change notification settings - Fork 2
/
copy-markdownParser.js
81 lines (68 loc) · 1.9 KB
/
copy-markdownParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
===============================================================
===============================================================
===============================================================
INITIAL UTILITY FUNCTIONS:
Converts the inputted text into an array of objects with `type` and `content` properties.
===============================================================
===============================================================
===============================================================
*/
function getParagraphs(file) {
/*
parameter type: string
==>
return value type: array of strings
Description: Takes in a string of markdown and returns an array of paragraphs as separated by a blank line.
*/
var paragraphs = file.split("\n");
paragraphs = paragraphs.map(paragraph => {
paragraph = paragraph.trim(); // removes any whitespace around the paragraph
if (paragraph === "") {
return "\n";
} else {
return paragraph;
}
});
return paragraphs;
}
function setInitialType(headerCount) {
/*
parameter type: number
==>
return value type: string
Description: Takes in a number and returns the correct header (h1, h2, h3) or p (for paragraph) if the number is 0, or br if it is a line break.
*/
if (headerCount === -1) {
return 'br';
}
if (headerCount === 0) {
return 'p';
} else {
return 'h' + headerCount;
}
}
function convertToObject(paragraph) {
/*
parameter type: string
==>
return value type: object
Description: Takes in a paragraph, and returns an object with its tag type and content
ex. {type: 'h2', content: 'this is a header2'}.
*/
var headerCount = 0;
var content = "";
paragraph.split("").forEach(char => {
if (char === "#") {
headerCount++;
} else if (char === "\n") {
headerCount = -1;
} else {
content += char;
}
});
return {
type: setInitialType(headerCount),
content: content.trim()
};
}