git ssb

9+

cel / ssb-viewer



Tree: 1143975d12695c4db419ff9b75aa4d7177a7922b

Files: 1143975d12695c4db419ff9b75aa4d7177a7922b / render.js

13858 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[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 mentions = opts.mentions;
62 var url = mentions[emoji]
63 ? opts.blob_base + encodeURIComponent(mentions[emoji])
64 : emoji in emojis && opts.emoji_base + escape(emoji) + '.png';
65 return url
66 ? h('img.ssb-emoji',
67 { src: url,
68 alt: ':' + escape(emoji) + ':',
69 title: ':' + escape(emoji) + ':',
70 height: 16, width: 16
71 }).outerHTML
72 : ":" + emoji + ":";
73}
74
75function escape(str) {
76 return String(str)
77 .replace(/&/g, "&")
78 .replace(/</g, "&lt;")
79 .replace(/>/g, "&gt;")
80 .replace(/"/g, "&quot;");
81}
82
83function formatMsgs(id, ext, opts) {
84 switch (ext || "html") {
85 case "html":
86 return pull(renderThread(opts, id, ''), wrapPage(id));
87 case "js":
88 return pull(renderThread(opts), wrapJSEmbed(opts));
89 case "json":
90 return wrapJSON();
91 case "rss":
92 return pull(renderRssItem(opts), wrapRss(id, opts));
93 default:
94 return null;
95 }
96}
97
98function wrap(before, after) {
99 return function(read) {
100 return cat([pull.once(before), read, pull.once(after)]);
101 };
102}
103
104function callToAction() {
105 return h('a.call-to-action',
106 { href: 'https://www.scuttlebutt.nz' },
107 'Join Scuttlebutt now').outerHTML;
108}
109
110function toolTipTop() {
111 return h('span.top-tip',
112 'You are reading content from ',
113 h('a', { href: 'https://www.scuttlebutt.nz' },
114 'Scuttlebutt')).outerHTML;
115}
116
117function renderAbout(opts, about, showAllHTML = "") {
118 var figCaption = h('figcaption');
119 figCaption.innerHTML = 'Feed of ' + about.name + '<br>' +
120 (about.description != undefined ?
121 marked(about.description, opts.marked) : '');
122 return pull(
123 pull.map(renderMsg.bind(this, opts, '')),
124 wrap(toolTipTop() + '<main>' +
125 h('article',
126 h('header',
127 h('figure',
128 h('img',
129 { src: opts.img_base + about.image,
130 style: 'max-height: 200px; max-width: 200px;'
131 }),
132 figCaption)
133 )).outerHTML,
134 showAllHTML + '</main>' + callToAction())
135 );
136}
137
138function renderThread(opts, id, showAllHTML = "") {
139 return pull(
140 pull.map(renderMsg.bind(this, opts, id)),
141 wrap(toolTipTop() + '<main>',
142 showAllHTML + '</main>' + callToAction())
143 );
144}
145
146function renderRssItem(opts) {
147 return pull(
148 pull.map(renderRss.bind(this, opts))
149 );
150}
151
152function wrapPage(id) {
153 return wrap(
154 "<!doctype html><html><head>" +
155 "<meta charset=utf-8>" +
156 "<title>" +
157 id + " | ssb-viewer" +
158 "</title>" +
159 '<meta name=viewport content="width=device-width,initial-scale=1">' +
160 styles +
161 "</head><body>",
162 "</body></html>"
163 );
164}
165
166function wrapRss(id, opts) {
167 return wrap(
168 '<?xml version="1.0" encoding="UTF-8" ?>' +
169 '<rss version="2.0">' +
170 '<channel>' +
171 '<title>' + id + ' | ssb-viewer</title>',
172
173 '</channel>'+
174 '</rss>'
175 );
176}
177
178var styles = `
179 <style>
180 html { background-color: #f1f3f5; }
181 body {
182 color: #212529;
183 font-family: "Helvetica Neue", "Calibri Light", Roboto, sans-serif;
184 -webkit-font-smoothing: antialiased;
185 -moz-osx-font-smoothing: grayscale;
186 letter-spacing: 0.02em;
187 padding-top: 30px;
188 padding-bottom: 50px;
189 }
190 a { color: #364fc7; }
191
192 .top-tip, .top-tip a {
193 color: #868e96;
194 }
195 .top-tip {
196 text-align: center;
197 display: block;
198 margin-bottom: 10px;
199 font-size: 14px;
200 }
201 main { margin: 0 auto; max-width: 40rem; }
202 main article:first-child { border-radius: 3px 3px 0 0; }
203 main article:last-child { border-radius: 0 0 3px 3px; }
204 article {
205 background-color: white;
206 padding: 20px;
207 box-shadow: 0 1px 3px #949494;
208 position: relative;
209 }
210 .top-right { position: absolute; top: 20px; right: 20px; }
211 article > header { margin-bottom: 20px; }
212 article > header > figure {
213 margin: 0; display: flex;
214 }
215 article > header > figure > img {
216 border-radius: 2px; margin-right: 10px;
217 }
218 article > header > figure > figcaption {
219 display: flex; flex-direction: column; justify-content: space-around;
220 }
221 .ssb-avatar-name { font-size: 1.2em; font-weight: bold; }
222 time a { color: #868e96; }
223 .ssb-avatar-name, time a {
224 text-decoration: none;
225 }
226 .ssb-avatar-name:hover, time:hover a {
227 text-decoration: underline;
228 }
229 section p { line-height: 1.45em; }
230 section p img {
231 max-width: 100%;
232 max-height: 50vh;
233 margin: 0 auto;
234 }
235 .status {
236 font-style: italic;
237 }
238
239 code {
240 display: inline;
241 padding: 2px 5px;
242 font-weight: 600;
243 background-color: #e9ecef;
244 border-radius: 3px;
245 color: #495057;
246 }
247 blockquote {
248 padding-left: 1.2em;
249 margin: 0;
250 color: #868e96;
251 border-left: 5px solid #ced4da;
252 }
253 pre {
254 background-color: #212529;
255 color: #ced4da;
256 font-weight: bold;
257 padding: 5px;
258 border-radius: 3px;
259 position: relative;
260 }
261 pre::before {
262 content: "METADATA";
263 position: absolute;
264 top: -7px;
265 left: 0px;
266 background-color: #212529;
267 padding: 2px 4px 0;
268 border-radius: 2px;
269 font-family: "Helvetica Neue", "Calibri Light", Roboto, sans-serif;
270 font-size: 9px;
271 }
272 .call-to-action {
273 display: block;
274 margin: 0 auto;
275 width: 13em;
276 text-align: center;
277 text-decoration: none;
278 margin-top: 20px;
279 margin-bottom: 60px;
280 background-color: #5c7cfa;
281 padding: 15px 0;
282 color: #edf2ff;
283 border-radius: 3px;
284 border-bottom: 3px solid #3b5bdb;
285 }
286 .call-to-action:hover {
287 background-color: #748ffc;
288 border-bottom: 3px solid #4c6ef5;
289 }
290 </style>
291`;
292
293function wrapJSON() {
294 var first = true;
295 return pull(pull.map(JSON.stringify), join(","), wrap("[", "]"));
296}
297
298function wrapJSEmbed(opts) {
299 return pull(
300 wrap('<link rel=stylesheet href="' + opts.base + 'static/base.css">', ""),
301 pull.map(docWrite),
302 opts.base_token && rewriteBase(new RegExp(opts.base_token, "g"))
303 );
304}
305
306function rewriteBase(token) {
307 // detect the origin of the script and rewrite the js/html to use it
308 return pull(
309 replace(token, '" + SSB_VIEWER_ORIGIN + "/'),
310 wrap(
311 "var SSB_VIEWER_ORIGIN = (function () {" +
312 'var scripts = document.getElementsByTagName("script")\n' +
313 "var script = scripts[scripts.length-1]\n" +
314 "if (!script) return location.origin\n" +
315 'return script.src.replace(/\\/%.*$/, "")\n' +
316 "}())\n",
317 ""
318 )
319 );
320}
321
322function join(delim) {
323 var first = true;
324 return pull.map(function(val) {
325 if (!first) return delim + String(val);
326 first = false;
327 return val;
328 });
329}
330
331function replace(re, rep) {
332 return pull.map(function(val) {
333 return String(val).replace(re, rep);
334 });
335}
336
337function docWrite(str) {
338 return "document.write(" + JSON.stringify(str) + ")\n";
339}
340
341function renderMsg(opts, id, msg) {
342 var c = msg.value.content || {};
343 var name = encodeURIComponent(msg.key);
344 return h('article#' + name,
345 h('header',
346 h('figure',
347 h('img', { alt: '',
348 src: opts.img_base + msg.author.image,
349 height: 50, width: 50 }),
350 h('figcaption',
351 h('a.ssb-avatar-name',
352 { href: opts.base + escape(msg.value.author) },
353 msg.author.name),
354 msgTimestamp(msg, opts.base + name)))),
355 render(opts, id, c)).outerHTML;
356}
357
358function renderRss(opts, msg) {
359 var c = msg.value.content || {};
360 var name = encodeURIComponent(msg.key);
361
362 let content = h('div', render(opts, c)).innerHTML;
363
364 if (!content) {
365 return null;
366 }
367
368 return (
369 '<item>' +
370 '<title>' + escape(c.type || 'private') + '</title>' +
371 '<author>' + escape(msg.author.name) + '</author>' +
372 '<description><![CDATA[' + content + ']]></description>' +
373 '<link>' + opts.base + escape(name) + '</link>' +
374 '<pubDate>' + new Date(msg.value.timestamp).toUTCString() + '</pubDate>' +
375 '<guid>' + msg.key + '</guid>' +
376 '</item>'
377 );
378}
379
380function msgTimestamp(msg, link) {
381 var date = new Date(msg.value.timestamp);
382 var isoStr = date.toISOString();
383 return h('time.ssb-timestamp',
384 { datetime: isoStr },
385 h('a',
386 { href: link,
387 title: isoStr },
388 formatDate(date)));
389}
390
391function formatDate(date) {
392 return htime(date);
393}
394
395function render(opts, id, c) {
396 var base = opts.base;
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 == "channel" && c.subscribed)
477 return h('span.status',
478 'Subscribed to channel ',
479 h('a',
480 { href: base + 'channel/' + c.channel },
481 '#' + c.channel));
482 else if (c.type == "channel" && !c.subscribed)
483 return h('span.status',
484 'Unsubscribed from channel ',
485 h('a',
486 { href: base + 'channel/' + c.channel },
487 '#' + c.channel))
488 else return renderDefault(c);
489}
490
491function renderPost(opts, id, c) {
492 opts.mentions = {};
493 if (Array.isArray(c.mentions)) {
494 c.mentions.forEach(function (link) {
495 if (link && link.name && link.link)
496 opts.mentions[link.name] = link.link;
497 });
498 }
499 var s = h('section');
500 var content = '';
501 if (c.root && c.root != id)
502 content += 'Re: ' + h('a',
503 { href: '/' + encodeURIComponent(c.root) },
504 c.root.substring(0, 10)).outerHTML + '<br>';
505 s.innerHTML = content + marked(String(c.text), opts.marked);
506 return s;
507}
508
509function renderDefault(c) {
510 return h('pre', JSON.stringify(c, 0, 2));
511}
512
513function renderShowAll(showAll, url) {
514 if (showAll)
515 return '';
516 else
517 return '<br>' + h('a', { href : url + '?showAll' }, 'Show whole feed').outerHTML;
518}
519

Built with git-ssb-web