This commit is contained in:
syuilo 2017-02-05 20:53:13 +09:00
parent 50c88af9ed
commit 70bf14ad35
3 changed files with 25 additions and 10 deletions

View File

@ -1,5 +1,6 @@
unreleased unreleased
------------------ ------------------
* Add user-defined plugin support #22
* Add `followRedirects` option #16 * Add `followRedirects` option #16
* Add `url` property to result #15 * Add `url` property to result #15

View File

@ -4,34 +4,49 @@
*/ */
import * as URL from 'url'; import * as URL from 'url';
import requireAll from 'require-all';
import tracer from 'trace-redirect'; import tracer from 'trace-redirect';
import ISummary from './isummary'; import ISummary from './isummary';
import IPlugin from './iplugin'; import IPlugin from './iplugin';
import general from './general'; import general from './general';
// Load plugins // Load builtin plugins
const plugins: IPlugin[] = require('require-all')({ const builtinPlugins = requireAll({
dirname: __dirname + '/plugins' dirname: __dirname + '/plugins'
}); }) as IPlugin[];
export interface IOptions { export interface IOptions {
/** /**
* *
*/ */
followRedirects: boolean; followRedirects?: boolean;
/**
* Custom Plugins
*/
plugins?: IPlugin[];
} }
type Result = ISummary & {
url: string;
};
/** /**
* Summary an web page * Summary an web page
* @param {string} url URL of web page you want to summary * @param {string} url URL of web page you want to summary
* @param {IOptions} options The options * @param {IOptions} options The options
* @return {Promise<ISummary>} Promised summary * @return {Promise<Result>} Promised summary
*/ */
export default async (url: string, options: IOptions): Promise<ISummary> => { export default async (url: string, options: IOptions): Promise<Result> => {
const opts = Object.assign({ const opts = Object.assign({
followRedirects: true followRedirects: true,
plugins: null
}, options); }, options);
const plugins = Object.keys(builtinPlugins)
.map(key => builtinPlugins[key])
.concat(opts.plugins || []);
// Follow redirects // Follow redirects
const actualUrl = opts.followRedirects ? await tracer(url) : url; const actualUrl = opts.followRedirects ? await tracer(url) : url;

View File

@ -4,7 +4,6 @@ interface ISummary {
sitename: string; sitename: string;
thumbnail: string; thumbnail: string;
title: string; title: string;
url: string;
} }
export default ISummary; export default ISummary;