mod chrome;
pub mod highlight;
#[cfg(feature = "mermaid")]
pub mod mermaid;
use std::rc::Rc;
use gpui::{AnyElement, App, Global, SharedString, Window};
pub(crate) use chrome::{CODE_PADDING_X, CODE_PADDING_Y, copy_button};
pub use chrome::{NOTE_SELECTOR, fence_band, fence_note, fence_panel};
pub type Rewrite = Rc<dyn Fn(String, &mut Window, &mut App)>;
pub struct Fence<'a> {
pub language: &'a str,
pub code: &'a str,
pub height: Option<u32>,
pub rewrite: Option<Rewrite>,
}
pub type BlockRenderer = fn(&Fence<'_>, &mut Window, &mut App) -> Option<AnyElement>;
struct Installed(BlockRenderer);
impl Global for Installed {}
pub(crate) fn set_block_renderer(cx: &mut App, renderer: BlockRenderer) {
cx.set_global(Installed(renderer));
}
pub(crate) enum Painted {
Block(AnyElement),
Code(Option<SharedString>),
}
pub(crate) fn render(
fence: &Fence<'_>,
#[cfg_attr(not(feature = "mermaid"), allow(unused_variables))] block: usize,
window: &mut Window,
cx: &mut App,
) -> Painted {
if let Some(renderer) = cx.try_global::<Installed>().map(|installed| installed.0)
&& let Some(painted) = renderer(fence, window, cx)
{
return Painted::Block(painted);
}
#[cfg(feature = "mermaid")]
if fence.language == self::mermaid::LANGUAGE {
return match self::mermaid::render(fence.code, fence.height, block, window, cx) {
Ok(painted) => Painted::Block(painted),
Err(note) => Painted::Code(Some(note)),
};
}
Painted::Code(None)
}