refactor: ref

This commit is contained in:
thek4n 2026-07-13 18:52:28 +03:00
parent 48daaa3748
commit b6cbc9123d

View File

@ -1,7 +1,9 @@
use pulldown_cmark::{
BlockQuoteKind, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd,
TextMergeStream,
};
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;
use pulldown_cmark::{CodeBlockKind, BlockQuoteKind, Event, Options, Parser, Tag, TagEnd, TextMergeStream, CowStr, LinkType};
pub fn markdown_to_html(markdown: &str, ss: &SyntaxSet, ts: &ThemeSet, _file_path: &str) -> String {
let mut options = Options::empty();
@ -10,8 +12,6 @@ pub fn markdown_to_html(markdown: &str, ss: &SyntaxSet, ts: &ThemeSet, _file_pat
options.insert(Options::ENABLE_STRIKETHROUGH);
options.insert(Options::ENABLE_TASKLISTS);
options.insert(Options::ENABLE_HEADING_ATTRIBUTES);
options.insert(Options::ENABLE_DEFINITION_LIST);
options.insert(Options::ENABLE_MATH);
options.insert(Options::ENABLE_GFM);
let mut in_code_block = false;
@ -29,40 +29,45 @@ pub fn markdown_to_html(markdown: &str, ss: &SyntaxSet, ts: &ThemeSet, _file_pat
code_content.clear();
}
html_output.push_str(render_start_tag(tag).as_str());
},
}
Event::End(tag) => {
if let TagEnd::CodeBlock = tag {
in_code_block = false;
html_output.push_str(escape_html(code_content.as_str()).as_str());
html_output.push_str(escape_html(&code_content).as_str());
}
html_output.push_str(render_end_tag(tag).as_str());
},
}
Event::Text(text) => {
if in_code_block {
code_content.push_str(&text);
} else {
html_output.push_str(&escape_html(&text));
}
},
Event::TaskListMarker(checked) => {
if checked {
}
Event::TaskListMarker(true) => {
html_output.push_str("<input type=\"checkbox\" checked/>");
} else {
}
Event::TaskListMarker(false) => {
html_output.push_str("<input type=\"checkbox\"/>");
}
},
Event::Code(code) => {
html_output.push_str(format!("<code>{}</code>", escape_html(&code)).as_str());
},
Event::SoftBreak => html_output.push_str("<wbr>"),
}
Event::SoftBreak => html_output.push_str(" "),
Event::HardBreak => html_output.push_str("<br>"),
Event::Html(html) => html_output.push_str(&html),
Event::Rule => html_output.push_str("<hr>"),
Event::InlineHtml(_) => (),
Event::Html(_) => (),
Event::FootnoteReference(fref) => html_output.push_str(&format!(
"<sup><a href=\"#{}\" id=\"{}\"></a></sup>",
fref, fref
)),
other => println!("[*] Event {:?}", other),
Event::InlineHtml(_) => (),
Event::InlineMath(_) => (),
Event::DisplayMath(_) => (),
}
}
@ -74,10 +79,10 @@ fn render_start_tag(tag: Tag<'_>) -> String {
match tag {
Tag::Heading { level, .. } => {
output.push_str(format!("<{level}>").as_str());
},
}
Tag::Strikethrough => {
output.push_str("<strike>");
},
}
Tag::List(Some(_)) => output.push_str("<ol>"),
Tag::List(None) => output.push_str("<ul>"),
Tag::Item => output.push_str("<li>"),
@ -86,14 +91,36 @@ fn render_start_tag(tag: Tag<'_>) -> String {
Tag::Emphasis => output.push_str("<em>"),
Tag::Strong => output.push_str("<strong>"),
Tag::BlockQuote(kind) => output.push_str(render_blockquote(kind).as_str()),
Tag::Link{link_type, dest_url, title, ..} => output.push_str(render_link(link_type, dest_url, title).as_str()),
Tag::Link {
link_type,
dest_url,
title,
..
} => output.push_str(render_link(link_type, dest_url, title).as_str()),
Tag::Image {
link_type,
dest_url,
title,
..
} => output.push_str(render_image(link_type, dest_url, title).as_str()),
Tag::Table(alignments) => output.push_str("<table>"),
Tag::Table(_) => output.push_str("<table>"), // TODO: fix table
Tag::TableHead => output.push_str("<thead>"),
Tag::TableCell => output.push_str("<td>"),
Tag::TableRow => output.push_str("<tr>"),
other => println!("* [Tag] {:?}", other),
Tag::HtmlBlock => (), // TODO: implement
Tag::FootnoteDefinition(fref) => {
output.push_str(&format!("<sup><a href=\"#{}\" id=\"{}\">", fref, fref))
}
Tag::DefinitionList => (),
Tag::DefinitionListTitle => (),
Tag::DefinitionListDefinition => (),
Tag::Superscript => (),
Tag::Subscript => (),
Tag::MetadataBlock(_) => (),
}
output
@ -104,10 +131,10 @@ fn render_end_tag(tag: TagEnd) -> String {
match tag {
TagEnd::Heading(level) => {
output.push_str(format!("</{level}>").as_str());
},
}
TagEnd::Strikethrough => {
output.push_str("</strike>");
},
}
TagEnd::List(true) => output.push_str("</ol>"),
TagEnd::List(false) => output.push_str("</ul>"),
TagEnd::Item => output.push_str("</li>"),
@ -117,13 +144,22 @@ fn render_end_tag(tag: TagEnd) -> String {
TagEnd::Strong => output.push_str("</strong>"),
TagEnd::BlockQuote(_) => output.push_str("</blockquote>"),
TagEnd::Link => output.push_str("</a>"),
TagEnd::Image => output.push_str("</img>"),
TagEnd::Table => output.push_str("</table>"),
TagEnd::TableHead => output.push_str("</thead>"),
TagEnd::TableCell => output.push_str("</td>"),
TagEnd::TableRow => output.push_str("</tr>"),
other => println!("* [TagEnd] {:?}", other),
TagEnd::FootnoteDefinition => output.push_str("</a></sup>"),
TagEnd::HtmlBlock => (), // TODO: implement
TagEnd::DefinitionList => (),
TagEnd::DefinitionListTitle => (),
TagEnd::DefinitionListDefinition => (),
TagEnd::Superscript => (),
TagEnd::Subscript => (),
TagEnd::MetadataBlock(_) => (),
}
output
@ -132,6 +168,12 @@ fn render_end_tag(tag: TagEnd) -> String {
fn render_codeblock(block_kind: CodeBlockKind<'_>) -> String {
match block_kind {
CodeBlockKind::Indented => "<pre><code>".to_string(),
CodeBlockKind::Fenced(CowStr::Borrowed("mermaid")) => {
"<pre><code class=\"language-text mermaid\">".to_string()
}
CodeBlockKind::Fenced(CowStr::Borrowed("rawmermaid")) => {
"<pre><code class=\"language-text\">".to_string()
}
CodeBlockKind::Fenced(code_type) => format!("<pre><code class=\"language-{}\">", code_type),
}
}
@ -141,15 +183,11 @@ fn render_blockquote(quote_kind: Option<BlockQuoteKind>) -> String {
None => {
"<blockquote>".to_string()
},
Some(kind) => {
match kind {
BlockQuoteKind::Note => "<blockquote><div class=\"markdown-alert markdown-alert-note\"><p class=\"markdown-alert-title\">Note</p>".to_string(),
BlockQuoteKind::Tip => "<blockquote><div class=\"markdown-alert markdown-alert-tip\"><p class=\"markdown-alert-title\">Tip</p>".to_string(),
BlockQuoteKind::Important =>"<blockquote><div class=\"markdown-alert markdown-alert-important\"><p class=\"markdown-alert-title\">Important</p>".to_string(),
BlockQuoteKind::Warning => "<blockquote><div class=\"markdown-alert markdown-alert-warning\"><p class=\"markdown-alert-title\">Warning</p>".to_string(),
BlockQuoteKind::Caution => "<blockquote><div class=\"markdown-alert markdown-alert-caution\"><p class=\"markdown-alert-title\">Caution</p>".to_string(),
}
},
Some(BlockQuoteKind::Note) => "<blockquote><div class=\"markdown-alert markdown-alert-note\"><p class=\"markdown-alert-title\">Note</p>".to_string(),
Some(BlockQuoteKind::Tip) => "<blockquote><div class=\"markdown-alert markdown-alert-tip\"><p class=\"markdown-alert-title\">Tip</p>".to_string(),
Some(BlockQuoteKind::Important) =>"<blockquote><div class=\"markdown-alert markdown-alert-important\"><p class=\"markdown-alert-title\">Important</p>".to_string(),
Some(BlockQuoteKind::Warning) => "<blockquote><div class=\"markdown-alert markdown-alert-warning\"><p class=\"markdown-alert-title\">Warning</p>".to_string(),
Some(BlockQuoteKind::Caution) => "<blockquote><div class=\"markdown-alert markdown-alert-caution\"><p class=\"markdown-alert-title\">Caution</p>".to_string(),
}
}
fn render_link(link_type: LinkType, dest_url: CowStr<'_>, title: CowStr<'_>) -> String {
@ -163,9 +201,28 @@ fn render_link(link_type: LinkType, dest_url: CowStr<'_>, title: CowStr<'_>) ->
LinkType::ShortcutUnknown => format!("<a href=\"{}\" title=\"{}\">", dest_url, title),
LinkType::Autolink => format!("<a href=\"{}\" title=\"{}\">", dest_url, title),
LinkType::Email => format!("<a href=\"{}\" title=\"{}\">", dest_url, title),
LinkType::WikiLink{has_pothole: _} => { // TODO
LinkType::WikiLink { has_pothole: _ } => {
// TODO: refactor
format!("<a href=\"{}\" title=\"{}\">", dest_url, title)
},
}
}
}
fn render_image(link_type: LinkType, dest_url: CowStr<'_>, title: CowStr<'_>) -> String {
match link_type {
LinkType::Inline => format!("<img src=\"{}\" alt=\"{}\">", dest_url, title),
LinkType::Reference => format!("<img src=\"{}\" title=\"{}\">", dest_url, title),
LinkType::ReferenceUnknown => format!("<img src=\"{}\" alt=\"{}\">", dest_url, title),
LinkType::Collapsed => format!("<img src=\"{}\" alt=\"{}\">", dest_url, title),
LinkType::CollapsedUnknown => format!("<img src=\"{}\" alt=\"{}\">", dest_url, title),
LinkType::Shortcut => format!("<img src=\"{}\" alt=\"{}\">", dest_url, title),
LinkType::ShortcutUnknown => format!("<img src=\"{}\" alt=\"{}\">", dest_url, title),
LinkType::Autolink => format!("<img src=\"{}\" alt=\"{}\">", dest_url, title),
LinkType::Email => format!("<img src=\"{}\" alt=\"{}\">", dest_url, title),
LinkType::WikiLink { has_pothole: _ } => {
// TODO: refactor
format!("<img src=\"{}\" alt=\"{}\">", dest_url, title)
}
}
}