2018-06-14 00:13:38 +03:00
|
|
|
use pulldown_cmark::{Parser, Tag, html, OPTION_ENABLE_TABLES, OPTION_DISABLE_HTML};
|
|
|
|
use pulldown_cmark::Event::{Text, End};
|
2017-09-21 11:58:54 +03:00
|
|
|
|
|
|
|
pub fn render_markdown(src: &str) -> String {
|
2017-10-28 15:26:01 +03:00
|
|
|
let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML;
|
|
|
|
let p = Parser::new_ext(src, opts);
|
2017-09-21 11:58:54 +03:00
|
|
|
let mut buf = String::new();
|
|
|
|
html::push_html(&mut buf, p);
|
|
|
|
buf
|
|
|
|
}
|
2018-01-19 18:49:15 +03:00
|
|
|
|
2018-06-14 00:13:38 +03:00
|
|
|
fn is_html_special(c: char) -> bool {
|
|
|
|
c == '&' || c == '<' || c == '>'
|
|
|
|
}
|
|
|
|
|
2018-01-19 18:49:15 +03:00
|
|
|
pub fn render_markdown_for_fts(src: &str) -> String {
|
|
|
|
let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML;
|
|
|
|
let p = Parser::new_ext(src, opts);
|
|
|
|
let mut buf = String::new();
|
|
|
|
|
|
|
|
for event in p {
|
|
|
|
match event {
|
2018-06-14 00:13:38 +03:00
|
|
|
Text(text) =>
|
|
|
|
buf.push_str(&text.replace(is_html_special, " ")),
|
|
|
|
End(Tag::Link(uri, _title)) => {
|
|
|
|
buf.push_str(" (");
|
|
|
|
buf.push_str(&uri.replace(is_html_special, " "));
|
|
|
|
buf.push_str(") ");
|
|
|
|
}
|
2018-01-19 18:49:15 +03:00
|
|
|
_ => buf.push_str(" "),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf
|
|
|
|
}
|