blocks/lib.rs
1//! Painted fenced blocks for `markdown`.
2//!
3//! ```ignore
4//! cx.set_block_renderer(blocks::render); // once, at boot
5//! ```
6//!
7//! A fence already round trips byte for byte, already holds a caret, and
8//! already degrades to its own source where nothing paints it — so a block of
9//! an app's own is a renderer over ` ```chart ` rather than a new
10//! `markdown::BlockKind`. This crate is one answer to that seam; an app with
11//! its own block writes the same function and never depends on this.
12//!
13//! It is a peer crate rather than a feature on `markdown` for the reason
14//! `syntax` is: cargo features are additive across the whole graph, so a
15//! `markdown/mermaid` any dependency turned on is one no consumer can turn back
16//! off, and a block carrying a parser would break a target nobody asked about.
17//! A crate you do not name costs nothing.
18//!
19//! `markdown` is not a dependency here. A renderer is a fence tag and a string
20//! in, an element out, which needs no document model — the consumer's call to
21//! `set_block_renderer` is what pins the signature.
22
23use gpui::{AnyElement, App, Window};
24
25#[cfg(feature = "chart")]
26pub mod chart;
27#[cfg(feature = "vega-lite")]
28pub mod vega_lite;
29
30/// Paint the block a fence names, or `None` to leave it to the ordinary code
31/// block. `height` is the fence's stated height in whole pixels, for a block
32/// that takes one.
33///
34/// One answer for a tag no enabled block claims, a block turned off at compile
35/// time, and a block that read the source and declined.
36pub fn render(
37 language: &str,
38 code: &str,
39 height: Option<u32>,
40 window: &mut Window,
41 cx: &mut App,
42) -> Option<AnyElement> {
43 match language {
44 #[cfg(feature = "chart")]
45 chart::LANGUAGE => chart::render(code, window, cx),
46 #[cfg(feature = "vega-lite")]
47 vega_lite::LANGUAGE => vega_lite::render(code, height, window, cx),
48 // Spelled out rather than bare, so that turning every block off leaves
49 // a signature nothing reads and no warning about it.
50 _ => {
51 let _ = (code, height, window, cx);
52 None
53 }
54 }
55}
56
57/// The fence tags the enabled blocks answer to, for a language picker that
58/// would otherwise offer a block this build cannot paint.
59pub fn languages() -> &'static [&'static str] {
60 &[
61 #[cfg(feature = "chart")]
62 chart::LANGUAGE,
63 #[cfg(feature = "vega-lite")]
64 vega_lite::LANGUAGE,
65 ]
66}