slack-message-parser

JavaScript library for parsing Slack message format (mrkdwn)

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
# Guide

slack-message-parser is a JavaScript library which parses the message returned by Slack API and give you a tree object (imagine AST).
You could use the result tree to create a message viewer (HTML, React component, etc...) or whatever you want :)

## Installation

The package is available as `slack-message-parser` on npm.

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

## Quick Start

Just parse the message!

```ts
import slackMessageParser from 'slack-message-parser'

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

console.dir(tree)
```

## Supported Message Features

- Plain text
- Links
  - Channels (`#channel`)
  - User (`@someone`)
  - URL (`https://foo.bar`, `mailto:foo@bar`)
  - Commands (Represented as `<!foo>`, more detail [here](https://api.slack.com/docs/message-formatting))
- Emojis :heart_eyes:
- Code block
- Inline code (<code>\`foo\`</code>)
- Italic (`_foo_`)
- Bold (`*foo*`)
- Strikethrough (`~foo~`)
- Quotes (`> foo`)

## Examples

### Plain HTML with TypeScript

<iframe src="https://codesandbox.io/embed/gracious-rgb-kmqbu?fontsize=14&module=%2Fsrc%2Findex.ts&view=editor" title="slack-message-parser--ts-vanilla" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>

### React with TypeScript

<iframe src="https://codesandbox.io/embed/condescending-heyrovsky-y5jw3?fontsize=14&module=%2Fsrc%2FSlackMessage.tsx" title="slack-message-parser--react-ts" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>