Compare commits

..

3 Commits

Author SHA1 Message Date
kirbylife bbba1ead64 Add visitor counter 2024-06-01 01:26:37 -06:00
kirbylife b016fc5bab Add header ids 2024-06-01 01:00:42 -06:00
kirbylife 64d09e0061 Update some dependencies 2024-05-31 00:54:09 -06:00
5 changed files with 37 additions and 30 deletions

View File

@ -1,16 +1,16 @@
[package] [package]
name = "codigocomentado" name = "codigocomentado"
version = "0.1.0" version = "0.1.0"
authors = ["kirbylife <gabriel13m@gmail.com>"] authors = ["kirbylife <kirbylife@protonmail.com>"]
edition = "2021" edition = "2021"
[dependencies] [dependencies]
rocket = { version = "0.5.0", features = ["secrets"] } rocket = { version = "0.5.0", features = ["secrets"] }
rocket_dyn_templates = { version = "0.1.0", features = ["tera"] } rocket_dyn_templates = { version = "0.1.0", features = ["tera"] }
diesel = { version = "1.0.0", features = ["postgres", "chrono"] } diesel = { version = "1.0.0", features = ["postgres", "chrono"] }
dotenv = "0.9.0" dotenv = "0.15.0"
rand = "0.6.0" rand = "0.8.5"
chrono = { version = "0.4.9", features = ["serde"] } chrono = { version = "0.4.9", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
comrak = "0.6" comrak = "0.24.1"
tera = "1.19.1" tera = "1.19.1"

View File

@ -79,4 +79,14 @@ pub mod posts {
)) ))
.get_result(&connection) .get_result(&connection)
} }
pub fn add_visit(post_id: i32) {
use crate::schema::posts::dsl::*;
let connection = establish_connection();
diesel::update(posts.filter(id.eq(post_id)))
.set(views.eq(views + 1))
.execute(&connection)
.unwrap();
}
} }

View File

@ -10,7 +10,7 @@ extern crate rocket;
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
use comrak::{markdown_to_html, ComrakOptions}; use comrak::{markdown_to_html, Options};
use controllers::posts; use controllers::posts;
use dotenv::dotenv; use dotenv::dotenv;
use rocket::fs::{FileServer, NamedFile}; use rocket::fs::{FileServer, NamedFile};
@ -35,13 +35,11 @@ fn index(page: Option<u64>) -> Template {
fn rss_feed() -> RawXml<Template> { fn rss_feed() -> RawXml<Template> {
let (mut posts, _) = posts::get_posts(None); let (mut posts, _) = posts::get_posts(None);
let comrak_options = ComrakOptions { let mut comrak_options = Options::default();
ext_table: true, comrak_options.extension.table = true;
ext_autolink: true, comrak_options.extension.autolink = true;
ext_tasklist: true, comrak_options.extension.tasklist = true;
unsafe_: true, comrak_options.render.unsafe_ = true;
..ComrakOptions::default()
};
posts.iter_mut().for_each(|post| { posts.iter_mut().for_each(|post| {
let content = markdown_to_html(&post.content, &comrak_options); let content = markdown_to_html(&post.content, &comrak_options);
@ -57,13 +55,11 @@ fn rss_feed() -> RawXml<Template> {
fn atom_feed() -> RawXml<Template> { fn atom_feed() -> RawXml<Template> {
let (mut posts, _) = posts::get_posts(None); let (mut posts, _) = posts::get_posts(None);
let comrak_options = ComrakOptions { let mut comrak_options = Options::default();
ext_table: true, comrak_options.extension.table = true;
ext_autolink: true, comrak_options.extension.autolink = true;
ext_tasklist: true, comrak_options.extension.tasklist = true;
unsafe_: true, comrak_options.render.unsafe_ = true;
..ComrakOptions::default()
};
posts.iter_mut().for_each(|post| { posts.iter_mut().for_each(|post| {
let content = markdown_to_html(&post.content, &comrak_options); let content = markdown_to_html(&post.content, &comrak_options);
@ -82,13 +78,14 @@ fn show_post(title: String) -> Template {
match posts::get_post(id) { match posts::get_post(id) {
Ok(mut post) => { Ok(mut post) => {
let comrak_options = ComrakOptions { posts::add_visit(id);
ext_table: true,
ext_autolink: true, let mut comrak_options = Options::default();
ext_tasklist: true, comrak_options.extension.table = true;
unsafe_: true, comrak_options.extension.autolink = true;
..ComrakOptions::default() comrak_options.extension.tasklist = true;
}; comrak_options.render.unsafe_ = true;
comrak_options.extension.header_ids = Some("id-".to_string());
let content = markdown_to_html(&post.content, &comrak_options); let content = markdown_to_html(&post.content, &comrak_options);
post.content = content; post.content = content;

View File

@ -16,7 +16,7 @@ const TITLES: [&str; 11] = [
pub fn gen_title() -> String { pub fn gen_title() -> String {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; let title_fmt = TITLES[rng.gen_range(0..TITLES.len())];
let title = str::replace(title_fmt, "{}", "CódigoComentado"); let title = str::replace(title_fmt, "{}", "CódigoComentado");
title.to_string() title.to_string()
} }

View File

@ -43,13 +43,13 @@
{% if loop.index is odd %} {% if loop.index is odd %}
<p class="colored">{{ post.id }}</p> <p class="colored">{{ post.id }}</p>
<p class="colored"><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p> <p class="colored"><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p>
<p class="colored center">0</p> <p class="colored center">{{ post.views }}</p>
<p class="colored center">{{ post.published }}</p> <p class="colored center">{{ post.published }}</p>
<p class="colored"><a href="/admin/edit_post/{{ post.id }}">Editar</a></p> <p class="colored"><a href="/admin/edit_post/{{ post.id }}">Editar</a></p>
{% else %} {% else %}
<p>{{ post.id }}</p> <p>{{ post.id }}</p>
<p><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p> <p><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p>
<p class="center">0</p> <p class="center">{{ post.views }}</p>
<p class="center">{{ post.published }}</p> <p class="center">{{ post.published }}</p>
<p><a href="/admin/edit_post/{{ post.id }}">Editar</a></p> <p><a href="/admin/edit_post/{{ post.id }}">Editar</a></p>
{% endif %} {% endif %}