Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions library.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

var controllers = require('./lib/controllers');
var YoutubeLite = {},
embed = '<div class="js-lazyYT" data-youtube-id="$4" data-width="640" data-height="360"><iframe class="lazytube" src="//www.youtube.com/embed/$4"></iframe></div>';
embed = '<div class="js-lazyYT" data-youtube-id="$5" data-width="640" data-height="360" data-start="$7"><iframe class="lazytube" src="//www.youtube.com/embed/$5$6$7"></iframe></div>',
timeStamp = /t(\=\d+)/g,
startParam = /&start=/g;

var regularUrl = /<a.*?href="((https?:\/\/www\.)?youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\?(?:\S*?&?v\=))|(https?:\/\/)?youtu\.be\/)([a-zA-Z0-9_-]{6,11})".*?<\/a>/g;
var regularUrl = /(?:<p>|^)<a.*?href="((https?:\/\/(?:www\.)?)?youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\?(?:\S*?&?v\=))|(https?:\/\/)?youtu\.be\/)(([a-zA-Z0-9_-]{6,11})(?:([&\?])(t\=\d+)s)?)"[^>]*?>\1\4<\/a>(?:<br\/?>|<\/p>)/gm;
YoutubeLite.regularUrl = regularUrl;

YoutubeLite.init = function(params, callback) {
var router = params.router,
Expand Down Expand Up @@ -34,11 +37,12 @@ YoutubeLite.parse = function(data, callback) {
if (!data || !data.postData || !data.postData.content) {
return callback(null, data);
}
if (data.postData.content.match(regularUrl)) {
if (data.postData.content.match(regularUrl)) {
data.postData.content = data.postData.content.replace(regularUrl, embed);
data.postData.content = data.postData.content.replace(timeStamp, 'start$1');
data.postData.content = data.postData.content.replace(startParam, '?start=');
}
callback(null, data);

};

module.exports = YoutubeLite;
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
"description": "NodeBB Youtube Lite Plugin",
"main": "library.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha ./tests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/a5mith/nodebb-plugin-youtube-lite.git"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"winston": "^2.2.0"
},
"keywords": [
"nodebb",
"plugin",
Expand Down
3 changes: 2 additions & 1 deletion static/lib/lazyYT.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
innerHtml = [],
$thumb,
thumb_img,
start = $el.data('start'),
loading_text = $el.text() ? $el.text() : settings.loading_text,
youtube_parameters = $el.data('parameters') || '';

Expand Down Expand Up @@ -109,7 +110,7 @@
.on('click', function (e) {
e.preventDefault();
if (!$el.hasClass('lazyYT-video-loaded') && $thumb.hasClass('lazyYT-image-loaded')) {
$el.html('<iframe src="//www.youtube.com/embed/' + id + '?' + youtube_parameters + '&autoplay=1" frameborder="0" allowfullscreen></iframe>')
$el.html('<iframe src="//www.youtube.com/embed/' + id + '?' + youtube_parameters + (start ? '&' + start : '') + '&autoplay=1" frameborder="0" allowfullscreen></iframe>')
.addClass(settings.video_loaded_class);

// execute callback
Expand Down
76 changes: 76 additions & 0 deletions tests/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';
var winston = require('winston');

process.on('uncaughtException', function (err) {
winston.error('Encountered error while running test suite: ' + err.message);
});

var expect = require("chai").expect;

var youtubeLite = require("../library");

var posts = [
{
description: 'Simple post with only the youtube link',
content: '<p><a href="https://youtu.be/fXhUgV9qzI0">https://youtu.be/fXhUgV9qzI0</a></p>',
expected: '<div class="js-lazyYT" data-youtube-id="fXhUgV9qzI0" data-width="640" data-height="360" data-start=""><iframe class="lazytube" src="//www.youtube.com/embed/fXhUgV9qzI0"></iframe></div>'
},
{
description: 'Markdown link with a youtube URL should be left alone',
content: '<p><a href="https://youtu.be/fXhUgV9qzI0">linked</a></p>',
expected: '<p><a href="https://youtu.be/fXhUgV9qzI0">linked</a></p>'
},
{
description: 'Video URLs on consecutive lines',
content: '<p><a href="https://youtu.be/fXhUgV9qzI0">https://youtu.be/fXhUgV9qzI0</a><br/>\n<a href="https://youtu.be/fXhUgV9qzI0">https://youtu.be/fXhUgV9qzI0</a></p>',
expected: '<div class="js-lazyYT" data-youtube-id="fXhUgV9qzI0" data-width="640" data-height="360" data-start=""><iframe class="lazytube" src="//www.youtube.com/embed/fXhUgV9qzI0"></iframe></div>\n<div class="js-lazyYT" data-youtube-id="fXhUgV9qzI0" data-width="640" data-height="360" data-start=""><iframe class="lazytube" src="//www.youtube.com/embed/fXhUgV9qzI0"></iframe></div>'
},
{
description: 'Link with text in front should be left alone',
content: '<p>Look at this<a href="https://youtu.be/fXhUgV9qzI0">https://youtu.be/fXhUgV9qzI0</a></p>',
expected: '<p>Look at this<a href="https://youtu.be/fXhUgV9qzI0">https://youtu.be/fXhUgV9qzI0</a></p>'
},
{
description: 'Link with text behind should be left alone',
content: '<p><a href="https://youtu.be/fXhUgV9qzI0">https://youtu.be/fXhUgV9qzI0</a> uh huh</p>',
expected: '<p><a href="https://youtu.be/fXhUgV9qzI0">https://youtu.be/fXhUgV9qzI0</a> uh huh</p>'
},
{
description: 'Link with text in front and behind should be left alone',
content: '<p>Look: <a href="https://youtu.be/fXhUgV9qzI0">https://youtu.be/fXhUgV9qzI0</a> uh huh</p>',
expected: '<p>Look: <a href="https://youtu.be/fXhUgV9qzI0">https://youtu.be/fXhUgV9qzI0</a> uh huh</p>'
},
{
description: 'Simple post with only the youtu.be link with a start time',
content: '<p><a href="https://youtu.be/fXhUgV9qzI0?t=55s">https://youtu.be/fXhUgV9qzI0?t=55s</a></p>',
expected: '<div class="js-lazyYT" data-youtube-id="fXhUgV9qzI0" data-width="640" data-height="360" data-start="start=55"><iframe class="lazytube" src="//www.youtube.com/embed/fXhUgV9qzI0?start=55"></iframe></div>'
},
{
description: 'Simple post with only the youtube.com link with a start time',
content: '<p><a href="https://youtube.com/watch?v=fXhUgV9qzI0&t=55s">https://youtube.com/watch?v=fXhUgV9qzI0&t=55s</a></p>',
expected: '<div class="js-lazyYT" data-youtube-id="fXhUgV9qzI0" data-width="640" data-height="360" data-start="start=55"><iframe class="lazytube" src="//www.youtube.com/embed/fXhUgV9qzI0?start=55"></iframe></div>'
}
];


describe( 'regex tests', function(){
var i;
for( i = 0; i < posts.length; ++i ){
var post = posts[i];
describe( post.description, function(){

var data = new Object();
var expectedValue = post.expected;
data.postData = new Object();
data.postData.content = post.content;
it('converts the post correctly', function(){
youtubeLite.parse( data, function( callback, theData ){
expect( theData.postData.content ).to.equal( expectedValue );
});

});
});
}
});