git ssb

0+

Daan Patchwork / ssb-viewer



forked from cel / ssb-viewer

Tree: 69a32cc3435ccd2d5b83ef5b83832745abe29af5

Files: 69a32cc3435ccd2d5b83ef5b83832745abe29af5 / render.js

15288 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 && 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 if (about.publicWebHosting === false) {
118 return pull(
119 pull.map(renderMsg.bind(this, opts, '')),
120 wrap(toolTipTop() + '<main>', '</main>' + callToAction())
121 );
122 }
123
124 var figCaption = h('figcaption');
125 figCaption.innerHTML = 'Feed of ' + about.name + '<br>' + marked(String(about.description || ''), opts.marked);
126 return pull(
127 pull.map(renderMsg.bind(this, opts, '')),
128 wrap(toolTipTop() + '<main>' +
129 h('article',
130 h('header',
131 h('figure',
132 h('img',
133 { src: opts.img_base + about.image,
134 style: 'max-height: 200px; max-width: 200px;'
135 }),
136 figCaption)
137 )).outerHTML,
138 showAllHTML + '</main>' + callToAction())
139 );
140}
141
142function renderThread(opts, id, showAllHTML = "") {
143 return pull(
144 pull.map(renderMsg.bind(this, opts, id)),
145 wrap(toolTipTop() + '<main>',
146 showAllHTML + '</main>' + callToAction())
147 );
148}
149
150function renderRssItem(opts) {
151 return pull(
152 pull.map(renderRss.bind(this, opts))
153 );
154}
155
156function wrapPage(id) {
157 return wrap(
158 "<!doctype html><html><head>" +
159 "<meta charset=utf-8>" +
160 "<title>" +
161 id + " | ssb-viewer" +
162 "</title>" +
163 '<meta name=viewport content="width=device-width,initial-scale=1">' +
164 styles +
165 "</head><body>",
166 "</body></html>"
167 );
168}
169
170function wrapRss(id, opts) {
171 return wrap(
172 '<?xml version="1.0" encoding="UTF-8" ?>' +
173 '<rss version="2.0">' +
174 '<channel>' +
175 '<title>' + id + ' | ssb-viewer</title>',
176
177 '</channel>'+
178 '</rss>'
179 );
180}
181
182var styles = `
183 <style>
184 html { background-color: #f1f3f5; }
185 body {
186 color: #212529;
187 font-family: "Helvetica Neue", "Calibri Light", Roboto, sans-serif;
188 -webkit-font-smoothing: antialiased;
189 -moz-osx-font-smoothing: grayscale;
190 letter-spacing: 0.02em;
191 padding-top: 30px;
192 padding-bottom: 50px;
193 }
194 a { color: #364fc7; }
195
196 .top-tip, .top-tip a {
197 color: #868e96;
198 }
199 .top-tip {
200 text-align: center;
201 display: block;
202 margin-bottom: 10px;
203 font-size: 14px;
204 }
205 main { margin: 0 auto; max-width: 40rem; }
206 main article:first-child { border-radius: 3px 3px 0 0; }
207 main article:last-child { border-radius: 0 0 3px 3px; }
208 article {
209 background-color: white;
210 padding: 20px;
211 box-shadow: 0 1px 3px #949494;
212 position: relative;
213 }
214 .top-right { position: absolute; top: 20px; right: 20px; }
215 article > header { margin-bottom: 20px; }
216 article > header > figure {
217 margin: 0; display: flex;
218 }
219 article > header > figure > img {
220 border-radius: 2px; margin-right: 10px;
221 }
222 article > header > figure > figcaption {
223 display: flex; flex-direction: column;
224 }
225 article > section {
226 word-wrap: break-word;
227 }
228 .ssb-avatar-name { font-size: 1.2em; font-weight: bold; }
229 time a { color: #868e96; }
230 .ssb-avatar-name, time a {
231 text-decoration: none;
232 }
233 .ssb-avatar-name:hover, time:hover a {
234 text-decoration: underline;
235 }
236 section p { line-height: 1.45em; }
237 section p img {
238 max-width: 100%;
239 max-height: 50vh;
240 margin: 0 auto;
241 }
242 .status {
243 font-style: italic;
244 }
245
246 code {
247 display: inline;
248 padding: 2px 5px;
249 font-weight: 600;
250 background-color: #e9ecef;
251 border-radius: 3px;
252 color: #495057;
253 }
254 blockquote {
255 padding-left: 1.2em;
256 margin: 0;
257 color: #868e96;
258 border-left: 5px solid #ced4da;
259 }
260 pre {
261 background-color: #212529;
262 color: #ced4da;
263 font-weight: bold;
264 padding: 5px;
265 border-radius: 3px;
266 position: relative;
267 overflow: hidden;
268 text-overflow: ellipsis;
269 }
270 pre::before {
271 content: "METADATA";
272 position: absolute;
273 top: -3px;
274 right: 0px;
275 background-color: #212529;
276 padding: 2px 4px 0;
277 border-radius: 2px;
278 font-family: "Helvetica Neue", "Calibri Light", Roboto, sans-serif;
279 font-size: 9px;
280 }
281 .call-to-action {
282 display: block;
283 margin: 0 auto;
284 width: 13em;
285 text-align: center;
286 text-decoration: none;
287 margin-top: 20px;
288 margin-bottom: 60px;
289 background-color: #5c7cfa;
290 padding: 15px 0;
291 color: #edf2ff;
292 border-radius: 3px;
293 border-bottom: 3px solid #3b5bdb;
294 }
295 .call-to-action:hover {
296 background-color: #748ffc;
297 border-bottom: 3px solid #4c6ef5;
298 }
299 </style>
300`;
301
302function wrapJSON() {
303 var first = true;
304 return pull(pull.map(JSON.stringify), join(","), wrap("[", "]"));
305}
306
307function wrapJSEmbed(opts) {
308 return pull(
309 wrap('<link rel=stylesheet href="' + opts.base + 'static/base.css">', ""),
310 pull.map(docWrite),
311 opts.base_token && rewriteBase(new RegExp(opts.base_token, "g"))
312 );
313}
314
315function rewriteBase(token) {
316 // detect the origin of the script and rewrite the js/html to use it
317 return pull(
318 replace(token, '" + SSB_VIEWER_ORIGIN + "/'),
319 wrap(
320 "var SSB_VIEWER_ORIGIN = (function () {" +
321 'var scripts = document.getElementsByTagName("script")\n' +
322 "var script = scripts[scripts.length-1]\n" +
323 "if (!script) return location.origin\n" +
324 'return script.src.replace(/\\/%.*$/, "")\n' +
325 "}())\n",
326 ""
327 )
328 );
329}
330
331function join(delim) {
332 var first = true;
333 return pull.map(function(val) {
334 if (!first) return delim + String(val);
335 first = false;
336 return val;
337 });
338}
339
340function replace(re, rep) {
341 return pull.map(function(val) {
342 return String(val).replace(re, rep);
343 });
344}
345
346function docWrite(str) {
347 return "document.write(" + JSON.stringify(str) + ")\n";
348}
349
350function renderMsg(opts, id, msg) {
351 var c = msg.value.content || {};
352
353 if (opts.renderPrivate == false && typeof(msg.value.content) == 'string') return ''
354 if (opts.renderSubscribe == false && c.type == "channel" && c.subscribed != undefined) return ''
355 if (opts.renderVote == false && c.type == "vote") return ''
356 if (opts.renderChess == false && c.type.startsWith("chess")) return ''
357 if (opts.renderTalenet == false && c.type.startsWith("talenet")) return ''
358 if (opts.renderFollow == false && c.type == "contact") return ''
359 if (opts.renderAbout == false && c.type == "about") return ''
360 if (opts.renderPub == false && c.type == "pub") return ''
361 if (msg.author.publicWebHosting === false) return h('article', 'User has chosen not to be hosted publicly').outerHTML;
362
363 var name = encodeURIComponent(msg.key);
364 return h('article#' + name,
365 h('header',
366 h('figure',
367 h('img', { alt: '',
368 src: opts.img_base + msg.author.image,
369 height: 50, width: 50 }),
370 h('figcaption',
371 h('a.ssb-avatar-name',
372 { href: opts.base + escape(msg.value.author) },
373 msg.author.name),
374 msgTimestamp(msg, opts.base + name)))),
375 render(opts, id, c)).outerHTML;
376}
377
378function renderRss(opts, msg) {
379 var c = msg.value.content || {};
380 var name = encodeURIComponent(msg.key);
381
382 let content = h('div', render(opts, c)).innerHTML;
383
384 if (!content) {
385 return null;
386 }
387
388 return (
389 '<item>' +
390 '<title>' + escape(c.type || 'private') + '</title>' +
391 '<author>' + escape(msg.author.name) + '</author>' +
392 '<description><![CDATA[' + content + ']]></description>' +
393 '<link>' + opts.base + escape(name) + '</link>' +
394 '<pubDate>' + new Date(msg.value.timestamp).toUTCString() + '</pubDate>' +
395 '<guid>' + msg.key + '</guid>' +
396 '</item>'
397 );
398}
399
400function msgTimestamp(msg, link) {
401 var date = new Date(msg.value.timestamp);
402 var isoStr = date.toISOString();
403 return h('time.ssb-timestamp',
404 { datetime: isoStr },
405 h('a',
406 { href: link,
407 title: isoStr },
408 formatDate(date)));
409}
410
411function formatDate(date) {
412 return htime(date);
413}
414
415function render(opts, id, c) {
416 var base = opts.base;
417 if (!c) return
418 if (c.type === "post") {
419 var channel = c.channel
420 ? h('div.top-right',
421 h('a',
422 { href: base + 'channel/' + c.channel },
423 '#' + c.channel))
424 : "";
425 return [channel, renderPost(opts, id, c)];
426 } else if (c.type == "vote" && c.vote.expression == "Dig") {
427 var channel = c.channel
428 ? [' in ',
429 h('a',
430 { href: base + 'channel/' + c.channel },
431 '#' + c.channel)]
432 : "";
433 var linkedText = "this";
434 if (typeof c.vote.linkedText != "undefined")
435 linkedText = c.vote.linkedText.substring(0, 75);
436 return h('span.status',
437 ['Liked ',
438 h('a', { href: base + c.vote.link }, linkedText),
439 channel]);
440 } else if (c.type == "vote") {
441 var linkedText = "this";
442 if (typeof c.vote.linkedText != "undefined")
443 linkedText = c.vote.linkedText.substring(0, 75);
444 return h('span.status',
445 ['Voted ',
446 h('a', { href: base + c.vote.link }, linkedText)]);
447 } else if (c.type == "contact" && c.following) {
448 var name = c.contact;
449 if (typeof c.contactAbout != "undefined")
450 name = c.contactAbout.name;
451 return h('span.status',
452 ['Followed ',
453 h('a', { href: base + c.contact }, name)]);
454 } else if (c.type == "contact" && !c.following) {
455 var name = c.contact;
456 if (typeof c.contactAbout != "undefined")
457 name = c.contactAbout.name;
458 return h('span.status',
459 ['Unfollowed ',
460 h('a', { href: base + c.contact }, name)]);
461 } else if (typeof c == "string") {
462 return h('span.status', 'Wrote something private')
463 } else if (c.type == "chess_move") {
464 return h('span.status', 'Moved a chess piece')
465 } else if (c.type == "chess_invite") {
466 return h('span.status', 'Started a chess game')
467 }
468 else if (c.type == "about") {
469 return [h('span.status', 'Changed something in about'),
470 renderDefault(c)];
471 }
472 else if (c.type == "issue") {
473 return [h('span.status',
474 "Created a git issue" +
475 (c.repoName != undefined ? " in repo " + c.repoName : ""),
476 renderPost(opts, id, c))];
477 }
478 else if (c.type == "git-repo") {
479 return h('span.status',
480 "Created a git repo " + c.name);
481 }
482 else if (c.type == "git-update") {
483 var s = h('span.status');
484 s.innerHTML = "Did a git update " +
485 (c.repoName != undefined ? " in repo " + c.repoName : "") +
486 '<br>' +
487 (c.commits != undefined ?
488 c.commits.map(com => { return "-" +com.title; }).join('<br>') : "");
489 return s;
490 }
491 else if (c.type == "ssb-dns") {
492 return [h('span.status', 'Updated DNS'), renderDefault(c)];
493 }
494 else if (c.type == "pub") {
495 return h('span.status', 'Connected to the pub ' + c.address.host);
496 }
497 else if (c.type == "npm-packages") {
498 return [h('span.status', 'Pushed npm packages')];
499 }
500 else if (c.type == "channel" && c.subscribed)
501 return h('span.status',
502 'Subscribed to channel ',
503 h('a',
504 { href: base + 'channel/' + c.channel },
505 '#' + c.channel));
506 else if (c.type == "channel" && !c.subscribed)
507 return h('span.status',
508 'Unsubscribed from channel ',
509 h('a',
510 { href: base + 'channel/' + c.channel },
511 '#' + c.channel))
512 else if (c.type == "blog") {
513 //%RTXvyZ2fZWwTyWdlk0lYGk5sKw5Irj+Wk4QwxyOVG5g=.sha256
514 var channel = c.channel
515 ? h('div.top-right',
516 h('a',
517 { href: base + 'channel/' + c.channel },
518 '#' + c.channel))
519 : "";
520
521 var s = h('section');
522 s.innerHTML = marked(String(c.blogContent), opts.marked)
523
524 return [channel, h('h2', c.title), s];
525 }
526 else return renderDefault(c);
527}
528
529function renderPost(opts, id, c) {
530 opts.mentions = {};
531 if (Array.isArray(c.mentions)) {
532 c.mentions.forEach(function (link) {
533 if (link && link.name && link.link)
534 opts.mentions[link.name] = link.link;
535 });
536 }
537 var s = h('section');
538 var content = '';
539 if (c.root && c.root != id)
540 content += 'Re: ' + h('a',
541 { href: '/' + encodeURIComponent(c.root) },
542 c.root.substring(0, 10)).outerHTML + '<br>';
543 s.innerHTML = content + marked(String(c.text), opts.marked);
544 return s;
545}
546
547function renderDefault(c) {
548 return h('pre', JSON.stringify(c, 0, 2));
549}
550
551function renderShowAll(showAll, url) {
552 if (!showAll)
553 return '<br>' + h('a', { href : url + '?showAll' }, 'Show whole feed').outerHTML;
554}
555

Built with git-ssb-web