Skip to content

Commit

Permalink
Add language to Item::CodeBlock in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Feb 5, 2025
1 parent f8c71a2 commit c7711e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
20 changes: 11 additions & 9 deletions examples/markdown/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use iced::highlighter;
use iced::task;
use iced::time::{self, milliseconds, Instant};
use iced::widget::{
self, button, center_x, horizontal_space, hover, image, markdown, pop,
right, row, scrollable, text_editor, toggler,
self, button, center_x, container, horizontal_space, hover, image,
markdown, pop, right, row, scrollable, text_editor, toggler,
};
use iced::window;
use iced::{Animation, Element, Fill, Font, Subscription, Task, Theme};
Expand Down Expand Up @@ -294,20 +294,22 @@ impl<'a> markdown::Viewer<'a, Message> for CustomViewer<'a> {
fn code_block(
&self,
settings: markdown::Settings,
_language: Option<&'a str>,
code: &'a str,
lines: &'a [markdown::Text],
) -> Element<'a, Message> {
let code_block =
markdown::code_block(settings, code, lines, Message::LinkClicked);
markdown::code_block(settings, lines, Message::LinkClicked);

let copy = button(icon::copy().size(12))
.padding(2)
.on_press_with(|| Message::Copy(code.to_owned()))
.style(button::text);

hover(
code_block,
right(
button(icon::copy().size(12))
.padding(settings.spacing / 2)
.on_press_with(|| Message::Copy(code.to_owned()))
.style(button::text),
),
right(container(copy).style(container::dark))
.padding(settings.spacing / 2),
)
}
}
Expand Down
28 changes: 20 additions & 8 deletions widget/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ pub enum Item {
///
/// You can enable the `highlighter` feature for syntax highlighting.
CodeBlock {
/// The language of the code block, if any.
language: Option<String>,
/// The raw code of the code block.
code: String,
/// The styled lines of text in the code block.
Expand Down Expand Up @@ -464,6 +466,7 @@ fn parse_with<'a>(

let mut spans = Vec::new();
let mut code = String::new();
let mut code_language = None;
let mut code_lines = Vec::new();
let mut strong = false;
let mut emphasis = false;
Expand Down Expand Up @@ -603,7 +606,7 @@ fn parse_with<'a>(
None
}
pulldown_cmark::Tag::CodeBlock(
pulldown_cmark::CodeBlockKind::Fenced(_language),
pulldown_cmark::CodeBlockKind::Fenced(language),
) if !metadata && !table => {
#[cfg(feature = "highlighter")]
{
Expand All @@ -613,16 +616,19 @@ fn parse_with<'a>(
.highlighter
.take()
.filter(|highlighter| {
highlighter.language == _language.as_ref()
highlighter.language == language.as_ref()
})
.unwrap_or_else(|| Highlighter::new(&_language));
.unwrap_or_else(|| Highlighter::new(&language));

highlighter.prepare();

highlighter
});
}

code_language =
(!language.is_empty()).then(|| language.into_string());

let prev = if spans.is_empty() {
None
} else {
Expand Down Expand Up @@ -734,6 +740,7 @@ fn parse_with<'a>(
state.borrow_mut(),
&mut stack,
Item::CodeBlock {
language: code_language.take(),
code: mem::take(&mut code),
lines: code_lines.drain(..).collect(),
},
Expand Down Expand Up @@ -1029,9 +1036,11 @@ where
viewer.heading(settings, level, text, index)
}
Item::Paragraph(text) => viewer.paragraph(settings, text),
Item::CodeBlock { code, lines } => {
viewer.code_block(settings, code, lines)
}
Item::CodeBlock {
language,
code,
lines,
} => viewer.code_block(settings, language.as_deref(), code, lines),
Item::List { start: None, items } => {
viewer.unordered_list(settings, items)
}
Expand Down Expand Up @@ -1171,7 +1180,6 @@ where
/// Displays a code block using the default look.
pub fn code_block<'a, Message, Theme, Renderer>(
settings: Settings,
_code: &'a str,
lines: &'a [Text],
on_link_click: impl Fn(Url) -> Message + Clone + 'a,
) -> Element<'a, Message, Theme, Renderer>
Expand Down Expand Up @@ -1266,10 +1274,14 @@ where
fn code_block(
&self,
settings: Settings,
language: Option<&'a str>,
code: &'a str,
lines: &'a [Text],
) -> Element<'a, Message, Theme, Renderer> {
code_block(settings, code, lines, Self::on_link_click)
let _language = language;
let _code = code;

code_block(settings, lines, Self::on_link_click)
}

/// Displays an unordered list.
Expand Down

0 comments on commit c7711e5

Please sign in to comment.