Skip to content

Commit

Permalink
fix index 0 being trimmed from blog aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDTR committed Feb 13, 2025
1 parent e5d8170 commit 1eee7ac
Showing 1 changed file with 77 additions and 59 deletions.
136 changes: 77 additions & 59 deletions src/pages/blog/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,89 @@ import Layout from "../../layouts/Layout.astro";
import BlogPostCard from "../../components/BlogPostCard.astro";
// Get regular blog posts
const blogPosts = (await getCollection("blog")).map(post => ({
...post,
collection: "blog"
const blogPosts = (await getCollection("blog")).map((post) => ({
...post,
collection: "blog",
}));
// Get harvested posts
const harvestPosts = await getCollection("harvest");
const blogPostsFile = harvestPosts.find(file => file.id === 'output/blog_posts.md');
const blogPostsFile = harvestPosts.find(
(file) => file.id === "output/blog_posts.md"
);
const harvestedContent = blogPostsFile?.body || "";
// Parse the harvested posts
const harvestedPosts = harvestedContent
.split("\n---\n")
.map(section => section.trim())
.filter(section => section && !section.startsWith("# UPL Blog Posts"))
.map(section => {
// Ensure we're working with clean lines
const lines = section.split("\n").filter(line => line.trim());
const titleMatch = lines[0].match(/## \[(.*?)\]\((.*?)\)/);
const authorMatch = lines[1].match(/\*By (.*?) on (.*?)\*/);
.split("\n---\n")
.map((section) => section.trim())
// remove the "upl blog posts" header that's in here
.map((section) => {
if (section.startsWith("# UPL Blog Posts")) {
const lines = section.split("\n");
return lines.slice(1).join("\n").trim();
}
return section;
})
// if section is empty don't return it
.filter((section) => section !== "")
.map((section) => {
// Ensure we're working with clean lines
const lines = section.split("\n").filter((line) => line.trim());
if (lines.length < 2) {
return null; // invalid section (< 2 lines)
}
const titleMatch = lines[0].match(/## \[(.*?)\]\((.*?)\)/);
const authorMatch = lines[1].match(/\*By (.*?) on (.*?)\*/);
if (!titleMatch || !authorMatch) {
console.log("Failed to parse section:", section); // Debug log
return null;
}
if (!titleMatch || !authorMatch) {
return null; // either fails
}
return {
collection: "harvest",
data: {
title: titleMatch[1],
sourceUrl: titleMatch[2],
author: authorMatch[1],
pubDate: new Date(authorMatch[2]),
description: lines.slice(2).join("\n").trim(),
}
};
})
.filter(post => post !== null);
return {
collection: "harvest",
data: {
title: titleMatch[1],
sourceUrl: titleMatch[2],
author: authorMatch[1],
pubDate: new Date(authorMatch[2]),
description: lines.slice(2).join("\n").trim(),
},
};
})
.filter((post) => post !== null);
// Create a Set of existing blog titles for deduplication
const existingTitles = new Set(blogPosts.map(post => post.data.title.toLowerCase()));
const existingTitles = new Set(
blogPosts.map((post) => post.data.title.toLowerCase())
);
// Filter out harvested posts that already exist in blog posts
const uniqueHarvestedPosts = harvestedPosts.filter(
post => !existingTitles.has(post.data.title.toLowerCase())
(post) => !existingTitles.has(post.data.title.toLowerCase())
);
// Combine and sort all posts
const posts = [...blogPosts, ...uniqueHarvestedPosts].sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()
);
const formatter = new Intl.DateTimeFormat("en-US", {
month: "long",
year: "numeric",
month: "long",
year: "numeric",
});
let previousMonthYear = "";
---

<Layout>
<div class="max-w-4xl mx-auto px-4">
<h1 class="text-4xl font-semibold font-lato text-center pb-2">Blog</h1>
<div class="text-center mb-12">
<div class="max-w-4xl mx-auto px-4">
<h1 class="text-4xl font-semibold font-lato text-center pb-2">Blog</h1>
<div class="text-center mb-12">
<p class="font-sans lg:text-lg text-md text-gray-600 font-light mb-4">
Ramblings from coords and other friends. Learn something new
about technology, and maybe even yourself.
Ramblings from coords and other friends. Learn something new about
technology, and maybe even yourself.
</p>
<a
href="https://github.com/UW-UPL/harvest/blob/main/docs/CONTRIBUTING.md"
Expand All @@ -81,26 +97,28 @@ let previousMonthYear = "";
Add your blog
</a>
</div>
<div class="space-y-8">
{posts.map((post) => {
let monthYear = null;
const currMonthYear = formatter.format(post.data.pubDate);
if (currMonthYear !== previousMonthYear) {
monthYear = (
<h2 class="text-2xl font-bold text-gray-700 mt-12 mb-6 first:mt-0">
{currMonthYear}
</h2>
);
previousMonthYear = currMonthYear;
}
<div class="space-y-8">
{
posts.map((post) => {
let monthYear = null;
const currMonthYear = formatter.format(post.data.pubDate);
if (currMonthYear !== previousMonthYear) {
monthYear = (
<h2 class="text-2xl font-bold text-gray-700 mt-12 mb-6 first:mt-0">
{currMonthYear}
</h2>
);
previousMonthYear = currMonthYear;
}

return (
<>
{monthYear}
<BlogPostCard {...post} />
</>
);
})}
</div>
</div>
return (
<>
{monthYear}
<BlogPostCard {...post} />
</>
);
})
}
</div>
</div>
</Layout>

0 comments on commit 1eee7ac

Please sign in to comment.