2023-04-09 11:25:12 +01:00
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' ;
2023-04-09 11:25:12 +01:00
import sanitizeHtml from 'sanitize-html' ;
import { getCollection } from 'astro:content' ;
2022-10-07 09:14:10 +01:00
2023-04-09 11:25:12 +01:00
export async function get ( ) {
const emails = await getCollection ( 'daily-email' ) ;
2022-10-13 00:15:24 +01:00
2023-04-09 11:25:12 +01:00
const sortedEmails = Object . values ( emails )
. sort ( ( a , b ) =>
new Date ( b . data . pubDate ) . valueOf ( ) -
new Date ( a . data . pubDate ) . valueOf ( )
) ;
const parser = new MarkdownIt ( ) ;
2023-05-02 00:33:10 +01:00
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 don’ t know what’ s 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.' ,
2023-04-17 23:05:00 +01:00
'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 ) ;
}
2023-04-09 11:25:12 +01:00
return rss ( {
2022-10-13 08:40:06 +01:00
title : 'Daily email list' ,
2022-10-13 00:15:24 +01:00
description : 'A daily newsletter on software development, DevOps, community, and open-source.' ,
site : import . meta . env . SITE ,
2023-04-09 11:25:12 +01:00
items : sortedEmails . slice ( 0 , 1 ) . map ( ( email ) => ( {
2023-04-17 23:05:00 +01:00
description : `
< div style = "max-width: 550px;" >
$ { sanitizeHtml ( parser . render ( email . body ) ) }
2023-05-02 00:33:10 +01:00
$ { callToAction ( email . body ) }
2023-04-17 23:05:00 +01:00
< / d i v >
` ,
2023-04-09 11:25:12 +01:00
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 >
2023-08-05 10:03:47 +01:00
# dev $ { email . data . tags . map ( tag => ` # ${ convertTag ( tag ) } ` ) . join ( ' ' ) }
2023-04-19 23:15:39 +01:00
< / t a g s > ` ,
2022-10-13 00:15:24 +01:00
} ) )
} ) ;
2023-04-09 11:25:12 +01:00
} ;