Small frontmatter getter refactor, more questions

This commit is contained in:
Will Boyd
2025-02-01 15:10:45 -05:00
parent f6197d1d70
commit 3bbf0274ce
5 changed files with 26 additions and 16 deletions
+11 -11
View File
@@ -3,12 +3,12 @@ import * as settings from './settings.js';
// get author, without decoding
// WordPress doesn't allow funky characters in usernames anyway
export function getAuthor(post) {
export function author(post) {
return post.data.creator[0];
}
// get array of decoded category names, filtered as specified in settings
export function getCategories(post) {
export function categories(post) {
if (!post.data.category) {
return [];
}
@@ -22,18 +22,18 @@ export function getCategories(post) {
// get cover image filename, previously decoded and set on post.meta
// this one is unique as it relies on special logic executed by the parser
export function getCoverImage(post) {
export function coverImage(post) {
return post.meta.coverImage;
}
// get post date, optionally formatted as specified in settings
// this value is also used for year/month folders, date prefixes, etc. as needed
export function getDate(post) {
export function date(post, config) {
const dateTime = luxon.DateTime.fromRFC2822(post.data.pubDate[0], { zone: settings.custom_date_timezone });
if (settings.custom_date_formatting) {
return dateTime.toFormat(settings.custom_date_formatting);
} else if (settings.include_time_with_date) {
} else if (config.includeTimeWithDate) {
return dateTime.toISO();
} else {
return dateTime.toISODate();
@@ -41,22 +41,22 @@ export function getDate(post) {
}
// get excerpt, not decoded, newlines collapsed
export function getExcerpt(post) {
export function excerpt(post) {
return post.data.encoded[1].replace(/[\r\n]+/gm, ' ');
}
// get ID
export function getId(post) {
export function id(post) {
return post.data.post_id[0];
}
// get slug, previously decoded and set on post.meta
export function getSlug(post) {
export function slug(post) {
return post.meta.slug;
}
// get array of decoded tag names
export function getTags(post) {
export function tags(post) {
if (!post.data.category) {
return [];
}
@@ -69,12 +69,12 @@ export function getTags(post) {
}
// get simple post title, but not decoded like other frontmatter string fields
export function getTitle(post) {
export function title(post) {
return post.data.title[0];
}
// get type, often this will always be "post"
// but can also be "page" or other custom types
export function getType(post) {
export function type(post) {
return post.data.post_type[0];
}
+2 -2
View File
@@ -168,12 +168,12 @@ function populateFrontmatter(posts, config) {
config.frontmatterFields.forEach(field => {
const [key, alias] = field.split(':');
let frontmatterGetter = frontmatter['get' + key.replace(/^./, (match) => match.toUpperCase())];
let frontmatterGetter = frontmatter[key];
if (!frontmatterGetter) {
throw `Could not find a frontmatter getter named "${key}".`;
}
post.frontmatter[alias || key] = frontmatterGetter(post);
post.frontmatter[alias || key] = frontmatterGetter(post, config);
});
});
}
+10
View File
@@ -115,4 +115,14 @@ export const all = [
type: 'integer',
default: 500
},
{
name: 'markdown-file-write-delay',
type: 'integer',
default: 25
},
{
name: 'include-time-with-date',
type: 'boolean',
default: false
},
];
+2 -2
View File
@@ -16,11 +16,11 @@
// Time in ms to wait between saving Markdown files. Increase this if your file system becomes
// overloaded.
export const markdown_file_write_delay = 25;
// export const markdown_file_write_delay = 25;
// Enable this to include time with post dates. For example, "2020-12-25" would become
// "2020-12-25T11:20:35.000Z".
export const include_time_with_date = false;
// export const include_time_with_date = false;
// Override post date formatting with a custom formatting string (for example: 'yyyy LLL dd').
// Tokens are documented here: https://moment.github.io/luxon/#/parsing?id=table-of-tokens. If
+1 -1
View File
@@ -58,7 +58,7 @@ async function writeMarkdownFilesPromise(posts, config) {
destinationPath,
delay
};
delay += settings.markdown_file_write_delay;
delay += config.markdownFileWriteDelay;
return [payload];
}
});