-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
readme.js
93 lines (87 loc) · 2.46 KB
/
readme.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
class ReadmeComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: "open"});
this.path = this.getAttribute("path") || "readme";
}
connectedCallback() {
Promise.all([
this.loadScript(
"https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.1/showdown.min.js"
),
])
.then(() => {
this.fetchReadme();
})
.catch((error) => {
console.error(error);
});
}
loadScript(src, defer) {
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = src;
if (defer) {
script.defer = true;
}
script.onload = () => resolve(script);
script.onerror = (error) =>
reject(new Error(`Script load error: ${error}`));
document.head.appendChild(script);
});
}
async fetchReadme() {
try {
const response = await fetch(
`https://api.github.com/repos/CodeYourFuture/CYF-Workshops/${this.path}`
);
if (!response.ok) {
console.error("Failed to fetch README: " + response.status);
return;
}
const json = await response.json();
const decodedContent = atob(json.content);
this.render(decodedContent);
} catch (error) {
console.error(error);
}
}
render(md) {
try {
showdown.extension("objectives", function () {
"use strict";
return [
{
type: "lang",
regex: /```objectives\s*([\s\S]*?)\s*```/g,
replace: (_, codeContent) => `${codeContent.trim()}`,
},
];
});
let converter = new showdown.Converter({
omitExtraWLInCodeBlocks: true,
simplifiedAutoLink: true,
literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
tablesHeaderId: true,
ghCodeBlocks: true,
tasklists: true,
disableForced4SpacesIndentedSublists: true,
simpleLineBreaks: true,
requireSpaceBeforeHeadingText: true,
ghCompatibleHeaderId: true,
ghMentions: true,
backslashEscapesHTMLTags: true,
emoji: true,
splitAdjacentBlockquotes: true,
extensions: ["objectives"],
});
let html = converter.makeHtml(md);
this.shadowRoot.innerHTML = `${html}`;
} catch (error) {
console.error("Failed to convert markdown to HTML: " + error);
}
}
}
customElements.define("readme-component", ReadmeComponent);