Skip to content

Commit

Permalink
Update pulldown cmark (#1120)
Browse files Browse the repository at this point in the history
* Update to version 0.7.0

* Use new Options struct

* Fix rule support

* Fix link tag

* Remove code tag

* Fix code block tag

* Fix image tag

* Fix heading tag

* Add strikethrough tag

* Run cargo fmt
  • Loading branch information
lukerandall committed Apr 23, 2020
1 parent 05c8e4e commit ce020d6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/crm/Cargo.toml
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
serde = "1"
serde_derive = "1"
yew = { path = "../../yew" }
pulldown-cmark = "0.1.2"
pulldown-cmark = { version = "0.7.0", default-features = false }

[features]
std_web = ["yew/std_web"]
Expand Down
55 changes: 34 additions & 21 deletions examples/crm/src/markdown.rs
@@ -1,6 +1,6 @@
/// Original author of this code is [Nathan Ringo](https://github.com/remexre)
/// Source: https://github.com/acmumn/mentoring/blob/master/web-client/src/view/markdown.rs
use pulldown_cmark::{Alignment, Event, Parser, Tag, OPTION_ENABLE_TABLES};
use pulldown_cmark::{Alignment, CodeBlockKind, Event, Options, Parser, Tag};
use yew::virtual_dom::{VNode, VTag, VText};
use yew::{html, Html};

Expand All @@ -18,7 +18,10 @@ pub fn render_markdown(src: &str) -> Html {
}};
}

for ev in Parser::new_ext(src, OPTION_ENABLE_TABLES) {
let mut options = Options::empty();
options.insert(Options::ENABLE_TABLES);

for ev in Parser::new_ext(src, options) {
match ev {
Event::Start(tag) => {
spine.push(make_tag(tag));
Expand Down Expand Up @@ -63,6 +66,7 @@ pub fn render_markdown(src: &str) -> Html {
}
}
Event::Text(text) => add_child!(VText::new(text.to_string()).into()),
Event::Rule => add_child!(VTag::new("hr").into()),
Event::SoftBreak => add_child!(VText::new("\n".to_string()).into()),
Event::HardBreak => add_child!(VTag::new("br").into()),
_ => println!("Unknown event: {:#?}", ev),
Expand All @@ -81,8 +85,7 @@ pub fn render_markdown(src: &str) -> Html {
fn make_tag(t: Tag) -> VTag {
match t {
Tag::Paragraph => VTag::new("p"),
Tag::Rule => VTag::new("hr"),
Tag::Header(n) => {
Tag::Heading(n) => {
assert!(n > 0);
assert!(n < 7);
VTag::new(format!("h{}", n))
Expand All @@ -92,19 +95,23 @@ fn make_tag(t: Tag) -> VTag {
el.add_class("blockquote");
el
}
Tag::CodeBlock(lang) => {
Tag::CodeBlock(code_block_kind) => {
let mut el = VTag::new("code");
// Different color schemes may be used for different code blocks,
// but a different library (likely js based at the moment) would be necessary to actually provide the
// highlighting support by locating the language classes and applying dom transforms
// on their contents.
match lang.as_ref() {
"html" => el.add_class("html-language"),
"rust" => el.add_class("rust-language"),
"java" => el.add_class("java-language"),
"c" => el.add_class("c-language"),
_ => {} // Add your own language highlighting support
};

if let CodeBlockKind::Fenced(lang) = code_block_kind {
// Different color schemes may be used for different code blocks,
// but a different library (likely js based at the moment) would be necessary to actually provide the
// highlighting support by locating the language classes and applying dom transforms
// on their contents.
match lang.as_ref() {
"html" => el.add_class("html-language"),
"rust" => el.add_class("rust-language"),
"java" => el.add_class("java-language"),
"c" => el.add_class("c-language"),
_ => {} // Add your own language highlighting support
};
}

el
}
Tag::List(None) => VTag::new("ul"),
Expand Down Expand Up @@ -133,23 +140,29 @@ fn make_tag(t: Tag) -> VTag {
el.add_class("font-weight-bold");
el
}
Tag::Code => VTag::new("code"),
Tag::Link(ref href, ref title) => {
Tag::Link(_link_type, ref href, ref title) => {
let mut el = VTag::new("a");
el.add_attribute("href", href);
let title = title.clone().into_string();
if title != "" {
el.add_attribute("title", title);
el.add_attribute("title", &title);
}
el
}
Tag::Image(ref src, ref title) => {
Tag::Image(_link_type, ref src, ref title) => {
let mut el = VTag::new("img");
el.add_attribute("src", src);
let title = title.clone().into_string();
if title != "" {
el.add_attribute("title", title);
el.add_attribute("title", &title);
}
el
}
Tag::FootnoteDefinition(ref _footnote_id) => VTag::new("span"), // Footnotes are not rendered as anything special
Tag::Strikethrough => {
let mut el = VTag::new("span");
el.add_class("text-decoration-strikethrough");
el
}
}
}

0 comments on commit ce020d6

Please sign in to comment.