Skip to content

Commit

Permalink
Add support for imgur embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
freshlogic committed Nov 28, 2018
1 parent 3b75689 commit 1fc0518
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
});
```
```
21 changes: 8 additions & 13 deletions lib/plugins/imgur.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 `<div class="imgur-gifv"><video autoplay loop muted><source type="video/webm" src="https://i.imgur.com/${id}.webm" /><source type="video/mp4" src="https://i.imgur.com/${id}.mp4" /></video></div>`;
}
return `<div class="imgur-embed"><blockquote class="imgur-embed-pub" lang="en" data-id="${id}"><a href="//imgur.com/${id}">imgur.com/${id}</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script></div>`;
}
}
}
Expand All @@ -46,4 +41,4 @@ module.exports = function(md) {

return defaultRender(tokens, idx, options, env, self);
};
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions test/fixtures/mehdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2 id="supports-turning-urls-from-many-popular-sites-into-html-embeds">Supports
<p>Image URLs are converted to images.<br />
<img height="128" src="https://res.cloudinary.com/mediocre/image/upload/Meh36Faces128px_01_t366di.png" width="128" /></p>
<p>imgur GIFV URLs are converted to high quality looping GIF videos.<br />
<div class="imgur-gifv"><video autoplay loop muted><source type="video/webm" src="https://i.imgur.com/zvATqgs.webm" /><source type="video/mp4" src="https://i.imgur.com/zvATqgs.mp4" /></video></div></p>
<div class="imgur-embed"><blockquote class="imgur-embed-pub" lang="en" data-id="zvATqgs"><a href="//imgur.com/zvATqgs">imgur.com/zvATqgs</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script></div></p>
<p>Kickstarter URLs are converted to campaign previews.<br />
<iframe class="kickstarter" frameborder="0" scrolling="no" src="https://www.kickstarter.com/projects/1270124222/crossing-paths-film-photography-documentary/widget/card.html"></iframe></p>
<p>Reddit permalink URLs are converted to embeddable comment threads.<br />
Expand All @@ -51,4 +51,4 @@ <h2 id="usage">Usage</h2>
mehdown.render('markdown text here', { commands: true, detectImageSizes: false }, function(err, html) {
console.log(html);
});
</code></pre>
</code></pre>
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<p><img height="528" src="https://res.cloudinary.com/mediocre/image/upload/kekjvvhpkxh0v8x9o6u7.png" width="528" /> <img height="250" src="https://i.imgur.com/8peBgQn.png" width="300" /> <img height="528" src="https://res.cloudinary.com/mediocre/image/upload/kekjvvhpkxh0v8x9o6u7.png" width="528" /></p>');
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, '<p><img height="528" src="https://res.cloudinary.com/mediocre/image/upload/kekjvvhpkxh0v8x9o6u7.png" width="528" /> <img height="528" src="https://res.cloudinary.com/mediocre/image/upload/kekjvvhpkxh0v8x9o6u7.png" width="528" /></p>');
done();
});
});
Expand Down Expand Up @@ -953,4 +953,4 @@ describe('security', function() {
done();
});
});
});
});
6 changes: 3 additions & 3 deletions test/plugins/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<p><img src="http://i.imgur.com/9oiY1aO.jpg" /></p>');
mehdown.render('http://example.com/image.jpg', function(err, html) {
assert.equal(html, '<p><img src="http://example.com/image.jpg" /></p>');
done();
});
});
Expand Down Expand Up @@ -58,4 +58,4 @@ describe('images', function() {
done();
});
});
});
});
24 changes: 19 additions & 5 deletions test/plugins/imgur.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<p><div class="imgur-gifv"><video autoplay loop muted><source type="video/webm" src="https://i.imgur.com/zvATqgs.webm" /><source type="video/mp4" src="https://i.imgur.com/zvATqgs.mp4" /></video></div></p>');
assert.equal(html, '<p><div class="imgur-embed"><blockquote class="imgur-embed-pub" lang="en" data-id="zvATqgs"><a href="//imgur.com/zvATqgs">imgur.com/zvATqgs</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script></div></p>');
done();
});
});
Expand All @@ -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, '<p><div class="imgur-gifv"><video autoplay loop muted><source type="video/webm" src="https://i.imgur.com/zvATqgs.webm" /><source type="video/mp4" src="https://i.imgur.com/zvATqgs.mp4" /></video></div></p>');
assert.equal(html, '<p><a href="http://imgur.com/zvATqgs.gifv">imgur.com/zvATqgs.gifv</a></p>');
done();
});
});

it('i.imgur.com/zvATqgs.gifv', function(done) {
mehdown.render('i.imgur.com/zvATqgs.gifv', function(err, html) {
assert.equal(html, '<p><div class="imgur-gifv"><video autoplay loop muted><source type="video/webm" src="https://i.imgur.com/zvATqgs.webm" /><source type="video/mp4" src="https://i.imgur.com/zvATqgs.mp4" /></video></div></p>');
assert.equal(html, '<p><div class="imgur-embed"><blockquote class="imgur-embed-pub" lang="en" data-id="zvATqgs"><a href="//imgur.com/zvATqgs">imgur.com/zvATqgs</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script></div></p>');
done();
});
});

it('www.imgur.com/zvATqgs.gifv', function(done) {
mehdown.render('www.imgur.com/zvATqgs.gifv', function(err, html) {
assert.equal(html, '<p><div class="imgur-gifv"><video autoplay loop muted><source type="video/webm" src="https://i.imgur.com/zvATqgs.webm" /><source type="video/mp4" src="https://i.imgur.com/zvATqgs.mp4" /></video></div></p>');
assert.equal(html, '<p><a href="http://www.imgur.com/zvATqgs.gifv">www.imgur.com/zvATqgs.gifv</a></p>');
done();
});
});
});

it('https://i.imgur.com/Al5Q80f.jpg', function(done) {
mehdown.render('https://i.imgur.com/Al5Q80f.jpg', function(err, html) {
assert.equal(html, '<p><div class="imgur-embed"><blockquote class="imgur-embed-pub" lang="en" data-id="Al5Q80f"><a href="//imgur.com/Al5Q80f">imgur.com/Al5Q80f</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script></div></p>');
done();
});
});

it('https://i.imgur.com/PySOiro.gif', function(done) {
mehdown.render('https://i.imgur.com/PySOiro.gif', function(err, html) {
assert.equal(html, '<p><div class="imgur-embed"><blockquote class="imgur-embed-pub" lang="en" data-id="PySOiro"><a href="//imgur.com/PySOiro">imgur.com/PySOiro</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script></div></p>');
done();
});
});
});

0 comments on commit 1fc0518

Please sign in to comment.