Slack Message Parser

3.0.2 · active · verified Tue Apr 21

slack-message-parser is a JavaScript/TypeScript library designed to parse Slack's rich message formatting syntax into a structured Abstract Syntax Tree (AST). This allows developers to programmatically inspect, transform, or render Slack messages outside of the Slack client itself. The current stable version is 3.0.2. The library maintains a steady release cadence, primarily focusing on bug fixes, improvements to parsing accuracy, and adapting to changes in Node.js environments, with major versions introducing breaking changes like the shift to ESM and higher Node.js version requirements. A key differentiator of slack-message-parser is its output: instead of directly rendering to a format like HTML, it provides a hierarchical AST, enabling highly customizable processing, such as converting to various markup languages, performing content analysis, or implementing custom rendering logic. It ships with comprehensive TypeScript types, ensuring robust and type-safe development.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to parse a Slack message string into an AST and then recursively convert that AST into an HTML string, illustrating custom rendering.

import slackMessageParser, { Node, NodeType } from "slack-message-parser";

const tree = slackMessageParser("Slack *message* ~to~ _parse_");

// Write your own!
const toHTML = (node: Node): string => {
  switch (node.type) {
    case NodeType.Root:
      return `<p>${node.children.map(toHTML).join("")}</p>`;
    case NodeType.Text:
      return node.text;
    case NodeType.Bold:
      return `<strong>${node.children.map(toHTML).join("")}</strong>`;
    case NodeType.Italic:
      return `<i>${node.children.map(toHTML).join("")}</i>`;
    case NodeType.Strike:
      return `<del>${node.children.map(toHTML).join("")}</del>`;
    default:
      // You can use `source` property, which every nodes have, to serialize unknown nodes as-is
      return node.source;
  }
};

console.log(toHTML(tree));
// Expected Output:
// '<p>Slack <strong>message</strong> <del>to</del> <i>parse</i></p>'

view raw JSON →