From 1fc0518928acc7848d7096f54c7665a36a2d6a7e Mon Sep 17 00:00:00 2001 From: Shawn Miller Date: Tue, 27 Nov 2018 22:26:21 -0600 Subject: [PATCH] Add support for imgur embeds https://meh.com/forum/topics/whos-a-guy-gotta-bribe-to-get-the-effing-forums-fixed#5bfdece84af8d5656d288365 --- README.md | 4 ++-- lib/plugins/imgur.js | 21 ++++++++------------- package.json | 2 +- test/fixtures/mehdown.html | 4 ++-- test/index.js | 6 +++--- test/plugins/images.js | 6 +++--- test/plugins/imgur.js | 24 +++++++++++++++++++----- 7 files changed, 38 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index d9d98b3..48e28eb 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ The Markdown parser used on the forums at meh.com. - GitHub gists are embedded inline. - Image URLs are converted to images. -- imgur GIFV URLs are converted to high quality looping GIF videos. +- imgur URLs are converted to embeded images/videos. - Kickstarter URLs are converted to campaign previews. - Reddit permalink URLs are converted to embeddable comment threads. - SoundCloud URLs are converted to audio players. @@ -75,4 +75,4 @@ var mehdown = require('mehdown'); mehdown.render('markdown text here', { baseUrl: 'https://meh.com', commands: true, detectImageSizes: false }, function(err, html) { console.log(html); }); -``` +``` \ No newline at end of file diff --git a/lib/plugins/imgur.js b/lib/plugins/imgur.js index 928d618..1dfe0af 100644 --- a/lib/plugins/imgur.js +++ b/lib/plugins/imgur.js @@ -1,6 +1,6 @@ const url = require('url'); -const regExp = /(?:imgur\.com\/)(\w+)\.gifv/i; +const regExp = /(i\.imgur\.com\/)(\w+)\./i; module.exports = function(md) { var defaultRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) { @@ -24,20 +24,15 @@ module.exports = function(md) { var uri = url.parse(href); if (uri && uri.pathname) { - var filename = uri.pathname.split('/').pop(); - var extension = filename.toLowerCase().split('.')[1]; + var matches = href.match(regExp); - if (extension === 'gifv') { - var matches = href.match(regExp); + if (matches && matches[2]) { + var id = matches[2]; - if (matches && matches[1]) { - var id = matches[1]; + textToken.content = ''; + tokens[idx + 2].hidden = true; - textToken.content = ''; - tokens[idx + 2].hidden = true; - - return `
`; - } + return `
imgur.com/${id}
`; } } } @@ -46,4 +41,4 @@ module.exports = function(md) { return defaultRender(tokens, idx, options, env, self); }; -}; +}; \ No newline at end of file diff --git a/package.json b/package.json index f36465f..4dc01f8 100644 --- a/package.json +++ b/package.json @@ -36,5 +36,5 @@ "coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec test/* && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", "test": "mocha --reporter spec test/*" }, - "version": "2.40.0" + "version": "2.41.0" } \ No newline at end of file diff --git a/test/fixtures/mehdown.html b/test/fixtures/mehdown.html index 476eaf3..d0387ab 100644 --- a/test/fixtures/mehdown.html +++ b/test/fixtures/mehdown.html @@ -24,7 +24,7 @@

Usage

mehdown.render('markdown text here', { commands: true, detectImageSizes: false }, function(err, html) { console.log(html); }); - + \ No newline at end of file diff --git a/test/index.js b/test/index.js index a4828ca..f9e0b97 100644 --- a/test/index.js +++ b/test/index.js @@ -644,8 +644,8 @@ describe('commands', function() { describe('detect image sizes', function() { it('images', function(done) { - mehdown.render('https://res.cloudinary.com/mediocre/image/upload/kekjvvhpkxh0v8x9o6u7.png https://i.imgur.com/8peBgQn.png https://res.cloudinary.com/mediocre/image/upload/kekjvvhpkxh0v8x9o6u7.png', { detectImageSizes: true }, function(err, html) { - assert.equal(html, '

'); + mehdown.render('https://res.cloudinary.com/mediocre/image/upload/kekjvvhpkxh0v8x9o6u7.png https://res.cloudinary.com/mediocre/image/upload/kekjvvhpkxh0v8x9o6u7.png', { detectImageSizes: true }, function(err, html) { + assert.equal(html, '

'); done(); }); }); @@ -953,4 +953,4 @@ describe('security', function() { done(); }); }); -}); +}); \ No newline at end of file diff --git a/test/plugins/images.js b/test/plugins/images.js index 0b3051c..bc4cd90 100644 --- a/test/plugins/images.js +++ b/test/plugins/images.js @@ -11,8 +11,8 @@ describe('images', function() { }); it('.jpg', function(done) { - mehdown.render('http://i.imgur.com/9oiY1aO.jpg', function(err, html) { - assert.equal(html, '

'); + mehdown.render('http://example.com/image.jpg', function(err, html) { + assert.equal(html, '

'); done(); }); }); @@ -58,4 +58,4 @@ describe('images', function() { done(); }); }); -}); +}); \ No newline at end of file diff --git a/test/plugins/imgur.js b/test/plugins/imgur.js index 60dfea3..d09bd9d 100644 --- a/test/plugins/imgur.js +++ b/test/plugins/imgur.js @@ -5,7 +5,7 @@ const mehdown = require('../../lib'); describe('imgur', function() { it('http://i.imgur.com/zvATqgs.gifv', function(done) { mehdown.render('http://i.imgur.com/zvATqgs.gifv', function(err, html) { - assert.equal(html, '

'); + assert.equal(html, '

imgur.com/zvATqgs

'); done(); }); }); @@ -19,22 +19,36 @@ describe('imgur', function() { it('imgur.com/zvATqgs.gifv', function(done) { mehdown.render('imgur.com/zvATqgs.gifv', function(err, html) { - assert.equal(html, '

'); + assert.equal(html, '

imgur.com/zvATqgs.gifv

'); done(); }); }); it('i.imgur.com/zvATqgs.gifv', function(done) { mehdown.render('i.imgur.com/zvATqgs.gifv', function(err, html) { - assert.equal(html, '

'); + assert.equal(html, '

imgur.com/zvATqgs

'); done(); }); }); it('www.imgur.com/zvATqgs.gifv', function(done) { mehdown.render('www.imgur.com/zvATqgs.gifv', function(err, html) { - assert.equal(html, '

'); + assert.equal(html, '

www.imgur.com/zvATqgs.gifv

'); done(); }); }); -}); + + it('https://i.imgur.com/Al5Q80f.jpg', function(done) { + mehdown.render('https://i.imgur.com/Al5Q80f.jpg', function(err, html) { + assert.equal(html, '

imgur.com/Al5Q80f

'); + done(); + }); + }); + + it('https://i.imgur.com/PySOiro.gif', function(done) { + mehdown.render('https://i.imgur.com/PySOiro.gif', function(err, html) { + assert.equal(html, '

imgur.com/PySOiro

'); + done(); + }); + }); +}); \ No newline at end of file