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
------------------
* Add user-defined plugin support #22
* Add `followRedirects` option #16
* Add `url` property to result #15

View File

@ -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<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({
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;

View File

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