From 4c1acd2290b733b3addfe764a023e70cafbdbce1 Mon Sep 17 00:00:00 2001 From: Caleb Date: Thu, 8 Feb 2018 19:18:33 -0700 Subject: [PATCH] Allow different beginning and ending frontmatter delimiters. --- src/formats/__tests__/frontmatter.spec.js | 30 +++++++++++++++++++ src/formats/formats.js | 5 +++- .../content/docs/configuration-options.md | 2 +- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/formats/__tests__/frontmatter.spec.js b/src/formats/__tests__/frontmatter.spec.js index cdf41ba3af6d..3c12116b71ae 100644 --- a/src/formats/__tests__/frontmatter.spec.js +++ b/src/formats/__tests__/frontmatter.spec.js @@ -39,6 +39,18 @@ describe('Frontmatter', () => { ); }); + it('should parse YAML with custom delimiters when it is explicitly set as the format with different custom delimiters', () => { + expect( + frontmatterYAML(["~~~", "^^^"]).fromFile('~~~\ntitle: YAML\ndescription: Something longer\n^^^\nContent') + ).toEqual( + { + title: 'YAML', + description: 'Something longer', + body: 'Content', + } + ); + }); + it('should parse YAML with ---yaml delimiters', () => { expect( FrontmatterInfer.fromFile('---yaml\ntitle: YAML\ndescription: Something longer\n---\nContent') @@ -216,6 +228,24 @@ describe('Frontmatter', () => { ); }); + it('should stringify YAML with --- delimiters when it is explicitly set as the format with different custom delimiters', + () => { + expect( + frontmatterYAML(["~~~", "^^^"]).toFile({ body: 'Some content\nOn another line', tags: ['front matter', 'yaml'], title: 'YAML' }) + ).toEqual( + [ + '~~~', + 'tags:', + ' - front matter', + ' - yaml', + 'title: YAML', + '^^^', + 'Some content', + 'On another line\n', + ].join('\n') + ); + }); + it('should stringify TOML with +++ delimiters when it is explicitly set as the format without a custom delimiter', () => { expect( diff --git a/src/formats/formats.js b/src/formats/formats.js index 38be81e89fc8..eeb5749ab715 100644 --- a/src/formats/formats.js +++ b/src/formats/formats.js @@ -1,3 +1,4 @@ +import { List } from 'immutable'; import yamlFormatter from './yaml'; import tomlFormatter from './toml'; import jsonFormatter from './json'; @@ -54,7 +55,9 @@ function formatByName(name, customDelimiter) { export function resolveFormat(collectionOrEntity, entry) { // Check for custom delimiter - const customDelimiter = collectionOrEntity.get('frontmatter_delimiter'); + const frontmatter_delimiter = collectionOrEntity.get('frontmatter_delimiter'); + const customDelimiter = List.isList(frontmatter_delimiter) ? frontmatter_delimiter.toArray() : frontmatter_delimiter; + // If the format is specified in the collection, use that format. const formatSpecification = collectionOrEntity.get('format'); if (formatSpecification) { diff --git a/website/site/content/docs/configuration-options.md b/website/site/content/docs/configuration-options.md index d744c27b4b5a..edc1d62020e9 100644 --- a/website/site/content/docs/configuration-options.md +++ b/website/site/content/docs/configuration-options.md @@ -101,7 +101,7 @@ You may also specify a custom `extension` not included in the list above, as lon - `toml-frontmatter`: same as the `frontmatter` format above, except frontmatter will be both parsed and saved only as TOML, followed by unparsed body text. The default delimiter for this option is `+++`. - `json-frontmatter`: same as the `frontmatter` format above, except frontmatter will be both parsed and saved as JSON, followed by unparsed body text. The default delimiter for this option is `{` `}`. -`frontmatter_delimiter`: if you have an explicit frontmatter format declared, this option allows you to specify a custom delimiter like `~~~`. +`frontmatter_delimiter`: if you have an explicit frontmatter format declared, this option allows you to specify a custom delimiter like `~~~`. If you need different beginning and ending delimiters, you can use an array like `["(", ")"]`. ### `slug`