git ssb

0+

Daan Patchwork / ssb-viewer



forked from cel / ssb-viewer

Tree: 2c291337776846d8c4edf05609c74fac530254e6

Files: 2c291337776846d8c4edf05609c74fac530254e6 / render.js

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

Built with git-ssb-web