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"
description: "General Site Settings"
fields:
- {label: "Global title", name: site_title, widget: "string"}
- {label: "Global title", name: "site_title", widget: "string"}
- name: "authors"
label: "Authors"

View File

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

View File

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