Skip to content

Commit

Permalink
refactor: add support for github repository name in markdown render
Browse files Browse the repository at this point in the history
  • Loading branch information
Kremilly committed Aug 16, 2024
1 parent 5f26362 commit 50f9ecd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/consts/uris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ pub struct Uris;

impl Uris {

pub const PROVIDERS_DOMAINS: [&'static str; 2] = [
pub const PROVIDERS_DOMAINS: [&'static str; 3] = [
"wikipedia.org",
"wikisource.org",
"raw.githubusercontent.com",
];

pub const WIKIPEDIA_API_REQUEST_PDF: &'static str = "https://en.wikipedia.org/api/rest_v1/page/pdf/";
Expand Down
10 changes: 5 additions & 5 deletions src/system/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use pulldown_cmark::{
};

use crate::{
system::pdf::Pdf,
system::pdf::Pdf,
cmd::tasks::Tasks,
configs::settings::Settings,
generator::generate::Generate,
ui::success_alerts::SuccessAlerts,
ui::success_alerts::SuccessAlerts,

render::{
render_io::RenderIO,
Expand All @@ -25,10 +25,10 @@ use crate::{
},

utils::{
url::UrlMisc,
remote::Remote,
file::FileUtils,
}
filename::FileName,
},
};

pub struct Markdown;
Expand Down Expand Up @@ -93,7 +93,7 @@ impl Markdown {
let html_content = Self::render(url).await?;
let content = RenderInject::html_content(contents, html_content).await?;

let original_name = UrlMisc::get_last_part(url);
let original_name = FileName::get_final_name(url);
let new_filename = FileUtils::replace_extension(&original_name, "pdf");
let output_path = FileUtils::get_output_path(&path, &new_filename);
let output_path_str = format!("{}{}", &path, &new_filename);
Expand Down
29 changes: 29 additions & 0 deletions src/utils/filename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{
consts::uris::Uris,

utils::{
url::UrlMisc,
domains::Domains,
},
};

pub struct FileName;

impl FileName {

fn github_repo_name(url: &str) -> String {
let segments: Vec<&str> = url.split('/').collect();
let repo_name = segments[segments.len() - 3];
repo_name.to_string()
}

pub fn get_final_name(url: &str) -> String {
let domain = Domains::get(url);

match domain.as_str() {
domain if domain == Uris::PROVIDERS_DOMAINS[2] => Self::github_repo_name(url),
_ => UrlMisc::get_last_part(url),
}
}

}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod file;
pub mod remote;
pub mod base64;
pub mod domains;
pub mod filename;
pub mod validation;

0 comments on commit 50f9ecd

Please sign in to comment.