mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-06-26 13:27:55 +09:00
Improve, Refactor: Add some notes
This commit is contained in:
parent
79226e5a6c
commit
a560fc5cb1
15
src/index.ts
15
src/index.ts
@ -1,26 +1,41 @@
|
||||
/**
|
||||
* summaly
|
||||
* https://github.com/syuilo/summaly
|
||||
*/
|
||||
|
||||
import * as URL from 'url';
|
||||
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')({
|
||||
dirname: __dirname + '/plugins'
|
||||
});
|
||||
|
||||
/**
|
||||
* Summary an web page
|
||||
* @param {string} url URL of web page you want to summary
|
||||
* @return {Promise<ISummary>} Promised summary
|
||||
*/
|
||||
export default async (url: string): Promise<ISummary> => {
|
||||
// Follow redirects
|
||||
const actualUrl = await tracer(url);
|
||||
|
||||
const _url = URL.parse(actualUrl, true);
|
||||
|
||||
// Find matching plugin
|
||||
const match = Object.keys(plugins)
|
||||
.map(key => plugins[key])
|
||||
.filter(plugin => plugin.test(_url))[0] as IPlugin;
|
||||
|
||||
// Get summary
|
||||
const summary = match
|
||||
? await match.summary(_url)
|
||||
: await general(_url);
|
||||
|
||||
// Replace '' to null
|
||||
Object.keys(summary).forEach(k => {
|
||||
if (summary[k]) {
|
||||
summary[k] = summary[k].trim();
|
||||
|
Loading…
x
Reference in New Issue
Block a user