From 9053debd771856ea9621508913f7e63c6fd19a74 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 28 Nov 2024 00:36:58 +0100 Subject: [PATCH] Internal link to another local document support --- src/routes/cours.rs | 2 +- src/utils/markdown.rs | 17 ++++++++++++++--- src/utils/metadata.rs | 3 ++- src/utils/routes/blog.rs | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/routes/cours.rs b/src/routes/cours.rs index f4a934e..4c8c7f0 100644 --- a/src/routes/cours.rs +++ b/src/routes/cours.rs @@ -54,7 +54,7 @@ fn get_content( return None; } - read_file(format!("{cours_dir}/{filename}"), MType::Generic) + read_file(format!("{cours_dir}/{filename}"), MType::Cours) } fn build_page(info: &web::Query, config: Config) -> String { diff --git a/src/utils/markdown.rs b/src/utils/markdown.rs index cdfd12d..ddb0cb8 100644 --- a/src/utils/markdown.rs +++ b/src/utils/markdown.rs @@ -8,6 +8,7 @@ use ramhorns::Content; use std::fmt::Debug; use std::fs; use std::path::Path; +use std::sync::Arc; use crate::utils::metadata::MType; @@ -21,7 +22,7 @@ pub struct File { } /// Options used for parser and compiler MD --> HTML -pub fn get_options() -> ComrakOptions { +pub fn get_options(metadata_type: MType) -> ComrakOptions { comrak::Options { extension: comrak::ExtensionOptions::builder() .strikethrough(true) @@ -37,6 +38,16 @@ pub fn get_options() -> ComrakOptions { .multiline_block_quotes(true) .math_dollars(true) .underline(true) + .maybe_link_url_rewriter(match metadata_type { + MType::Cours => Some(Arc::new(|url: &str| { + if url.contains("://") { + return String::from(url); + } + + format!("/cours?q={url}") + })), + _ => None, + }) .build(), parse: comrak::ParseOptions::builder() .smart(true) @@ -134,7 +145,7 @@ fn fix_images_and_integration( if let Ok(file) = fs::read_to_string(&img_path) { let mime = mime_guess::from_path(&img_path).first_or_octet_stream(); if recursive && mime == "text/markdown" { - let mut options = get_options(); + let mut options = get_options(metadata_type); options.extension.footnotes = false; let data = read_md(&img_path, &file, metadata_type, Some(options), false); @@ -174,7 +185,7 @@ pub fn read_md( ) -> File { let arena = Arena::new(); - let mut opt = options.map_or_else(get_options, |specific_opt| specific_opt); + let mut opt = options.map_or_else(|| get_options(metadata_type), |specific_opt| specific_opt); let root = parse_document(&arena, raw_text, &opt); // Find metadata diff --git a/src/utils/metadata.rs b/src/utils/metadata.rs index 80ba9c5..d287bc2 100644 --- a/src/utils/metadata.rs +++ b/src/utils/metadata.rs @@ -74,6 +74,7 @@ pub struct FileMetadataPortfolio { pub enum MType { Blog, Contact, + Cours, Generic, Index, Portfolio, @@ -140,7 +141,7 @@ pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile { ..MFile::default() } } - MType::Generic => MFile { + MType::Generic | MType::Cours => MFile { hardbreaks: deserialize_metadata(text), ..MFile::default() }, diff --git a/src/utils/routes/blog.rs b/src/utils/routes/blog.rs index 8ea63b8..9e63a67 100644 --- a/src/utils/routes/blog.rs +++ b/src/utils/routes/blog.rs @@ -79,7 +79,7 @@ pub fn get_posts(location: &str) -> Vec { |text| { let arena = Arena::new(); - let options = get_options(); + let options = get_options(MType::Generic); let root = parse_document(&arena, &text, &options); let mut metadata = get(root, MType::Blog).blog.unwrap();