-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
47 lines (43 loc) · 1.49 KB
/
Copy pathhandler.js
File metadata and controls
47 lines (43 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
// HTTP client
const axios = require('axios').default
// Readability, dom and dom purify
const {JSDOM} = require('jsdom')
var { Readability } = require('@mozilla/readability');
const createDOMPurify = require('dompurify')
const DOMPurify = createDOMPurify((new JSDOM('')).window)
// Not too happy to allow iframe, but it's the only way to get youtube videos
const domPurifyOptions = {
ADD_TAGS: ['iframe', 'video']
}
module.exports.extract = (event, context, callback) => {
axios
.get(event.url)
.then((response) => {
const dom = new JSDOM(response.data, {
url: event.url
})
var parsed = new Readability(dom.window.document).parse();
console.log('Fetched and parsed ' + event.url + ' successfully')
return callback(null, {
statusCode: 200,
headers: {'Content-Type': 'text/html'},
body: {
url: event.url,
content: DOMPurify.sanitize(parsed.content, domPurifyOptions),
excerpt: parsed.excerpt || ''
},
});
})
.catch((error) => {
console.log(error)
const response = {
statusCode: 200,
body: JSON.stringify({
error: 'Error while fetching the content',
details: error
}),
};
return callback(null, response);
});
};