Add json format support

This commit is contained in:
Mathias Biilmann Christensen 2016-10-27 15:33:15 +02:00
parent 7713c4c6f8
commit 80a2cefbf0
4 changed files with 28 additions and 16 deletions

View File

@ -36,7 +36,7 @@ collections: # A list of collections the CMS should be able to edit
file: "_data/settings.json" file: "_data/settings.json"
description: "General Site Settings" description: "General Site Settings"
fields: fields:
- {label: "Global title", name: site_title, widget: "string"} - {label: "Global title", name: "site_title", widget: "string"}
- name: "authors" - name: "authors"
label: "Authors" label: "Authors"

View File

@ -87,13 +87,8 @@ class Backend {
// We have the file path. Fetch and parse the file. // We have the file path. Fetch and parse the file.
getEntry(collection, slug, path) { getEntry(collection, slug, path) {
return this.implementation.getEntry(collection, slug, path).then(this.entryWithFormat(collection)); return this.implementation.getEntry(collection, slug, path)
} .then(loadedEntry => this.entryWithFormat(collection, slug)(createEntry(
lookupEntry(collection, slug) {
const collectionModel = new Collection(collection);
return this.implementation.getEntry(collection, slug, collectionModel.entryPath(slug))
.then(loadedEntry => this.entryWithFormat(collection)(createEntry(
collection.get('name'), collection.get('name'),
slug, slug,
loadedEntry.file.path, loadedEntry.file.path,
@ -102,6 +97,10 @@ class Backend {
); );
} }
lookupEntry(collection, slug) {
return this.getEntry(collection, slug, new Collection(collection).entryPath(slug));
}
newEntry(collection) { newEntry(collection) {
return createEntry(collection.get('name')); return createEntry(collection.get('name'));
} }

View File

@ -1,7 +1,9 @@
import YAML from './yaml'; import YAML from './yaml';
import JSONFormatter from './json';
import YAMLFrontmatter from './yaml-frontmatter'; import YAMLFrontmatter from './yaml-frontmatter';
const yamlFormatter = new YAML(); const yamlFormatter = new YAML();
const jsonFormatter = new JSONFormatter();
const YamlFrontmatterFormatter = new YAMLFrontmatter(); const YamlFrontmatterFormatter = new YAMLFrontmatter();
function formatByType(type) { function formatByType(type) {
@ -12,17 +14,18 @@ function formatByType(type) {
function formatByExtension(extension) { function formatByExtension(extension) {
return { return {
'yml': yamlFormatter, yml: yamlFormatter,
'md': YamlFrontmatterFormatter, json: jsonFormatter,
'markdown': YamlFrontmatterFormatter, md: YamlFrontmatterFormatter,
'html': YamlFrontmatterFormatter markdown: YamlFrontmatterFormatter,
html: YamlFrontmatterFormatter,
}[extension] || YamlFrontmatterFormatter; }[extension] || YamlFrontmatterFormatter;
} }
function formatByName(name) { function formatByName(name) {
return { return {
'yaml': yamlFormatter, yaml: yamlFormatter,
'frontmatter': YamlFrontmatterFormatter frontmatter: YamlFrontmatterFormatter,
}[name] || YamlFrontmatterFormatter; }[name] || YamlFrontmatterFormatter;
} }
@ -30,8 +33,9 @@ export function resolveFormat(collectionOrEntity, entry) {
if (typeof collectionOrEntity === 'string') { if (typeof collectionOrEntity === 'string') {
return formatByType(collectionOrEntity); return formatByType(collectionOrEntity);
} }
if (entry && entry.path) { const path = entry && entry.path;
return formatByExtension(entry.path.split('.').pop()); if (path) {
return formatByExtension(path.split('.').pop());
} }
return formatByName(collectionOrEntity.get('format')); return formatByName(collectionOrEntity.get('format'));
} }

9
src/formats/json.js Normal file
View File

@ -0,0 +1,9 @@
export default class JSONFormatter {
fromFile(content) {
return JSON.parse(content);
}
toFile(data) {
return JSON.generate(data);
}
}