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