Update pulldown cmark (#1120)

* 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
This commit is contained in:
Luke Randall 2020-04-23 01:43:26 +01:00 committed by GitHub
parent 05c8e4edff
commit ce020d6eb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 22 deletions

View File

@ -8,7 +8,7 @@ edition = "2018"
serde = "1" serde = "1"
serde_derive = "1" serde_derive = "1"
yew = { path = "../../yew" } yew = { path = "../../yew" }
pulldown-cmark = "0.1.2" pulldown-cmark = { version = "0.7.0", default-features = false }
[features] [features]
std_web = ["yew/std_web"] std_web = ["yew/std_web"]

View File

@ -1,6 +1,6 @@
/// Original author of this code is [Nathan Ringo](https://github.com/remexre) /// 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 /// 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::virtual_dom::{VNode, VTag, VText};
use yew::{html, Html}; use yew::{html, Html};
@ -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 { match ev {
Event::Start(tag) => { Event::Start(tag) => {
spine.push(make_tag(tag)); spine.push(make_tag(tag));
@ -63,6 +66,7 @@ pub fn render_markdown(src: &str) -> Html {
} }
} }
Event::Text(text) => add_child!(VText::new(text.to_string()).into()), 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::SoftBreak => add_child!(VText::new("\n".to_string()).into()),
Event::HardBreak => add_child!(VTag::new("br").into()), Event::HardBreak => add_child!(VTag::new("br").into()),
_ => println!("Unknown event: {:#?}", ev), _ => println!("Unknown event: {:#?}", ev),
@ -81,8 +85,7 @@ pub fn render_markdown(src: &str) -> Html {
fn make_tag(t: Tag) -> VTag { fn make_tag(t: Tag) -> VTag {
match t { match t {
Tag::Paragraph => VTag::new("p"), Tag::Paragraph => VTag::new("p"),
Tag::Rule => VTag::new("hr"), Tag::Heading(n) => {
Tag::Header(n) => {
assert!(n > 0); assert!(n > 0);
assert!(n < 7); assert!(n < 7);
VTag::new(format!("h{}", n)) VTag::new(format!("h{}", n))
@ -92,19 +95,23 @@ fn make_tag(t: Tag) -> VTag {
el.add_class("blockquote"); el.add_class("blockquote");
el el
} }
Tag::CodeBlock(lang) => { Tag::CodeBlock(code_block_kind) => {
let mut el = VTag::new("code"); 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 if let CodeBlockKind::Fenced(lang) = code_block_kind {
// highlighting support by locating the language classes and applying dom transforms // Different color schemes may be used for different code blocks,
// on their contents. // but a different library (likely js based at the moment) would be necessary to actually provide the
match lang.as_ref() { // highlighting support by locating the language classes and applying dom transforms
"html" => el.add_class("html-language"), // on their contents.
"rust" => el.add_class("rust-language"), match lang.as_ref() {
"java" => el.add_class("java-language"), "html" => el.add_class("html-language"),
"c" => el.add_class("c-language"), "rust" => el.add_class("rust-language"),
_ => {} // Add your own language highlighting support "java" => el.add_class("java-language"),
}; "c" => el.add_class("c-language"),
_ => {} // Add your own language highlighting support
};
}
el el
} }
Tag::List(None) => VTag::new("ul"), Tag::List(None) => VTag::new("ul"),
@ -133,23 +140,29 @@ fn make_tag(t: Tag) -> VTag {
el.add_class("font-weight-bold"); el.add_class("font-weight-bold");
el el
} }
Tag::Code => VTag::new("code"), Tag::Link(_link_type, ref href, ref title) => {
Tag::Link(ref href, ref title) => {
let mut el = VTag::new("a"); let mut el = VTag::new("a");
el.add_attribute("href", href); el.add_attribute("href", href);
let title = title.clone().into_string();
if title != "" { if title != "" {
el.add_attribute("title", title); el.add_attribute("title", &title);
} }
el el
} }
Tag::Image(ref src, ref title) => { Tag::Image(_link_type, ref src, ref title) => {
let mut el = VTag::new("img"); let mut el = VTag::new("img");
el.add_attribute("src", src); el.add_attribute("src", src);
let title = title.clone().into_string();
if title != "" { if title != "" {
el.add_attribute("title", title); el.add_attribute("title", &title);
} }
el el
} }
Tag::FootnoteDefinition(ref _footnote_id) => VTag::new("span"), // Footnotes are not rendered as anything special 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
}
} }
} }