From 17b40b58ea0513b67e4fa47a130ee7e523b5d680 Mon Sep 17 00:00:00 2001 From: Shawn Erquhart Date: Thu, 20 Apr 2017 15:37:52 -0400 Subject: [PATCH] fix frontmatter stringification for date fields Switches back to custom frontmatter stringification until support lands in preliminaries. This is necessary because we use custom schemas for certain data types, such as dates and times. --- example/config.yml | 2 +- src/formats/frontmatter.js | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/example/config.yml b/example/config.yml index 8bbad3d4..12e565e1 100644 --- a/example/config.yml +++ b/example/config.yml @@ -12,10 +12,10 @@ collections: # A list of collections the CMS should be able to edit create: true # Allow users to create new documents in this collection fields: # The fields each document in this collection have - {label: "Title", name: "title", widget: "string", tagname: "h1"} + - {label: "Publish Date", name: "date", widget: "datetime", format: "YYYY-MM-DD hh:mma"} - {label: "Cover Image", name: "image", widget: "image", required: false, tagname: ""} - {label: "Body", name: "body", widget: "markdown"} meta: - - {label: "Publish Date", name: "date", widget: "datetime", format: "YYYY-MM-DD hh:mma"} - {label: "SEO Description", name: "description", widget: "text"} card: {type: "image", image: "image", text: "title"} diff --git a/src/formats/frontmatter.js b/src/formats/frontmatter.js index a469810c..7aaaf035 100644 --- a/src/formats/frontmatter.js +++ b/src/formats/frontmatter.js @@ -1,6 +1,7 @@ import preliminaries from 'preliminaries'; import yamlParser from 'preliminaries-parser-yaml'; import tomlParser from 'preliminaries-parser-toml'; +import YAML from './yaml'; // Automatically register parsers preliminaries(true); @@ -25,7 +26,13 @@ export default class Frontmatter { meta[key] = data[key]; } }); + // always stringify to YAML - return preliminaries.stringify(body, meta, { lang: 'yaml', delims: '---' }); + const parser = { + stringify(metadata) { + return new YAML().toFile(metadata, sortedKeys); + }, + }; + return preliminaries.stringify(body, meta, { lang: "yaml", delims: "---", parser }); } }