Merge pull request #3606 from thelounge/xpaw/text-plain-preview

Add preview for text/plain urls
This commit is contained in:
Pavel Djundik 2019-12-19 15:25:22 +02:00 committed by GitHub
commit c04beb8b08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 0 deletions

View File

@ -92,6 +92,14 @@ function parseHtml(preview, res, client) {
$('link[rel="image_src"]').attr("href") || $('link[rel="image_src"]').attr("href") ||
""; "";
if (preview.head.length) {
preview.head = preview.head.substr(0, 100);
}
if (preview.body.length) {
preview.body = preview.body.substr(0, 300);
}
// Make sure thumbnail is a valid and absolute url // Make sure thumbnail is a valid and absolute url
if (thumb.length) { if (thumb.length) {
thumb = normalizeURL(thumb, preview.link) || ""; thumb = normalizeURL(thumb, preview.link) || "";
@ -187,6 +195,11 @@ function parse(msg, chan, preview, res, client) {
promise = parseHtml(preview, res, client); promise = parseHtml(preview, res, client);
break; break;
case "text/plain":
preview.type = "link";
preview.body = res.data.toString().substr(0, 300);
break;
case "image/png": case "image/png":
case "image/gif": case "image/gif":
case "image/jpg": case "image/jpg":

View File

@ -11,6 +11,16 @@ describe("Link plugin", function() {
this.timeout(process.env.CI ? 25000 : 5000); this.timeout(process.env.CI ? 25000 : 5000);
this.slow(200); this.slow(200);
const loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit.\
Vivamus at pretium mauris. Aenean eu orci id erat pulvinar\
commodo cursus ac augue. Ut dui quam, tempus ac felis et,\
efficitur auctor nisl. Sed semper sit amet metus eu fringilla.\
Vivamus vitae ligula quis eros rutrum tristique. Suspendisse\
luctus molestie tortor, in finibus nulla rutrum quis. Proin\
congue ut elit eget congue. Nam pretium blandit nibh nec laoreet.\
Suspendisse vehicula turpis urna, sit amet molestie diam dapibus in.\
Vivamus bibendum vulputate tincidunt. Sed vitae ligula felis.`;
let app; let app;
beforeEach(function(done) { beforeEach(function(done) {
@ -70,6 +80,67 @@ describe("Link plugin", function() {
}); });
}); });
it("should be able to display body for text/plain", function(done) {
const url = "http://localhost:" + this.port + "/basic-text";
const message = this.irc.createMessage({
text: url,
});
link(this.irc, this.network.channels[0], message);
expect(message.previews).to.deep.equal([
{
body: "",
head: "",
link: url,
thumb: "",
size: -1,
type: "loading",
shown: true,
},
]);
app.get("/basic-text", function(req, res) {
res.type("text").send(loremIpsum);
});
this.irc.once("msg:preview", function(data) {
expect(data.preview.type).to.equal("link");
expect(data.preview.head).to.equal("Untitled page");
expect(data.preview.body).to.equal(loremIpsum.substring(0, 300));
expect(data.preview.body).to.have.length(300);
expect(data.preview.link).to.equal(url);
expect(message.previews).to.deep.equal([data.preview]);
done();
});
});
it("should truncate head and body", function(done) {
const url = "http://localhost:" + this.port + "/truncate";
const message = this.irc.createMessage({
text: url,
});
link(this.irc, this.network.channels[0], message);
app.get("/truncate", function(req, res) {
res.send(
`<title>${loremIpsum}</title><meta name='description' content='${loremIpsum}'>`
);
});
this.irc.once("msg:preview", function(data) {
expect(data.preview.type).to.equal("link");
expect(data.preview.head).to.equal(loremIpsum.substring(0, 100));
expect(data.preview.body).to.equal(loremIpsum.substring(0, 300));
expect(data.preview.link).to.equal(url);
expect(message.previews).to.deep.equal([data.preview]);
done();
});
});
it("should prefer og:title over title", function(done) { it("should prefer og:title over title", function(done) {
const message = this.irc.createMessage({ const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/basic-og", text: "http://localhost:" + this.port + "/basic-og",
@ -243,6 +314,27 @@ describe("Link plugin", function() {
}); });
}); });
it("should send untitled page if there is body", function(done) {
const port = this.port;
const message = this.irc.createMessage({
text: "http://localhost:" + this.port + "/body-no-title",
});
link(this.irc, this.network.channels[0], message);
app.get("/body-no-title", function(req, res) {
res.send("<meta name='description' content='hello world'>");
});
this.irc.once("msg:preview", function(data) {
expect(data.preview.head).to.equal("Untitled page");
expect(data.preview.body).to.equal("hello world");
expect(data.preview.thumb).to.equal("");
expect(data.preview.link).to.equal("http://localhost:" + port + "/body-no-title");
done();
});
});
it("should not send thumbnail if image is 404", function(done) { it("should not send thumbnail if image is 404", function(done) {
const port = this.port; const port = this.port;
const message = this.irc.createMessage({ const message = this.irc.createMessage({