Skip to content

Commit

Permalink
Merge pull request #299 from auscompgeek/links
Browse files Browse the repository at this point in the history
Fix Markdown link substitution
  • Loading branch information
Half-Shot committed Oct 12, 2019
2 parents 655ac64 + ecd14d3 commit 9cf487a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/299.bugfix
@@ -0,0 +1 @@
Fix Markdown link replacements deleting link text, links, and text between links.
6 changes: 3 additions & 3 deletions src/substitutions.ts
Expand Up @@ -104,15 +104,15 @@ class Substitutions {
return null;
}

// Replace markdown urls with plain urls to make them match.
body = body.replace(/!?\[.*\]\((.+)\)/gm, "$1");

if (isAttachment) {
// If it's an attachment, we can allow the body.
body = typeof(body) === "string" ? body : "";
}
body = this.htmlEscape(body);

// Convert markdown links to slack mrkdwn links
body = body.replace(/!?\[(.*?)\]\((.+?)\)/gm, "<$2|$1>");

// emotes in slack are just italicised
if (msgType === "m.emote") {
body = `_${body}_`;
Expand Down
15 changes: 14 additions & 1 deletion src/tests/unit/substitutionsTest.ts
Expand Up @@ -258,7 +258,20 @@ describe("Substitutions", () => {
}, fakeMain, "footeam");
expect(res).to.deep.equal({
link_names: true,
text: "This bridge is built on the https://matrix.org protocol.",
text: "This bridge is built on the <https://matrix.org|Matrix> protocol.",
username: "@alice:localhost",
});
});
it ("should replace multiple matrix links with slack links", async () => {
const res = await subsitutions.matrixToSlack({
content: {
body: "[a](http://example.com) b [c](http://example.net)",
},
sender: "@alice:localhost",
}, fakeMain, "footeam");
expect(res).to.deep.equal({
link_names: true,
text: "<http://example.com|a> b <http://example.net|c>",
username: "@alice:localhost",
});
});
Expand Down

0 comments on commit 9cf487a

Please sign in to comment.