Skip to content

Commit

Permalink
fix(preset-hugo): use camelcased frontmatter keys. fixes #345
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed May 30, 2021
1 parent 6b0f60a commit 1d0c30a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
18 changes: 12 additions & 6 deletions packages/preset-hugo/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import camelcaseKeys from 'camelcase-keys';
import TOML from '@iarna/toml';
import YAML from 'yaml';

Expand Down Expand Up @@ -161,6 +162,11 @@ export const HugoPreset = class {
* @returns {string} Rendered template
*/
postTemplate(properties) {
// Go templates don’t accept hyphens in property names
// and Hugo camelCases its predefined front matter keys
// https://gohugo.io/content-management/front-matter/
properties = camelcaseKeys(properties, {deep: true});

let content;
if (properties.content) {
content = properties.content.text || properties.content.html || properties.content;
Expand All @@ -182,14 +188,14 @@ export const HugoPreset = class {
...(properties.audio && {audio: properties.audio}),
...(properties.photo && {images: properties.photo}),
...(properties.video && {videos: properties.video}),
...(properties['bookmark-of'] && {'bookmark-of': properties['bookmark-of']}),
...(properties['like-of'] && {'like-of': properties['like-of']}),
...(properties['repost-of'] && {'repost-of': properties['repost-of']}),
...(properties['in-reply-to'] && {'in-reply-to': properties['in-reply-to']}),
...(properties['post-status'] === 'draft' && {draft: true}),
...(properties.bookmarkOf && {bookmarkOf: properties.bookmarkOf}),
...(properties.likeOf && {likeOf: properties.likeOf}),
...(properties.repostOf && {repostOf: properties.repostOf}),
...(properties.inReplyTo && {inReplyTo: properties.inReplyTo}),
...(properties.postStatus === 'draft' && {draft: true}),
...(properties.visibility && {visibility: properties.visibility}),
...(properties.syndication && {syndication: properties.syndication}),
...(properties['mp-syndicate-to'] && {'mp-syndicate-to': properties['mp-syndicate-to']})
...(properties.mpSyndicateTo && {mpSyndicateTo: properties.mpSyndicateTo})
};

const frontMatter = this._getFrontMatter(properties);
Expand Down
1 change: 1 addition & 0 deletions packages/preset-hugo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"dependencies": {
"@iarna/toml": "^2.2.5",
"camelcase-keys": "^6.2.2",
"yaml": "^1.10.2"
},
"publishConfig": {
Expand Down
36 changes: 18 additions & 18 deletions packages/preset-hugo/tests/unit/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ test('Renders post template with JSON frontmatter', t => {
"rsvp": "Yes",
"location": {
"type": "adr",
"country-name": "United Kingdom"
"countryName": "United Kingdom"
},
"checkin": {
"type": "card",
Expand All @@ -104,14 +104,14 @@ test('Renders post template with JSON frontmatter', t => {
"url": "https://website.example/video.mp4"
}
],
"bookmark-of": "https://website.example",
"like-of": "https://website.example",
"repost-of": "https://website.example",
"in-reply-to": "https://website.example",
"bookmarkOf": "https://website.example",
"likeOf": "https://website.example",
"repostOf": "https://website.example",
"inReplyTo": "https://website.example",
"draft": true,
"visibility": "private",
"syndication": "https://website.example/post/12345",
"mp-syndicate-to": "https://social.example"
"mpSyndicateTo": "https://social.example"
}
I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.
`);
Expand All @@ -130,18 +130,18 @@ category = [ "lunch", "food" ]
start = "2020-02-02"
end = "2020-02-20"
rsvp = "Yes"
bookmark-of = "https://website.example"
like-of = "https://website.example"
repost-of = "https://website.example"
in-reply-to = "https://website.example"
bookmarkOf = "https://website.example"
likeOf = "https://website.example"
repostOf = "https://website.example"
inReplyTo = "https://website.example"
draft = true
visibility = "private"
syndication = "https://website.example/post/12345"
mp-syndicate-to = "https://social.example"
mpSyndicateTo = "https://social.example"
[location]
type = "adr"
country-name = "United Kingdom"
countryName = "United Kingdom"
[checkin]
type = "card"
Expand Down Expand Up @@ -179,7 +179,7 @@ end: 2020-02-20
rsvp: Yes
location:
type: adr
country-name: United Kingdom
countryName: United Kingdom
checkin:
type: card
latitude:
Expand All @@ -193,14 +193,14 @@ images:
url: https://website.example/photo.jpg
videos:
- url: https://website.example/video.mp4
bookmark-of: https://website.example
like-of: https://website.example
repost-of: https://website.example
in-reply-to: https://website.example
bookmarkOf: https://website.example
likeOf: https://website.example
repostOf: https://website.example
inReplyTo: https://website.example
draft: true
visibility: private
syndication: https://website.example/post/12345
mp-syndicate-to: https://social.example
mpSyndicateTo: https://social.example
---
I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.
`);
Expand Down

0 comments on commit 1d0c30a

Please sign in to comment.