markdown_it/plugins/html/
html_inline.rs

1//! HTML inline syntax from CommonMark
2//!
3//! <https://spec.commonmark.org/0.30/#raw-html>
4use super::utils::regexps::*;
5use crate::parser::inline::{InlineRule, InlineState};
6use crate::{MarkdownIt, Node, NodeValue, Renderer};
7
8#[derive(Debug)]
9pub struct HtmlInline {
10    pub content: String,
11}
12
13impl NodeValue for HtmlInline {
14    fn render(&self, _: &Node, fmt: &mut dyn Renderer) {
15        fmt.text_raw(&self.content);
16    }
17}
18
19pub fn add(md: &mut MarkdownIt) {
20    md.inline.add_rule::<HtmlInlineScanner>();
21}
22
23#[doc(hidden)]
24pub struct HtmlInlineScanner;
25impl InlineRule for HtmlInlineScanner {
26    const MARKER: char = '<';
27
28    fn run(state: &mut InlineState) -> Option<(Node, usize)> {
29        // Check start
30        let mut chars = state.src[state.pos..state.pos_max].chars();
31        if chars.next().unwrap() != '<' { return None; }
32
33        // Quick fail on second char
34        let Some('!' | '?' | '/' | 'A'..='Z' | 'a'..='z') = chars.next() else { return None; };
35
36        let capture = HTML_TAG_RE.captures(&state.src[state.pos..state.pos_max])?.get(0).unwrap().as_str();
37        let capture_len = capture.len();
38
39        let content = capture.to_owned();
40
41        if HTML_LINK_OPEN.is_match(&content) {
42            state.link_level += 1;
43        } else if HTML_LINK_CLOSE.is_match(&content) {
44            state.link_level -= 1;
45        }
46
47        let node = Node::new(HtmlInline { content });
48        Some((node, capture_len))
49    }
50}