Skip to content

Commit af33926

Browse files
committed
add basic markdown support
1 parent db6b43a commit af33926

File tree

7 files changed

+45
-128
lines changed

7 files changed

+45
-128
lines changed

devblog/devblog/client/Cargo.lock

Lines changed: 1 addition & 117 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

devblog/devblog/client/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ edition = "2021"
77

88
[dependencies]
99
chrono = { version = "0.4.33", features = ["serde"] }
10-
cookie = "0.18.0"
1110
gloo = "0.11.0"
1211
gloo-net = "0.5.0"
13-
reqwasm = "0.5.0"
1412
serde = "1.0.196"
15-
serde-wasm-bindgen = "0.6.3"
1613
serde_json = "1.0.113"
1714
stylist = { version = "0.13.0", features = ["yew", "parser"] }
1815
wasm-bindgen = "0.2.90"
1916
wasm-bindgen-futures = "0.4.40"
20-
wasm-cookies = "0.2.1"
2117
web-sys = { version = "0.3.67", features = ["HtmlInputElement"] }
2218
yew = { version = "0.21.0", features = ["csr"] }
2319
yew-router = "0.18.0"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use yew::{function_component, html, Html, Properties};
2+
3+
#[derive(Properties, PartialEq, Clone)]
4+
pub struct Props {
5+
pub content: String,
6+
}
7+
8+
#[function_component(Markdown)]
9+
pub fn markdown(props: &Props) -> Html {
10+
let html = props.content.lines().map(|line| match line {
11+
l if l.starts_with("# ") => html! {<h1>{&l[1..]}</h1>},
12+
l if l.starts_with("## ") => html! {<h2>{&l[2..]}</h2>},
13+
l if l.starts_with("### ") => html! {<h3>{&l[3..]}</h3>},
14+
l if l.starts_with("#### ") => html! {<h4>{&l[4..]}</h4>},
15+
l if l.starts_with("```") => {
16+
html! {<p><code lang="rust">{line.replace("```", "")}</code></p>}
17+
}
18+
l if l.starts_with("---") => html! {<hr/>},
19+
l if l.starts_with("___") => html! {<hr/>},
20+
l if l.starts_with("-") => html! {<li>{&l[1..]}</li>},
21+
l if l.is_empty() => html! {<br/>},
22+
_ => html! {<p>{line}</p>},
23+
});
24+
25+
html! {
26+
<div>
27+
{for html}
28+
</div>
29+
}
30+
}

devblog/devblog/client/src/components/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod comment_delete;
44
pub mod comment_edit;
55
pub mod footer;
66
pub mod items;
7+
pub mod markdown;
78
pub mod navbar;
89
pub mod notifications;
910
pub mod pager;

devblog/devblog/client/src/components/post.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::{
22
components::{
3-
comment::Comment, comment_add::AddComment, post_delete::DeletePost, post_edit::EditPost,
4-
vote::Vote,
3+
comment::Comment, comment_add::AddComment, markdown::Markdown, post_delete::DeletePost,
4+
post_edit::EditPost, vote::Vote,
55
},
66
CommentModel, PostModel,
77
};
88
use chrono::{Local, TimeZone};
9+
use gloo::console::log;
910
use std::ops::Deref;
1011
use stylist::Style;
1112
use yew::{function_component, html, use_effect_with, use_state, Callback, Html, Properties};
@@ -46,6 +47,7 @@ pub fn post(props: &Props) -> Html {
4647
Callback::from(move |id| {
4748
let mut new_comments = comments_clone.deref().clone();
4849
if let Some(idx) = new_comments.iter().position(|c| c.id == id) {
50+
log!("IDXXXX: ", idx);
4951
new_comments.remove(idx);
5052
comments_clone.set(new_comments);
5153
}
@@ -88,7 +90,10 @@ pub fn post(props: &Props) -> Html {
8890
<Vote up_votes={props.post.up_votes.len()} down_votes={props.post.down_votes.len()} post_id={props.post.id}/>
8991

9092
// DESCRIPTION
91-
<div class="description">{description.deref()}</div>
93+
// <div class="description">{description.deref()}</div>
94+
<div class="description">
95+
<Markdown content={description.deref().clone()}/>
96+
</div>
9297

9398
// COMMENTS
9499
<AddComment post_id={props.post.id} on_comment_add={&on_comment_add}/>

devblog/devblog/client/src/pages/post_add.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{helpers, store::Store, Api};
1+
use crate::{components::markdown::Markdown, helpers, store::Store, Api};
22
use gloo_net::http::Method;
33
use std::ops::Deref;
44
use stylist::{css, Style};
@@ -199,9 +199,7 @@ pub fn add_post() -> Html {
199199
<p>{"Preview:"}</p>
200200
<div class="post-preview">
201201
<span>{"preview content:"}</span>
202-
<p>
203-
// {parser}
204-
</p>
202+
<Markdown content={description.deref().clone()}/>
205203
</div>
206204
<button>{"Create Post"}</button>
207205
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# hello world
2+
3+
---

0 commit comments

Comments
 (0)