diff --git a/library.js b/library.js index 33f8014..c12131b 100644 --- a/library.js +++ b/library.js @@ -4,7 +4,8 @@ var controllers = require('./lib/controllers'); var YoutubeLite = {}, embed = '
'; - var regularUrl = //g; +var regularUrl = /(?:

|^)]*?>\1\4<\/a>(?:|<\/p>)/mg; +YoutubeLite.regularUrl = regularUrl; YoutubeLite.init = function(params, callback) { var router = params.router, diff --git a/package.json b/package.json index 2e2e027..b57a368 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/regex.js b/tests/regex.js new file mode 100644 index 0000000..571395e --- /dev/null +++ b/tests/regex.js @@ -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: '

https://youtu.be/fXhUgV9qzI0

', + expected: '
' + }, + { + description: 'Markdown link with a youtube URL should be left alone', + content: '

linked

', + expected: '

linked

' + }, + { + description: 'Video URLs on consecutive lines', + content: '

https://youtu.be/fXhUgV9qzI0
\nhttps://youtu.be/fXhUgV9qzI0

', + expected: '
\n
' + }, + { + description: 'Link with text in front should be left alone', + content: '

Look at thishttps://youtu.be/fXhUgV9qzI0

', + expected: '

Look at thishttps://youtu.be/fXhUgV9qzI0

' + }, + { + description: 'Link with text behind should be left alone', + content: '

https://youtu.be/fXhUgV9qzI0 uh huh

', + expected: '

https://youtu.be/fXhUgV9qzI0 uh huh

' + }, + { + description: 'Link with text in front and behind should be left alone', + content: '

Look: https://youtu.be/fXhUgV9qzI0 uh huh

', + expected: '

Look: https://youtu.be/fXhUgV9qzI0 uh huh

' + } +]; + + +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 ); + }); + + }); + }); + } +}); + +