-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
find-generator.js
104 lines (86 loc) · 2.37 KB
/
find-generator.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
import fetch from "node-fetch";
import cheerio from "cheerio";
import EleventyImage from "@11ty/eleventy-img";
const Generators = {
eleventy: "https://www.11ty.dev/",
"11ty": "https://www.11ty.dev/",
hugo: "https://gohugo.io/",
gatsby: "https://www.gatsbyjs.com/",
wordpress: "https://wordpress.com/",
silex: "https://www.silex.me/",
jekyll: "https://jekyllrb.com/",
docusaurus: "https://docusaurus.io/",
gridsome: "https://gridsome.org/",
vuepress: "https://vuepress.vuejs.org/",
hexo: "https://hexo.io/",
astro: "https://astro.build/",
}
class FindGenerator {
constructor(url) {
this.url = url;
if(!this.isFullUrl(url)) {
throw new Error(`Invalid \`url\`: ${url}`);
}
}
isFullUrl(url) {
try {
new URL(url);
return true;
} catch(e) {
// invalid url OR local path
return false;
}
}
async fetch() {
let response = await fetch(this.url);
let body = await response.text();
this.body = body;
this.$ = cheerio.load(body);
return body;
}
// <meta name="generator" content="Eleventy v2.0.0">
findData() {
let metas = this.$("meta[name='generator']");
for(let meta of metas) {
let value = meta.attribs.content;
return value;
}
throw new Error("No <meta name='generator' content> element found.");
}
getImageUrl(generatorName) {
generatorName = generatorName.toLowerCase();
let url;
for(let key in Generators) {
if(generatorName.includes(key)) {
url = Generators[key];
break;
}
}
if(url) {
return `https://v1.indieweb-avatar.11ty.dev/${encodeURIComponent(url)}/`;
} else {
// found a generator name but was not supported
if(generatorName) {
// notably this is different than no generator found (error state is a transparent image)
return "./x.svg";
}
throw new Error("No indieweb avatar known for generator: " + generatorInfo.name);
}
}
async getImage(generatorName, width) {
let imageUrl = this.getImageUrl(generatorName);
let stats = await EleventyImage(imageUrl, {
widths: [width],
formats: ["auto"],
dryRun: true,
});
let format = Object.keys(stats).pop();
let stat = stats[format][0];
return {
format: format,
contentType: stat.sourceType,
body: stat.buffer,
}
}
}
export default FindGenerator;