Skip to content

Commit 4bbb5cb

Browse files
authored
Merge pull request #2786 from iced-rs/customizable-markdown
Customizable Markdown Rendering and Image Support
2 parents 1f9723a + ef25dfb commit 4bbb5cb

File tree

16 files changed

+877
-233
lines changed

16 files changed

+877
-233
lines changed

Cargo.lock

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

core/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,12 @@ pub use vector::Vector;
8484
pub use widget::Widget;
8585

8686
pub use smol_str::SmolStr;
87+
88+
/// A function that can _never_ be called.
89+
///
90+
/// This is useful to turn generic types into anything
91+
/// you want by coercing them into a type with no possible
92+
/// values.
93+
pub fn never<T>(never: std::convert::Infallible) -> T {
94+
match never {}
95+
}

core/src/padding.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,9 @@ impl From<Padding> for Size {
202202
Self::new(padding.horizontal(), padding.vertical())
203203
}
204204
}
205+
206+
impl From<Pixels> for Padding {
207+
fn from(pixels: Pixels) -> Self {
208+
Self::from(pixels.0)
209+
}
210+
}

core/src/pixels.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@ impl std::ops::Div<f32> for Pixels {
7979
Pixels(self.0 / rhs)
8080
}
8181
}
82+
83+
impl std::ops::Div<u32> for Pixels {
84+
type Output = Pixels;
85+
86+
fn div(self, rhs: u32) -> Self {
87+
Pixels(self.0 / rhs as f32)
88+
}
89+
}

examples/changelog/src/main.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -267,25 +267,21 @@ impl Generator {
267267
} => {
268268
let details = {
269269
let title = rich_text![
270-
span(&pull_request.title).size(24).link(
271-
Message::OpenPullRequest(pull_request.id)
272-
),
270+
span(&pull_request.title)
271+
.size(24)
272+
.link(pull_request.id),
273273
span(format!(" by {}", pull_request.author))
274274
.font(Font {
275275
style: font::Style::Italic,
276276
..Font::default()
277277
}),
278278
]
279+
.on_link_click(Message::OpenPullRequest)
279280
.font(Font::MONOSPACE);
280281

281-
let description = markdown::view(
282-
description,
283-
markdown::Settings::default(),
284-
markdown::Style::from_palette(
285-
self.theme().palette(),
286-
),
287-
)
288-
.map(Message::UrlClicked);
282+
let description =
283+
markdown(description, self.theme())
284+
.map(Message::UrlClicked);
289285

290286
let labels =
291287
row(pull_request.labels.iter().map(|label| {
@@ -348,11 +344,11 @@ impl Generator {
348344
} else {
349345
container(
350346
scrollable(
351-
markdown::view(
347+
markdown(
352348
preview,
353-
markdown::Settings::with_text_size(12),
354-
markdown::Style::from_palette(
355-
self.theme().palette(),
349+
markdown::Settings::with_text_size(
350+
12,
351+
self.theme(),
356352
),
357353
)
358354
.map(Message::UrlClicked),

examples/markdown/Cargo.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ publish = false
77

88
[dependencies]
99
iced.workspace = true
10-
iced.features = ["markdown", "highlighter", "tokio", "debug"]
10+
iced.features = ["markdown", "highlighter", "image", "tokio", "debug"]
11+
12+
reqwest.version = "0.12"
13+
reqwest.features = ["json"]
14+
15+
image.workspace = true
16+
tokio.workspace = true
1117

1218
open = "5.3"
19+
20+
# Disabled to keep amount of build dependencies low
21+
# This can be re-enabled on demand
22+
# [build-dependencies]
23+
# iced_fontello = "0.13"

examples/markdown/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn main() {
2+
// println!("cargo::rerun-if-changed=fonts/markdown-icons.toml");
3+
// iced_fontello::build("fonts/markdown-icons.toml")
4+
// .expect("Build icons font");
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module = "icon"
2+
3+
[glyphs]
4+
copy = "fontawesome-docs"
5.72 KB
Binary file not shown.

examples/markdown/src/icon.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Generated automatically by iced_fontello at build time.
2+
// Do not edit manually. Source: ../fonts/markdown-icons.toml
3+
// dcd2f0c969d603e2ee9237a4b70fa86b1a6e84d86f4689046d8fdd10440b06b9
4+
use iced::widget::{text, Text};
5+
use iced::Font;
6+
7+
pub const FONT: &[u8] = include_bytes!("../fonts/markdown-icons.ttf");
8+
9+
pub fn copy<'a>() -> Text<'a> {
10+
icon("\u{F0C5}")
11+
}
12+
13+
fn icon(codepoint: &str) -> Text<'_> {
14+
text(codepoint).font(Font::with_name("markdown-icons"))
15+
}

0 commit comments

Comments
 (0)