|
| 1 | +# @discord-message-components/markdown |
| 2 | + |
| 3 | +A markdown parser for Discord messages. |
| 4 | + |
| 5 | +**This is an adaption of [discord-markdown](https://github.com/brussell98/discord-markdown) by [brussell98](https://github.com/brussell98).** |
| 6 | + |
| 7 | +## Using |
| 8 | + |
| 9 | +```bash |
| 10 | +yarn add @discord-message-components/markdown |
| 11 | +npm i @discord-message-components/markdown |
| 12 | +``` |
| 13 | + |
| 14 | +For browser use, import `dist/markdown.min.js` |
| 15 | + |
| 16 | +```js |
| 17 | +const { parser, htmlOutput, toHTML } = require('@discord-message-components/markdown') |
| 18 | + |
| 19 | +console.log(toHTML('This **is** a __test__')) |
| 20 | +// => This <strong>is</strong> a <u>test</u> |
| 21 | +``` |
| 22 | + |
| 23 | +## Options |
| 24 | + |
| 25 | +```js |
| 26 | +const { toHTML } = require('@discord-message-components/markdown') |
| 27 | +toHTML('This **is** a __test__', options) |
| 28 | +``` |
| 29 | + |
| 30 | +`options` is an object with the following properties (all are optional): |
| 31 | + |
| 32 | +* `embed`: Boolean (default: false), if it should parse embed contents (rules are slightly different) |
| 33 | +* `escapeHTML`: Boolean (default: true), if it should escape HTML |
| 34 | +* `discordOnly`: Boolean (default: false), if it should only parse the discord-specific stuff |
| 35 | +* `discordCallback`: Object, callbacks used for discord parsing. Each receive an object with different properties, and are expected to return an HTML escaped string |
| 36 | + * `user`: (`id`: Number) User mentions "@someperson" |
| 37 | + * `channel`: (`id`: Number) Channel mentions "#somechannel" |
| 38 | + * `role`: (`id`: Number) Role mentions "@somerole" |
| 39 | + * `everyone`: () Everyone mention "@everyone" |
| 40 | + * `here`: () Here mention "@here" |
| 41 | +* `cssModuleNames`: Object, maps CSS class names to CSS module class names |
| 42 | + |
| 43 | +### Mention and Emoji Handling |
| 44 | + |
| 45 | +Using the `discordCallback` option you can define custom functions to handle parsing mention and emoji content. You can use these to turn IDs into names. |
| 46 | + |
| 47 | +Example: |
| 48 | + |
| 49 | +```js |
| 50 | +const { toHTML } = require('@discord-message-components/markdown') |
| 51 | +toHTML('This is a mention for <@95286900801146880>', { |
| 52 | + discordCallback: { |
| 53 | + user: node => '@' + users[node.id], |
| 54 | + }, |
| 55 | +}) // -> This is a mention for @Brussell |
| 56 | +``` |
| 57 | + |
| 58 | +## Customizing |
| 59 | + |
| 60 | +It is possible to change the rules used by this package. Take a look at the code to see how to create your own modified rule set. |
0 commit comments