Filterting for categories, omit empty lists in frontmatter

This commit is contained in:
Will Boyd
2020-12-22 10:28:45 -05:00
parent 9e6d38adc9
commit 9799ce5929
3 changed files with 30 additions and 12 deletions
+19 -7
View File
@@ -56,14 +56,26 @@ async function writeMarkdownFilesPromise(posts, config ) {
async function loadMarkdownFilePromise(post) {
let output = '---\n';
Object.entries(post.frontmatter).forEach(pair => {
const key = pair[0];
const value = Array.isArray(pair[1])
? (pair[1].length === 0 ? "" : "\n - \"" + pair[1].join("\"\n - \"") + "\"")
: '"' + (pair[1] || '').replace(/"/g, '\\"') +'"';
output += key + ': ' + value + '\n';
Object.entries(post.frontmatter).forEach(([key, value]) => {
let outputValue;
if (Array.isArray(value)) {
if (value.length > 0) {
// array of one or more strings
outputValue = value.reduce((list, item) => `${list}\n - "${item}"`, '');
}
} else {
// single string value
const escapedValue = (value || '').replace(/"/g, '\\"');
outputValue = `"${escapedValue}"`;
}
if (outputValue !== undefined) {
output += `${key}: ${outputValue}\n`;
}
});
output += '---\n\n' + post.content + '\n';
output += `---\n\n${post.content}\n`;
return output;
}