-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebtori.js
More file actions
84 lines (77 loc) · 2.77 KB
/
webtori.js
File metadata and controls
84 lines (77 loc) · 2.77 KB
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
(async () => {
const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
const dirs = ["prev", "next", "below", "above", "behind", "front"];
const opp = (dir) =>
dirs[(dirs.indexOf(dir) / 2) * 2 + !(dirs.indexOf(dir) % 2)];
/* get the opposite direction: next one if even index or prev one if odd */
let myPage = fs.readFileSync("index.html", "utf8");
const $ = cheerio.load(myPage);
const getNeighbors = async () =>
Object.fromEntries(
await Promise.all(
$("*[id*='wr-link-']")
.map(async function () {
let $n;
try {
let theirPage = await axios.get($(this).attr("href"));
$n = cheerio.load(theirPage.data); /* get neighbor page DOM */
} catch (e) {
$n = cheerio.load("");
}
return [
$(this)
.attr("id")
.split(" ")
.filter((e) => e.includes("wr-link-"))[0]
.split("wr-link-")[1] /* get direction of your links */,
Object.fromEntries(
await Promise.all(
$n("*[id*='wr-link-']") /* find links in neighbor */
.map(async function () {
return [
$n(this)
.attr("id")
.split(" ")
.filter((e) => e.includes("wr-link-"))[0]
.split("wr-link-")[1] /* get directions of neighbor's
links */,
{
text: $n(this).text(),
href: $n(this).attr("href"),
} /* make object of neighbor's link text/url */,
];
})
.get()
)
),
];
})
.get()
)
); /* summary: get all ring links and build an object with their directions,
corresponding neighbor's ring links */
const neighbors = await getNeighbors();
console.log(neighbors);
for (const dir of dirs) {
const theirURL = neighbors?.[dir]?.[opp(dir)]?.href;
if (
theirURL &&
new URL(theirURL).hostname != process.argv[2] /* this argument
is your domain name, default "$GITHUB_REPOSITORY_OWNER.github.io" */
) {
matchLinks = new RegExp(
`(.*)(<a.*?id="wr-link-${dir}".*?href=")([^"]*?)(".*?>)(.+?)(<\/a>)`,
"gis"
);
myPage = myPage.replace(
matchLinks,
`$1$2${neighbors[dir][opp(dir)].href}$4${
neighbors[dir][opp(dir)].text
}$6`
);
} /* update ring links if neighbor's neighbor isn't you anymore */
}
fs.writeFileSync("index.html", myPage);
})();