Skip to content

Commit

Permalink
feat(latest-news): fetch metadata from Blog articles + update list (#…
Browse files Browse the repository at this point in the history
…10614)

* chore(latest-news): stop fetching Hacks feed

* feat(latest-news): fetch metadata from Blog articles

* chore(latest-news): update Blog articles

* fix(latest-news): filter empty items (when BLOG_ROOT is not set)

* chore(latest-news): keep Playground blog post
  • Loading branch information
caugner authored Mar 5, 2024
1 parent ed9cbf3 commit 734c37c
Showing 1 changed file with 35 additions and 83 deletions.
118 changes: 35 additions & 83 deletions build/spas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

import cheerio from "cheerio";
import frontmatter from "front-matter";
import { fdir, PathsOutput } from "fdir";
import got from "got";
Expand Down Expand Up @@ -38,6 +37,12 @@ const FEATURED_ARTICLES = [
"docs/Web/CSS/color_value",
];

const LATEST_NEWS: (NewsItem | string)[] = [
"blog/mdn-curriculum-launch/",
"blog/baseline-evolution-on-mdn/",
"blog/introducing-the-mdn-playground/",
];

const contributorSpotlightRoot = CONTRIBUTOR_SPOTLIGHT_ROOT;

async function buildContributorSpotlight(
Expand Down Expand Up @@ -419,90 +424,37 @@ async function fetchRecentContributions() {
}

async function fetchLatestNews() {
const items: NewsItem[] = [];

items.push(
{
title: "Responsibly empowering developers with AI on MDN",
url: `https://blog.mozilla.org/en/products/mdn/responsibly-empowering-developers-with-ai-on-mdn/`,
author: "Steve Teixeira",
published_at: new Date("Thu, 06 Jul 2023 14:41:20 +0000").toString(),
source: {
name: "blog.mozilla.org",
url: `https://blog.mozilla.org/en/latest/`,
},
},
{
title: "Introducing AI Help: Your Trusted Companion for Web Development",
url: `/${DEFAULT_LOCALE}/blog/introducing-ai-help/`,
author: "Hermina Condei",
published_at: new Date("2023-06-27").toString(),
source: {
name: "developer.mozilla.org",
url: `/${DEFAULT_LOCALE}/blog/`,
},
},
{
title: "Introducing the MDN Playground: Bring your code to life!",
url: `/${DEFAULT_LOCALE}/blog/introducing-the-mdn-playground/`,
author: "Florian Dieminger",
published_at: new Date("2023-06-22").toString(),
source: {
name: "developer.mozilla.org",
url: `/${DEFAULT_LOCALE}/blog/`,
},
},
{
title: "Introducing Baseline: a unified view of stable web features",
url: `/${DEFAULT_LOCALE}/blog/baseline-unified-view-stable-web-features/`,
author: "Hermina Condei",
published_at: new Date("2023-05-10").toString(),
source: {
name: "developer.mozilla.org",
url: `/${DEFAULT_LOCALE}/blog/`,
},
},
...(await fetchHacksNews())
);
const items: NewsItem[] = (
await Promise.all(
LATEST_NEWS.map(async (itemOrUrl) => {
if (typeof itemOrUrl !== "string") {
return itemOrUrl;
}
const url = itemOrUrl;
const post = await findPostBySlug(
getSlugByBlogPostUrl(`/${DEFAULT_LOCALE}/${url}`)
);
if (post) {
const {
doc: { title },
blogMeta: { author, date, slug },
} = post;
return {
title,
url: `/${DEFAULT_LOCALE}/blog/${slug}/`,
author: author?.name || "The MDN Team",
published_at: new Date(date).toString(),
source: {
name: "developer.mozilla.org",
url: `/${DEFAULT_LOCALE}/blog/`,
},
};
}
})
)
).filter(Boolean);

return {
items,
};
}

async function fetchHacksNews(): Promise<NewsItem[]> {
try {
const xml = await got(
"https://hacks.mozilla.org/category/mdn/feed/"
).text();

const $ = cheerio.load(xml, { xmlMode: true });

const items: NewsItem[] = [];
$("item").each((i, item) => {
const $item = $(item);

items.push({
title: $item.find("title").text(),
url: $item.find("guid").text(),
author: $item.find("dc\\:creator").text(),
published_at: $item.find("pubDate").text(),
source: {
name: "hacks.mozilla.org",
url: "https://hacks.mozilla.org/category/mdn/",
},
});
});

return items;
} catch (e) {
const msg = "Couldn't fetch hacks.mozilla.org feed!";
if (!DEV_MODE) {
console.error(`Error: ${msg}`);
throw e;
}

console.warn(`Warning: ${msg}`);
return [];
}
}

0 comments on commit 734c37c

Please sign in to comment.