-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preset-hugo): support configured post types
- Loading branch information
1 parent
ffa3e90
commit 05d3105
Showing
7 changed files
with
438 additions
and
434 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import camelcaseKeys from "camelcase-keys"; | ||
import TOML from "@iarna/toml"; | ||
import YAML from "yaml"; | ||
|
||
/** | ||
* Get content | ||
* @access private | ||
* @param {object} properties - JF2 properties | ||
* @returns {string} Content | ||
*/ | ||
const getContent = (properties) => { | ||
if (properties.content) { | ||
const content = | ||
properties.content.text || properties.content.html || properties.content; | ||
return `\n${content}\n`; | ||
} else { | ||
return ""; | ||
} | ||
}; | ||
|
||
/** | ||
* Get front matter | ||
* @access private | ||
* @param {object} properties - JF2 properties | ||
* @param {string} frontMatterFormat - Front matter format | ||
* @returns {string} Front matter in chosen format | ||
*/ | ||
const getFrontMatter = (properties, frontMatterFormat) => { | ||
let delimiters; | ||
let frontMatter; | ||
|
||
/* | ||
* Go templates don’t accept hyphens in property names | ||
* and Hugo camelCases its predefined front matter keys | ||
* @see {link: https://gohugo.io/content-management/front-matter/} | ||
*/ | ||
properties = camelcaseKeys(properties, { deep: true }); | ||
|
||
/* | ||
* Replace Microformat properties with Hugo equivalents | ||
* @see {link: https://gohugo.io/content-management/front-matter/} | ||
*/ | ||
properties = { | ||
date: properties.published, | ||
publishDate: properties.published, | ||
...(properties.postStatus === "draft" && { draft: true }), | ||
...(properties.updated && { lastmod: properties.updated }), | ||
...(properties.deleted && { expiryDate: properties.deleted }), | ||
...(properties.name && { title: properties.name }), | ||
...(properties.photo && { | ||
images: properties.photo.map((image) => image.url), | ||
}), | ||
...properties, | ||
}; | ||
|
||
delete properties.content; // Shown below front matter | ||
delete properties.deleted; // Use `expiryDate` | ||
delete properties.name; // Use `title` | ||
delete properties.postStatus; // Use `draft` | ||
delete properties.published; // Use `date` | ||
delete properties.type; // Not required | ||
delete properties.updated; // Use `lastmod` | ||
delete properties.url; // Not required | ||
|
||
switch (frontMatterFormat) { | ||
case "json": { | ||
delimiters = ["", "\n"]; | ||
frontMatter = JSON.stringify(properties, undefined, 2); | ||
break; | ||
} | ||
|
||
case "toml": { | ||
delimiters = ["+++\n", "+++\n"]; | ||
frontMatter = TOML.stringify(properties); | ||
break; | ||
} | ||
|
||
default: { | ||
delimiters = ["---\n", "---\n"]; | ||
frontMatter = YAML.stringify(properties, { lineWidth: 0 }); | ||
break; | ||
} | ||
} | ||
|
||
return `${delimiters[0]}${frontMatter}${delimiters[1]}`; | ||
}; | ||
|
||
/** | ||
* Get post template | ||
* @param {object} properties - JF2 properties | ||
* @param {string} [frontMatterFormat] - Front matter format | ||
* @returns {string} Rendered template | ||
*/ | ||
export const getPostTemplate = (properties, frontMatterFormat = "yaml") => { | ||
const content = getContent(properties); | ||
const frontMatter = getFrontMatter(properties, frontMatterFormat); | ||
|
||
return frontMatter + content; | ||
}; |
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,34 @@ | ||
import plur from "plur"; | ||
|
||
/** | ||
* Get paths and URLs for configured post types | ||
* @param {object} postTypes - Post type configuration | ||
* @returns {object} Updated post type configuration | ||
*/ | ||
export const getPostTypes = (postTypes) => { | ||
const types = []; | ||
|
||
for (const postType of postTypes) { | ||
const { type } = postType; | ||
const section = plur(type); | ||
|
||
/** | ||
* Follow Hugo content management guidelines | ||
* @see {@link https://gohugo.io/content-management/organization/} | ||
* @see {@link https://gohugo.io/content-management/static-files/} | ||
*/ | ||
types.push({ | ||
type, | ||
post: { | ||
path: `content/${section}/{slug}.md`, | ||
url: `${section}/{slug}`, | ||
}, | ||
media: { | ||
path: `static/${section}/{filename}`, | ||
url: `${section}/{filename}`, | ||
}, | ||
}); | ||
} | ||
|
||
return types; | ||
}; |
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
"main": "index.js", | ||
"files": [ | ||
"assets", | ||
"lib", | ||
"index.js" | ||
], | ||
"bugs": { | ||
|
Oops, something went wrong.