Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RSS feed #379

Merged
merged 8 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export default defineConfig({
base: "",
integrations: [mdx(), solidJs()],
output: "static",
site: "https://maplibre.org/",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noting that this is also needed in a few places by the RSS feed, could look at another approach if you'd prefer.

});
42 changes: 41 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
},
"dependencies": {
"@astrojs/mdx": "^4.0.2",
"@astrojs/rss": "^4.0.11",
"@astrojs/solid-js": "^5.0.0",
"@fontsource/alata": "^5.1.1",
"@igor.dvlpr/astro-post-excerpt": "^3.0.4",
"astro": "^5.1.0",
"bootstrap": "^5.3.3",
"maplibre-gl": "^5.0.0",
"sass": "1.76.0",
"solid-js": "^1.9.3"
"solid-js": "^1.9.3",
"ultrahtml": "^1.5.3"
},
"devDependencies": {
"@types/node": "^22.10.2",
Expand Down
11 changes: 11 additions & 0 deletions public/img/icon/rss.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/components/Footer.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import { Image } from "astro:assets";
import maplibreLogo from "../../public/img/maplibre-logo.svg";
import rssIcon from "../../public/img/icon/rss.svg";
import bskyIcon from "../../public/img/icon/bsky.svg";
import mstdnIcon from "../../public/img/icon/mstdn.svg";
import xIcon from "../../public/img/icon/x.svg";
Expand All @@ -21,6 +22,13 @@ import ghIcon from "../../public/img/icon/gh.svg";

<div class="col-8">
<div class="nav justify-content-end">
<a
class="nav-link text-white"
href={new URL("news/index.xml", Astro.site)}
aria-label="MapLibre RSS Feed"
>
<Image src={rssIcon} alt="RSS icon" height={16} width={16} />
</a>
<a
class="nav-link text-white"
href="https://bsky.app/profile/maplibre.org"
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const SITE_TITLE = "MapLibre";
export const SITE_DESCRIPTION =
"The MapLibre Organization is an umbrella for open-source mapping libraries.";
Comment on lines +1 to +3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this on src/pages/index.astro and didn't want to repeat it elsewhere since it's also needed on the RSS feed. I moved the string here. Feel free to move somewhere else or do it differently.

10 changes: 9 additions & 1 deletion src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import "../styles/bootstrap.scss";
import Footer from "../components/Footer.astro";
import Nav from "../components/Nav.astro";
import { SITE_TITLE } from "../constants";
const { title, description } = Astro.props;
// import { ClientRouter } from 'astro:transitions';
---
Expand Down Expand Up @@ -70,8 +71,15 @@ const { title, description } = Astro.props;
crossorigin
/>

<link
rel="alternate"
type="application/rss+xml"
title={SITE_TITLE}
href={new URL("news/index.xml", Astro.site)}
/>

<title>
{title ? `${title} | MapLibre` : "MapLibre"}
{title ? `${title} | ${SITE_TITLE}` : SITE_TITLE}
</title>
</head>
<body>
Expand Down
8 changes: 2 additions & 6 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { Image } from "astro:assets";
import maplibreLogo from "../../public/img/maplibre-logo.svg";
import Layout from "../layouts/Layout.astro";
import { LandingPageMap } from "../components/LandingPageMap";

const title = "MapLibre";
const description =
"The MapLibre Organization is an umbrella for open-source mapping libraries.";
import { SITE_DESCRIPTION } from "../constants";
---

<Layout>
Expand All @@ -27,8 +24,7 @@ const description =
<p
style="word-break:normal; margin-top:30px; max-width: 800px;"
>
The MapLibre Organization is an umbrella for open-source
mapping libraries.
{SITE_DESCRIPTION}
</p>

<!-- <p>Open-source map libraries for web and mobile.</p> -->
Expand Down
87 changes: 87 additions & 0 deletions src/pages/news/[slug].xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { APIContext } from "astro";
import rss, { type RSSFeedItem } from "@astrojs/rss";
import {
getCollection,
getEntry,
render,
type CollectionEntry,
} from "astro:content";
import { experimental_AstroContainer as AstroContainer } from "astro/container";
import mdxRenderer from "@astrojs/mdx/server.js";
import solidRenderer from "@astrojs/solid-js/server.js";

import { transform, walk } from "ultrahtml";
import sanitize from "ultrahtml/transformers/sanitize";

import { SITE_TITLE, SITE_DESCRIPTION } from "../../constants";

// need to make this endpoint dynamic with only one path
// otherwise the astro file will take precedence
export function getStaticPaths() {
return [{ params: { slug: "index" } }];
}

const container = await AstroContainer.create({});
container.addServerRenderer({ renderer: mdxRenderer, name: "mdx" });
container.addServerRenderer({ renderer: solidRenderer, name: "solid" });

const getAuthors = async (authors: string[]): Promise<string> => {
const authorEntries = await Promise.all(
authors.map(async (author) => await getEntry("authors", author)),
);

const authorTitles = authorEntries
.map((author) => author?.data?.title)
.filter(Boolean);

if (authorTitles.length > 1) {
const lastAuthor = authorTitles.pop();
return `${authorTitles.join(", ")} and ${lastAuthor}`;
}

return authorTitles.join(", ");
};

const getPost = async (
context: APIContext,
post: CollectionEntry<"news">,
): Promise<RSSFeedItem> => {
const content = await transform(
await container.renderToString((await render(post)).Content),
[
async (node) => {
await walk(node, async (node) => {
if (node.name === "img" && node.attributes.src?.startsWith("/")) {
node.attributes.src = context.site + node.attributes.src.slice(1);
}
});
return node;
},
sanitize({
dropElements: ["script", "style"],
}),
],
);

return {
title: post.data.title,
pubDate: post.data.date,
description: post.data.description,
author: await getAuthors(post.data.authors),
content,
link: `/news/${post.id}/`,
};
};

export async function GET(context: APIContext) {
const news = await getCollection("news");
const items = news.sort((a, b) => +b.data.date - +a.data.date).slice(0, 15);

return rss({
title: SITE_TITLE,
description: SITE_DESCRIPTION,
site: context.site || "",
items: await Promise.all(items.map(async (post) => getPost(context, post))),
customData: `<language>en-US</language><lastBuildDate>${new Date().toUTCString()}</lastBuildDate>`,
});
}
26 changes: 25 additions & 1 deletion src/pages/news/index.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
import { getCollection } from "astro:content";
import { Image } from "astro:assets";
import Layout from "../../layouts/Layout.astro";
import { render } from "astro:content";
import TitleHeader from "../../components/TitleHeader.astro";
import PostExcerpt from "@igor.dvlpr/astro-post-excerpt";
import iconRss from "../../../public/img/icon/rss.svg";

const posts = (await getCollection("news")).sort((a, b) => {
console.log(b.data);
Expand All @@ -26,10 +28,32 @@ const posts = (await getCollection("news")).sort((a, b) => {
}
}
}

.badge {
display: block;
width: max-content;
margin: auto;
margin-top: 12px;
padding-bottom: 3px;

:global(img) {
top: -2px;
position: relative;
}
}
</style>

<Layout title="News">
<TitleHeader>News</TitleHeader>
<TitleHeader>
News
<span class="badge bg-primary text-white">
<a href={new URL("news/index.xml", Astro.site)}>
<Image src={iconRss} alt="MapLibre RSS feed" height={16} width={16} />
RSS
</a>
</span>
</TitleHeader>

<div class="container">
<div class="row justify-content-center">
<div class="col-md-10 col-lg-8">
Expand Down
Loading