Socket
Socket
Sign inDemoInstall

slack-message-parser

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    slack-message-parser

Parser for Slack message


Version published
Weekly downloads
12K
decreased by-21.31%
Maintainers
1
Install size
38.4 kB
Created
Weekly downloads
 

Changelog

Source

[3.0.2] - 2023-06-03

Added

  • Support for multiline quote without >>>. (Issue: #38)

Readme

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

Last updated on 02 Jun 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc