Skip to content

Commit

Permalink
Add Link definition node to the AST
Browse files Browse the repository at this point in the history
For HTML rendering, this is not necessary, since it is not rendered.
But for use cases, such a "round-trip" rendering (MD -> MD), or syntax analysis tools, it would be necessary to have this present in the AST.

(similarly capturing the labels of link references would also be required but, if accepted, I can add that in a separate PR)
  • Loading branch information
chrisjsewell authored and rlidwka committed Aug 3, 2023
1 parent e698936 commit 19f4a9e
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/plugins/cmark/block/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::common::utils::normalize_reference;
use crate::generics::inline::full_link;
use crate::parser::block::{BlockRule, BlockState};
use crate::parser::extset::RootExt;
use crate::{MarkdownIt, Node};
use crate::{MarkdownIt, Node, NodeValue};

/// Storage for parsed references
///
Expand Down Expand Up @@ -195,6 +195,16 @@ pub fn add(md: &mut MarkdownIt) {
md.block.add_rule::<ReferenceScanner>();
}

#[derive(Debug)]
pub struct Definition {
pub label: String,
pub destination: String,
pub title: Option<String>,
}
impl NodeValue for Definition {
fn render(&self, _: &Node, _: &mut dyn crate::Renderer) {}
}

#[doc(hidden)]
pub struct ReferenceScanner;
impl BlockRule for ReferenceScanner {
Expand Down Expand Up @@ -350,8 +360,15 @@ impl BlockRule for ReferenceScanner {
}

let references = state.root_ext.get_or_insert_default::<ReferenceMap>();
if !references.insert(str[1..label_end].to_owned(), href, title) { return None; }

Some((Node::default(), lines + 1))
if !references.insert(str[1..label_end].to_owned(), href.clone(), title.clone()) { return None; }

Some((Node::new(
Definition {
label: str[1..label_end].to_owned(),
destination: href,
title
}),
lines + 1
))
}
}

0 comments on commit 19f4a9e

Please sign in to comment.