-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #348 from josephearl/multi-format-frontmatter
Add multi-format frontmatter parser
- Loading branch information
Showing
8 changed files
with
192 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import Frontmatter from '../frontmatter'; | ||
|
||
const FrontmatterFormatter = new Frontmatter(); | ||
|
||
describe('Frontmatter', () => { | ||
it('should parse YAML with --- delimiters', () => { | ||
expect( | ||
FrontmatterFormatter.fromFile('---\ntitle: YAML\ndescription: Something longer\n---\nContent') | ||
).toEqual( | ||
{ | ||
title: 'YAML', | ||
description: 'Something longer', | ||
body: 'Content', | ||
} | ||
); | ||
}); | ||
|
||
it('should parse YAML with ---yaml delimiters', () => { | ||
expect( | ||
FrontmatterFormatter.fromFile('---yaml\ntitle: YAML\ndescription: Something longer\n---\nContent') | ||
).toEqual( | ||
{ | ||
title: 'YAML', | ||
description: 'Something longer', | ||
body: 'Content', | ||
} | ||
); | ||
}); | ||
|
||
it('should overwrite any body param in the front matter', () => { | ||
expect( | ||
FrontmatterFormatter.fromFile('---\ntitle: The Title\nbody: Something longer\n---\nContent') | ||
).toEqual( | ||
{ | ||
title: 'The Title', | ||
body: 'Content', | ||
} | ||
); | ||
}); | ||
|
||
it('should parse TOML with +++ delimiters', () => { | ||
expect( | ||
FrontmatterFormatter.fromFile('+++\ntitle = "TOML"\ndescription = "Front matter"\n+++\nContent') | ||
).toEqual( | ||
{ | ||
title: 'TOML', | ||
description: 'Front matter', | ||
body: 'Content', | ||
} | ||
); | ||
}); | ||
|
||
it('should parse TOML with ---toml delimiters', () => { | ||
expect( | ||
FrontmatterFormatter.fromFile('---toml\ntitle = "TOML"\ndescription = "Something longer"\n---\nContent') | ||
).toEqual( | ||
{ | ||
title: 'TOML', | ||
description: 'Something longer', | ||
body: 'Content', | ||
} | ||
); | ||
}); | ||
|
||
it('should parse JSON with { } delimiters', () => { | ||
expect( | ||
FrontmatterFormatter.fromFile('{\n"title": "The Title",\n"description": "Something longer"\n}\nContent') | ||
).toEqual( | ||
{ | ||
title: 'The Title', | ||
description: 'Something longer', | ||
body: 'Content', | ||
} | ||
); | ||
}); | ||
|
||
it('should parse JSON with ---json delimiters', () => { | ||
expect( | ||
FrontmatterFormatter.fromFile('---json\n{\n"title": "The Title",\n"description": "Something longer"\n}\n---\nContent') | ||
).toEqual( | ||
{ | ||
title: 'The Title', | ||
description: 'Something longer', | ||
body: 'Content', | ||
} | ||
); | ||
}); | ||
|
||
it('should stringify YAML with --- delimiters', () => { | ||
expect( | ||
FrontmatterFormatter.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') | ||
); | ||
}); | ||
}); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import preliminaries from 'preliminaries'; | ||
import yamlParser from 'preliminaries-parser-yaml'; | ||
import tomlParser from 'preliminaries-parser-toml'; | ||
|
||
// Automatically register parsers | ||
preliminaries(true); | ||
yamlParser(true); | ||
tomlParser(true); | ||
|
||
export default class Frontmatter { | ||
fromFile(content) { | ||
const result = preliminaries.parse(content); | ||
const data = result.data; | ||
data.body = result.content; | ||
return data; | ||
} | ||
|
||
toFile(data, sortedKeys) { | ||
const meta = {}; | ||
let body = ''; | ||
Object.keys(data).forEach((key) => { | ||
if (key === 'body') { | ||
body = data[key]; | ||
} else { | ||
meta[key] = data[key]; | ||
} | ||
}); | ||
// always stringify to YAML | ||
return preliminaries.stringify(body, meta, { lang: 'yaml', delims: '---' }); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters