Skip to content

Commit

Permalink
Allow different beginning and ending frontmatter delimiters.
Browse files Browse the repository at this point in the history
  • Loading branch information
tech4him1 committed Feb 9, 2018
1 parent afce6ab commit 4c1acd2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/formats/__tests__/frontmatter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion src/formats/formats.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { List } from 'immutable';
import yamlFormatter from './yaml';
import tomlFormatter from './toml';
import jsonFormatter from './json';
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion website/site/content/docs/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 4c1acd2

Please sign in to comment.