Skip to main content

slack_blocks_render/
lib.rs

1//! Slack blocks render is a Rust library to render [Slack blocks](https://api.slack.com/reference/block-kit) as Markdown.
2//!
3//! # Usage
4//!
5//! First, add the `slack_blocks_render` crate as a dependency:
6//! ```sh
7//! cargo add slack_blocks_render
8//! ```
9//!
10//! Slack blocks render uses the `slack_morphism` data model as input so you should also add it as a dependency:
11//! ```sh
12//! cargo add slack_morphism
13//! ```
14//!
15//! ## Simple usage (without Slack references resolution)
16//! ```
17//! use slack_morphism::prelude::*;
18//! use slack_blocks_render::{render_blocks_as_markdown, SlackReferences};
19//!
20//! let blocks: Vec<SlackBlock> = vec![
21//!     SlackBlock::RichText(serde_json::from_value(serde_json::json!({
22//!         "elements": [
23//!             {
24//!                 "type": "rich_text_section",
25//!                 "elements": [
26//!                     {
27//!                         "type": "text",
28//!                         "text": "Hello World"
29//!                     }
30//!                 ]
31//!             },
32//!         ]
33//!     })).unwrap()),
34//! ];
35//! let markdown_text = render_blocks_as_markdown(blocks, SlackReferences::default(), None);
36//! ```
37//!
38//! ## Usage with Slack references resolution
39//!
40//! Slack references resolution is useful when you want to resolve user ID, channel ID, or user group ID in the Slack blocks.
41//! Here is an example on how to use it:
42//! ```
43//! use slack_morphism::prelude::*;
44//! use slack_blocks_render::{
45//!   find_slack_references_in_blocks, render_blocks_as_markdown, SlackReferences
46//! };
47//!
48//! let blocks: Vec<SlackBlock> = vec![
49//!     SlackBlock::RichText(serde_json::from_value(serde_json::json!({
50//!         "elements": [
51//!             {
52//!                 "type": "rich_text_section",
53//!                 "elements": [
54//!                     {
55//!                         "type": "text",
56//!                         "text": "Hello "
57//!                     }
58//!                 ]
59//!             },
60//!             {
61//!                 "type": "rich_text_section",
62//!                 "elements": [
63//!                     {
64//!                         "type": "user",
65//!                         "user_id": "U123456"
66//!                     }
67//!                 ]
68//!             },
69//!         ]
70//!     })).unwrap()),
71//! ];
72//! // First, extract Slack references from the blocks
73//! let slack_references = find_slack_references_in_blocks(&blocks);
74//! // Then, resolve the references before rendering the blocks, this is on your own
75//! // For example, you can use Slack API to resolve them
76//! // ...
77//! // let slack_user_ids = slack_references.users.keys().cloned().collect::<Vec<_>>();
78//! // for slack_user_id in slack_user_ids {
79//! //     let user_info = slack_api_client.users_info(slack_user_id).await?;
80//! //     slack_references.users.insert(slack_user_id, user_info.name);
81//! // }
82//! // Finally, render the blocks as Markdown
83//! let markdown_text = render_blocks_as_markdown(blocks, slack_references, None);
84//! ```
85pub mod html;
86pub mod markdown;
87pub mod references;
88pub mod text;
89pub mod visitor;
90
91#[cfg(test)]
92pub(crate) mod test_utils;
93
94pub use html::{render_blocks_as_html, render_slack_mrkdwn_text_as_html};
95pub use markdown::render_blocks_as_markdown;
96pub use references::{find_slack_references_in_blocks, SlackReferences};