Internal link to another local document support

This commit is contained in:
Mylloon 2024-11-28 00:36:58 +01:00
parent 533d2c1044
commit 9053debd77
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 18 additions and 6 deletions

View file

@ -54,7 +54,7 @@ fn get_content(
return None; return None;
} }
read_file(format!("{cours_dir}/{filename}"), MType::Generic) read_file(format!("{cours_dir}/{filename}"), MType::Cours)
} }
fn build_page(info: &web::Query<PathRequest>, config: Config) -> String { fn build_page(info: &web::Query<PathRequest>, config: Config) -> String {

View file

@ -8,6 +8,7 @@ use ramhorns::Content;
use std::fmt::Debug; use std::fmt::Debug;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::sync::Arc;
use crate::utils::metadata::MType; use crate::utils::metadata::MType;
@ -21,7 +22,7 @@ pub struct File {
} }
/// Options used for parser and compiler MD --> HTML /// Options used for parser and compiler MD --> HTML
pub fn get_options() -> ComrakOptions { pub fn get_options(metadata_type: MType) -> ComrakOptions {
comrak::Options { comrak::Options {
extension: comrak::ExtensionOptions::builder() extension: comrak::ExtensionOptions::builder()
.strikethrough(true) .strikethrough(true)
@ -37,6 +38,16 @@ pub fn get_options() -> ComrakOptions {
.multiline_block_quotes(true) .multiline_block_quotes(true)
.math_dollars(true) .math_dollars(true)
.underline(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(), .build(),
parse: comrak::ParseOptions::builder() parse: comrak::ParseOptions::builder()
.smart(true) .smart(true)
@ -134,7 +145,7 @@ fn fix_images_and_integration(
if let Ok(file) = fs::read_to_string(&img_path) { if let Ok(file) = fs::read_to_string(&img_path) {
let mime = mime_guess::from_path(&img_path).first_or_octet_stream(); let mime = mime_guess::from_path(&img_path).first_or_octet_stream();
if recursive && mime == "text/markdown" { if recursive && mime == "text/markdown" {
let mut options = get_options(); let mut options = get_options(metadata_type);
options.extension.footnotes = false; options.extension.footnotes = false;
let data = let data =
read_md(&img_path, &file, metadata_type, Some(options), false); read_md(&img_path, &file, metadata_type, Some(options), false);
@ -174,7 +185,7 @@ pub fn read_md(
) -> File { ) -> File {
let arena = Arena::new(); 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); let root = parse_document(&arena, raw_text, &opt);
// Find metadata // Find metadata

View file

@ -74,6 +74,7 @@ pub struct FileMetadataPortfolio {
pub enum MType { pub enum MType {
Blog, Blog,
Contact, Contact,
Cours,
Generic, Generic,
Index, Index,
Portfolio, Portfolio,
@ -140,7 +141,7 @@ pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile {
..MFile::default() ..MFile::default()
} }
} }
MType::Generic => MFile { MType::Generic | MType::Cours => MFile {
hardbreaks: deserialize_metadata(text), hardbreaks: deserialize_metadata(text),
..MFile::default() ..MFile::default()
}, },

View file

@ -79,7 +79,7 @@ pub fn get_posts(location: &str) -> Vec<Post> {
|text| { |text| {
let arena = Arena::new(); let arena = Arena::new();
let options = get_options(); let options = get_options(MType::Generic);
let root = parse_document(&arena, &text, &options); let root = parse_document(&arena, &text, &options);
let mut metadata = get(root, MType::Blog).blog.unwrap(); let mut metadata = get(root, MType::Blog).blog.unwrap();