From 4bfcba8af70b757ab2465fbd6f65b188ca215b65 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Sat, 25 Apr 2020 18:01:33 +0200 Subject: [PATCH] fix weird slack escaping for _ --- index.js | 14 +++++++++++++- package.json | 2 +- test/escape.test.js | 5 +++++ test/single.test.js | 4 ++-- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 22044a2..ed12828 100644 --- a/index.js +++ b/index.js @@ -102,7 +102,6 @@ const rules = { }, }), newline: markdown.defaultRules.newline, - escape: markdown.defaultRules.escape, autolink: Object.assign({}, markdown.defaultRules.autolink, { order: markdown.defaultRules.strong.order + 1, match: markdown.inlineRegex(/^<((?:https?:\/\/|mailto:)[^|>]+)(\|([^>]*))?>/), @@ -134,6 +133,19 @@ const rules = { return htmlTag("a", output(node.content, state), { href: markdown.sanitizeUrl(node.target) }, state); }, }), + noem: { + order: markdown.defaultRules.text.order, + match: (source) => /^\\_/.exec(source), + parse: function(capture) { + return { + type: "text", + content: "\\_", + }; + }, + html: function(node, output, state) { + return output(node.content, state); + }, + }, em: Object.assign({}, markdown.defaultRules.em, { match: markdown.inlineRegex(/^\b_(\S(?:\\[\s\S]|[^\\])*?\S|\S)_(?!_)/), parse: (capture, parse) => { diff --git a/package.json b/package.json index ea4bb19..21c05b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slack-markdown", - "version": "0.0.3", + "version": "0.0.4", "description": "A markdown parser for Slack messages", "keywords": [ "slack", diff --git a/test/escape.test.js b/test/escape.test.js index ab5164f..2040c1f 100644 --- a/test/escape.test.js +++ b/test/escape.test.js @@ -17,3 +17,8 @@ it("Should not escape HTML, if the flag is set", () => { expect(markdown.toHTML("test", { escapeHTML: false })) .toBe("test"); }); + +it("Should not lose arms", () => { + expect(markdown.toHTML("¯\\_(ツ)_/¯")) + .toBe("¯\\_(ツ)_/¯"); +}); diff --git a/test/single.test.js b/test/single.test.js index 694e87b..a46bab1 100644 --- a/test/single.test.js +++ b/test/single.test.js @@ -68,9 +68,9 @@ it("Should HTML-escape fenced code blocks", () => { .toBe("test

<>
"); }); -it("Should escape marks", () => { +it("Should not escape marks", () => { expect(markdown.toHTML("Code: \\`1 + 1` = 2`")) - .toBe("Code: `1 + 1 = 2"); + .toBe("Code: \\1 + 1 = 2`"); }); it("Should do simple block quotes", () => {