Crates.io | slack-blocks-render |
lib.rs | slack-blocks-render |
version | |
source | src |
created_at | 2024-06-10 08:54:15.859889 |
updated_at | 2024-12-09 21:39:24.852414 |
description | Slack blocks render is a Rust library to render Slack blocks as Markdown. |
homepage | https://github.com/dax/slack-blocks-render |
repository | https://github.com/dax/slack-blocks-render |
max_upload_size | |
id | 1266888 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Slack blocks render is a Rust library to render Slack blocks as Markdown.
First, add the slack_blocks_render
crate as a dependency:
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:
cargo add slack_morphism
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());
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);
This project is distributed under the terms of the Apache License (Version 2.0).
See LICENSE