Registry / communication / slack-message-parser

slack-message-parser

JSON →
library3.0.2jsnpmunverified

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.

npm install slack-message-parser
INSTALL
IMPORT
SIG · SLACK-MESSAGE-PARS
S
slack-message-parser
communicationjavascriptv3.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

slackMessageParser
import slackMessageParser from 'slack-message-parser';
const slackMessageParser = require('slack-message-parser');
Since v3.0.0, the package is primarily distributed as ES Modules (ESM). Direct CommonJS `require()` for the default export will not correctly import the parsing function.
Node
import { Node } from 'slack-message-parser';
const { Node } = require('slack-message-parser');
The `Node` interface is a TypeScript type export. For runtime access to named exports in CommonJS after v3.0.0, dynamic `import()` or specific Node.js configuration for ESM interoperability is required.
NodeType
import { NodeType } from 'slack-message-parser';
const { NodeType } = require('slack-message-parser');
The `NodeType` enum is a named export. Like other named exports, it is part of the ESM build since v3.0.0. CommonJS users may face issues with direct `require()` calls.

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>'
Debug
Known issues
breakingVersion 3.0.0 introduced a breaking change requiring Node.js version 16 or greater due to the shift in shipped code from ES2015 to ES2021.
fix
Ensure your Node.js environment is at least version 16.0.0. Update Node.js if necessary.
affects: >=3.0.0
breakingWith v3.0.0, the package transitioned to ES Modules (ESM) builds. Projects using CommonJS (CJS) may encounter issues with `require()` statements or need to configure Node.js for ESM interoperability.
fix
Migrate to `import` statements and ensure your project or Node.js environment is configured for ESM. For CJS projects that cannot migrate, consider dynamic `import()` or use a transpiler.
affects: >=3.0.0
breakingIn v2.0.0, the TypeScript typing for the return type of the `parse` function changed from `Node` to `Root`. While `Node` previously encompassed `Root`, this is a breaking change for code that directly inferred or expected `Node` when specifically working with the root of the parsed tree.
fix
Explicitly type the return of `parse` as `Root` or adjust type assertions/guards to correctly handle the `Root` type for the top-level AST structure.
affects: >=2.0.0 <3.0.0
gotchaPrior to v3.0.2, the parser did not correctly support multiline quotes that omitted the `>>>` prefix, potentially leading to incorrect or incomplete parsing of such message blocks.
fix
Upgrade to slack-message-parser v3.0.2 or newer to ensure accurate parsing of multiline quotes, regardless of whether the `>>>` prefix is used.
affects: <3.0.2
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Attempting to use ES Module `import` syntax in a CommonJS (CJS) project context, particularly after v3.0.0 shifted the library to ESM.
fix
Ensure your project is configured as an ES Module (e.g., by adding `"type": "module"` to `package.json` or using `.mjs` file extensions), or use dynamic `import()` if you must remain in CJS.
TypeError: slackMessageParser is not a function
Incorrectly attempting to `require()` the default export of `slack-message-parser` in a Node.js environment where the package is treated as an ESM, especially in v3.0.0 and above.
fix
Use the ESM `import` syntax: `import slackMessageParser from 'slack-message-parser';`. If you must use `require()` in a CJS context, try `const { default: slackMessageParser } = require('slack-message-parser');` or ensure Node.js correctly interops.
Property 'children' does not exist on type 'Node'.
This TypeScript error occurs if you are iterating or accessing `children` directly on a generic `Node` type, especially after the v2.0.0 change, without first narrowing the type to `Root` or other node types that define `children`.
fix
Implement type guards or switch statements based on `node.type` to correctly narrow the `Node` type to `Root` or other specific types (e.g., `NodeType.Bold`, `NodeType.Italic`) before accessing properties like `children`.
Upgrade
Version history
3.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
Bingbot
1
OpenAI (training)
1
Resources
slack-message-parser — npm install slack-message-parser · libregistry