-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
409 lines (320 loc) · 11.7 KB
/
publish.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import {
parseGemData, naturalSorter, entryIterator, entryPathToId
} from './utils.js';
marked.setOptions({
highlight: function(code, lang) {
const highlighted = hljs.highlightAuto(code);
return highlighted.value;
},
});
const navHtml = `
<nav class='semi-transparent'>
<a class='nav-btn' href='../feed/'>Feed</a>
<a class='nav-btn' href='../about/'>About</a>
</nav>
`;
const entryNavHtml = `
<nav class='semi-transparent'>
<a class='nav-btn' href='../feed/'>Feed</a>
<a class='nav-btn' href='../about/'>About</a>
</nav>
`;
const importsHtml = `
<link rel="stylesheet"
href="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/tomorrow-night.min.css">
`;
const analyticsHtml = `
<script data-goatcounter="https://stats.apitman.com/count"
async src="//gc.zgo.at/count.js"></script>
`;
const minimalFavicon = '<link rel="icon" href="data:image/gif;base64,R0lGODlhEAAQAAAAACwAAAAAAQABAAACASgAOw==">';
export async function publishAllEntries(src, token) {
console.log("Begin Publishing");
(async () => {
const themeCssUrl = src + 'theme.css?access_token=' + token;
const themeCss = await fetch(themeCssUrl).then(r => r.text());
const hlCssUrl = 'https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/tomorrow-night.min.css';
const hlCss = await fetch(hlCssUrl).then(r => r.text());
const inlineCss = themeCss + hlCss;
await publishAboutPage(inlineCss);
await publishFeedPage(src, token, inlineCss);
console.log("Done Publishing");
})();
async function publishAboutPage(inlineCss) {
const aboutHtml = `
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Anders' little corner of the internet</title>
${minimalFavicon}
<style>
${inlineCss}
</style>
</head>
<body>
<div class='main'>
${navHtml}
<div id='portrait-container'>
<div class='portrait'>
<img src="../gemdrive/images/512/portrait.jpg" width="100%">
</div>
</div>
<div class='text-content semi-transparent'>
<p>
<strong>Hi there</strong>. My name is Anders Pitman. I am a data
visualization software engineer. I'm passionate about the power of a good
visualization to catalyze learning and insight. I'm particularly drawn to
problems in medicine, education, and social issues. Hans Rosling is my
hero.
</p>
<p>
You can email me at <strong>anders</strong> at this domain. Here are some
other useful links:
</p>
<section class='contact-links'>
<a target="_blank" href="https://github.com/anderspitman" rel="me">GitHub</a>
<a target="_blank" href="https://stackoverflow.com/users/943814/anderspitman" rel="me">StackOverflow</a>
<a target="_blank" href="https://www.linkedin.com/in/anderspitman/" rel="me">LinkedIn</a>
<a target="_blank" href="https://twitter.com/anderspitman" rel="me">Twitter</a>
</section>
<!--
<p>
Note: The background visualization is actually a bit cooler than it
looks. The code is pulled live from my public GitHub contributions. You
can learn more about it
<a target='_blank' href='/projects/redpill'>here</a>.
</p>
-->
</div>
<!-- creates space so background visualization continues past content -->
<div class='spacer'></div>
</div>
${analyticsHtml}
</body>
</html>
`;
await fetch(src + 'about/?recursive=true&access_token=' + token, {
method: 'PUT',
});
await fetch(src + 'about/index.html?overwrite=true&access_token=' + token, {
method: 'PUT',
body: aboutHtml,
});
// Main page is just an exact copy of the about page currently. Note that
// I'm using a bit of a hack here. The template string above uses ../ to
// access a couple resources, but it works for the root page as well
// since the browser stops at the top level.
await fetch(src + 'index.html?overwrite=true&access_token=' + token, {
method: 'PUT',
body: aboutHtml,
});
const portraitBlob = await fetch(src + 'portrait.jpg?access_token=' + token)
.then(r => r.blob());
await fetch(src + 'portrait.jpg?overwrite=true&access_token=' + token, {
method: 'PUT',
body: portraitBlob,
});
}
}
export async function publishEntry(entryUrl, token, inlineCss) {
console.log("Publishing " + entryUrl);
const metaUrl = entryUrl + 'entry.json';
const metaResponse = await fetch(metaUrl + '?access_token=' + token);
if (metaResponse.status !== 200) {
return null;
}
const meta = await metaResponse.json();
const textUrl = entryUrl + 'entry.md';
const textResponse = await fetch(textUrl + '?access_token=' + token);
const text = await textResponse.text();
const contentHtml = marked.parse(text);
const entryHtml = `
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>${meta.title}</title>
${minimalFavicon}
<style>
${inlineCss}
</style>
</head>
<body>
<div class='main'>
${entryNavHtml}
<div class='entry'>
<div class='entry__header'>
<h1>${meta.title}</h1>
<h2>${meta.timestamp}</h2>
${genTagsHtml(meta.tags)}
</div>
<div class='entry__content'>
${contentHtml}
</div>
</div>
<!-- Begin Mailchimp Signup Form -->
<link href="//cdn-images.mailchimp.com/embedcode/slim-10_7.css" rel="stylesheet" type="text/css">
<style type="text/css">
#mc_embed_signup{
background: var(--bg-color);
clear:left;
font:1.2em Helvetica, Arial, sans-serif;
color: var(--font-color);
border: 2px solid #ddd;
margin-top: 50px;
padding: 20px;
}
/* Add your own Mailchimp form style overrides in your site stylesheet or in this style block.
We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
</style>
<div id="mc_embed_signup">
<p>
Like what I have to say? Consider subscribing to my newsletter.
I don't send very many updates.
</p>
<form action="https://emauth.us20.list-manage.com/subscribe/post?u=0c3ecbf40cf99a4c67c9e9659&id=902af5df88" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
<div id="mc_embed_signup_scroll">
<label for="mce-EMAIL">Subscribe</label>
<input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" placeholder="email address" required>
<!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_0c3ecbf40cf99a4c67c9e9659_902af5df88" tabindex="-1" value=""></div>
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
</div>
</form>
</div>
<!--End mc_embed_signup-->
</div>
${analyticsHtml}
</body>
</html>
`;
const indexHtmlUrl = entryUrl + 'index.html';
await fetch(indexHtmlUrl + '?overwrite=true&access_token=' + token, {
method: 'PUT',
body: entryHtml,
});
if (meta.visibility !== 'public') {
const aclUrl = entryUrl + 'gemdrive/acl.json';
await fetch(aclUrl + '?overwrite=true&access_token=' + token, {
method: 'PUT',
body: JSON.stringify([
{
idType: 'email',
id: '[email protected]',
perm: 'own',
},
], null, 2),
});
}
return { meta, content: contentHtml, entryUrl };
}
export async function publishFeedPage(src, token, inlineCss) {
const allTags = {};
const allEntries = [];
for await (const entryUrl of entryIterator(src, token)) {
const entry = await publishEntry(entryUrl, token, inlineCss);
if (entry) {
for (const tag of entry.meta.tags) {
if (!allTags[tag]) {
allTags[tag] = [];
}
const entryPath = entryUrl.slice((src).length);
const entryId = entryPathToId(entryPath);
allTags[tag].push(entryId);
}
allEntries.push(entry);
}
}
const dbUrl = `${src}db.json?overwrite=true&access_token=${token}`;
const db = await fetch(dbUrl)
.then(r => r.json());
db.tags = allTags;
await fetch(dbUrl, {
method: 'PUT',
body: JSON.stringify(db, null, 2),
});
// sort in reverse-chronological order (the key is the entry id, which
// increases monotonically).
const sortedEntries = allEntries.slice()
.sort((a, b) => naturalSorter.compare(a.meta.timestamp, b.meta.timestamp))
.reverse();
let entryListHtml = '';
for (const entry of sortedEntries) {
if (entry.meta.visibility !== 'public') {
continue;
}
const content = entry.content.length < 1024 ? entry.content : '';
const entryPath = entry.entryUrl.slice((src).length);
const urlName = entry.meta.urlName ? entry.meta.urlName : entry.meta.title
.toLowerCase().replace(/ /g, '-').replace("'", '');
const entryHtml = `
<div class='entry-list__entry'>
<div class='list-entry'>
<div class='list-entry__controls'>
<span>${entry.meta.timestamp}</span>
<a href='../${entryPath}#${urlName}' target='_blank' id='open-in-tab-btn' class='list-entry__control-btn'>Open in Tab</a>
<a href='../${entryPath}#${urlName}' id='fullscreen-btn' class='list-entry__control-btn'>Fullscreen</a>
</div>
<div class='entry'>
<div class='entry__header'>
<h1>${entry.meta.title}</h1>
</div>
${genTagsHtml(entry.meta.tags)}
<div class='entry__content'>
${content}
</div>
</div>
</div>
</div>
`;
entryListHtml += entryHtml;
}
const feedHtml = `
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Anders' little corner of the internet</title>
${minimalFavicon}
<style>
${inlineCss}
</style>
</head>
<body>
<div class='main'>
${navHtml}
<div class='entry-list'>
${entryListHtml}
</div>
</div>
${analyticsHtml}
</body>
</html>
`;
await fetch(src + 'feed/?recursive=true&access_token=' + token, {
method: 'PUT',
});
await fetch(src + 'feed/index.html?overwrite=true&access_token=' + token, {
method: 'PUT',
body: feedHtml,
});
}
function genTagsHtml(tags) {
let tagsHtml = '';
for (const tag of tags) {
tagsHtml += `
<div class='tag-list__tag'>
${tag}
</div>
`;
}
return `
<div class='tag-list'>
${tagsHtml}
</div>
`;
}