Allow different beginning and ending frontmatter delimiters.
This commit is contained in:
parent
f97e2d2853
commit
d10f97f561
@ -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(
|
||||
|
@ -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) {
|
||||
|
@ -102,7 +102,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`
|
||||
|
Loading…
x
Reference in New Issue
Block a user