forked from OpenTermsArchive/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (135 loc) · 5.63 KB
/
index.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import url from 'url';
import ciceroMark from '@accordproject/markdown-cicero';
import mardownPdf from '@accordproject/markdown-pdf';
import turndownPluginGithubFlavouredMarkdown from 'joplin-turndown-plugin-gfm';
import jsdom from 'jsdom';
import TurndownService from 'turndown';
import { InaccessibleContentError } from '../errors.js';
const { JSDOM } = jsdom;
const turndownService = new TurndownService();
turndownService.use(turndownPluginGithubFlavouredMarkdown.gfm);
export const LINKS_TO_CONVERT_SELECTOR = 'a[href]:not([href^="#"])';
const { PdfTransformer } = mardownPdf;
const { CiceroMarkTransformer } = ciceroMark;
const ciceroMarkTransformer = new CiceroMarkTransformer();
/**
* Filter document content and convert it to Markdown
*
* @param {Object} params - Filter parameters
* @param {string|Buffer} params.content - Content to filter: a buffer containing PDF data in case mimetype associated is PDF or a DOM dump of an HTML page given as a string
* @param {string} params.mimeType - MIME type of the given content
* @param {string} params.documentDeclaration - see {@link ./src/archivist/services/documentDeclaration.js}
* @returns {Promise<string>} Promise which is fulfilled once the content is filtered and converted in Markdown. The promise will resolve into a string containing the filtered content in Markdown format
*/
export default async function filter({ content, mimeType, documentDeclaration }) {
if (mimeType == 'application/pdf') {
return filterPDF({ content });
}
return filterHTML({
content,
documentDeclaration,
});
}
export async function filterHTML({ content, documentDeclaration }) {
const {
location,
contentSelectors = [],
noiseSelectors = [],
filters: serviceSpecificFilters = [],
} = documentDeclaration;
const jsdomInstance = new JSDOM(content, {
url: location,
virtualConsole: new jsdom.VirtualConsole(),
});
const { document: webPageDOM } = jsdomInstance.window;
for (const filterFunction of serviceSpecificFilters) {
try {
/* eslint-disable no-await-in-loop */
// We want this to be made in series
await filterFunction(webPageDOM, {
fetch: location,
select: contentSelectors,
remove: noiseSelectors,
filter: serviceSpecificFilters.map(filter => filter.name),
});
/* eslint-enable no-await-in-loop */
} catch (error) {
throw new InaccessibleContentError(`The filter function "${filterFunction.name}" failed: ${error}`);
}
}
remove(webPageDOM, noiseSelectors); // remove function works in place
const domFragment = select(webPageDOM, contentSelectors);
if (!domFragment.children.length) {
throw new InaccessibleContentError(`The provided selector "${contentSelectors}" has no match in the web page at '${location}'`);
}
convertRelativeURLsToAbsolute(domFragment, location);
domFragment.querySelectorAll('script, style').forEach(node => node.remove());
// clean code from common changing patterns - initially for Windstream
domFragment.querySelectorAll('a[href*="/email-protection"]').forEach(node => {
if (node.href.match(/((.*?)\/email-protection#)[0-9a-fA-F]+/gim)) {
node.href = `${node.href.split('#')[0]}#removed`;
}
});
const markdownContent = transform(domFragment);
if (!markdownContent) {
throw new InaccessibleContentError(`The provided selector "${contentSelectors}" matches an empty content in the web page at '${location}'`);
}
return markdownContent;
}
export async function filterPDF({ content: pdfBuffer }) {
try {
const ciceroMarkdown = await PdfTransformer.toCiceroMark(pdfBuffer);
return ciceroMarkTransformer.toMarkdown(ciceroMarkdown);
} catch (error) {
if (error.parserError) {
throw new InaccessibleContentError("Can't parse PDF file");
}
throw error;
}
}
function selectRange(document, rangeSelector) {
const { startBefore, startAfter, endBefore, endAfter } = rangeSelector;
const selection = document.createRange();
const startNode = document.querySelector(startBefore || startAfter);
const endNode = document.querySelector(endBefore || endAfter);
if (!startNode) {
throw new InaccessibleContentError(`The "start" selector has no match in document in: ${JSON.stringify(rangeSelector)}`);
}
if (!endNode) {
throw new InaccessibleContentError(`The "end" selector has no match in document in: ${JSON.stringify(rangeSelector)}`);
}
selection[startBefore ? 'setStartBefore' : 'setStartAfter'](startNode);
selection[endBefore ? 'setEndBefore' : 'setEndAfter'](endNode);
return selection;
}
export function convertRelativeURLsToAbsolute(document, baseURL) {
Array.from(document.querySelectorAll(LINKS_TO_CONVERT_SELECTOR)).forEach(link => {
link.href = url.resolve(baseURL, link.href);
});
}
// Works in place
function remove(webPageDOM, noiseSelectors) {
[].concat(noiseSelectors).forEach(selector => {
if (typeof selector === 'object') {
const rangeSelection = selectRange(webPageDOM, selector);
rangeSelection.deleteContents();
} else {
Array.from(webPageDOM.querySelectorAll(selector)).forEach(node => node.remove());
}
});
}
function select(webPageDOM, contentSelectors) {
const result = webPageDOM.createDocumentFragment();
[].concat(contentSelectors).forEach(selector => {
if (typeof selector === 'object') {
const rangeSelection = selectRange(webPageDOM, selector);
result.appendChild(rangeSelection.cloneContents());
} else {
webPageDOM.querySelectorAll(selector).forEach(element => result.appendChild(element));
}
});
return result;
}
function transform(domFragment) {
return turndownService.turndown(domFragment);
}