oliverdavies.uk/src/pages/rss/daily.xml.js

63 lines
2.1 KiB
JavaScript
Raw Normal View History

import MarkdownIt from 'markdown-it';
2023-04-17 23:23:35 +01:00
import _ from 'lodash';
2022-10-07 09:14:10 +01:00
import rss from '@astrojs/rss';
import sanitizeHtml from 'sanitize-html';
import { getCollection } from 'astro:content';
2022-10-07 09:14:10 +01:00
export async function get() {
const emails = await getCollection('daily-email');
const sortedEmails = Object.values(emails)
.sort((a, b) =>
new Date(b.data.pubDate).valueOf() -
new Date(a.data.pubDate).valueOf()
);
const parser = new MarkdownIt();
const callToAction = (emailBody) => {
if (emailBody.includes('P.S.')) {
return '';
}
return '<br />P.S. ' + _.sample([
2023-07-18 08:15:16 +01:00
'Are you still using Drupal 7 and dont know whats involved to upgrade to Drupal 10? <a href="https://www.oliverdavies.uk/call">Book a Drupal 7 upgrade consultation call</a> or <a href="https://www.oliverdavies.uk/drupal7">an upgrade roadmap</a>.',
2023-06-27 23:13:51 +01:00
'Need help or want another pair of eyes on your code? Book a <a href="https://www.oliverdavies.uk/call">1-on-1 consulting call</a> or an <a href="https://www.oliverdavies.uk/pair">online pair programming session</a> with a 100% money-back guarantee.',
'If you\'re creating a new Drupal module, try my <a href="https://github.com/opdavies/drupal-module-template">free Drupal module template</a>.',
]);
};
2023-04-30 12:08:03 +01:00
const convertTag = (tag) => {
const words = tag.split("-");
const wordCount = words.length;
if (wordCount > 1) {
return _.upperFirst(_.camelCase(tag));
}
return _.lowerCase(tag);
}
return rss({
title: 'Daily email list',
description: 'A daily newsletter on software development, DevOps, community, and open-source.',
site: import.meta.env.SITE,
items: sortedEmails.slice(0, 1).map((email) => ({
description: `
<div style="max-width: 550px;">
${sanitizeHtml(parser.render(email.body))}
${callToAction(email.body)}
</div>
`,
link: `${import.meta.env.SITE}/${email.data.permalink}`,
pubDate: email.data.pubDate,
2023-04-30 12:08:03 +01:00
title: `${email.data.title.trim()}`,
2023-04-19 23:15:39 +01:00
customData: `
<tags>
#dev ${email.data.tags.map(tag => `#${convertTag(tag)}`).join(' ')}
2023-04-19 23:15:39 +01:00
</tags>`,
}))
});
};