From af33926eb0e578c7626648948e2ee05f21eb2d0b Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 5 Mar 2024 20:32:15 -0500 Subject: [PATCH] add basic markdown support --- devblog/devblog/client/Cargo.lock | 118 +----------------- devblog/devblog/client/Cargo.toml | 4 - .../devblog/client/src/components/markdown.rs | 30 +++++ devblog/devblog/client/src/components/mod.rs | 1 + devblog/devblog/client/src/components/post.rs | 11 +- devblog/devblog/client/src/pages/post_add.rs | 6 +- devblog/devblog/client/src/pages/test.md | 3 + 7 files changed, 45 insertions(+), 128 deletions(-) create mode 100644 devblog/devblog/client/src/components/markdown.rs create mode 100644 devblog/devblog/client/src/pages/test.md diff --git a/devblog/devblog/client/Cargo.lock b/devblog/devblog/client/Cargo.lock index c6d538f..124335e 100644 --- a/devblog/devblog/client/Cargo.lock +++ b/devblog/devblog/client/Cargo.lock @@ -127,17 +127,13 @@ name = "client" version = "0.1.0" dependencies = [ "chrono", - "cookie", "gloo 0.11.0", "gloo-net 0.5.0", - "reqwasm", "serde", - "serde-wasm-bindgen 0.6.3", "serde_json", "stylist", "wasm-bindgen", "wasm-bindgen-futures", - "wasm-cookies", "web-sys", "yew 0.21.0", "yew-router", @@ -155,16 +151,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "cookie" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cd91cf61412820176e137621345ee43b3f4423e589e7ae4e50d601d93e35ef8" -dependencies = [ - "time", - "version_check", -] - [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -206,15 +192,6 @@ dependencies = [ "syn 2.0.48", ] -[[package]] -name = "deranged" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" -dependencies = [ - "powerfmt", -] - [[package]] name = "either" version = "1.9.0" @@ -541,26 +518,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "gloo-net" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2899cb1a13be9020b010967adc6b2a8a343b6f1428b90238c9d53ca24decc6db" -dependencies = [ - "futures-channel", - "futures-core", - "futures-sink", - "gloo-utils 0.1.7", - "js-sys", - "pin-project", - "serde", - "serde_json", - "thiserror", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", -] - [[package]] name = "gloo-net" version = "0.3.1" @@ -981,12 +938,6 @@ dependencies = [ "minimal-lexical", ] -[[package]] -name = "num-conv" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" - [[package]] name = "num-traits" version = "0.2.17" @@ -1070,12 +1021,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "powerfmt" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" - [[package]] name = "prettyplease" version = "0.2.16" @@ -1155,15 +1100,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "reqwasm" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05b89870d729c501fa7a68c43bf4d938bbb3a8c156d333d90faa0e8b3e3212fb" -dependencies = [ - "gloo-net 0.1.0", -] - [[package]] name = "route-recognizer" version = "0.3.1" @@ -1363,37 +1299,6 @@ dependencies = [ "syn 2.0.48", ] -[[package]] -name = "time" -version = "0.3.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" -dependencies = [ - "deranged", - "itoa", - "num-conv", - "powerfmt", - "serde", - "time-core", - "time-macros", -] - -[[package]] -name = "time-core" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" - -[[package]] -name = "time-macros" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" -dependencies = [ - "num-conv", - "time-core", -] - [[package]] name = "tokio" version = "1.35.1" @@ -1469,12 +1374,6 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" -[[package]] -name = "urlencoding" -version = "1.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a1f0175e03a0973cf4afd476bef05c26e228520400eb1fd473ad417b1c00ffb" - [[package]] name = "urlencoding" version = "2.1.3" @@ -1500,8 +1399,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406" dependencies = [ "cfg-if", - "serde", - "serde_json", "wasm-bindgen-macro", ] @@ -1561,19 +1458,6 @@ version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" -[[package]] -name = "wasm-cookies" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "792b7c4fd62cde421f0e757620053efa7b0c23441c05c6ec02baf96cd74973be" -dependencies = [ - "chrono", - "js-sys", - "urlencoding 1.3.3", - "wasm-bindgen", - "web-sys", -] - [[package]] name = "web-sys" version = "0.3.67" @@ -1763,7 +1647,7 @@ dependencies = [ "serde", "serde_urlencoded", "tracing", - "urlencoding 2.1.3", + "urlencoding", "wasm-bindgen", "web-sys", "yew 0.21.0", diff --git a/devblog/devblog/client/Cargo.toml b/devblog/devblog/client/Cargo.toml index dfce2bd..beb0117 100644 --- a/devblog/devblog/client/Cargo.toml +++ b/devblog/devblog/client/Cargo.toml @@ -7,17 +7,13 @@ edition = "2021" [dependencies] chrono = { version = "0.4.33", features = ["serde"] } -cookie = "0.18.0" gloo = "0.11.0" gloo-net = "0.5.0" -reqwasm = "0.5.0" serde = "1.0.196" -serde-wasm-bindgen = "0.6.3" serde_json = "1.0.113" stylist = { version = "0.13.0", features = ["yew", "parser"] } wasm-bindgen = "0.2.90" wasm-bindgen-futures = "0.4.40" -wasm-cookies = "0.2.1" web-sys = { version = "0.3.67", features = ["HtmlInputElement"] } yew = { version = "0.21.0", features = ["csr"] } yew-router = "0.18.0" diff --git a/devblog/devblog/client/src/components/markdown.rs b/devblog/devblog/client/src/components/markdown.rs new file mode 100644 index 0000000..cb8cc22 --- /dev/null +++ b/devblog/devblog/client/src/components/markdown.rs @@ -0,0 +1,30 @@ +use yew::{function_component, html, Html, Properties}; + +#[derive(Properties, PartialEq, Clone)] +pub struct Props { + pub content: String, +} + +#[function_component(Markdown)] +pub fn markdown(props: &Props) -> Html { + let html = props.content.lines().map(|line| match line { + l if l.starts_with("# ") => html! {

{&l[1..]}

}, + l if l.starts_with("## ") => html! {

{&l[2..]}

}, + l if l.starts_with("### ") => html! {

{&l[3..]}

}, + l if l.starts_with("#### ") => html! {

{&l[4..]}

}, + l if l.starts_with("```") => { + html! {

{line.replace("```", "")}

} + } + l if l.starts_with("---") => html! {
}, + l if l.starts_with("___") => html! {
}, + l if l.starts_with("-") => html! {
  • {&l[1..]}
  • }, + l if l.is_empty() => html! {
    }, + _ => html! {

    {line}

    }, + }); + + html! { +
    + {for html} +
    + } +} diff --git a/devblog/devblog/client/src/components/mod.rs b/devblog/devblog/client/src/components/mod.rs index ce9de86..260f120 100644 --- a/devblog/devblog/client/src/components/mod.rs +++ b/devblog/devblog/client/src/components/mod.rs @@ -4,6 +4,7 @@ pub mod comment_delete; pub mod comment_edit; pub mod footer; pub mod items; +pub mod markdown; pub mod navbar; pub mod notifications; pub mod pager; diff --git a/devblog/devblog/client/src/components/post.rs b/devblog/devblog/client/src/components/post.rs index 4463383..5120557 100644 --- a/devblog/devblog/client/src/components/post.rs +++ b/devblog/devblog/client/src/components/post.rs @@ -1,11 +1,12 @@ use crate::{ components::{ - comment::Comment, comment_add::AddComment, post_delete::DeletePost, post_edit::EditPost, - vote::Vote, + comment::Comment, comment_add::AddComment, markdown::Markdown, post_delete::DeletePost, + post_edit::EditPost, vote::Vote, }, CommentModel, PostModel, }; use chrono::{Local, TimeZone}; +use gloo::console::log; use std::ops::Deref; use stylist::Style; use yew::{function_component, html, use_effect_with, use_state, Callback, Html, Properties}; @@ -46,6 +47,7 @@ pub fn post(props: &Props) -> Html { Callback::from(move |id| { let mut new_comments = comments_clone.deref().clone(); if let Some(idx) = new_comments.iter().position(|c| c.id == id) { + log!("IDXXXX: ", idx); new_comments.remove(idx); comments_clone.set(new_comments); } @@ -88,7 +90,10 @@ pub fn post(props: &Props) -> Html { // DESCRIPTION -
    {description.deref()}
    + //
    {description.deref()}
    +
    + +
    // COMMENTS diff --git a/devblog/devblog/client/src/pages/post_add.rs b/devblog/devblog/client/src/pages/post_add.rs index 955748d..0b0b4da 100644 --- a/devblog/devblog/client/src/pages/post_add.rs +++ b/devblog/devblog/client/src/pages/post_add.rs @@ -1,4 +1,4 @@ -use crate::{helpers, store::Store, Api}; +use crate::{components::markdown::Markdown, helpers, store::Store, Api}; use gloo_net::http::Method; use std::ops::Deref; use stylist::{css, Style}; @@ -199,9 +199,7 @@ pub fn add_post() -> Html {

    {"Preview:"}

    {"preview content:"} -

    - // {parser} -

    +
    diff --git a/devblog/devblog/client/src/pages/test.md b/devblog/devblog/client/src/pages/test.md new file mode 100644 index 0000000..9cec771 --- /dev/null +++ b/devblog/devblog/client/src/pages/test.md @@ -0,0 +1,3 @@ +# hello world + +--- \ No newline at end of file