Markdown and Subreddit Sidebars
This commit is contained in:
23
src/post.rs
23
src/post.rs
@ -6,7 +6,6 @@ use async_recursion::async_recursion;
|
||||
|
||||
use askama::Template;
|
||||
use chrono::{TimeZone, Utc};
|
||||
use pulldown_cmark::{html, Options, Parser};
|
||||
|
||||
// STRUCTS
|
||||
#[derive(Template)]
|
||||
@ -27,8 +26,8 @@ async fn render(id: String, sort: Option<String>, comment_id: Option<String>) ->
|
||||
|
||||
// Build the Reddit JSON API url
|
||||
let url: String = match comment_id {
|
||||
None => format!("{}.json?sort={}", id, sorting),
|
||||
Some(val) => format!("{}.json?sort={}&comment={}", id, sorting, val),
|
||||
None => format!("{}.json?sort={}&raw_json=1", id, sorting),
|
||||
Some(val) => format!("{}.json?sort={}&comment={}&raw_json=1", id, sorting, val),
|
||||
};
|
||||
|
||||
// Send a request to the url, receive JSON in response
|
||||
@ -95,20 +94,6 @@ async fn media(data: &serde_json::Value) -> (String, String) {
|
||||
(post_type.to_string(), url)
|
||||
}
|
||||
|
||||
async fn markdown_to_html(md: &str) -> String {
|
||||
let mut options = Options::empty();
|
||||
options.insert(Options::ENABLE_TABLES);
|
||||
options.insert(Options::ENABLE_FOOTNOTES);
|
||||
options.insert(Options::ENABLE_STRIKETHROUGH);
|
||||
options.insert(Options::ENABLE_TASKLISTS);
|
||||
let parser = Parser::new_ext(md, options);
|
||||
|
||||
// Write to String buffer.
|
||||
let mut html_output = String::new();
|
||||
html::push_html(&mut html_output, parser);
|
||||
html_output
|
||||
}
|
||||
|
||||
// POSTS
|
||||
async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
|
||||
// Retrieve post (as opposed to comments) from JSON
|
||||
@ -126,7 +111,7 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
|
||||
let post = Post {
|
||||
title: val(post_data, "title").await,
|
||||
community: val(post_data, "subreddit").await,
|
||||
body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap()).await,
|
||||
body: val(post_data,"selftext_html").await,
|
||||
author: val(post_data, "author").await,
|
||||
author_flair: Flair(
|
||||
val(post_data, "author_flair_text").await,
|
||||
@ -169,7 +154,7 @@ async fn parse_comments(json: serde_json::Value) -> Result<Vec<Comment>, &'stati
|
||||
}
|
||||
|
||||
let score = comment["data"]["score"].as_i64().unwrap_or(0);
|
||||
let body = markdown_to_html(comment["data"]["body"].as_str().unwrap_or("")).await;
|
||||
let body = val(comment, "body_html").await;
|
||||
|
||||
let replies: Vec<Comment> = if comment["data"]["replies"].is_object() {
|
||||
parse_comments(comment["data"]["replies"].clone()).await.unwrap_or(Vec::new())
|
||||
|
@ -36,14 +36,7 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
|
||||
let sub_result = if !&sub_name.contains("+") {
|
||||
subreddit(&sub_name).await
|
||||
} else {
|
||||
Ok(Subreddit {
|
||||
name: String::new(),
|
||||
title: String::new(),
|
||||
description: String::new(),
|
||||
icon: String::new(),
|
||||
members: String::new(),
|
||||
active: String::new(),
|
||||
})
|
||||
Ok(Subreddit::default())
|
||||
};
|
||||
let items_result = fetch_posts(url, String::new()).await;
|
||||
|
||||
@ -73,7 +66,7 @@ pub async fn render(sub_name: String, sort: Option<String>, ends: (Option<String
|
||||
// SUBREDDIT
|
||||
async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
|
||||
// Build the Reddit JSON API url
|
||||
let url: String = format!("r/{}/about.json", sub);
|
||||
let url: String = format!("r/{}/about.json?raw_json=1", sub);
|
||||
|
||||
// Send a request to the url, receive JSON in response
|
||||
let req = request(url).await;
|
||||
@ -102,6 +95,7 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
|
||||
name: val(&res, "display_name").await,
|
||||
title: val(&res, "title").await,
|
||||
description: val(&res, "public_description").await,
|
||||
info: val(&res, "description_html").await.replace("\\", ""),
|
||||
icon: format_url(icon).await,
|
||||
members: format_num(members.try_into().unwrap()),
|
||||
active: format_num(active.try_into().unwrap()),
|
||||
|
@ -21,10 +21,10 @@ async fn render(username: String, sort: Option<String>, ends: (Option<String>, O
|
||||
|
||||
// Build the Reddit JSON API url
|
||||
let url = match ends.0 {
|
||||
Some(val) => format!("user/{}/.json?sort={}&before={}&count=25", username, sorting, val),
|
||||
Some(val) => format!("user/{}/.json?sort={}&before={}&count=25&raw_json=1", username, sorting, val),
|
||||
None => match ends.1 {
|
||||
Some(val) => format!("user/{}/.json?sort={}&after={}&count=25", username, sorting, val),
|
||||
None => format!("user/{}/.json?sort={}", username, sorting),
|
||||
Some(val) => format!("user/{}/.json?sort={}&after={}&count=25&raw_json=1", username, sorting, val),
|
||||
None => format!("user/{}/.json?sort={}&raw_json=1", username, sorting),
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -51,11 +51,13 @@ pub struct User {
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
// Subreddit struct containing metadata about community
|
||||
pub struct Subreddit {
|
||||
pub name: String,
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub info: String,
|
||||
pub icon: String,
|
||||
pub members: String,
|
||||
pub active: String,
|
||||
@ -149,7 +151,7 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
|
||||
posts.push(Post {
|
||||
title: if title.is_empty() { fallback_title.to_owned() } else { title },
|
||||
community: val(post, "subreddit").await,
|
||||
body: val(post, "body").await,
|
||||
body: val(post, "body_html").await,
|
||||
author: val(post, "author").await,
|
||||
author_flair: Flair(
|
||||
val(post, "author_flair_text").await,
|
||||
|
Reference in New Issue
Block a user