-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdocs.js
166 lines (119 loc) · 5.06 KB
/
docs.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const TWITTER_MAX_CHAR_COUNT = 280;
const TWITTER_HANDLE = "@codeurdreams";
const REMOVED_ELEMENTS = ["script", "link", "style"];
var currentPage = null;
var oldTweet = null;
function visitDocumentation(path, updateUrl = true) {
path = path.replace(/^docs\/\//g, "docs/");
currentPage = path;
var isExternalResource = path.startsWith("http://") || path.startsWith("https://");
if (window.inDocsPopout && updateUrl) {
window.history.pushState("", "", `${window.location.href.split("?")[0]}?page=${path}`);
}
fetch(path).then(function(response) {
return response.text();
}).then(function(data) {
var converter = new showdown.Converter();
var container = document.createElement("div");
container.innerHTML = converter.makeHtml(data.replace(/>/, ">"));
container.querySelectorAll(REMOVED_ELEMENTS.join(", ")).forEach((element) => element.remove());
container.querySelectorAll("*").forEach(function(element) {
if (element.matches(REMOVED_ELEMENTS.join(", "))) {
element.remove();
return;
}
[...element.attributes].forEach(function(attribute) {
if (attribute.name.startsWith("on")) {
element.setAttribute(attribute.name, null);
}
});
});
document.querySelector("#docsContent").innerHTML = container.innerHTML;
document.querySelector("#docsContent").scrollTo(0, 0);
document.querySelectorAll("a").forEach(function(element) {
var destination = element.getAttribute("href") || "";
if (destination.startsWith("http://") || destination.startsWith("https://") || destination.startsWith("./") || destination.startsWith("javascript:") || destination.startsWith("#")) {
if (isExternalResource && (destination.startsWith("http://") || destination.startsWith("https://"))) {
element.setAttribute("target", "_blank");
}
if (isExternalResource && destination.startsWith("javascript:")) {
element.setAttribute("href", "javascript:void(0);");
}
return;
}
if (isExternalResource) {
element.setAttribute("href", `javascript:void(0);`);
element.addEventListener("click", function() {
visitDocumentation(new URL(".", path) + "/" + destination);
});
return;
}
element.setAttribute("href", `javascript:visitDocumentation("docs/${destination.replace(/^\//, "")}");`);
});
document.querySelectorAll("code").forEach(function(element) {
var code = element.textContent;
element.innerHTML = syntax.renderDocumentationSyntaxHighlighting(code);
if (element.textContent != code) {
element.textContent = code; // Fallback
}
});
if (window.inDocsPopout) {
document.querySelectorAll("details").forEach(function(element) {
element.open = true;
});
if (window.location.hash == "") {
window.scrollTo(0, 0);
}
}
oldTweet = null;
});
}
function popOutDocumentation(path = currentPage) {
window.open(`docspopout.html?page=${path}`);
}
function toTweet(code, directHandle = true, inBase2048 = false) {
var tweet = "";
if (directHandle) {
tweet += "@codeurdreams ";
}
if (inBase2048) {
tweet += "🗜️ ";
}
tweet += code;
return tweet;
}
function startTweetIntent() {
window.open(`https://twitter.com/intent/tweet?text=${encodeURIComponent(oldTweet)}&url=`, "_blank", "noopener");
}
window.addEventListener("load", function() {
if (window.inDocsPopout) {
return;
}
visitDocumentation("docs/index.md");
setInterval(function() {
var code = basic.programToText();
var representedCode = code;
var tweet = toTweet(representedCode, true);
if (tweet.length > TWITTER_MAX_CHAR_COUNT) {
representedCode = base2048.encode(new TextEncoder().encode(representedCode));
tweet = toTweet(representedCode, true, true);
}
if (tweet != oldTweet) {
document.querySelectorAll(".tweetableCode").forEach(function(element) {
element.innerHTML = "";
var handle = document.createElement("a");
handle.href = "https://twitter.com/codeurdreams";
handle.target = "_blank";
handle.textContent = "@codeurdreams";
element.append(handle);
var code = document.createElement("span");
code = tweet.replace(/^@codeurdreams /, " ");
element.append(code);
});
document.querySelectorAll(".tweetableCodeSize").forEach(function(element) {
element.textContent = tweet.length;
});
oldTweet = tweet;
}
}, 100);
});