blob: 640186ee1d0781be7f0f5f299464518a027d14ce (
plain)
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
|
import grayMatter from 'gray-matter';
const parseSourceFile = raw => {
const remake = source => grayMatter(source, {});
let editable = remake(raw);
const syncContent = (newVal, isBody) => {
if (isBody) {
editable.content = newVal;
} else {
editable = remake(newVal);
}
};
const trimmedEditable = () => grayMatter.stringify(editable).trim();
const content = (isBody = false) => (isBody ? editable.content.trim() : trimmedEditable()); // gray-matter internally adds an eof newline so we trim to bypass, open issue: https://github.com/jonschlinkert/gray-matter/issues/96
const matter = () => editable.data;
const syncMatter = settings => {
const source = grayMatter.stringify(editable.content, settings);
syncContent(source);
};
const isModified = () => trimmedEditable() !== raw;
const hasMatter = () => editable.matter.length > 0;
return {
matter,
syncMatter,
content,
syncContent,
isModified,
hasMatter,
};
};
export default parseSourceFile;
|