Skip to content

Commit 36f5d29

Browse files
authored
Merge pull request #15 from YGGverse/markdown
Markdown MIME type support
2 parents 71f2597 + 12a557e commit 36f5d29

29 files changed

Lines changed: 2293 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ openssl = "0.10.72"
4141
plurify = "0.2.0"
4242
r2d2 = "0.8.10"
4343
r2d2_sqlite = "0.32.0"
44+
regex = "1.12.3"
4445
syntect = "5.2.0"
4546

4647
# development

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ The Gemini protocol was designed as a minimalistic, tracking-resistant alternati
135135

136136
#### Text
137137
* [x] `text/gemini`
138-
* [x] `text/plain`
138+
* [x] `text/markdown`
139139
* [x] `text/nex`
140+
* [x] `text/plain`
140141

141142
#### Images
142143
* [x] `image/gif`

src/app/browser/window/tab/item/client/driver/file.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ impl File {
7171
.set_mime(Some(content_type.to_string()));
7272
}
7373
match content_type.as_str() {
74+
"text/gemini" => {
75+
if matches!(*feature, Feature::Source) {
76+
load_contents_async(file, cancellable, move |result| {
77+
match result {
78+
Ok(data) => {
79+
Text::Source(uri, data).handle(&page)
80+
}
81+
Err(message) => {
82+
Status::Failure(message).handle(&page)
83+
}
84+
}
85+
})
86+
} else {
87+
load_contents_async(file, cancellable, move |result| {
88+
match result {
89+
Ok(data) => {
90+
Text::Gemini(uri, data).handle(&page)
91+
}
92+
Err(message) => {
93+
Status::Failure(message).handle(&page)
94+
}
95+
}
96+
})
97+
}
98+
}
7499
"text/plain" => {
75100
if matches!(*feature, Feature::Source) {
76101
load_contents_async(file, cancellable, move |result| {
@@ -94,6 +119,18 @@ impl File {
94119
}
95120
}
96121
});
122+
} else if url.ends_with(".md") || url.ends_with(".markdown")
123+
{
124+
load_contents_async(file, cancellable, move |result| {
125+
match result {
126+
Ok(data) => {
127+
Text::Markdown(uri, data).handle(&page)
128+
}
129+
Err(message) => {
130+
Status::Failure(message).handle(&page)
131+
}
132+
}
133+
})
97134
} else {
98135
load_contents_async(file, cancellable, move |result| {
99136
match result {
@@ -107,6 +144,31 @@ impl File {
107144
})
108145
}
109146
}
147+
"text/markdown" => {
148+
if matches!(*feature, Feature::Source) {
149+
load_contents_async(file, cancellable, move |result| {
150+
match result {
151+
Ok(data) => {
152+
Text::Source(uri, data).handle(&page)
153+
}
154+
Err(message) => {
155+
Status::Failure(message).handle(&page)
156+
}
157+
}
158+
})
159+
} else {
160+
load_contents_async(file, cancellable, move |result| {
161+
match result {
162+
Ok(data) => {
163+
Text::Markdown(uri, data).handle(&page)
164+
}
165+
Err(message) => {
166+
Status::Failure(message).handle(&page)
167+
}
168+
}
169+
})
170+
}
171+
}
110172
"image/png" | "image/gif" | "image/jpeg" | "image/webp" => {
111173
match gtk::gdk::Texture::from_file(&file) {
112174
Ok(texture) => {

src/app/browser/window/tab/item/client/driver/file/text.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use gtk::glib::Uri;
22

33
pub enum Text {
44
Gemini(Uri, String),
5+
Markdown(Uri, String),
56
Plain(Uri, String),
67
Source(Uri, String),
78
}
@@ -22,6 +23,14 @@ impl Text {
2223
.set_mime(Some("text/gemini".to_string()));
2324
page.content.to_text_gemini(uri, data)
2425
}),
26+
Self::Markdown(uri, data) => (uri, {
27+
page.navigation
28+
.request
29+
.info
30+
.borrow_mut()
31+
.set_mime(Some("text/markdown".to_string()));
32+
page.content.to_text_markdown(uri, data)
33+
}),
2534
Self::Plain(uri, data) => (uri, page.content.to_text_plain(data)),
2635
Self::Source(uri, data) => (uri, page.content.to_text_source(data)),
2736
};

src/app/browser/window/tab/item/client/driver/gemini.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ fn handle(
358358
} else {
359359
match m.as_str() {
360360
"text/gemini" => page.content.to_text_gemini(&uri, data),
361+
"text/markdown" => page.content.to_text_markdown(&uri, data),
361362
"text/plain" => page.content.to_text_plain(data),
362363
_ => panic!() // unexpected
363364
}

src/app/browser/window/tab/item/page/content.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ impl Content {
154154
}
155155
}
156156

157+
/// `text/markdown`
158+
pub fn to_text_markdown(&self, base: &Uri, data: &str) -> Text {
159+
self.clean();
160+
let m = Text::markdown((&self.window_action, &self.item_action), base, data);
161+
self.g_box.append(&m.scrolled_window);
162+
m
163+
}
164+
157165
/// `text/plain`
158166
pub fn to_text_plain(&self, data: &str) -> Text {
159167
self.clean();

src/app/browser/window/tab/item/page/content/directory/column/format.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ impl Format for FileInfo {
1515
if content_type == "text/plain" {
1616
if display_name.ends_with(".gmi") || display_name.ends_with(".gemini") {
1717
"text/gemini".into()
18+
} else if display_name.ends_with(".md") || display_name.ends_with(".markdown") {
19+
"text/markdown".into()
1820
} else {
1921
content_type
2022
}

src/app/browser/window/tab/item/page/content/text.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod gemini;
2+
mod markdown;
23
mod nex;
34
mod plain;
45
mod source;
@@ -7,6 +8,7 @@ use super::{ItemAction, WindowAction};
78
use adw::ClampScrollable;
89
use gemini::Gemini;
910
use gtk::{ScrolledWindow, TextView, glib::Uri};
11+
use markdown::Markdown;
1012
use nex::Nex;
1113
use plain::Plain;
1214
use source::Source;
@@ -51,6 +53,21 @@ impl Text {
5153
}
5254
}
5355

56+
pub fn markdown(
57+
actions: (&Rc<WindowAction>, &Rc<ItemAction>),
58+
base: &Uri,
59+
gemtext: &str,
60+
) -> Self {
61+
let markdown = Markdown::build(actions, base, gemtext);
62+
Self {
63+
scrolled_window: reader(&markdown.text_view),
64+
text_view: markdown.text_view,
65+
meta: Meta {
66+
title: markdown.title,
67+
},
68+
}
69+
}
70+
5471
pub fn plain(data: &str) -> Self {
5572
let text_view = TextView::plain(data);
5673
Self {

src/app/browser/window/tab/item/page/content/text/gemini.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl Gemini {
217217
// Is link
218218
if let Some(link) = ggemtext::line::Link::parse(line) {
219219
if let Some(uri) = link.uri(Some(base)) {
220-
let mut alt = Vec::new();
220+
let mut alt = Vec::with_capacity(2);
221221

222222
if uri.scheme() != base.scheme() {
223223
alt.push("⇖".to_string());

0 commit comments

Comments
 (0)