Skip to content

Commit 04b1c0c

Browse files
committed
feat(markdown): Initial markdown package commit
1 parent e2c1475 commit 04b1c0c

File tree

8 files changed

+481
-103
lines changed

8 files changed

+481
-103
lines changed

packages/markdown/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
*.local

packages/markdown/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Brussell
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/markdown/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.

packages/markdown/index.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as markdown from 'simple-markdown'
2+
3+
interface DiscordCallback {
4+
user?: (node: { id: string }) => string;
5+
channel?: (node: { id: string }) => string;
6+
role?: (node: { id: string }) => string;
7+
everyone?: () => string;
8+
here?: () => string;
9+
}
10+
11+
interface HTMLOptions {
12+
embed?: boolean;
13+
escapeHTML?: boolean;
14+
discordOnly?: boolean;
15+
discordCallback: DiscordCallback;
16+
cssModuleNames: Record<string, string>;
17+
}
18+
19+
export function parser(source: string): markdown.SingleASTNode[]
20+
export function htmlOutput(node: markdown.ASTNode, state?: markdown.OptionalState): unknown;
21+
export function toHTML(source: string, options?: HTMLOptions): string;
22+
export function htmlTag(tagName: string, content: string, attributes: Record<string, string>, isClosed?: boolean, state?: markdown.State): string
23+
24+
type MarkdownRules = Record<string, Record<string, unknown>>;
25+
26+
export const rules: MarkdownRules
27+
export const rulesDiscordOnly: MarkdownRules
28+
export const rulesEmbed: MarkdownRules
29+
export const markdownEngine: typeof markdown

0 commit comments

Comments
 (0)