-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontMatter.js
133 lines (104 loc) · 3.99 KB
/
frontMatter.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// frontMatter.js
// Detect file line ending type
function getLineEndingType(data) {
const indexOfLF = data.indexOf('\n', 1);
if (indexOfLF === -1) {
if (data.indexOf('\r') !== -1) {
return '\r';
}
return '\n';
}
if (data[indexOfLF - 1] === '\r') {
return '\r\n';
}
return '\n';
}
// Usage: frontMatter = getFrontMatter(data, format);
// where format = 'markdown' or 'HTML'
function getFrontMatter(data, format) {
//
if (data == "") {
return data;
}
var frontMatter = '';
if (format == 'markdown') {
// YAML front matter (---)
if (data.match(/^((---)(?:\r\n|\n))/) != null) {
/*
var frontMatter = data.match(/^(---(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)---(?:\r\n|\n))$/m); // For some reason this returns phantom extra chars at the end
*/
frontMatter = data.match(/^(---(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)---(?:\r\n|\n))$/gm)[0]; // And this fixes it (only take first match)
// TOML front matter (+++)
} else if (data.match(/^((\+\+\+)(?:\r\n|\n))/) != null) {
/*
var frontMatter = data.match(/^(\+\+\+(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)\+\+\+(?:\r\n|\n))$/m); // Phantom extra chars
*/
frontMatter = data.match(/^(\+\+\+(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)\+\+\+(?:\r\n|\n))$/gm)[0]; // Fix
}
} else if (format == 'html') {
// YAML front matter (---)
if (data.match(/^((<pre><code>---)(?:\r\n|\n))/) != null) {
/*
var frontMatter = data.match(/^(<pre><code>---(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)---(?:\r\n|\n)<\/code><\/pre>)$/m); // Phantom extra chars
*/
frontMatter = data.match(/^(<pre><code>---(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)---(?:\r\n|\n)<\/code><\/pre>)$/gm)[0]; // Fix
// TOML front matter (+++)
} else if (data.match(/^((<pre><code>\+\+\+)(?:\r\n|\n))/) != null) {
/*
var frontMatter = data.match(/^(<pre><code>\+\+\+(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)\+\+\+(?:\r\n|\n)<\/code><\/pre>)$/m); // Phantom extra chars
*/
frontMatter = data.match(/^(<pre><code>\+\+\+(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)\+\+\+(?:\r\n|\n)<\/code><\/pre>)$/gm)[0]; // Fix
}
}
return frontMatter;
}
// Usage: data = removeFrontMatter(data, format);
// where format = 'markdown' or 'HTML'
function removeFrontMatter(data, format) {
//
if (data == "") {
return data;
}
if (format == 'markdown') {
// YAML front matter (---)
if (data.match(/^((---)(?:\r\n|\n))/) != null) {
data = data.replace(/^(---(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)---(?:\r\n|\n))$/m, function (match) {
return '';
});
// TOML front matter (+++)
} else if (data.match(/^((\+\+\+)(?:\r\n|\n))/) != null) {
data = data.replace(/^(\+\+\+(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)\+\+\+(?:\r\n|\n))$/m, function (match) {
return '';
});
}
} else if (format == 'html') {
// YAML front matter (---)
if (data.match(/^((<pre><code>---)(?:\r\n|\n))/) != null) {
data = data.replace(/^(<pre><code>---(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)---(?:\r\n|\n)<\/code><\/pre>)$/m, function (match) {
return '';
});
// TOML front matter (+++)
} else if (data.match(/^((<pre><code>\+\+\+)(?:\r\n|\n))/) != null) {
data = data.replace(/^(<pre><code>\+\+\+(?:\r\n|\n))(.|(?:\r\n|\n))*((?:\r\n|\n)\+\+\+(?:\r\n|\n)<\/code><\/pre>)$/m, function (match) {
return '';
});
}
}
return data;
}
// Usage: data = addFrontMatter(data, frontMatter, format);
// where format = 'markdown' or 'HTML'
function addFrontMatter(data, frontMatter, format) {
//
if (frontMatter == "") {
return data;
}
// Get the correct line ending type
var lineEndingType = getLineEndingType(data);
if (format == 'markdown') {
data = frontMatter + lineEndingType + data;
} else if (format == 'html') {
data = '<pre><code>' + frontMatter + '</code></pre>' + lineEndingType + data;
}
return data;
}