git ssb

9+

cel / ssb-viewer



Tree: 9f0354990d92384a0a2d222469e672f428402bfd

Files: 9f0354990d92384a0a2d222469e672f428402bfd / render.js

14048 bytesRaw
1var path = require('path');
2var pull = require("pull-stream");
3var marked = require("ssb-marked");
4var htime = require("human-time");
5var emojis = require("emoji-named-characters");
6var cat = require("pull-cat");
7var h = require('hyperscript');
8
9var emojiDir = path.join(require.resolve("emoji-named-characters"), "../pngs");
10
11exports.wrapPage = wrapPage;
12exports.MdRenderer = MdRenderer;
13exports.renderEmoji = renderEmoji;
14exports.formatMsgs = formatMsgs;
15exports.renderThread = renderThread;
16exports.renderAbout = renderAbout;
17exports.renderShowAll = renderShowAll;
18exports.renderRssItem = renderRssItem;
19exports.wrapRss = wrapRss;
20
21function MdRenderer(opts) {
22 marked.Renderer.call(this, {});
23 this.opts = opts;
24}
25
26MdRenderer.prototype = new marked.Renderer();
27
28MdRenderer.prototype.urltransform = function(href) {
29 if (!href) return false;
30 switch (href[0]) {
31 case "#":
32 return this.opts.base + "channel/" + href.slice(1);
33 case "%":
34 return this.opts.msg_base + encodeURIComponent(href);
35 case "@":
36 href = (this.opts.mentions && this.opts.mentions[href.substr(1)]) || href;
37 return this.opts.feed_base + encodeURIComponent(href);
38 case "&":
39 return this.opts.blob_base + encodeURIComponent(href);
40 }
41 if (href.indexOf("javascript:") === 0) return false;
42 return href;
43};
44
45MdRenderer.prototype.image = function(href, title, text) {
46 if (text.endsWith(".svg"))
47 return h('object',
48 { type: 'image/svg+xml',
49 data: href,
50 alt: text }).outerHTML;
51 else
52 return h('img',
53 { src: this.opts.img_base + href,
54 alt: text,
55 title: title
56 }).outerHTML;
57};
58
59function renderEmoji(emoji) {
60 var opts = this.renderer.opts;
61 var url = opts.mentions[emoji]
62 ? opts.blob_base + encodeURIComponent(opts.mentions[emoji])
63 : emoji in emojis && opts.emoji_base + escape(emoji) + '.png';
64 return url
65 ? h('img.ssb-emoji',
66 { src: url,
67 alt: ':' + escape(emoji) + ':',
68 title: ':' + escape(emoji) + ':',
69 height: 16, width: 16
70 }).outerHTML
71 : ":" + emoji + ":";
72}
73
74function escape(str) {
75 return String(str)
76 .replace(/&/g, "&")
77 .replace(/</g, "&lt;")
78 .replace(/>/g, "&gt;")
79 .replace(/"/g, "&quot;");
80}
81
82function formatMsgs(id, ext, opts) {
83 switch (ext || "html") {
84 case "html":
85 return pull(renderThread(opts, id, ''), wrapPage(id));
86 case "js":
87 return pull(renderThread(opts), wrapJSEmbed(opts));
88 case "json":
89 return wrapJSON();
90 case "rss":
91 return pull(renderRssItem(opts), wrapRss(id, opts));
92 default:
93 return null;
94 }
95}
96
97function wrap(before, after) {
98 return function(read) {
99 return cat([pull.once(before), read, pull.once(after)]);
100 };
101}
102
103function callToAction() {
104 return h('a.call-to-action',
105 { href: 'https://www.scuttlebutt.nz' },
106 'Join Scuttlebutt now').outerHTML;
107}
108
109function toolTipTop() {
110 return h('span.top-tip',
111 'You are reading content from ',
112 h('a', { href: 'https://www.scuttlebutt.nz' },
113 'Scuttlebutt')).outerHTML;
114}
115
116function renderAbout(opts, about, showAllHTML = "") {
117 opts.mentions = {};
118 var figCaption = h('figcaption');
119 figCaption.innerHTML = 'Feed of ' + about.name + '<br>' + marked(String(about.description), opts.marked);
120 return pull(
121 pull.map(renderMsg.bind(this, opts, '')),
122 wrap(toolTipTop() + '<main>' +
123 h('article',
124 h('header',
125 h('figure',
126 h('img',
127 { src: opts.img_base + about.image,
128 style: 'max-height: 200px; max-width: 200px;'
129 }),
130 figCaption)
131 )).outerHTML,
132 showAllHTML + '</main>' + callToAction())
133 );
134}
135
136function renderThread(opts, id, showAllHTML = "") {
137 return pull(
138 pull.map(renderMsg.bind(this, opts, id)),
139 wrap(toolTipTop() + '<main>',
140 showAllHTML + '</main>' + callToAction())
141 );
142}
143
144function renderRssItem(opts) {
145 return pull(
146 pull.map(renderRss.bind(this, opts))
147 );
148}
149
150function wrapPage(id) {
151 return wrap(
152 "<!doctype html><html><head>" +
153 "<meta charset=utf-8>" +
154 "<title>" +
155 id + " | ssb-viewer" +
156 "</title>" +
157 '<meta name=viewport content="width=device-width,initial-scale=1">' +
158 styles +
159 "</head><body>",
160 "</body></html>"
161 );
162}
163
164function wrapRss(id, opts) {
165 return wrap(
166 '<?xml version="1.0" encoding="UTF-8" ?>' +
167 '<rss version="2.0">' +
168 '<channel>' +
169 '<title>' + id + ' | ssb-viewer</title>',
170
171 '</channel>'+
172 '</rss>'
173 );
174}
175
176var styles = `
177 <style>
178 html { background-color: #f1f3f5; }
179 body {
180 color: #212529;
181 font-family: "Helvetica Neue", "Calibri Light", Roboto, sans-serif;
182 -webkit-font-smoothing: antialiased;
183 -moz-osx-font-smoothing: grayscale;
184 letter-spacing: 0.02em;
185 padding-top: 30px;
186 padding-bottom: 50px;
187 }
188 a { color: #364fc7; }
189
190 .top-tip, .top-tip a {
191 color: #868e96;
192 }
193 .top-tip {
194 text-align: center;
195 display: block;
196 margin-bottom: 10px;
197 font-size: 14px;
198 }
199 main { margin: 0 auto; max-width: 40rem; }
200 main article:first-child { border-radius: 3px 3px 0 0; }
201 main article:last-child { border-radius: 0 0 3px 3px; }
202 article {
203 background-color: white;
204 padding: 20px;
205 box-shadow: 0 1px 3px #949494;
206 position: relative;
207 }
208 .top-right { position: absolute; top: 20px; right: 20px; }
209 article > header { margin-bottom: 20px; }
210 article > header > figure {
211 margin: 0; display: flex;
212 }
213 article > header > figure > img {
214 border-radius: 2px; margin-right: 10px;
215 }
216 article > header > figure > figcaption {
217 display: flex; flex-direction: column; justify-content: space-around;
218 }
219 .ssb-avatar-name { font-size: 1.2em; font-weight: bold; }
220 time a { color: #868e96; }
221 .ssb-avatar-name, time a {
222 text-decoration: none;
223 }
224 .ssb-avatar-name:hover, time:hover a {
225 text-decoration: underline;
226 }
227 section p { line-height: 1.45em; }
228 section p img {
229 max-width: 100%;
230 max-height: 50vh;
231 margin: 0 auto;
232 }
233 .status {
234 font-style: italic;
235 }
236
237 code {
238 display: inline;
239 padding: 2px 5px;
240 font-weight: 600;
241 background-color: #e9ecef;
242 border-radius: 3px;
243 color: #495057;
244 }
245 blockquote {
246 padding-left: 1.2em;
247 margin: 0;
248 color: #868e96;
249 border-left: 5px solid #ced4da;
250 }
251 pre {
252 background-color: #212529;
253 color: #ced4da;
254 font-weight: bold;
255 padding: 5px;
256 border-radius: 3px;
257 position: relative;
258 }
259 pre::before {
260 content: "METADATA";
261 position: absolute;
262 top: -7px;
263 left: 0px;
264 background-color: #212529;
265 padding: 2px 4px 0;
266 border-radius: 2px;
267 font-family: "Helvetica Neue", "Calibri Light", Roboto, sans-serif;
268 font-size: 9px;
269 }
270 .call-to-action {
271 display: block;
272 margin: 0 auto;
273 width: 13em;
274 text-align: center;
275 text-decoration: none;
276 margin-top: 20px;
277 margin-bottom: 60px;
278 background-color: #5c7cfa;
279 padding: 15px 0;
280 color: #edf2ff;
281 border-radius: 3px;
282 border-bottom: 3px solid #3b5bdb;
283 }
284 .call-to-action:hover {
285 background-color: #748ffc;
286 border-bottom: 3px solid #4c6ef5;
287 }
288 </style>
289`;
290
291function wrapJSON() {
292 var first = true;
293 return pull(pull.map(JSON.stringify), join(","), wrap("[", "]"));
294}
295
296function wrapJSEmbed(opts) {
297 return pull(
298 wrap('<link rel=stylesheet href="' + opts.base + 'static/base.css">', ""),
299 pull.map(docWrite),
300 opts.base_token && rewriteBase(new RegExp(opts.base_token, "g"))
301 );
302}
303
304function rewriteBase(token) {
305 // detect the origin of the script and rewrite the js/html to use it
306 return pull(
307 replace(token, '" + SSB_VIEWER_ORIGIN + "/'),
308 wrap(
309 "var SSB_VIEWER_ORIGIN = (function () {" +
310 'var scripts = document.getElementsByTagName("script")\n' +
311 "var script = scripts[scripts.length-1]\n" +
312 "if (!script) return location.origin\n" +
313 'return script.src.replace(/\\/%.*$/, "")\n' +
314 "}())\n",
315 ""
316 )
317 );
318}
319
320function join(delim) {
321 var first = true;
322 return pull.map(function(val) {
323 if (!first) return delim + String(val);
324 first = false;
325 return val;
326 });
327}
328
329function replace(re, rep) {
330 return pull.map(function(val) {
331 return String(val).replace(re, rep);
332 });
333}
334
335function docWrite(str) {
336 return "document.write(" + JSON.stringify(str) + ")\n";
337}
338
339function renderMsg(opts, id, msg) {
340 if (opts.renderPrivate == false && typeof(msg.value.content) == 'string') return ''
341 var c = msg.value.content || {};
342 var name = encodeURIComponent(msg.key);
343 return h('article#' + name,
344 h('header',
345 h('figure',
346 h('img', { alt: '',
347 src: opts.img_base + msg.author.image,
348 height: 50, width: 50 }),
349 h('figcaption',
350 h('a.ssb-avatar-name',
351 { href: opts.base + escape(msg.value.author) },
352 msg.author.name),
353 msgTimestamp(msg, opts.base + name)))),
354 render(opts, id, c)).outerHTML;
355}
356
357function renderRss(opts, msg) {
358 var c = msg.value.content || {};
359 var name = encodeURIComponent(msg.key);
360
361 let content = h('div', render(opts, c)).innerHTML;
362
363 if (!content) {
364 return null;
365 }
366
367 return (
368 '<item>' +
369 '<title>' + escape(c.type || 'private') + '</title>' +
370 '<author>' + escape(msg.author.name) + '</author>' +
371 '<description><![CDATA[' + content + ']]></description>' +
372 '<link>' + opts.base + escape(name) + '</link>' +
373 '<pubDate>' + new Date(msg.value.timestamp).toUTCString() + '</pubDate>' +
374 '<guid>' + msg.key + '</guid>' +
375 '</item>'
376 );
377}
378
379function msgTimestamp(msg, link) {
380 var date = new Date(msg.value.timestamp);
381 var isoStr = date.toISOString();
382 return h('time.ssb-timestamp',
383 { datetime: isoStr },
384 h('a',
385 { href: link,
386 title: isoStr },
387 formatDate(date)));
388}
389
390function formatDate(date) {
391 return htime(date);
392}
393
394function render(opts, id, c) {
395 var base = opts.base;
396 if (!c) return
397 if (c.type === "post") {
398 var channel = c.channel
399 ? h('div.top-right',
400 h('a',
401 { href: base + 'channel/' + c.channel },
402 '#' + c.channel))
403 : "";
404 return [channel, renderPost(opts, id, c)];
405 } else if (c.type == "vote" && c.vote.expression == "Dig") {
406 var channel = c.channel
407 ? [' in ',
408 h('a',
409 { href: base + 'channel/' + c.channel },
410 '#' + c.channel)]
411 : "";
412 var linkedText = "this";
413 if (typeof c.vote.linkedText != "undefined")
414 linkedText = c.vote.linkedText.substring(0, 75);
415 return h('span.status',
416 ['Liked ',
417 h('a', { href: base + c.vote.link }, linkedText),
418 channel]);
419 } else if (c.type == "vote") {
420 var linkedText = "this";
421 if (typeof c.vote.linkedText != "undefined")
422 linkedText = c.vote.linkedText.substring(0, 75);
423 return h('span.status',
424 ['Voted ',
425 h('a', { href: base + c.vote.link }, linkedText)]);
426 } else if (c.type == "contact" && c.following) {
427 var name = c.contact;
428 if (typeof c.contactAbout != "undefined")
429 name = c.contactAbout.name;
430 return h('span.status',
431 ['Followed ',
432 h('a', { href: base + c.contact }, name)]);
433 } else if (c.type == "contact" && !c.following) {
434 var name = c.contact;
435 if (typeof c.contactAbout != "undefined")
436 name = c.contactAbout.name;
437 return h('span.status',
438 ['Unfollowed ',
439 h('a', { href: base + c.contact }, name)]);
440 } else if (typeof c == "string") {
441 return h('span.status', 'Wrote something private')
442 } else if (c.type == "chess_move") {
443 return h('span.status', 'Moved a chess piece')
444 } else if (c.type == "chess_invite") {
445 return h('span.status', 'Started a chess game')
446 }
447 else if (c.type == "about") {
448 return [h('span.status', 'Changed something in about'),
449 renderDefault(c)];
450 }
451 else if (c.type == "issue") {
452 return [h('span.status',
453 "Created a git issue" +
454 (c.repoName != undefined ? " in repo " + c.repoName : ""),
455 renderPost(opts, id, c))];
456 }
457 else if (c.type == "git-repo") {
458 return h('span.status',
459 "Created a git repo " + c.name);
460 }
461 else if (c.type == "git-update") {
462 var s = h('span.status');
463 s.innerHTML = "Did a git update " +
464 (c.repoName != undefined ? " in repo " + c.repoName : "") +
465 '<br>' +
466 (c.commits != undefined ?
467 c.commits.map(com => { return "-" +com.title; }).join('<br>') : "");
468 return s;
469 }
470 else if (c.type == "ssb-dns") {
471 return [h('span.status', 'Updated DNS'), renderDefault(c)];
472 }
473 else if (c.type == "pub") {
474 return h('span.status', 'Connected to the pub ' + c.address.host);
475 }
476 else if (c.type == "npm-packages") {
477 return [h('span.status', 'Pushed npm packages')];
478 }
479 else if (c.type == "channel" && c.subscribed)
480 return h('span.status',
481 'Subscribed to channel ',
482 h('a',
483 { href: base + 'channel/' + c.channel },
484 '#' + c.channel));
485 else if (c.type == "channel" && !c.subscribed)
486 return h('span.status',
487 'Unsubscribed from channel ',
488 h('a',
489 { href: base + 'channel/' + c.channel },
490 '#' + c.channel))
491 else return renderDefault(c);
492}
493
494function renderPost(opts, id, c) {
495 opts.mentions = {};
496 if (Array.isArray(c.mentions)) {
497 c.mentions.forEach(function (link) {
498 if (link && link.name && link.link)
499 opts.mentions[link.name] = link.link;
500 });
501 }
502 var s = h('section');
503 var content = '';
504 if (c.root && c.root != id)
505 content += 'Re: ' + h('a',
506 { href: '/' + encodeURIComponent(c.root) },
507 c.root.substring(0, 10)).outerHTML + '<br>';
508 s.innerHTML = content + marked(String(c.text), opts.marked);
509 return s;
510}
511
512function renderDefault(c) {
513 return h('pre', JSON.stringify(c, 0, 2));
514}
515
516function renderShowAll(showAll, url) {
517 if (showAll)
518 return '';
519 else
520 return '<br>' + h('a', { href : url + '?showAll' }, 'Show whole feed').outerHTML;
521}
522

Built with git-ssb-web