Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow different beginning and ending frontmatter delimiters. #1094

Merged
merged 1 commit into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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