mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-05-10 16:17:23 +09:00
This commit is contained in:
parent
50c88af9ed
commit
70bf14ad35
@ -1,5 +1,6 @@
|
||||
unreleased
|
||||
------------------
|
||||
* Add user-defined plugin support #22
|
||||
* Add `followRedirects` option #16
|
||||
* Add `url` property to result #15
|
||||
|
||||
|
29
src/index.ts
29
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<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;
|
||||
|
||||
|
@ -4,7 +4,6 @@ interface ISummary {
|
||||
sitename: string;
|
||||
thumbnail: string;
|
||||
title: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export default ISummary;
|
||||
|
Loading…
x
Reference in New Issue
Block a user