Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider c3bb2360 rédigé par Sorunome's avatar Sorunome
Parcourir les fichiers

Merge remote-tracking branch 'origin/soru/links' into soru/html-to-markdown

parents 27c3adf6 56e5f65c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -62,9 +62,15 @@ export class MatrixMessageProcessor {
}
msg = msg.replace(/@room/g, "@here");
const escapeChars = ["\\", "*", "_", "~", "`"];
msg = msg.split(" ").map((s) => {
if (s.match(/^https?:\/\//)) {
return s;
}
escapeChars.forEach((char) => {
msg = msg.replace(new RegExp("\\" + char, "g"), "\\" + char);
s = s.replace(new RegExp("\\" + char, "g"), "\\" + char);
});
return s;
}).join(" ");
return msg;
}
......@@ -109,10 +115,19 @@ export class MatrixMessageProcessor {
return `<#${match[1]}>`;
}
private async parseLinkContent(node: Parser.HTMLElement): Promise<string> {
const attrs = node.attributes;
const content = await this.walkChildNodes(node);
if (!attrs.href || content === attrs.href) {
return content;
}
return `[${content}](${attrs.href})`;
}
private async parsePillContent(node: Parser.HTMLElement): Promise<string> {
const attrs = node.attributes;
if (!attrs.href || !attrs.href.startsWith(MATRIX_TO_LINK)) {
return await this.walkChildNodes(node);
return await this.parseLinkContent(node);
}
const id = attrs.href.replace(MATRIX_TO_LINK, "");
let reply = "";
......@@ -126,7 +141,7 @@ export class MatrixMessageProcessor {
break;
}
if (!reply) {
return await this.walkChildNodes(node);
return await this.parseLinkContent(node);
}
return reply;
}
......
......@@ -73,6 +73,13 @@ describe("MatrixMessageProcessor", () => {
const result = await mp.FormatMessage(msg, guild as any);
expect(result).is.equal("hey @here");
});
it("doesn't discord-escape links", async () => {
const mp = new MatrixMessageProcessor(bot, opts);
const guild = new MockGuild("1234");
const msg = getPlainMessage("http://example.com/_test_/");
const result = await mp.FormatMessage(msg, guild as any);
expect(result).is.equal("http://example.com/_test_/");
});
});
describe("FormatMessage / formatted_body / simple", () => {
it("leaves blank stuff untouched", async () => {
......@@ -200,6 +207,34 @@ code
const result = await mp.FormatMessage(msg, guild as any);
expect(result).is.equal("test reply");
});
it("parses links", async () => {
const mp = new MatrixMessageProcessor(bot, opts);
const guild = new MockGuild("1234");
const msg = getHtmlMessage("<a href=\"http://example.com\">link</a>");
const result = await mp.FormatMessage(msg, guild as any);
expect(result).is.equal("[link](http://example.com)");
});
it("parses links with same content", async () => {
const mp = new MatrixMessageProcessor(bot, opts);
const guild = new MockGuild("1234");
const msg = getHtmlMessage("<a href=\"http://example.com\">http://example.com</a>");
const result = await mp.FormatMessage(msg, guild as any);
expect(result).is.equal("http://example.com");
});
it("doesn't discord-escape links", async () => {
const mp = new MatrixMessageProcessor(bot, opts);
const guild = new MockGuild("1234");
const msg = getHtmlMessage("<a href=\"http://example.com/_blah_/\">link</a>");
const result = await mp.FormatMessage(msg, guild as any);
expect(result).is.equal("[link](http://example.com/_blah_/)");
});
it("doesn't discord-escape links with same content", async () => {
const mp = new MatrixMessageProcessor(bot, opts);
const guild = new MockGuild("1234");
const msg = getHtmlMessage("<a href=\"http://example.com/_blah_/\">http://example.com/_blah_/</a>");
const result = await mp.FormatMessage(msg, guild as any);
expect(result).is.equal("http://example.com/_blah_/");
});
});
describe("FormatMessage / formatted_body / discord", () => {
it("Parses user pills", async () => {
......@@ -218,7 +253,7 @@ code
guild.members.set("12345", member);
const msg = getHtmlMessage("<a href=\"https://matrix.to/#/@_discord_789:localhost\">TestUsername</a>");
const result = await mp.FormatMessage(msg, guild as any);
expect(result).is.equal("TestUsername");
expect(result).is.equal("[TestUsername](https://matrix.to/#/@_discord_789:localhost)");
});
it("Parses channel pills", async () => {
const mp = new MatrixMessageProcessor(bot, opts);
......@@ -258,7 +293,7 @@ code
const guild = new MockGuild("1234");
const msg = getHtmlMessage("<a href=\"http://example.com\"><em>yay?</em></a>");
const result = await mp.FormatMessage(msg, guild as any);
expect(result).is.equal("*yay?*");
expect(result).is.equal("[*yay?*](http://example.com)");
});
it("Converts @room to @here", async () => {
const mp = new MatrixMessageProcessor(bot, opts);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment