UNPKG

2.39 kBJavaScriptView Raw
1try {
2 // Pick the original html
3 const fs = require('fs');
4 const path = require('path');
5
6 var original = fs.readFileSync(path.join('interim', 'original.html'));
7
8} catch (e) {
9 if (e.code === 'ENOENT') {
10 console.log('File not found!');
11
12 } else {
13 throw e;
14 }
15 process.exit(1);
16}
17
18function sanitize() {
19
20 const cleaner = require('clean-html');
21 const sanitizeHtml = require('sanitize-html');
22
23 const strippedHTML = sanitizeHtml(original, {
24 // TODO: move these into a config
25 allowedTags: ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'em', 'strong', 'i', 'b', 'pre', 'img'],
26 allowedAttributes: {
27 img: ['src', 'name', 'alt', 'width'],
28 '*': ['class']
29 },
30 nonTextTags: ['style', 'script', 'textarea', 'noscript', 'a', 'title'],
31 selfClosing: ['img'],
32 exclusiveFilter(elem) {
33 return elem.tag !== 'img' && !elem.text.trim();
34 },
35 transformTags: {
36 'pre' (tagName, attribs) {
37 return {
38 tagName: 'p',
39 attribs: {
40 class: 'bottom'
41 }
42 };
43 }
44 },
45 allowedSchemes: ['http', 'https', 'mailto'],
46 allowedSchemesByTag: {
47 img: ['http', 'data', 'https']
48 }
49 });
50
51 cleaner.clean(strippedHTML, { wrap: 0 }, book => {
52 const path = require('path');
53 const fsp = require('fs-promise');
54 const chalk = require('chalk');
55
56
57 fsp.readJson(path.join('.', '.abelonerc'))
58 .then((abelonerc) => {
59 let repo_url = abelonerc.repo_url;
60 return book.replace(new RegExp("src=\"images/", 'g'), `width=\"100\%\" height=\"100\%\" src=\"${repo_url}/images/`);
61 }).then((book) => {
62 return book.replace(new RegExp(""", 'g'), '"');
63 }).then((book) => {
64 let finalbook = `<body>${book}</body>`;
65
66 const saver = require(path.join('..', 'lib', 'saver.js'));
67 const filename = 'sanitized';
68
69 saver.save(finalbook, filename);
70
71 }).catch((err) => {
72 if (err)
73 console.log(chalk.bold.red('Failed to pick up contents', err));
74
75 });
76
77
78 });
79}
80
81module.exports.sanitize = sanitize;