Skip to content

Commit 2faa721

Browse files
committed
feat: Add (and default to-) darkmode. Can toggle to lightmode with flag.
1 parent 2bc8edf commit 2faa721

6 files changed

Lines changed: 69 additions & 17 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "meread"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
description = "preview github flavored markdown locally"
55
repository = "https://github.com/sermuns/meread"
66
license = "WTFPL"

assets/styles-light.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
:root {
2+
--borderColor-default: #d1d9e0;
3+
--borderColor-muted: #d1d9e0b3;
4+
5+
--bgColor-neutral-muted: #818b981f;
6+
--bgColor-default: #fff;
7+
--bgColor-muted: #f6f8fa;
8+
9+
--fgColor-default: #1f2328;
10+
--fgColor-muted: #59636e;
11+
}
12+
13+
/* --borderColor-default: #3d444d; */
14+
/* --borderColor-muted: #3d444db3; */
15+
/**/
16+
/* --bgColor-default: #0d1117; */
17+
/* --bgColor-muted: #151b23; */
18+
/**/
19+
/* --fgColor-default: #f0f6fc; */
20+
/* --fgColor-muted: #9198a1; */
21+
/* --fgColor-accent: #4493f8; */

assets/styles.css

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,21 @@
2626
}
2727

2828
:root {
29+
--borderColor-default: #3d444d;
30+
--borderColor-muted: #3d444db3;
31+
32+
--bgColor-default: #0d1117;
33+
--bgColor-muted: #151b23;
34+
--bgColor-neutral-muted: #656c7633;
35+
36+
--fgColor-default: #f0f6fc;
37+
--fgColor-muted: #9198a1;
38+
--fgColor-accent: #4493f8;
39+
40+
color: var(--fgColor-default);
41+
background-color: var(--bgColor-default);
2942
font-family: "Noto Sans", sans-serif;
30-
font-size: 16px;
31-
--borderColor-default: #d1d9e0;
32-
--borderColor-muted: #d1d9e0b3;
33-
--bgColor-default: #fff;
34-
--bgColor-muted: #f6f8fa;
43+
font-size: 17px;
3544
}
3645

3746
* {
@@ -81,9 +90,9 @@ code {
8190
border-radius: 6px;
8291
}
8392

84-
p > code {
85-
color: rgb(31, 35, 40);
86-
background-color: #f0f1f2;
93+
p > code,
94+
p > a > code {
95+
background-color: var(--bgColor-neutral-muted);
8796
}
8897

8998
/* footnote */
@@ -99,6 +108,7 @@ sup > a {
99108
h1,
100109
h2 {
101110
border-bottom: 1px solid var(--borderColor-default);
111+
padding-bottom: .3em;
102112
}
103113

104114
section.footnotes {
@@ -114,7 +124,7 @@ section.footnotes {
114124
}
115125

116126
a {
117-
color: #0969da;
127+
color: var(--fgColor-accent);
118128
text-underline-offset: 0.2rem;
119129
cursor: pointer;
120130
}
@@ -140,3 +150,9 @@ table {
140150
}
141151
}
142152
}
153+
154+
hr {
155+
height: 0.25em;
156+
background-color: var(--borderColor-default);
157+
border: 0;
158+
}

src/main.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ struct RenderedMarkdown {
6060
}
6161

6262
impl RenderedMarkdown {
63-
fn new(path: &Path) -> Result<Self> {
63+
fn new(path: &Path, light: bool) -> Result<Self> {
6464
let markdown_content = fs::read_to_string(path).context("Failed to read markdown file")?;
6565

6666
let rendered_markdown = render_markdown(
6767
&markdown_content,
6868
path.file_name().unwrap().to_str().unwrap(),
69+
light,
6970
)?;
7071

7172
Ok(Self {
@@ -74,13 +75,14 @@ impl RenderedMarkdown {
7475
})
7576
}
7677

77-
fn rebuild(&mut self) -> Result<()> {
78+
fn rebuild(&mut self, light: bool) -> Result<()> {
7879
let markdown_content =
7980
fs::read_to_string(&self.path).context("Failed to read markdown file")?;
8081

8182
self.content = render_markdown(
8283
&markdown_content,
8384
self.path.file_name().unwrap().to_str().unwrap(),
85+
light,
8486
)?;
8587

8688
Ok(())
@@ -144,6 +146,7 @@ async fn main() -> Result<()> {
144146
let rendered_html = render_markdown(
145147
&markdown_content,
146148
markdown_file_path.file_name().unwrap().to_str().unwrap(), // FIXME: horrible unwrap chain..
149+
args.light,
147150
)?;
148151

149152
fs::create_dir_all(export_dir).context("Failed to create export directory")?;
@@ -161,7 +164,10 @@ async fn main() -> Result<()> {
161164
return Ok(());
162165
}
163166

164-
let state = Arc::new(RwLock::new(RenderedMarkdown::new(&markdown_file_path)?));
167+
let state = Arc::new(RwLock::new(RenderedMarkdown::new(
168+
&markdown_file_path,
169+
args.light,
170+
)?));
165171

166172
use notify::EventKind::{Create, Modify, Remove};
167173
use notify_debouncer_full::{DebounceEventResult, new_debouncer};
@@ -185,7 +191,7 @@ async fn main() -> Result<()> {
185191
let state = Arc::clone(&state);
186192

187193
rt.spawn(async move {
188-
match state.write().await.rebuild() {
194+
match state.write().await.rebuild(args.light) {
189195
Ok(_) => {
190196
let _ = RELOAD_TX.send("reload".to_string());
191197
}
@@ -204,7 +210,7 @@ async fn main() -> Result<()> {
204210
.watch(root_path.as_path(), notify::RecursiveMode::Recursive)
205211
.with_context(|| format!("Failed to watch path: {}", root_path.display()))?;
206212

207-
state.write().await.rebuild()?;
213+
state.write().await.rebuild(args.light)?;
208214

209215
let app = Router::new()
210216
.route("/", get(serve))
@@ -268,6 +274,7 @@ async fn append_livereload_script(request: Request, next: Next) -> Response {
268274
struct HtmlTemplate<'a> {
269275
title: &'a str,
270276
contents: &'a str,
277+
light: bool,
271278
}
272279

273280
struct ComrakConfig {
@@ -333,7 +340,11 @@ fn init_comrak_config(light: bool) {
333340
let _ = COMRAK_CONFIG.set(ComrakConfig { options, plugins });
334341
}
335342

336-
fn render_markdown(markdown_content: &str, title: &str) -> Result<String, askama::Error> {
343+
fn render_markdown(
344+
markdown_content: &str,
345+
title: &str,
346+
light: bool,
347+
) -> Result<String, askama::Error> {
337348
let comrak_config = COMRAK_CONFIG.get().unwrap();
338349

339350
let rendered_markdown = comrak::markdown_to_html_with_plugins(
@@ -345,6 +356,7 @@ fn render_markdown(markdown_content: &str, title: &str) -> Result<String, askama
345356
HtmlTemplate {
346357
title,
347358
contents: &rendered_markdown,
359+
light,
348360
}
349361
.render()
350362
}

templates/template.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<link rel="stylesheet" href="/styles.css" />
7+
{%if light%}
8+
<link rel="stylesheet" href="/styles-light.css" />
9+
{%endif%}
710
<script src="/script.js"></script>
811
<title>{{ title }}</title>
912
</head>

0 commit comments

Comments
 (0)