Files
holybar/helpers/html.mjs
T
2026-04-17 15:36:11 +02:00

129 lines
2.8 KiB
JavaScript

import { format } from "date-fns";
import Handlebars from "handlebars";
import romcal from "romcal";
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(getYear(startOfToday()))
.find((e) => isToday(new Date(e.moment)));
if (event) {
if (event.data.meta.liturgicalColor.key.toLowerCase() === "white") {
color = "goldenrod"; // Use goldenrod for better visibility on white
} else {
color = event.data.meta.liturgicalColor.value;
}
}
return `
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 0;
background-color: #f9f9f9;
}
h1 {
color: #333;
transition: color 0.3s;
}
h1:hover {
color: ${color};
cursor: pointer;
}
strong {
color: #333;
}
h2 {
color: ${color};
font-size: 18px;
margin-top: 5px;
}
p {
font-size: 16px;
line-height: 1.5;
color: #555;
}
</style>
`;
}
export const base = `
<html>
<head>
${css()}
</head>
<body onload="glimpse.send({ action: 'ready' })">
<h1>Loading...</h1>
</body>
</html>
`;
const readingTemplate = Handlebars.compile(`
<html>
<head>
${css()}
</head>
<body>
<h2>{{title}}</h2>
<h5>{{date}}</h2>
{{#each readings}}
<hr />
<h1 onclick="glimpse.send({ action: 'openUrl', url: '{{this.url}}' })">
{{this.title}}
</h1>
<p>{{{this.content}}}</p>
{{/each}}
</body>
</html>
`);
/**
* 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 = startOfToday();
const url = `https://bible.usccb.org/bible/readings/${format(now, "MMddyy")}.cfm`;
const date = format(now, "MMMM d, yyyy");
return readingTemplate({
title,
readings,
url,
date,
});
}
/**
* 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 `
<html>
<head>
${css}
</head>
<body>
<h1>Error fetching reading</h1>
<p>Status: ${status}</p>
</body>
</html>
`;
}