Explorar o código

Fix bug where asynchronous fs call was being given a callback and hence no patterns were ever required; make pattern regexp's case-insensitive

sbkwgh %!s(int64=7) %!d(string=hai) anos
pai
achega
8cba378cbb

+ 5 - 8
lib/linkPreview/index.js

@@ -3,15 +3,12 @@ let path = require('path')
 
 let getOGPreviewData = require('./getOGPreviewData');
 let getPreviewHTML = require('./getPreviewHTML');
-let previewPatterns = [];
 
-fs.readdirSync(path.join(__dirname, 'patterns'), (err, files) => {
-	if(!err) {
-		previewPatterns = files.map(file => {
-			return require(path.join(__dirname, file));
-		});
-	}
-});
+let previewPatterns = 
+	fs.readdirSync(path.join(__dirname, 'patterns'))
+	  .map(file => {
+	      return require(path.join(__dirname, 'patterns', file));
+	  });
 
 module.exports =  async function linkPreview(url) {
 	let previewData;

+ 1 - 1
lib/linkPreview/patterns/github.js

@@ -3,7 +3,7 @@ let axios = require('axios');
 
 module.exports = {
 	matches (url) {
-		return url.match(/^https?:\/\/(www\.)?github\.com\/.+\/.+/);
+		return url.match(/^https?:\/\/(www\.)?github\.com\/.+\/.+/i);
 	},
 	async getPreviewData (link_url) {
 		try {

+ 1 - 1
lib/linkPreview/patterns/twitter.js

@@ -2,7 +2,7 @@ let axios = require('axios');
 
 module.exports = {
 	matches (url) {
-		return url.match(/^https?:\/\/(www\.)?twitter\.com\/.+\/status\/\d+/);
+		return url.match(/^https?:\/\/(www\.)?twitter\.com\/.+\/status\/\d+/i);
 	},
 	async getPreviewData (url) {
 		try {

+ 2 - 1
lib/linkPreview/patterns/wikipedia.js

@@ -3,7 +3,7 @@ let axios = require('axios');
 
 module.exports = {
 	matches (url) {
-		return url.match(/^https?:\/\/[a-z]+\.wikipedia\.org\/wiki\/.+/);
+		return url.match(/^https?:\/\/[a-z]+\.wikipedia\.org\/wiki\/.+/i);
 	},
 	async getPreviewData (link_url) {
 		try {
@@ -20,6 +20,7 @@ module.exports = {
 				description: content.length < 500 ? content : content + '...'
 			}
 		} catch (e) {
+			console.log(e)
 			return null;
 		}
 	}

+ 9 - 0
test/link_preview.js

@@ -87,7 +87,16 @@ describe('link_expansion', () => {
 			let HTML = await linkPreview('https://www.theguardian.com/news/2018/mar/17/cambridge-analytica-facebook-influence-us-election');
 
 			(typeof HTML).should.equal('string');
+			HTML.length.should.be.above(0);
 		});
+
+		it('should get a HTML string from a custom pattern', async () => {
+			let HTML = await linkPreview('https://en.wikipedia.org/wiki/google');
+
+			(typeof HTML).should.equal('string');
+			HTML.length.should.be.above(0);
+		});
+
 		it('should return an empty string from an invalid site', async () => {
 			let HTML = await linkPreview('http://blank.org');