forked from paparitsidis-nci/docroot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.js
executable file
·44 lines (36 loc) · 1.03 KB
/
fix.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
const fs = require("fs");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
let idDict = {};
const inputFilePath = process.argv[2];
const outputFilePath = process.argv[3];
fs.readFile(inputFilePath, "utf-8", (err, html) => {
if (err) {
console.error(err);
return;
}
const dom = new JSDOM(html);
const document = dom.window.document;
// Fix duplicate ids
document.querySelectorAll("[id]").forEach(element => {
let id = element.getAttribute("id");
if (idDict[id]) {
let newId = id + "_" + Math.floor(Math.random() * 100000);
element.setAttribute("id", newId);
idDict[newId] = true;
} else {
idDict[id] = true;
}
});
// Remove the `role='doc-endnote'` from the `li` elements
document.querySelectorAll("li[role='doc-endnote']").forEach(element => {
element.removeAttribute("role");
});
fs.writeFile(outputFilePath, dom.serialize(), err => {
if (err) {
console.error(err);
return;
}
console.log(`Output written to ${outputFilePath}`);
});
});