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
3 changes: 2 additions & 1 deletion library.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ 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>';

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})"[^>]*?>\1\4<\/a>(?:<br\/?>|<\/p>)/mg;
YoutubeLite.regularUrl = regularUrl;

YoutubeLite.init = function(params, callback) {
var router = params.router,
Expand Down
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
66 changes: 66 additions & 0 deletions tests/regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'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"><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"><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"><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>'
}
];


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 );
});

});
});
}
});