From 87436bb887c435461e371e0b8d83cfe79dd3c9b2 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 7 Jan 2025 17:41:46 +1300 Subject: A collection of fixes Fixed a lot of issues around internal relative links and link validity checks. Added a math fragment syntax and the $$..$$ math form. Added heading link validity checks. --- src/generate_html.rs | 88 ++++++++++++++++++++++----------------- src/main.rs | 114 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 144 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/generate_html.rs b/src/generate_html.rs index 3f22d3b..1e3acc7 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -20,8 +20,8 @@ pub fn generate_html(document: &MarkdownDocument, page: &SourceFile, website: &W \ ", page.name, website.name, - get_html_head(document), - document_to_html(document, page, website) + get_html_head(document).trim(), + document_to_html(document, page, website).trim() ) } @@ -34,9 +34,9 @@ pub fn get_html_head(document: &MarkdownDocument) -> String { } } String::from("\ - - - \ + + + \ ") } @@ -50,7 +50,8 @@ pub fn document_to_html(document: &MarkdownDocument, page: &SourceFile, website: macro_rules! html { ($($arg:tt)*) => {{ html.push_str(&format!($($arg)*)); html.push('\n'); }}; } macro_rules! tag { - ($t:expr,$l:expr) => { html!("<{}>{}", $t, line_to_html!($l), $t) }; } + ($t:expr,$l:expr,$c:expr) => { html!("<{} {}>{}", $t, $c, line_to_html!($l), $t) }; + ($t:expr,$l:expr) => { html!("<{}>{}", $t, line_to_html!($l), $t) }; } macro_rules! wrap { ($t:expr,$f:expr) => {{ html!("<{}>", $t); $f; html!("", $t); }}; } @@ -58,11 +59,19 @@ pub fn document_to_html(document: &MarkdownDocument, page: &SourceFile, website: for block in &document.blocks { match block { Block::Heading { level, line } => match level { - Level::Heading1 => tag!("h1", line), - Level::Heading2 => tag!("h2", line), - Level::Heading3 => tag!("h3", line), + Level::Heading1 => tag!("h1", line, format!("id='{}'", make_url_safe(&line_to_html!(line)))), + Level::Heading2 => tag!("h2", line, format!("id='{}'", make_url_safe(&line_to_html!(line)))), + Level::Heading3 => tag!("h3", line, format!("id='{}'", make_url_safe(&line_to_html!(line)))), + } + Block::Paragraph(line) => { + if let Some(stripped) = line.to_string().strip_prefix("$$ ") { + if let Some(stripped) = stripped.strip_suffix(" $$") { + html!("
{stripped}
"); + continue; + } + } + tag!("p", line); } - Block::Paragraph(line) => tag!("p", line), Block::List(lines) => wrap!("ul", for line in lines { // Insert a
tag directly after the first untagged colon. let mut depth = 0; @@ -88,19 +97,18 @@ pub fn document_to_html(document: &MarkdownDocument, page: &SourceFile, website: Block::Embedded { label, path } => match path.rsplit_once('.') { Some((_, extension)) => match extension.to_lowercase().as_str() { "jpg"|"jpeg"|"png"|"webp"|"gif"|"tiff" => html!( - "
{}
", - path, path, label, label - ), + "
{label}
"), "mp3"|"wav"|"m4a" => html!("