Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

slack-message-parser

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

slack-message-parser

Parser for Slack message

  • 3.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
22K
increased by13.75%
Maintainers
1
Weekly downloads
 
Created
Source

slack-message-parser

npm Deno Lint Workflow Status Test Workflow Status GitHub Workflow Status codecov

Parser library for Slack message format.

[Document]

Install

Requires Node.js >=v16.

npm i --save slack-message-parser
# yarn add slack-message-parser

Usage

Usage with Typescript (recommended).

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

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

// tree is:
// {
//   type: NodeType.Root,
//   children: [
//     {
//       type: NodeType.Text,
//       text: "Slack ",
// .     source: "Slack "
//     },
//     {
//       type: NodeType.Bold,
//       children: [
//         {
//           type: NodeType.Text,
//           text: "message"
//         }
//       ],
// .     source: "*message*"
//     },
//     ...
//   ],
//   source: "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));

// Output:
// '<p>Slack <strong>message</strong> <del>to</del> <i>parse</i></p>'

Keywords

FAQs

Package last updated on 02 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc