2016-02-25 20:40:35 -08:00
|
|
|
import YAML from './yaml';
|
|
|
|
|
|
|
|
const regexp = /^---\n([^]*?)\n---\n([^]*)$/;
|
|
|
|
|
|
|
|
export default class YAMLFrontmatter {
|
|
|
|
fromFile(content) {
|
|
|
|
const match = content.match(regexp);
|
|
|
|
const obj = match ? new YAML().fromFile(match[1]) : {};
|
|
|
|
obj.body = match ? (match[2] || '').replace(/^\n+/, '') : content;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2017-01-19 12:09:18 -02:00
|
|
|
toFile(data, sortedKeys) {
|
2016-02-25 20:40:35 -08:00
|
|
|
const meta = {};
|
|
|
|
let body = '';
|
|
|
|
let content = '';
|
|
|
|
for (var key in data) {
|
|
|
|
if (key === 'body') {
|
|
|
|
body = data[key];
|
|
|
|
} else {
|
|
|
|
meta[key] = data[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
content += '---\n';
|
2017-01-19 12:09:18 -02:00
|
|
|
content += new YAML().toFile(meta, sortedKeys);
|
2016-02-25 20:40:35 -08:00
|
|
|
content += '---\n\n';
|
|
|
|
content += body;
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
}
|