Skip to content

Commit

Permalink
Merge pull request #2786 from iced-rs/customizable-markdown
Browse files Browse the repository at this point in the history
Customizable Markdown Rendering and Image Support
  • Loading branch information
hecrj authored Feb 5, 2025
2 parents 1f9723a + ef25dfb commit 4bbb5cb
Show file tree
Hide file tree
Showing 16 changed files with 877 additions and 233 deletions.
11 changes: 7 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,12 @@ pub use vector::Vector;
pub use widget::Widget;

pub use smol_str::SmolStr;

/// A function that can _never_ be called.
///
/// This is useful to turn generic types into anything
/// you want by coercing them into a type with no possible
/// values.
pub fn never<T>(never: std::convert::Infallible) -> T {
match never {}
}
6 changes: 6 additions & 0 deletions core/src/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,9 @@ impl From<Padding> for Size {
Self::new(padding.horizontal(), padding.vertical())
}
}

impl From<Pixels> for Padding {
fn from(pixels: Pixels) -> Self {
Self::from(pixels.0)
}
}
8 changes: 8 additions & 0 deletions core/src/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,11 @@ impl std::ops::Div<f32> for Pixels {
Pixels(self.0 / rhs)
}
}

impl std::ops::Div<u32> for Pixels {
type Output = Pixels;

fn div(self, rhs: u32) -> Self {
Pixels(self.0 / rhs as f32)
}
}
26 changes: 11 additions & 15 deletions examples/changelog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,21 @@ impl Generator {
} => {
let details = {
let title = rich_text![
span(&pull_request.title).size(24).link(
Message::OpenPullRequest(pull_request.id)
),
span(&pull_request.title)
.size(24)
.link(pull_request.id),
span(format!(" by {}", pull_request.author))
.font(Font {
style: font::Style::Italic,
..Font::default()
}),
]
.on_link_click(Message::OpenPullRequest)
.font(Font::MONOSPACE);

let description = markdown::view(
description,
markdown::Settings::default(),
markdown::Style::from_palette(
self.theme().palette(),
),
)
.map(Message::UrlClicked);
let description =
markdown(description, self.theme())
.map(Message::UrlClicked);

let labels =
row(pull_request.labels.iter().map(|label| {
Expand Down Expand Up @@ -348,11 +344,11 @@ impl Generator {
} else {
container(
scrollable(
markdown::view(
markdown(
preview,
markdown::Settings::with_text_size(12),
markdown::Style::from_palette(
self.theme().palette(),
markdown::Settings::with_text_size(
12,
self.theme(),
),
)
.map(Message::UrlClicked),
Expand Down
13 changes: 12 additions & 1 deletion examples/markdown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ publish = false

[dependencies]
iced.workspace = true
iced.features = ["markdown", "highlighter", "tokio", "debug"]
iced.features = ["markdown", "highlighter", "image", "tokio", "debug"]

reqwest.version = "0.12"
reqwest.features = ["json"]

image.workspace = true
tokio.workspace = true

open = "5.3"

# Disabled to keep amount of build dependencies low
# This can be re-enabled on demand
# [build-dependencies]
# iced_fontello = "0.13"
5 changes: 5 additions & 0 deletions examples/markdown/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn main() {
// println!("cargo::rerun-if-changed=fonts/markdown-icons.toml");
// iced_fontello::build("fonts/markdown-icons.toml")
// .expect("Build icons font");
}
4 changes: 4 additions & 0 deletions examples/markdown/fonts/markdown-icons.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module = "icon"

[glyphs]
copy = "fontawesome-docs"
Binary file added examples/markdown/fonts/markdown-icons.ttf
Binary file not shown.
15 changes: 15 additions & 0 deletions examples/markdown/src/icon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Generated automatically by iced_fontello at build time.
// Do not edit manually. Source: ../fonts/markdown-icons.toml
// dcd2f0c969d603e2ee9237a4b70fa86b1a6e84d86f4689046d8fdd10440b06b9
use iced::widget::{text, Text};
use iced::Font;

pub const FONT: &[u8] = include_bytes!("../fonts/markdown-icons.ttf");

pub fn copy<'a>() -> Text<'a> {
icon("\u{F0C5}")
}

fn icon(codepoint: &str) -> Text<'_> {
text(codepoint).font(Font::with_name("markdown-icons"))
}
Loading

0 comments on commit 4bbb5cb

Please sign in to comment.