Skip to content

Commit

Permalink
Do not unescape HTML in Slack messages
Browse files Browse the repository at this point in the history
This resulted in broken matrix formatting.
Tests are now also updated, in line with existing and expected behaviour.

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>
  • Loading branch information
tadzik committed Mar 29, 2024
1 parent ed85fc6 commit badc1eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
5 changes: 0 additions & 5 deletions src/substitutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { Logger } from "matrix-appservice-bridge";
import * as emoji from "node-emoji";
import { Main } from "./Main";
import { ISlackFile } from "./BaseSlackHandler";
import escapeStringRegexp from "escape-string-regexp";

const log = new Logger("substitutions");

const ATTACHMENT_TYPES = ["m.audio", "m.video", "m.file", "m.image"];
const PILL_REGEX = /<a href="https:\/\/matrix\.to\/#\/(#|@|\+)([^"]+)">([^<]+)<\/a>/g;

Expand Down Expand Up @@ -59,8 +56,6 @@ class Substitutions {
* @param file options slack file object
*/
public slackToMatrix(body: string, file?: ISlackFile): string {
log.debug("running substitutions on ", body);
body = this.htmlUnescape(body);
body = body.replace("<!channel>", "@room");
body = body.replace("<!here>", "@room");
body = body.replace("<!everyone>", "@room");
Expand Down
48 changes: 18 additions & 30 deletions tests/unit/substitutionsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,34 +297,22 @@ describe("Substitutions", () => {
});
});

// describe("slackTextToMatrixHTML", () => {
// it("should repeat a plain string", async () => {
// const res = await substitutions.slackTextToMatrixHTML("Hello World!");
// expect(res).to.equal("Hello World!");
// });
// it("should convert < and >", async () => {
// const res = await substitutions.slackTextToMatrixHTML("<html>Hello</html>");
// expect(res).to.equal("&lt;html&gt;Hello&lt;/html&gt;");
// });
// it("should convert a single new line to a <br />", async () => {
// const res = substitutions.slackTextToMatrixHTML("line 1\nline 2");
// expect(res).to.equal("line 1<br />line 2");
// });
// it("should convert two new lines to paragraphs", async () => {
// const res = substitutions.slackTextToMatrixHTML("line 1\n\nline 3");
// expect(res).to.equal("<p>line 1</p><p>line 3</p>");
// });
// it("should convert bold formatting", async () => {
// const res = substitutions.slackTextToMatrixHTML("This is *bold*!");
// expect(res).to.equal("This is <strong>bold</strong>!");
// });
// it("should convert italic formatting", async () => {
// const res = substitutions.slackTextToMatrixHTML("This is /italics/!");
// expect(res).to.equal("This is <em>italics</em>!");
// });
// it("should convert strikethrough formatting", async () => {
// const res = substitutions.slackTextToMatrixHTML("This is ~strikethrough~!");
// expect(res).to.equal("This is <del>strikethrough</del>");
// });
// });
describe("slackTextToMatrixHTML", () => {
it("should repeat a plain string", () => {
const res = substitutions.slackToMatrix("Hello World!");
expect(res).to.equal("Hello World!");
});
it("should leave newlines intact", () => {
const res = substitutions.slackToMatrix("Hello\nWorld!");
expect(res).to.equal("Hello\nWorld!");
});
it("should leave html encoding intact", () => {
const res = substitutions.slackToMatrix("<joke>");
expect(res).to.equal("<joke>");
});
it("should convert !here", () => {
const res = substitutions.slackToMatrix("<!here> hello");
expect(res).to.equal("@room hello");
});
});
});

0 comments on commit badc1eb

Please sign in to comment.