diff --git a/src/index.ts b/src/index.ts index aac72f7..da2b4f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,8 @@ require('babel-polyfill'); const plugins: IPlugin[] = [ require('./plugins/wikipedia'), - require('./plugins/soundcloud') + require('./plugins/soundcloud'), + require('./plugins/youtube') ]; export default async (url: string, options?: Options): Promise => { diff --git a/src/plugins/youtube/index.ts b/src/plugins/youtube/index.ts new file mode 100644 index 0000000..6322706 --- /dev/null +++ b/src/plugins/youtube/index.ts @@ -0,0 +1,27 @@ +import * as URL from 'url'; +const pug = require('pug'); +import Options from '../../options'; + +exports.test = (url: URL.Url) => + url.hostname == 'youtube.com' || + url.hostname == 'www.youtube.com' || + url.hostname == 'youtu.be' +; + +exports.compile = async (url: URL.Url, opts: Options) => { + let videoId: string; + + switch (url.hostname) { + case 'www.youtube.com': + case 'youtube.com': + videoId = url.query.v; + break; + case 'youtu.be': + videoId = url.pathname; + break; + } + + return pug.renderFile(`${__dirname}/view.pug`, { + videoId + }); +}; diff --git a/src/plugins/youtube/view.pug b/src/plugins/youtube/view.pug new file mode 100644 index 0000000..980d486 --- /dev/null +++ b/src/plugins/youtube/view.pug @@ -0,0 +1 @@ +iframe.youtube(src='https://www.youtube.com/embed/' + videoId, width='380', height='250', frameborder='0', allowfullscreen) diff --git a/tsconfig.json b/tsconfig.json index 3ad7ef0..5b5c67b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -30,6 +30,7 @@ "./src/options.ts", "./src/general/index.ts", "./src/plugins/wikipedia/index.ts", + "./src/plugins/youtube/index.ts", "./src/plugins/soundcloud/index.ts" ] }