Skip to content

Commit

Permalink
Add svgbob for markdown rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
erasin committed May 6, 2020
1 parent 79b2338 commit 60cf7b9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/rendering/Cargo.toml
Expand Up @@ -14,6 +14,7 @@ pest = "2"
pest_derive = "2"
regex = "1"
lazy_static = "1"
svgbob = "0.4.1"

errors = { path = "../errors" }
front_matter = { path = "../front_matter" }
Expand Down
44 changes: 42 additions & 2 deletions components/rendering/src/markdown.rs
Expand Up @@ -186,12 +186,21 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_STRIKETHROUGH);

let mut bob_has = false;
let mut bob_buffer = String::new();

{
let mut events = Parser::new_ext(content, opts)
.map(|event| {
match event {
Event::Text(text) => {
// if we are in the middle of a code block

if bob_has {
bob_buffer.push_str(&text);
return Event::Text("".into());
}

if let Some((ref mut highlighter, in_extra)) = highlighter {
let highlighted = if in_extra {
if let Some(ref extra) = context.config.extra_syntax_set {
Expand All @@ -213,8 +222,22 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
Event::Text(text)
}
Event::Start(Tag::CodeBlock(ref kind)) => {
let tag = match kind {
CodeBlockKind::Indented => "unknow",
CodeBlockKind::Fenced(info) => info,
};

if tag == "bob" {
bob_has = true;
return Event::Text("".into());
}

if tag == "mermaid" {
return Event::Html("<pre><code class='language-mermaid'>".into());
}

if !context.config.highlight_code {
return Event::Html("<pre><code>".into());
return Event::Html(format!("<pre><code 'langulage-{}'>", tag).into());
}

let theme = &THEME_SET.themes[&context.config.highlight_theme];
Expand All @@ -233,7 +256,24 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
let snippet = start_highlighted_html_snippet(theme);
Event::Html(snippet.0.into())
}
Event::End(Tag::CodeBlock(_)) => {
Event::End(Tag::CodeBlock(ref kind)) => {
let tag = match kind {
CodeBlockKind::Indented => "unknow",
CodeBlockKind::Fenced(info) => info,
};

// bob
if tag == "bob" && bob_has {
bob_has = false;
let bsvg = svgbob::to_svg(&bob_buffer.to_string()).to_string();
bob_buffer.clear();
return Event::Html(bsvg.into());
}

if tag == "mermaid" {
return Event::Html("</code></pre>\n".into());
}

if !context.config.highlight_code {
return Event::Html("</code></pre>\n".into());
}
Expand Down

0 comments on commit 60cf7b9

Please sign in to comment.