Skip to content

Commit

Permalink
Fix relative links in emails
Browse files Browse the repository at this point in the history
  • Loading branch information
tylermercer committed Mar 4, 2025
1 parent 2944557 commit fbc30fd
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions newsletter/create-email.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ function processFeedData(feedData) {
console.log("Building email html....");

const css = sass.compile(path.join(__dirname, 'style.scss')).css;

const fixRelativeLinks = (html) => {
const $ = cheerio.load(html);

$('a[href^="/"]').each(function () {
const link = $(this);
link.attr('href', `https://tylermercer.net${link.attr('href')}`);
});

return $.html();
};

const removeFootnoteLinks = (html) => {
const $ = cheerio.load(html);
Expand All @@ -73,7 +84,7 @@ function processFeedData(feedData) {
};

const format = (html, url, title) => {
return juice(removeFootnoteLinks(`
return juice(fixRelativeLinks(removeFootnoteLinks(`
<style>
${css}
</style>
Expand All @@ -87,7 +98,7 @@ function processFeedData(feedData) {
${html}
<hr>
<p><a href="${url}#comments">Leave a comment</a></p>
</div>`));
</div>`)));
};

const htmlContent = format(latestPost.content_html, latestPost.url, latestPost.title);
Expand Down

0 comments on commit fbc30fd

Please sign in to comment.