blog_tools/types.rs
1use std::path::Path;
2
3use chrono::NaiveDate;
4use markdown::mdast::Node;
5
6use crate::common::BlogError;
7
8// TODO: give these lifetimes so we don't need to clone
9/// Primary trait that describes a single blog post. Any struct which derives
10/// this is intended to be converted into a JSON with serde and used in a template.
11/// You can of course use any of these other methods for any purpose in the
12/// blog
13pub trait Blog: Clone {
14 /// Create a blog post
15 ///
16 /// Parameters
17 ///
18 /// - `blog`: Path to the root of the blog
19 /// For this to work, you should have a folder, for instance, `blog` which serves
20 /// as the root. Within this folder, you must have the following structure
21 ///
22 /// - blog
23 /// - 2023
24 /// - 2023-01-01
25 /// - my_first_blog.json
26 /// - my_first_blog.md
27 /// - (other folders)
28 ///
29 /// - `toc_generation_func` - A function which parses a blog and generates
30 /// a table of contents. Optional.
31 /// - `preview_chars` - number of chars to be taken in the preview of the
32 /// blog. Default is 320.
33 fn create<T: AsRef<Path>>(
34 blog: T,
35 toc_generation_func: Option<&dyn Fn(&Node) -> String>,
36 preview_chars: Option<usize>,
37 ) -> Result<Self, BlogError>;
38 /// Get the blog title
39 fn get_title(&self) -> String;
40 /// Get the original publication date
41 fn get_date_listed(&self) -> NaiveDate;
42 /// Get the SEO description
43 fn get_description(&self) -> Option<String>;
44 /// Get the HTML of the blog
45 fn get_html(&self) -> String;
46 /// Get the full slug - this would be e.g. `2024-03-19/my-blog`.
47 /// In the JSON, you should NOT include the date in the slug
48 fn get_full_slug(&self) -> String;
49 /// Get the partial slug of the blog. This would be the `slug` field from the
50 /// JSON
51 fn get_part_slug(&self) -> String;
52 /// Get a list of tags for the blog
53 fn get_tags(&self) -> Vec<String>;
54 /// Get the table of contents. Only present if a table of contents funciton
55 /// was provided
56 fn get_table_of_contents(&self) -> Option<String>;
57 /// Get keywords
58 fn get_keywords(&self) -> Option<Vec<String>>;
59 /// Get the canonicle link
60 fn get_canonicle_link(&self) -> Option<String>;
61 /// Get the author
62 fn get_author_name(&self) -> Option<String>; // TODO: Maybe support authors?
63 /// Get the author webpage
64 fn get_author_webpage(&self) -> Option<String>; // TODO: again, support multiple authors?
65 /// Get the blog preview. This is the first few hundred characters of the blog,
66 /// useful for an index page
67 fn get_preview(&self) -> String;
68 /// Get the last modified date, mostly use for sitemaps. This is not the
69 /// original publication date
70 fn get_last_modified(&self) -> Option<NaiveDate>;
71 /// Get the priority for the sitemap
72 fn get_priority(&self) -> Option<f64>;
73}