Use classes for headings from config file

master
Araozu 2023-06-26 21:50:07 -05:00
parent 27515360d4
commit 1da85c07f5
7 changed files with 70 additions and 8 deletions

View File

@ -11,7 +11,7 @@ Values:
String input # Path to the input folder
String output # Path to the output folder
?content: {
headings: {
String? h1 # Classes to add to h1 elements
String? h2 # Classes to add to h2 elements
String? h3 # Classes to add to h3 elements

View File

@ -11,6 +11,15 @@ pub struct Config {
pub input: String,
pub output: String,
pub template: String,
pub headings: Headings,
}
pub struct Headings {
pub h1: Option<String>,
pub h2: Option<String>,
pub h3: Option<String>,
pub h4: Option<String>,
pub h5: Option<String>,
}
/// Creates a `YAML::String` from a `&str`
@ -116,9 +125,53 @@ pub fn parse(yaml_str: &String) -> Result<Config, String> {
));
}
// Heading config
let headings_yaml = parse_headings(config_yaml.get(ystr!("headings")));
Ok(Config {
input: input_folder.clone(),
output: output_folder.clone(),
template: template_file.clone(),
headings: headings_yaml,
})
}
fn parse_headings(y: Option<&Yaml>) -> Headings {
let Some(Yaml::Hash(map)) = y
else {
return Headings {
h1: None,
h2: None,
h3: None,
h4: None,
h5: None,
}
};
let h1 = match map.get(ystr!("h1")) {
Some(Yaml::String(s)) => Some(s.clone()),
_ => None,
};
let h2 = match map.get(ystr!("h2")) {
Some(Yaml::String(s)) => Some(s.clone()),
_ => None,
};
let h3 = match map.get(ystr!("h3")) {
Some(Yaml::String(s)) => Some(s.clone()),
_ => None,
};
let h4 = match map.get(ystr!("h4")) {
Some(Yaml::String(s)) => Some(s.clone()),
_ => None,
};
let h5 = match map.get(ystr!("h5")) {
Some(Yaml::String(s)) => Some(s.clone()),
_ => None,
};
Headings { h1, h2, h3, h4, h5 }
}

View File

@ -1,6 +1,6 @@
use markdown::mdast::Emphasis;
use crate::{utils, config::Config};
use crate::{config::Config, utils};
use super::Printable;

View File

@ -1,6 +1,6 @@
use markdown::mdast::Heading;
use crate::{utils, config::Config};
use crate::{config::Config, utils};
use super::Printable;
@ -14,12 +14,21 @@ impl Printable for Heading {
let text: String = result.into_iter().collect();
let extra_classes = match self.depth {
1 => config.headings.h1.clone().unwrap_or(format!("")),
2 => config.headings.h2.clone().unwrap_or(format!("")),
3 => config.headings.h3.clone().unwrap_or(format!("")),
4 => config.headings.h4.clone().unwrap_or(format!("")),
5 => config.headings.h5.clone().unwrap_or(format!("")),
_ => String::from(""),
};
if self.depth < 4 {
let html_fragment_text = utils::to_html_fragment(&self.get_text());
format!(
"<h{} id=\"{}\" class=\"heading-linked\"><a href=\"#{}\">{}</a></h{}>",
self.depth, html_fragment_text, html_fragment_text, text, self.depth
"<h{} id=\"{}\" class=\"heading-linked {}\"><a href=\"#{}\">{}</a></h{}>",
self.depth, extra_classes, html_fragment_text, html_fragment_text, text, self.depth
)
} else {
format!("<h{}>{}</h{}>", self.depth, text, self.depth)

View File

@ -1,6 +1,6 @@
use markdown::mdast::{List, ListItem, Node};
use crate::{utils, config::Config};
use crate::{config::Config, utils};
use super::Printable;

View File

@ -1,6 +1,6 @@
use markdown::mdast::Strong;
use crate::{utils, config::Config};
use crate::{config::Config, utils};
use super::Printable;

View File

@ -1,6 +1,6 @@
use markdown::mdast::Text;
use crate::config::{Config};
use crate::config::Config;
use super::Printable;