forked from danburzo/percollate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhancements.js
181 lines (163 loc) Β· 4.2 KB
/
enhancements.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
174
175
176
177
178
179
180
181
const srcset = require('srcset');
const URL = require('url').URL;
const replaceElementType = require('./replace-element-type');
/*
Convert AMP markup to HMTL markup
(naive / incomplete implementation)
*/
function ampToHtml(doc) {
// Convert <amp-img> to <img>
Array.from(doc.querySelectorAll('amp-img')).forEach(ampImg => {
replaceElementType(ampImg, 'img', doc);
});
}
function extractImage(doc) {
const selectors = [
"meta[name='twitter:image']",
"meta[name='og:image']",
'amp-img',
'img'
];
for (let idx = 0; idx < selectors.length; idx++) {
let imgs = Array.from(doc.querySelectorAll(selectors[idx]));
if (imgs && imgs.length > 0) {
if (imgs[0].getAttribute('content')) {
return imgs[0].getAttribute('content');
} else {
return imgs[0].getAttribute('src');
}
}
}
return null;
}
function fixLazyLoadedImages(doc) {
Array.from(
doc.querySelectorAll(`
img[data-src],
img[data-srcset],
img[data-sizes]
`)
).forEach(img => {
['src', 'srcset', 'sizes'].forEach(attr => {
if (attr in img.dataset) {
img.setAttribute(attr, img.dataset[attr]);
}
});
});
}
function imagesAtFullSize(doc) {
/*
Replace:
<a href='original-size.png'>
<img src='small-size.png'/>
</a>
With:
<img src='original-size.png'/>
*/
Array.from(doc.querySelectorAll('a > img:only-child')).forEach(img => {
let anchor = img.parentNode;
let original = anchor.href;
// only replace if the HREF matches an image file
// and exclude wikipedia links to image file pages
if (
original.match(/\.(png|jpg|jpeg|gif|svg)$/i) &&
!original.match(/wiki\/File\:/)
) {
img.setAttribute('src', original);
anchor.parentNode.replaceChild(img, anchor);
}
});
/*
Remove width/height attributes from <img> elements
and style them in CSS. (Should we do this, actually?)
*/
Array.from(doc.querySelectorAll('img')).forEach(img => {
img.removeAttribute('width');
img.removeAttribute('height');
});
}
function wikipediaSpecific(doc) {
/*
Remove some screen-only things from wikipedia pages:
- edit links next to headings
*/
Array.from(
doc.querySelectorAll(`
.mw-editsection
`)
).forEach(el => el.remove());
}
/*
Mark some links as not needing their HREF appended:
- links whose text content is the HREF
- in-page anchors
*/
function noUselessHref(doc) {
Array.from(doc.querySelectorAll('a'))
.filter(function(el) {
return (
(el.getAttribute('href') || '').match(/^\#/) ||
el.getAttribute('href') === el.textContent.trim()
);
})
.forEach(el => el.classList.add('no-href'));
}
/*
Convert relative URIs to absolute URIs:
* the `href` attribute of <a> elements (except for in-page anchors)
* the `src` attribute of <img> elements
* the `srcset` attribute of <source> elements (inside <picture> elements)
*/
function relativeToAbsoluteURIs(doc) {
function absoluteSrcset(str) {
return srcset.stringify(
srcset.parse(str).map(item => ({
...item,
url: new URL(item.url, doc.baseURI).href
}))
);
}
Array.from(doc.querySelectorAll('a:not([href^="#"])')).forEach(el => {
el.setAttribute('href', el.href);
});
Array.from(doc.querySelectorAll('img')).forEach(el => {
el.setAttribute('src', el.src);
});
Array.from(
doc.querySelectorAll('picture source[srcset], img[srcset]')
).forEach(el => {
el.setAttribute('srcset', absoluteSrcset(el.getAttribute('srcset')));
});
}
/*
Wraps single images into <figure> elements,
adding the image's `alt` attribute as <figcaption>
*/
function singleImgToFigure(doc) {
Array.from(doc.querySelectorAll('img:only-child')).forEach(image => {
let fig = doc.createElement('figure');
fig.appendChild(image.cloneNode());
let alt = image.getAttribute('alt');
if (alt) {
let figcaption = doc.createElement('figcaption');
figcaption.textContent = alt;
fig.appendChild(figcaption);
}
// Replace paragraph with figure
if (image.parentNode.matches('p') && image.parentNode.parentNode) {
image.parentNode.parentNode.replaceChild(fig, image.parentNode);
} else {
image.parentNode.replaceChild(fig, image);
}
});
}
module.exports = {
ampToHtml,
fixLazyLoadedImages,
extractImage,
imagesAtFullSize,
noUselessHref,
wikipediaSpecific,
relativeToAbsoluteURIs,
singleImgToFigure
};