-
Notifications
You must be signed in to change notification settings - Fork 29
/
url_to_markdown_processor.js
executable file
·32 lines (30 loc) · 1.26 KB
/
url_to_markdown_processor.js
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
const formatter = require('./url_to_markdown_formatters.js');
const common_filters = require('./url_to_markdown_common_filters');
const { Readability } = require('@mozilla/readability');
const turndown = require('turndown');
const JSDOM = require('jsdom').JSDOM;
const service = new turndown();
module.exports = {
process_dom: function (url, document, res, inline_title, ignore_links, id="") {
let title = document.window.document.querySelector('title');
if (title)
res.header("X-Title", encodeURIComponent(title.textContent));
if (id) {
document = new JSDOM('<!DOCTYPE html>'+ document.window.document.querySelector("#"+id).innerHTML);
}
let reader = new Readability(document.window.document);
let readable = reader.parse().content;
let replacements = [];
readable = formatter.format_codeblocks(readable, replacements);
readable = formatter.format_tables(readable, replacements);
let markdown = service.turndown(readable);
for (let i=0;i<replacements.length;i++) {
markdown = markdown.replace(replacements[i].placeholder, replacements[i].replacement);
}
let result = (url) ? common_filters.filter(url, markdown, ignore_links) : markdown;
if (inline_title && title) {
result = "# " + title.textContent + "\n" + result;
}
return result;
}
}