discord-markdown
Advanced tools
Comparing version 2.3.1 to 2.4.0
{ | ||
"parserOptions": { | ||
"ecmaVersion": 2017 | ||
"ecmaVersion": 2017, | ||
"sourceType": "module" | ||
}, | ||
@@ -5,0 +6,0 @@ "env": { |
@@ -0,1 +1,18 @@ | ||
# [2.4.0](https://github.com/brussell98/discord-markdown/compare/v2.3.1...v2.4.0) (2020-08-25) | ||
### Features | ||
- Added a minified dist file for use in browsers | ||
- Added the ability to create custom rule sets | ||
### Improvements | ||
- Changed Discord emojis to image elements | ||
- Moved `discordCallback` to the state | ||
- Upgraded highlight.js to version 10 | ||
### Fixes | ||
- Added text sanitizing to all user input in the default `discordCallback` functions | ||
# [2.3.1](https://github.com/brussell98/discord-markdown/compare/v2.3.0...v2.3.1) (2020-02-20) | ||
@@ -2,0 +19,0 @@ |
53
index.js
@@ -32,3 +32,3 @@ const markdown = require('simple-markdown'); | ||
const rules = { | ||
blockQuote: Object.assign({}, markdown.defaultRules.blockQuote, { | ||
blockQuote: Object.assign({ }, markdown.defaultRules.blockQuote, { | ||
match: function(source, state, prevSource) { | ||
@@ -159,6 +159,5 @@ return !/^$|\n *$/.test(prevSource) || state.inQuote ? null : /^( *>>> ([\s\S]*))|^( *> [^\n]*(\n *> [^\n]*)*\n?)/.exec(source); | ||
const discordCallbackDefaults = { | ||
user: node => '@' + node.id, | ||
channel: node => '#' + node.id, | ||
role: node => '&' + node.id, | ||
emoji: node => ':' + markdown.sanitizeText(node.name) + ':', | ||
user: node => '@' + markdown.sanitizeText(node.id), | ||
channel: node => '#' + markdown.sanitizeText(node.id), | ||
role: node => '&' + markdown.sanitizeText(node.id), | ||
everyone: () => '@everyone', | ||
@@ -168,4 +167,2 @@ here: () => '@here' | ||
let discordCallback = discordCallbackDefaults; | ||
const rulesDiscord = { | ||
@@ -181,3 +178,3 @@ discordUser: { | ||
html: function(node, output, state) { | ||
return htmlTag('span', discordCallback.user(node), { class: 'd-mention d-user' }, state); | ||
return htmlTag('span', state.discordCallback.user(node), { class: 'd-mention d-user' }, state); | ||
} | ||
@@ -194,3 +191,3 @@ }, | ||
html: function(node, output, state) { | ||
return htmlTag('span', discordCallback.channel(node), { class: 'd-mention d-channel' }, state); | ||
return htmlTag('span', state.discordCallback.channel(node), { class: 'd-mention d-channel' }, state); | ||
} | ||
@@ -207,3 +204,3 @@ }, | ||
html: function(node, output, state) { | ||
return htmlTag('span', discordCallback.role(node), { class: 'd-mention d-role' }, state); | ||
return htmlTag('span', state.discordCallback.role(node), { class: 'd-mention d-role' }, state); | ||
} | ||
@@ -222,3 +219,7 @@ }, | ||
html: function(node, output, state) { | ||
return htmlTag('span', discordCallback.emoji(node), { class: `d-emoji${node.animated ? ' d-emoji-animated' : ''}` }, state); | ||
return htmlTag('img', '', { | ||
class: `d-emoji${node.animated ? ' d-emoji-animated' : ''}`, | ||
src: `https://cdn.discordapp.com/emojis/${node.id}.png`, | ||
alt: `:${node.name}:` | ||
}, state); | ||
} | ||
@@ -233,3 +234,3 @@ }, | ||
html: function(node, output, state) { | ||
return htmlTag('span', discordCallback.everyone(node), { class: 'd-mention d-user' }, state); | ||
return htmlTag('span', state.discordCallback.everyone(node), { class: 'd-mention d-user' }, state); | ||
} | ||
@@ -244,6 +245,7 @@ }, | ||
html: function(node, output, state) { | ||
return htmlTag('span', discordCallback.here(node), { class: 'd-mention d-user' }, state); | ||
return htmlTag('span', state.discordCallback.here(node), { class: 'd-mention d-user' }, state); | ||
} | ||
} | ||
}; | ||
Object.assign(rules, rulesDiscord); | ||
@@ -263,3 +265,2 @@ | ||
const rulesEmbed = Object.assign({ }, rules, { | ||
@@ -286,3 +287,6 @@ link: markdown.defaultRules.link | ||
*/ | ||
function toHTML(source, options) { | ||
function toHTML(source, options, customParser, customHtmlOutput) { | ||
if ((customParser || customHtmlOutput) && (!customParser || !customHtmlOutput)) | ||
throw new Error('You must pass both a custom parser and custom htmlOutput function, not just one'); | ||
options = Object.assign({ | ||
@@ -297,3 +301,6 @@ embed: false, | ||
let _htmlOutput = htmlOutput; | ||
if (options.discordOnly) { | ||
if (customParser) { | ||
_parser = customParser; | ||
_htmlOutput = customHtmlOutput; | ||
} else if (options.discordOnly) { | ||
_parser = parserDiscord; | ||
@@ -306,5 +313,2 @@ _htmlOutput = htmlOutputDiscord; | ||
// TODO: Move into state | ||
discordCallback = Object.assign({ }, discordCallbackDefaults, options.discordCallback); | ||
const state = { | ||
@@ -315,3 +319,4 @@ inline: true, | ||
escapeHTML: options.escapeHTML, | ||
cssModuleNames: options.cssModuleNames || null | ||
cssModuleNames: options.cssModuleNames || null, | ||
discordCallback: Object.assign({ }, discordCallbackDefaults, options.discordCallback) | ||
}; | ||
@@ -321,6 +326,12 @@ | ||
} | ||
module.exports = { | ||
parser: source => parser(source, { inline: true }), | ||
htmlOutput, | ||
toHTML | ||
toHTML, | ||
rules, | ||
rulesDiscordOnly, | ||
rulesEmbed, | ||
markdownEngine: markdown, | ||
htmlTag | ||
}; |
{ | ||
"name": "discord-markdown", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"description": "A markdown parser for Discord messages", | ||
@@ -21,6 +21,8 @@ "keywords": [ | ||
"test": "jest --verbose", | ||
"lint": "eslint ." | ||
"lint": "eslint .", | ||
"dist": "rollup -c", | ||
"prepublishOnly": "npm run dist" | ||
}, | ||
"dependencies": { | ||
"highlight.js": "^9.18.1", | ||
"highlight.js": "^10.1.2", | ||
"simple-markdown": "^0.7.2" | ||
@@ -30,4 +32,9 @@ }, | ||
"eslint": "^6.8.0", | ||
"jest": "^25.1.0" | ||
"jest": "^25.1.0", | ||
"rollup": "^1.32.1", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-replace": "^2.2.0", | ||
"rollup-plugin-terser": "^5.3.0" | ||
} | ||
} |
@@ -11,2 +11,4 @@ # discord-markdown | ||
For browser use, import `dist/discord-markdown.min.js` | ||
```js | ||
@@ -37,3 +39,2 @@ const { parser, htmlOutput, toHTML } = require('discord-markdown'); | ||
* `role`: (`id`: Number) Role mentions "@somerole" | ||
* `emoji`: (`animated`: Boolean, `name`: String, `id`: Number) emojis ":emote": | ||
* `everyone`: () Everyone mention "@everyone" | ||
@@ -58,4 +59,8 @@ * `here`: () Here mention "@here" | ||
## Customizing | ||
It is possible to change the rules used by discord-markdown. Take a look at the code to see how to create your own modified rule set. | ||
## Contributing | ||
Find an inconsistency? File an issue or submit a pull request with the fix and updated test(s). |
@@ -44,13 +44,5 @@ const markdown = require('../index'); | ||
expect(markdown.toHTML('heh <:blah:1234>')) | ||
.toBe('heh <span class="d-emoji">:blah:</span>'); | ||
.toBe('heh <img class="d-emoji" src="https://cdn.discordapp.com/emojis/1234.png" alt=":blah:"></img>'); | ||
}); | ||
test('custom emoji parsing', () => { | ||
expect(markdown.toHTML('heh <:blah:1234>', { | ||
discordCallback: { emoji: node => { | ||
return '++:' + node.id + ':++'; | ||
}} | ||
})).toBe('heh <span class="d-emoji">++:1234:++</span>'); | ||
}); | ||
test('everyone mentioning', () => { | ||
@@ -82,7 +74,4 @@ expect(markdown.toHTML('Hey @everyone!', { | ||
test('animated emojis work', () => { | ||
expect(markdown.toHTML('heh <a:blah:1234>', { | ||
discordCallback: { emoji: node => { | ||
return '++' + (node.animated ? 'animated' : '') + ':' + node.id + ':++'; | ||
}} | ||
})).toBe('heh <span class="d-emoji d-emoji-animated">++animated:1234:++</span>'); | ||
expect(markdown.toHTML('heh <a:blah:1234>', )) | ||
.toBe('heh <img class="d-emoji d-emoji-animated" src="https://cdn.discordapp.com/emojis/1234.png" alt=":blah:"></img>'); | ||
}); | ||
@@ -89,0 +78,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1028618
14
3654
64
7
1
+ Addedhighlight.js@10.7.3(transitive)
- Removedhighlight.js@9.18.5(transitive)
Updatedhighlight.js@^10.1.2