From 01d6fb5a1f79ff1ef48cc69b42d8a91ef9a6d2f1 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Fri, 14 Mar 2025 12:55:00 +1300 Subject: Add optional labels to internal links Internal links can have labels in the same manner as with external links, by separating the label and path from each other with a :: token. --- src/line.rs | 11 ++++++++++- src/token.rs | 13 ++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/line.rs b/src/line.rs index b60b55c..5ef940f 100644 --- a/src/line.rs +++ b/src/line.rs @@ -81,6 +81,15 @@ impl ToString for Line { } } +fn internal_link(inside: String) -> Option { + if let Some((label, path)) = inside.split_once("::") { + let label = label.trim().to_string(); + let path = path.trim().to_string(); + Some( Token::InternalLink { label, path } ) + } else { + Some( Token::InternalLink { label: String::new(), path: inside }) + } +} fn external_link(inside: String) -> Option { if let Some((label, path)) = inside.split_once("::") { @@ -101,7 +110,7 @@ const DELIMITERS: [(fn(String)->Option, &str, &str, &str); 6] = [ ( make!(Token::Italic), "_", "_", "_" ), ( make!(Token::Monospace), "`", "`", "`" ), ( make!(Token::Math), "$", "$", "$" ), - ( make!(Token::InternalLink), "{", "}", "{}" ), + ( internal_link, "{", "}", "{}" ), ( external_link, "<", ">", "<>" ), ]; diff --git a/src/token.rs b/src/token.rs index f1f8288..0f44dc2 100644 --- a/src/token.rs +++ b/src/token.rs @@ -5,7 +5,7 @@ pub enum Token { Italic(String), Monospace(String), Math(String), - InternalLink(String), + InternalLink { label: String, path: String }, ExternalLink { label: String, path: String }, } @@ -17,10 +17,13 @@ impl AsRef for Token { Token::Italic(text) => text, Token::Monospace(text) => text, Token::Math(text) => text, - Token::InternalLink(name) => name, - Token::ExternalLink { label, path } => match !label.is_empty() { - true => label, - false => path, + Token::InternalLink { label, path } => match label.is_empty() { + true => path, + false => label, + }, + Token::ExternalLink { label, path } => match label.is_empty() { + true => path, + false => label, }, } } -- cgit v1.2.3-70-g09d2