Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staging #260

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@ jobs:
run: |
ssh -i ${{ secrets.SSH_PRIVATE_KEY_PATH }}/${{ secrets.SSH_PRIVATE_KEY_NAME }} ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "\
cd ${{ secrets.DEVBLOG_ROOT_PATH }} && \
docker-compose up -d --force-recreate"
docker compose up -d --force-recreate"
39 changes: 39 additions & 0 deletions client/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 client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
chrono = { version = "0.4.33", features = ["serde"] }
gloo = "0.11.0"
gloo-net = "0.5.0"
regex = "1.10.5"
serde = "1.0.196"
serde_json = "1.0.113"
stylist = { version = "0.13.0", features = ["yew", "parser"] }
Expand Down
64 changes: 43 additions & 21 deletions client/src/components/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
use stylist::Style;
use regex::Regex;
use yew::{function_component, html, Html, Properties};

#[derive(Properties, PartialEq, Clone)]
pub struct Props {
pub content: String,
}

const STYLE: &str = include_str!("styles/markdown.css");

#[function_component(Markdown)]
pub fn markdown(props: &Props) -> Html {
let style = Style::new(STYLE).unwrap();

let html = props.content.lines().map(|line| match line {
l if l.starts_with("# ") => html! {<h1>{&l[1..]}</h1>},
l if l.starts_with("## ") => html! {<h2>{&l[2..]}</h2>},
l if l.starts_with("### ") => html! {<h3>{&l[3..]}</h3>},
l if l.starts_with("#### ") => html! {<h4>{&l[4..]}</h4>},
l if l.starts_with("```") => {
html! {<p><code lang="rust">{line.replace("```", "")}</code></p>}
let link_regex = Regex::new(r"\[([^\]]+)\]\((https?://[^\)]+)\)").unwrap();

let html = props.content.lines().map(|line| {
// replace markdown links with anchor tags
// eg: [thedevblog](https://thedevblog.net) -> <a href="https://thedevblog.net">thedevblog</a>
let mut line_html = vec![];
let mut last_pos = 0;
for chars in link_regex.captures_iter(line) {
let mat = chars.get(0).unwrap();
let before_match = &line[last_pos..mat.start()];
let link_text = &chars[1];
let link_url = &chars[2];

if !before_match.is_empty() {
line_html.push(html! {<>{before_match}</>});
}

line_html.push(html! {<a href={link_url.to_string()}>{link_text}</a>});
last_pos = mat.end();
}

if last_pos < line.len() {
line_html.push(html! {<>{&line[last_pos..]}</>});
}

let combined_html = html! { <>{for line_html}</> };

match line {
l if l.starts_with("# ") => html! {<h1>{&l[1..]}</h1>},
l if l.starts_with("## ") => html! {<h2>{&l[2..]}</h2>},
l if l.starts_with("### ") => html! {<h3>{&l[3..]}</h3>},
l if l.starts_with("#### ") => html! {<h4>{&l[4..]}</h4>},
l if l.starts_with("```") => {
html! {<p><code lang="rust">{line.replace("```", "")}</code></p>}
}
l if l.starts_with("---") => html! {<hr/>},
l if l.starts_with("___") => html! {<hr/>},
l if l.starts_with("-") => html! {<li>{&l[1..]}</li>},
l if l.is_empty() => html! {<br/>},
_ => html! {<p>{combined_html}</p>},
}
l if l.starts_with("---") => html! {<hr/>},
l if l.starts_with("___") => html! {<hr/>},
l if l.starts_with("-") => html! {<li>{&l[1..]}</li>},
l if l.is_empty() => html! {<br/>},
_ => html! {<p>{line}</p>},
});

html! {
<div class={style}>
<div class="markdown">
{for html}
</div>
<div>
{for html}
</div>
}
}
Loading