diff --git a/lib/middleware/markdown-escape.js b/lib/middleware/markdown-escape.js new file mode 100644 index 0000000..e5c9f5a --- /dev/null +++ b/lib/middleware/markdown-escape.js @@ -0,0 +1,18 @@ +function escape(str) { + // Markdown on Slack can't always be escaped, the best solution is to use a + // vertical tab character which doesn't embolden text + // http://webapps.stackexchange.com/questions/86557/how-do-i-escape-formatting-characters-in-slack + return str.replace(new RegExp('\\*', 'g'), '\u000B\*'); +} + + +module.exports = attachment => { + if (attachment.text) { + attachment.text = escape(attachment.text); + if (!('mrkdwn_in' in attachment)) { + attachment.mrkdwn_in = []; + } + attachment.mrkdwn_in.push('text'); + } + return attachment; +}; diff --git a/lib/response-types.js b/lib/response-types.js index f22a576..162a41e 100644 --- a/lib/response-types.js +++ b/lib/response-types.js @@ -5,6 +5,7 @@ const abilityWords = require('./middleware/ability-words'); const footer = require('./middleware/footer'); const manamoji = require('./middleware/manamoji'); const reminderText = require('./middleware/reminder-text'); +const markdownEscape = require('./middleware/markdown-escape'); const COLOR = '#431E3F'; @@ -68,7 +69,8 @@ TextResponse.prototype.middleware = [ footer, manamoji, reminderText, - abilityWords + abilityWords, + markdownEscape ]; TextResponse.prototype.url = 'https://api.scryfall.com/cards/named';