diff --git a/CHANGELOG.md b/CHANGELOG.md index 48b029e..c225371 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ unreleased ------------------ +* Add user-defined plugin support #22 * Add `followRedirects` option #16 * Add `url` property to result #15 diff --git a/src/index.ts b/src/index.ts index 6726a38..133a7b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,34 +4,49 @@ */ import * as URL from 'url'; +import requireAll from 'require-all'; import tracer from 'trace-redirect'; import ISummary from './isummary'; import IPlugin from './iplugin'; import general from './general'; -// Load plugins -const plugins: IPlugin[] = require('require-all')({ +// Load builtin plugins +const builtinPlugins = requireAll({ dirname: __dirname + '/plugins' -}); +}) as IPlugin[]; export interface IOptions { /** * リダイレクトを追跡するか否か */ - followRedirects: boolean; + followRedirects?: boolean; + + /** + * Custom Plugins + */ + plugins?: IPlugin[]; } +type Result = ISummary & { + url: string; +}; + /** * Summary an web page - * @param {string} url URL of web page you want to summary - * @param {IOptions} options The options - * @return {Promise} Promised summary + * @param {string} url URL of web page you want to summary + * @param {IOptions} options The options + * @return {Promise} Promised summary */ -export default async (url: string, options: IOptions): Promise => { +export default async (url: string, options: IOptions): Promise => { const opts = Object.assign({ - followRedirects: true + followRedirects: true, + plugins: null }, options); + const plugins = Object.keys(builtinPlugins) + .map(key => builtinPlugins[key]) + .concat(opts.plugins || []); + // Follow redirects const actualUrl = opts.followRedirects ? await tracer(url) : url; diff --git a/src/isummary.ts b/src/isummary.ts index e26449d..c6567b4 100644 --- a/src/isummary.ts +++ b/src/isummary.ts @@ -4,7 +4,6 @@ interface ISummary { sitename: string; thumbnail: string; title: string; - url: string; } export default ISummary;