-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
frontmatter.js
94 lines (85 loc) · 3.13 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
import matter from 'gray-matter';
import tomlFormatter from './toml';
import yamlFormatter from './yaml';
import jsonFormatter from './json';
const parsers = {
toml: {
parse: input => tomlFormatter.fromFile(input),
stringify: (metadata, { sortedKeys }) => tomlFormatter.toFile(metadata, sortedKeys),
},
json: {
parse: input => {
let JSONinput = input.trim();
// Fix JSON if leading and trailing brackets were trimmed.
if (JSONinput.substr(0, 1) !== '{') {
JSONinput = '{' + JSONinput;
}
if (JSONinput.substr(-1) !== '}') {
JSONinput = JSONinput + '}';
}
return jsonFormatter.fromFile(JSONinput);
},
stringify: (metadata, { sortedKeys }) => {
let JSONoutput = jsonFormatter.toFile(metadata, sortedKeys).trim();
// Trim leading and trailing brackets.
if (JSONoutput.substr(0, 1) === '{' && JSONoutput.substr(-1) === '}') {
JSONoutput = JSONoutput.substring(1, JSONoutput.length - 1);
}
return JSONoutput;
},
},
yaml: {
parse: input => yamlFormatter.fromFile(input),
stringify: (metadata, { sortedKeys }) => yamlFormatter.toFile(metadata, sortedKeys),
},
};
function inferFrontmatterFormat(str) {
const firstLine = str.substr(0, str.indexOf('\n')).trim();
if (firstLine.length > 3 && firstLine.substr(0, 3) === '---') {
// No need to infer, `gray-matter` will handle things like `---toml` for us.
return;
}
switch (firstLine) {
case '---':
return getFormatOpts('yaml');
case '+++':
return getFormatOpts('toml');
case '{':
return getFormatOpts('json');
default:
console.warn('Unrecognized front-matter format.');
}
}
export const getFormatOpts = format =>
({
yaml: { language: 'yaml', delimiters: '---' },
toml: { language: 'toml', delimiters: '+++' },
json: { language: 'json', delimiters: ['{', '}'] },
}[format]);
class FrontmatterFormatter {
constructor(format, customDelimiter) {
this.format = getFormatOpts(format);
this.customDelimiter = customDelimiter;
}
fromFile(content) {
const format = this.format || inferFrontmatterFormat(content);
if (this.customDelimiter) this.format.delimiters = this.customDelimiter;
const result = matter(content, { engines: parsers, ...format });
return {
...result.data,
body: result.content,
};
}
toFile(data, sortedKeys) {
const { body = '', ...meta } = data;
// Stringify to YAML if the format was not set
const format = this.format || getFormatOpts('yaml');
if (this.customDelimiter) this.format.delimiters = this.customDelimiter;
// `sortedKeys` is not recognized by gray-matter, so it gets passed through to the parser
return matter.stringify(body, meta, { engines: parsers, sortedKeys, ...format });
}
}
export const FrontmatterInfer = new FrontmatterFormatter();
export const frontmatterYAML = customDelimiter => new FrontmatterFormatter('yaml', customDelimiter);
export const frontmatterTOML = customDelimiter => new FrontmatterFormatter('toml', customDelimiter);
export const frontmatterJSON = customDelimiter => new FrontmatterFormatter('json', customDelimiter);