blocks/lib.rs
1//! Painted fenced blocks for `markdown`.
2//!
3//! ```ignore
4//! markdown::set_block_renderer(cx, 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
28/// Paint the block a fence names, or `None` to leave it to the ordinary code
29/// block.
30///
31/// One answer for a tag no enabled block claims, a block turned off at compile
32/// time, and a block that read the source and declined.
33pub fn render(language: &str, code: &str, window: &mut Window, cx: &mut App) -> Option<AnyElement> {
34 match language {
35 #[cfg(feature = "chart")]
36 chart::LANGUAGE => chart::render(code, window, cx),
37 // Spelled out rather than bare, so that turning every block off leaves
38 // a signature nothing reads and no warning about it.
39 _ => {
40 let _ = (code, window, cx);
41 None
42 }
43 }
44}
45
46/// The fence tags the enabled blocks answer to, for a language picker that
47/// would otherwise offer a block this build cannot paint.
48pub fn languages() -> &'static [&'static str] {
49 &[
50 #[cfg(feature = "chart")]
51 chart::LANGUAGE,
52 ]
53}