-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleHtml.js
36 lines (35 loc) · 946 Bytes
/
handleHtml.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
const jsdom = require('jsdom');
module.exports = (html) => {
const document = jsdom.jsdom(html);
document.head.innerHTML += `
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0 24px;
line-height: 1.8 !important;
}
body p {
line-height: 1.8 !important;
}
a:-webkit-any-link {
color: #228AE5;
text-decoration: none;
}
</style>`;
if (!document.head.querySelector('title')) {
document.head.innerHTML += `<title>${(() => {
for (const tag of ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p']) {
const el = document.body.querySelector(tag);
if (el && el.textContent) {
return el.textContent;
}
}
return '';
})()}</title>`;
}
return jsdom.serializeDocument(document);
// pt转px
// .replace(/(\d)pt/g, ($0, $1) => {
// return `${$1}px`;
// })
};