Skip to content

Commit

Permalink
Add explicit_image_uri_scheme method
Browse files Browse the repository at this point in the history
  • Loading branch information
lampsitter committed Sep 14, 2023
1 parent 7ae8297 commit 3d9494e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
- `CommonMarkCache` now implements `Debug` ([#7](https://github.com/lampsitter/egui_commonmark/pull/7) by [@ChristopherPerry6060](https://github.com/ChristopherPerry6060)).
- `CommonMarkCache::add_syntax_themes_from_folder`
- `CommonMarkCache::add_syntax_theme_from_bytes`
- `CommonMarkViewer::explicit_image_uri_scheme`

### Changed

- Use new image API from egui ([#8](https://github.com/lampsitter/egui_commonmark/pull/11) by [@jprochazk](https://github.com/jprochazk)).
- Use new image API from egui ([#11](https://github.com/lampsitter/egui_commonmark/pull/11) by [@jprochazk](https://github.com/jprochazk)).
- Updated dependencies.

### Removed
Expand Down
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ struct CommonMarkOptions {
theme_light: String,
#[cfg(feature = "syntax_highlighting")]
theme_dark: String,
use_explicit_uri_scheme: bool,
}

impl Default for CommonMarkOptions {
Expand All @@ -251,6 +252,7 @@ impl Default for CommonMarkOptions {
theme_light: DEFAULT_THEME_LIGHT.to_owned(),
#[cfg(feature = "syntax_highlighting")]
theme_dark: DEFAULT_THEME_DARK.to_owned(),
use_explicit_uri_scheme: false,
}
}
}
Expand Down Expand Up @@ -306,6 +308,15 @@ impl CommonMarkViewer {
self
}

/// By default any image without a uri scheme such as `foo://` is assumed to
/// be of the type `file://`. This assumption can sometimes be wrong or be done
/// incorrectly, so if you want to always be explicit with the scheme then set
/// this to `true`
pub fn explicit_image_uri_scheme(mut self, use_explicit: bool) -> Self {
self.options.use_explicit_uri_scheme = use_explicit;
self
}

#[cfg(feature = "syntax_highlighting")]
#[deprecated(note = "use `syntax_theme_light` or `syntax_theme_dark` instead")]
pub fn syntax_theme(mut self, theme: String) -> Self {
Expand Down Expand Up @@ -800,12 +811,13 @@ impl CommonMarkViewerInternal {
}
pulldown_cmark::Tag::Image(_, uri, _) => {
let has_scheme = uri.contains("://");
let uri = if has_scheme {
let uri = if options.use_explicit_uri_scheme || has_scheme {
uri.to_string()
} else {
// Assume file scheme
format!("file://{uri}")
};

self.start_image(uri);
}
}
Expand Down

0 comments on commit 3d9494e

Please sign in to comment.