-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess-content.js
110 lines (91 loc) · 2.43 KB
/
process-content.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
import smarkt from 'smarkt';
import YAML from 'yaml';
import JSON5 from 'json5';
import fs from 'fs';
import path from 'path';
import frontmatter from 'front-matter';
import toml from 'toml';
import hjson from 'hjson';
function getFileExt(item) {
if (item.match(/\.([0-9a-z]+)(?:[?#]|$)/i)) return item.match(/\.([0-9a-z]+)(?:[?#]|$)/i)[1];
}
function parseToml(dir, item) {
if (!process.browser) {
if (getFileExt(item) === 'toml') {
let content = fs.readFileSync(path.join(dir, item), 'utf8');
return toml.parse(content);
}
}
}
// function parseJson(dir, item) {
// if (!process.browser) {
// if (getFileExt(item) === 'json') {
// let content = fs.readFileSync(path.join(dir, item), 'utf8');
// return JSON.parse(content);
// }
// }
// }
function parseHjson(dir, item) {
if (!process.browser) {
if (getFileExt(item) === 'hjson' || getFileExt(item) === 'json') {
let content = fs.readFileSync(path.join(dir, item), 'utf8');
return hjson.parse(content);
}
}
}
function parseJson5(dir, item) {
if (!process.browser) {
if (getFileExt(item) === 'json5') {
let content = fs.readFileSync(path.join(dir, item), 'utf8');
return JSON5.parse(content);
}
}
}
function parseMarkdown(dir, item) {
if (!process.browser) {
// Removed automatic parsing of markdown, so that this can be done per domain
if (getFileExt(item) === 'md') {
var file = fs.readFileSync(path.join(dir, item), 'utf8');
const { attributes, body } = frontmatter(file);
let object = {
...attributes,
content: body
};
return object;
}
}
}
function parseText(dir, item) {
if (!process.browser) {
if (getFileExt(item) === 'txt') {
let object = smarkt.parse(fs.readFileSync(path.join(dir, item), 'utf8'));
return object;
}
}
}
function parseYaml(dir, item) {
if (!process.browser) {
if (getFileExt(item) === 'yml' || getFileExt(item) === 'yaml') {
let object = YAML.parse(fs.readFileSync(path.join(dir, item), 'utf8'));
return object;
}
}
}
// function parseCsson(dir, item) {
// if (getFileExt(item) === 'csson') {
// let object = csson(fs.readFileSync(path.join(dir, item), 'utf8'));
// return object;
// }
// }
export default function parseContent(dir, item) {
let result =
// parseJson(dir, item) ||
parseHjson(dir, item) ||
parseMarkdown(dir, item) ||
parseText(dir, item) ||
parseYaml(dir, item) ||
parseJson5(dir, item) ||
parseToml(dir, item);
// parseCsson(dir, item);
return result;
}