-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.js
113 lines (98 loc) · 2.56 KB
/
shared.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
import {selectAll} from 'hast-util-select';
/**
* @param {import('hast').Element} timeEl
* @param {string} date
*/
export function applyDateToTime(timeEl, date) {
timeEl.properties.datetime = date;
timeEl.children.push({
type: 'text',
value: new Date(date).toLocaleString('en-GB', {
weekday: 'long',
day: 'numeric',
month: 'long',
year: 'numeric'
})
});
}
/**
* Find the `Element` that has the given `element` in the given `tree`.
* @param {import('hast').Element} element
* @param {import('hast').Element} tree
* @returns {import('hast').Element | null} The parent of `element`.
*/
export function selectParent(element, tree) {
if (!tree || !Array.isArray(tree.children)) {
return null;
}
if (tree.children.includes(element)) {
return tree;
}
for (const childTree of tree.children) {
const parent = selectParent(element, childTree);
if (parent !== null) {
return parent;
}
}
return null;
}
/**
*
* @param {import('hast').Element} elementToRemove
* @param {import('hast').Element} tree
*/
export function removeElement(elementToRemove, tree) {
for (const element of tree.children) {
if (element === elementToRemove) {
tree.children.splice(
tree.children.indexOf(element),
1
);
return true;
}
if (Array.isArray(element.children) && element.children.length > 0) {
if (removeElement(elementToRemove, element)) {
return true;
}
}
}
return false;
}
const assetSelectors = new Set([
'link[rel=stylesheet]',
'link[rel=icon]',
'img',
'picture>source',
'script[src]'
]);
const tagNameToProp = new Map([
['a', 'href'],
['link', 'href'],
['img', 'src'],
['source', 'srcSet'],
['script', 'src']
]);
const elementsWithLinks = new Set([
...assetSelectors,
'a'
]);
const assetSelection = Array.from(assetSelectors).join(',');
const linkSelection = Array.from(elementsWithLinks).join(',');
function onlyIncludeLocalLinks(memo, el) {
const linkProp = tagNameToProp.get(el.tagName);
if (!linkProp) {
throw new Error(`No prop found to overwrite for "${el.tagName}"`);
}
const href = el.properties[linkProp];
if (!href || href.startsWith('http')) {
// Empty value or it's an external asset. Ignore it.
return memo;
}
return memo.concat({el, linkProp});
}
export function selectAllAssetElements(tree) {
return selectAll(assetSelection, tree).reduce(onlyIncludeLocalLinks, []);
}
export function selectAllLinkedElements(tree) {
return selectAll(linkSelection, tree).reduce(onlyIncludeLocalLinks, []);
}