Add RSS/Atom feeds

master
kirbylife 2023-12-10 22:53:43 -06:00
parent a0e447cf19
commit e1678fb679
4 changed files with 97 additions and 2 deletions

View File

@ -19,6 +19,7 @@ use comrak::{markdown_to_html, ComrakOptions};
use controllers::posts; use controllers::posts;
use dotenv::dotenv; use dotenv::dotenv;
use misc::get_context; use misc::get_context;
use rocket::response::content;
use rocket::response::NamedFile; use rocket::response::NamedFile;
use rocket::Request; use rocket::Request;
use rocket_contrib::serve::StaticFiles; use rocket_contrib::serve::StaticFiles;
@ -42,6 +43,52 @@ fn index(page: Option<u64>) -> Template {
Template::render("index", context) Template::render("index", context)
} }
#[get("/feed.xml")]
fn rss_feed() -> content::Xml<Template> {
let mut context = get_context();
let (mut posts, _) = posts::get_posts(None);
let comrak_options = ComrakOptions {
ext_table: true,
ext_autolink: true,
ext_tasklist: true,
unsafe_: true,
..ComrakOptions::default()
};
posts.iter_mut().for_each(|post| {
let content = markdown_to_html(&post.content, &comrak_options);
post.content = content;
});
context.insert("posts", &posts);
content::Xml(Template::render("rss", context))
}
#[get("/atom.xml")]
fn atom_feed() -> content::Xml<Template> {
let mut context = get_context();
let (mut posts, _) = posts::get_posts(None);
let comrak_options = ComrakOptions {
ext_table: true,
ext_autolink: true,
ext_tasklist: true,
unsafe_: true,
..ComrakOptions::default()
};
posts.iter_mut().for_each(|post| {
let content = markdown_to_html(&post.content, &comrak_options);
post.content = content;
});
context.insert("posts", &posts);
content::Xml(Template::render("atom", context))
}
#[get("/post/<title>")] #[get("/post/<title>")]
fn show_post(title: String) -> Template { fn show_post(title: String) -> Template {
let mut context = get_context(); let mut context = get_context();
@ -93,7 +140,7 @@ fn main() {
rocket::ignite() rocket::ignite()
.attach(Template::fairing()) .attach(Template::fairing())
.mount("/", routes![index, show_post, favicon]) .mount("/", routes![index, show_post, favicon, rss_feed, atom_feed])
.mount("/admin", admin::get_routes()) .mount("/admin", admin::get_routes())
.mount("/static", StaticFiles::from("static")) .mount("/static", StaticFiles::from("static"))
.register(catchers![not_found_404]) .register(catchers![not_found_404])

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Código Comentado</title>
<link href="https://blog.kirbylife.dev" rel="alternate" />
<link href="https://blog.kirbylife.dev/atom.xml" rel="self" />
<id>"https://blog.kirbylife.dev"</id>
<subtitle>Blog personal de kirbylife</subtitle>
<updated>{{ posts.0.created_at | date(format="%Y-%m-%d") }}</updated>
<author>
<name>kirbylife</name>
<email>hola@kirbylife.dev</email>
</author>
{%- for post in posts %}
<entry>
<title>{{ post.title }}</title>
<link>https://blog.kirbylife.dev/post/{{ post.title | slugify }}-{{ post.id }}</link>
<published>{{ post.created_at | date(format="%Y-%m-%d") }}</published>
<updated>{{ post.created_at | date(format="%Y-%m-%d") }}</updated>
<author>
<name>kirbylife</name>
<email>hola@kirbylife.dev</email>
</author>
<id>tag:blog.kirbylife.dev,{{ post.created_at | date(format="%Y-%m-%d") }}:/post/{{ post.id }}</id>
<content type="html">{{ post.content }}</content>
</entry>
{%- endfor %}
</feed>

View File

@ -38,7 +38,7 @@
</div> </div>
<footer> <footer>
<span> <span>
Hecho con Rust, Rocket y Emacs por <a href="https://kirbylife.gitlab.io">@kirbylife</a> Hecho con Rust y Rocket por <a href="https://kirbylife.dev">@kirbylife</a> - <a href="/feed.xml">RSS</a> - <a href="/atom.xml">Atom</a>
</span> </span>
</footer> </footer>
{% block extrajs %} {% block extrajs %}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Código Comentado</title>
<link>https://blog.kirbylife.dev</link>
<description>Blog personal de kirbylife</description>
<language>es-mx</language>
<webMaster>https://kirbylife.dev</webMaster>
<managingEditor>https://kirbylife.dev</managingEditor>
<generator>Rocket</generator>
{%- for post in posts %}
<item>
<title>{{ post.title }}</title>
<pubDate>{{ post.created_at | date(format="%Y-%m-%d") }}</pubDate>
<link>https://blog.kirbylife.dev/post/{{ post.title | slugify }}-{{ post.id }}</link>
<guid isPermaLink="true">https://blog.kirbylife.dev/post/{{ post.id }}</guid>
<description>{{ post.content }}</description>
</item>
{%- endfor %}
</channel>
</rss>