diff --git a/handlers/refresh.mjs b/handlers/refresh.mjs index a5a7c02..81c043a 100644 --- a/handlers/refresh.mjs +++ b/handlers/refresh.mjs @@ -3,6 +3,11 @@ import * as cheerio from "cheerio"; import { error, reading } from "../helpers/html.mjs"; import { log } from "../helpers/logger.mjs"; +/** + * Fetches the daily Bible reading from the USCCB website and updates the app's HTML. + * + * @param {Object} app - The application instance to update with the fetched reading. + */ export default async function refresh(app) { const key = format(startOfToday(), "MMddyy"); diff --git a/helpers/html.mjs b/helpers/html.mjs index e052e90..7fbb169 100644 --- a/helpers/html.mjs +++ b/helpers/html.mjs @@ -1,13 +1,19 @@ import { format } from "date-fns"; import Handlebars from "handlebars"; import romcal from "romcal"; -import { isToday } from "date-fns"; +import { isToday, startOfToday, getYear } from "date-fns"; +/** + * Generates CSS styles for the HTML content, dynamically adjusting colors based on the current liturgical event. + * + * @returns {string} A string containing the CSS styles. + */ function css() { let color = "#007acc"; // Default color + // Get today's liturgical event from the Roman calendar const event = romcal - .calendarFor(new Date().getFullYear()) + .calendarFor(getYear(startOfToday())) .find((e) => isToday(new Date(e.moment))); if (event) { @@ -81,8 +87,15 @@ const readingTemplate = Handlebars.compile(` `); +/** + * Generates the HTML content for the daily Bible reading, including the title, readings, and a link to the source. + * + * @param {string} title - The title of the reading. + * @param {Array} readings - An array of reading objects, each containing a title, content, and URL. + * @returns {string} A string containing the generated HTML content for the reading. + */ export function reading(title, readings) { - const now = new Date(); + const now = startOfToday(); const url = `https://bible.usccb.org/bible/readings/${format(now, "MMddyy")}.cfm`; const date = format(now, "MMMM d, yyyy"); @@ -94,6 +107,12 @@ export function reading(title, readings) { }); } +/** + * Generates the HTML content for an error message when fetching the reading fails. + * + * @param {number} status - The HTTP status code of the error. + * @returns {string} A string containing the generated HTML content for the error message. + */ export function error(status) { return ` diff --git a/helpers/logger.mjs b/helpers/logger.mjs index f5766e0..64920e3 100644 --- a/helpers/logger.mjs +++ b/helpers/logger.mjs @@ -1,3 +1,8 @@ +/** + * A simple logging utility that prefixes log messages with a timestamp and a tag. + * + * @param {...any} args - The arguments to log, which can be of any type. + */ export const log = (...args) => { const timestamp = new Date().toISOString(); console.log(`[holybar] [${timestamp}]`, ...args);