-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
54 lines (46 loc) · 1.37 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
import HTMLParser from "node-html-parser";
import util from "util";
const args = util.parseArgs({
options: {
url: {
type: "string",
short: "u",
multiple: false,
},
"exclude-selectors": {
type: "string",
short: "e",
multiple: true,
},
},
args: process.argv.slice(2),
});
if (!args.values.url) {
throw new Error("URL is must be specified");
}
const result = await fetch(args.values.url);
if (!result.ok) {
throw new Error(`${result.status} ${result.statusText}`);
}
const html = await result.text();
const dom = HTMLParser.parse(html);
const content = dom.querySelector(".entry-content");
for (const elem of content.querySelectorAll("*")) {
elem.setAttribute("data-ablog2ghr-innertext", elem.innerText);
}
for (const selector of args.values["exclude-selectors"] ?? []) {
content.querySelectorAll(selector).forEach(e => e.remove());
}
for (const elem of content.querySelectorAll("*")) {
elem.removeAttribute("data-ablog2ghr-innertext");
}
for (const elem of content.querySelectorAll("img")) {
elem.setAttribute("style", `width: auto; height: auto; max-width: 100%; ${elem.getAttribute("style") ?? ""}`)
}
let markdown;
if (content.querySelector(".ys-toc")) {
markdown = Array.from(content.querySelectorAll(".ys-toc ~ *")).map(e => e.outerHTML).join("\n");
} else {
markdown = content.outerHTML;
}
console.log(markdown);