Small library to create slack messages with rich text using the BlockKit.
npm i slack-rich-text
Use the exported function as a template string tag to create a rich text message.
To apply the transformation to a string inside some other function, like using bold
on a list, use the section
function.
Example:
import { bullets, emoji, ordered, richText, section } from "slack_rich_text";
const blocks = richText`
This is a test message for slack-rich-text.
It supports:
${bullets([
"Bullet lists",
"Yaay!",
ordered([
"Also",
"Ordered",
"Lists",
bullets([
"And",
"Nested",
"Lists",
section`${emoji("smile")} ${emoji("smile")} ${emoji("smile")}`
])
])
])}
`;
Renders as:
The richText
function does not support receiving a string as a value. That means that the following code will not work:
const name = "John";
const blocks = richText`
Hello, ${name}!
`; // This will throw an error
To fix this, use the section
function to transform the string into a block:
const name = "John";
const blocks = richText`
${section`Hello, ${name}!`}
`;
See the API Reference for a detailed explanation of the available functions.