From 80a2cefbf0081416890466864c2b941a9c70a529 Mon Sep 17 00:00:00 2001 From: Mathias Biilmann Christensen Date: Thu, 27 Oct 2016 15:33:15 +0200 Subject: [PATCH] Add json format support --- example/config.yml | 2 +- src/backends/backend.js | 13 ++++++------- src/formats/formats.js | 20 ++++++++++++-------- src/formats/json.js | 9 +++++++++ 4 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 src/formats/json.js diff --git a/example/config.yml b/example/config.yml index 3d5f55bf..c53282ca 100644 --- a/example/config.yml +++ b/example/config.yml @@ -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" diff --git a/src/backends/backend.js b/src/backends/backend.js index 82b8ba66..17dd7ff0 100644 --- a/src/backends/backend.js +++ b/src/backends/backend.js @@ -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')); } diff --git a/src/formats/formats.js b/src/formats/formats.js index fc8f91b1..48fa5367 100644 --- a/src/formats/formats.js +++ b/src/formats/formats.js @@ -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')); } diff --git a/src/formats/json.js b/src/formats/json.js new file mode 100644 index 00000000..8cfd87dd --- /dev/null +++ b/src/formats/json.js @@ -0,0 +1,9 @@ +export default class JSONFormatter { + fromFile(content) { + return JSON.parse(content); + } + + toFile(data) { + return JSON.generate(data); + } +}