-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-affiliate-links.mjs
43 lines (34 loc) · 1.02 KB
/
update-affiliate-links.mjs
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
import { globby } from "globby";
import { JSDOM } from "jsdom";
import fs from "node:fs/promises";
const files = await globby("./dist/**/*.html");
const domainList = [
"amzn.to",
"amazon.com",
"shareasale.com",
"www.shareasale.com",
];
const attributes = {
target: "_blank",
rel: "nofollow noopener sponsored",
};
const domainMatch = (domain) => domainList.indexOf(domain) > -1;
const setAttributes = (element, attributes) => {
Object.keys(attributes).forEach((attr) => {
element.setAttribute(attr, attributes[attr]);
});
};
await Promise.all(
files.map(async (file) => {
let html = await fs.readFile(file, "utf-8");
const dom = new JSDOM(html);
const links = dom.window.document.querySelectorAll("a");
const linksArr = Array.from(links);
linksArr.forEach(
(link) => domainMatch(link.host) && setAttributes(link, attributes)
);
html = dom.serialize();
await fs.writeFile(file, html);
console.log("Updating links:", file);
})
);