1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use daachorse::{CharwiseDoubleArrayAhoCorasick, CharwiseDoubleArrayAhoCorasickBuilder, MatchKind};
use markdown::{to_html_with_options, Options};
use rany::RANY_URL_ID as rany;

pub const QUOTE: &str = "\"";
pub const CODE_BEGIN: &str = "<code>";
pub const CODE_END: &str = "</code>";

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum State {
  Attr,
  Quote,
  Normal,
  CodeBegin,
  CodeEnd,
  VarBegin,
  VarEnd,
  MathBegin,
  MathEnd,
  Space,
  Dollar,
}

#[static_init::dynamic]
pub static ATTR: CharwiseDoubleArrayAhoCorasick<State> =
  CharwiseDoubleArrayAhoCorasickBuilder::new()
    .match_kind(MatchKind::LeftmostFirst)
    .build_with_values([
      ("src=\"", State::Attr),
      ("href=\"", State::Attr),
      (QUOTE, State::Quote),
      (CODE_BEGIN, State::CodeBegin),
      (CODE_END, State::CodeEnd),
      ("${", State::VarBegin),
      ("}", State::VarEnd),
      ("$`", State::MathBegin),
      ("`$", State::MathEnd),
      (" ", State::Space),
      ("$", State::Dollar),
    ])
    .unwrap();

#[derive(Debug, Clone, Default)]
pub struct Md {
  pub htm: String,
  pub code: Vec<String>,
  /// code 转 markdown 的时候需要加上`, ${var}不需要
  pub code_pos: Vec<usize>,
  pub attr: Vec<String>,
}

#[derive(Debug)]
pub enum MdHtm {
  Md(Md),
  Htm(String),
}

pub fn md_htm_new(htm: String) -> Option<MdHtm> {
  let len = htm.len();
  if len > 2 && htm.starts_with('<') {
    if let Some(c) = htm.chars().nth(2) {
      if c.is_alphabetic() {
        return Some(MdHtm::Htm(htm));
      }
    }
  }

  let mut li = Vec::new();
  let mut code = Vec::new();
  let mut code_pos = Vec::new();
  let mut attr = Vec::new();

  let mut pre = 0;
  let mut state = State::Normal;

  for i in ATTR.leftmost_find_iter(&htm) {
    let mut start = i.start();
    let end = i.end();
    let s = i.value();

    macro_rules! push {
      ($li:expr) => {
        li.push(rany.e($li.len()));
        if pre != start {
          $li.push((&htm[pre..start]).into());
        }
        pre = start;
        state = State::Normal;
      };
    }

    macro_rules! code {
      () => {
        start = end;
        push!(code);
        let pos = li.len() - 1;
        li[pos] = CODE_BEGIN.to_owned() + &li[pos] + CODE_END;
      };
    }

    match state {
      State::Space | State::Dollar => {
        if s == State::Dollar {
          code!();
        }
      }
      State::MathBegin => {
        if s == State::MathEnd {
          code!();
        }
      }
      State::VarBegin => {
        if s == State::VarEnd {
          code!();
        }
      }
      State::CodeBegin => {
        if s == State::CodeEnd {
          code_pos.push(code.len());
          code!();
        }
      }
      State::Attr => {
        if s == State::Quote {
          push!(attr);
        }
      }
      State::Normal => match s {
        State::VarBegin | State::MathBegin | State::Dollar | State::CodeBegin => {
          if pre != start {
            li.push(htm[pre..start].into());
          }
          pre = start;
          state = s;
        }
        State::Attr => {
          li.push(htm[pre..end].into());
          pre = end;
          state = s;
        }
        _ => {}
      },
      _ => {}
    }
  }
  if pre != htm.len() {
    li.push(htm[pre..].into());
  }

  if li.len() == code.len() {
    return None;
  }

  Some(MdHtm::Md(Md {
    htm: li.join(""),
    code,
    code_pos,
    attr,
  }))
}

pub fn md_htm(h: &str) -> Option<MdHtm> {
  let mut conf = Options::gfm();
  conf.compile.allow_dangerous_html = true;
  conf.compile.allow_dangerous_protocol = true;

  md_htm_new(match to_html_with_options(h, &conf) {
    Ok(r) => if r.starts_with("<p>") {
      r[3..r.len() - 4].into()
    } else {
      r
    }
    .replace(" />", ">"),
    Err(e) => {
      tracing::error!("{h}\n{:?}", e);
      htmlize::escape_text(h).into()
    }
  })
}