1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2025-01-11 14:35:48 +00:00

Support link label (fixes #52)

This commit is contained in:
Felix Ableitner 2024-12-18 22:36:12 +01:00
parent 793810f737
commit af09ee23bc

View file

@ -81,6 +81,7 @@ fn markdown_parser() -> MarkdownIt {
#[derive(Debug)] #[derive(Debug)]
pub struct ArticleLink { pub struct ArticleLink {
label: String,
title: String, title: String,
domain: String, domain: String,
} }
@ -94,7 +95,7 @@ impl NodeValue for ArticleLink {
attrs.push(("href", link)); attrs.push(("href", link));
fmt.open("a", &attrs); fmt.open("a", &attrs);
fmt.text(&self.title); fmt.text(&self.label);
fmt.close("a"); fmt.close("a");
} }
} }
@ -117,7 +118,10 @@ impl InlineRule for ArticleLinkScanner {
let i = start + length - SEPARATOR_LENGTH; let i = start + length - SEPARATOR_LENGTH;
let content = &state.src[start..i]; let content = &state.src[start..i];
content.split_once('@').map(|(title, domain)| { content.split_once('@').map(|(title, domain)| {
// Handle custom link label if provided, otherwise use title as label
let (domain, label) = domain.split_once('|').unwrap_or((&domain, &title));
let node = Node::new(ArticleLink { let node = Node::new(ArticleLink {
label: label.to_string(),
title: title.to_string(), title: title.to_string(),
domain: domain.to_string(), domain: domain.to_string(),
}); });
@ -180,12 +184,18 @@ impl InlineRule for MathEquationScanner {
#[test] #[test]
fn test_markdown_article_link() { fn test_markdown_article_link() {
let parser = markdown_parser(); let parser = markdown_parser();
let rendered = parser let plain = parser.parse("[[Title@example.com]]").render();
.parse("some text [[Title@example.com]] and more") assert_eq!(
"<p><a href=\"/article/Title@example.com\">Title</a></p>\n",
plain
);
let with_label = parser
.parse("[[Title@example.com|Example Article]]")
.render(); .render();
assert_eq!( assert_eq!(
"<p>some text <a href=\"/article/Title@example.com\">Title</a> and more</p>\n", "<p><a href=\"/article/Title@example.com\">Example Article</a></p>\n",
rendered with_label
); );
} }