-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.ts
More file actions
65 lines (50 loc) · 1.41 KB
/
parse.ts
File metadata and controls
65 lines (50 loc) · 1.41 KB
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
import matter from "gray-matter";
import util from "util";
import fs from "fs";
const readFile = util.promisify(fs.readFile);
function getLineBreakChar(string) {
const indexOfLF = string.indexOf("\n", 1); // No need to check first-character
if (indexOfLF === -1) {
if (string.indexOf("\r") !== -1) return "\r";
return "\n";
}
if (string[indexOfLF - 1] === "\r") return "\r\n";
return "\n";
}
export async function getMetaData(file: string): Promise<object | void> {
const contents: string = await readFile(file, "utf8");
const lines: string[] = contents.split(getLineBreakChar(contents));
const meta_block: string[] = [];
let meta_start: boolean = false;
for (const line of lines) {
if (line == "---" && !meta_start) {
meta_start = true;
} else if (line === "---" && meta_start) {
meta_start = false;
meta_block.push(line);
}
if (meta_start) {
meta_block.push(line);
}
}
if (meta_block.length) {
// it's okay to fail sometimes...
try {
const parsed = matter(meta_block.join("\n"));
return parsed.data;
} catch (e) {
console.log(`${file} has no front-matter`);
return;
}
}
}
export async function getAllMetaData(files: string[]): Promise<object> {
const meta = {};
for (const file of files) {
const info = await getMetaData(file);
if (info) {
meta[file] = info;
}
}
return meta;
}