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::json!({
22//!         "type": "rich_text",
23//!         "elements": [
24//!             {
25//!                 "type": "rich_text_section",
26//!                 "elements": [
27//!                     {
28//!                         "type": "text",
29//!                         "text": "Hello World"
30//!                     }
31//!                 ]
32//!             },
33//!         ]
34//!     })),
35//! ];
36//! let markdown_text = render_blocks_as_markdown(blocks, SlackReferences::default(), None);
37//! ```
38//!
39//! ## Usage with Slack references resolution
40//!
41//! Slack references resolution is useful when you want to resolve user ID, channel ID, or user group ID in the Slack blocks.
42//! Here is an example on how to use it:
43//! ```
44//! use slack_morphism::prelude::*;
45//! use slack_blocks_render::{
46//!   find_slack_references_in_blocks, render_blocks_as_markdown, SlackReferences
47//! };
48//!
49//! let blocks: Vec<SlackBlock> = vec![
50//!     SlackBlock::RichText(serde_json::json!({
51//!         "type": "rich_text",
52//!         "elements": [
53//!             {
54//!                 "type": "rich_text_section",
55//!                 "elements": [
56//!                     {
57//!                         "type": "text",
58//!                         "text": "Hello "
59//!                     }
60//!                 ]
61//!             },
62//!             {
63//!                 "type": "rich_text_section",
64//!                 "elements": [
65//!                     {
66//!                         "type": "user",
67//!                         "text": "U123456"
68//!                     }
69//!                 ]
70//!             },
71//!         ]
72//!     })),
73//! ];
74//! // First, extract Slack references from the blocks
75//! let slack_references = find_slack_references_in_blocks(&blocks);
76//! // Then, resolve the references before rendering the blocks, this is on your own
77//! // For example, you can use Slack API to resolve them
78//! // ...
79//! // let slack_user_ids = slack_references.users.keys().cloned().collect::<Vec<_>>();
80//! // for slack_user_id in slack_user_ids {
81//! //     let user_info = slack_api_client.users_info(slack_user_id).await?;
82//! //     slack_references.users.insert(slack_user_id, user_info.name);
83//! // }
84//! // Finally, render the blocks as Markdown
85//! let markdown_text = render_blocks_as_markdown(blocks, slack_references, None);
86//! ```
87pub mod markdown;
88pub mod references;
89pub mod text;
90pub mod visitor;
91
92pub use markdown::render_blocks_as_markdown;
93pub use references::{find_slack_references_in_blocks, SlackReferences};