git ssb

9+

cel / ssb-viewer



Tree: ff87105bd1d53bd820af183f22245f91f965e9e2

Files: ff87105bd1d53bd820af183f22245f91f965e9e2 / render.js

14354 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 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; justify-content: space-around;
225 }
226 .ssb-avatar-name { font-size: 1.2em; font-weight: bold; }
227 time a { color: #868e96; }
228 .ssb-avatar-name, time a {
229 text-decoration: none;
230 }
231 .ssb-avatar-name:hover, time:hover a {
232 text-decoration: underline;
233 }
234 section p { line-height: 1.45em; }
235 section p img {
236 max-width: 100%;
237 max-height: 50vh;
238 margin: 0 auto;
239 }
240 .status {
241 font-style: italic;
242 }
243
244 code {
245 display: inline;
246 padding: 2px 5px;
247 font-weight: 600;
248 background-color: #e9ecef;
249 border-radius: 3px;
250 color: #495057;
251 }
252 blockquote {
253 padding-left: 1.2em;
254 margin: 0;
255 color: #868e96;
256 border-left: 5px solid #ced4da;
257 }
258 pre {
259 background-color: #212529;
260 color: #ced4da;
261 font-weight: bold;
262 padding: 5px;
263 border-radius: 3px;
264 position: relative;
265 }
266 pre::before {
267 content: "METADATA";
268 position: absolute;
269 top: -7px;
270 left: 0px;
271 background-color: #212529;
272 padding: 2px 4px 0;
273 border-radius: 2px;
274 font-family: "Helvetica Neue", "Calibri Light", Roboto, sans-serif;
275 font-size: 9px;
276 }
277 .call-to-action {
278 display: block;
279 margin: 0 auto;
280 width: 13em;
281 text-align: center;
282 text-decoration: none;
283 margin-top: 20px;
284 margin-bottom: 60px;
285 background-color: #5c7cfa;
286 padding: 15px 0;
287 color: #edf2ff;
288 border-radius: 3px;
289 border-bottom: 3px solid #3b5bdb;
290 }
291 .call-to-action:hover {
292 background-color: #748ffc;
293 border-bottom: 3px solid #4c6ef5;
294 }
295 </style>
296`;
297
298function wrapJSON() {
299 var first = true;
300 return pull(pull.map(JSON.stringify), join(","), wrap("[", "]"));
301}
302
303function wrapJSEmbed(opts) {
304 return pull(
305 wrap('<link rel=stylesheet href="' + opts.base + 'static/base.css">', ""),
306 pull.map(docWrite),
307 opts.base_token && rewriteBase(new RegExp(opts.base_token, "g"))
308 );
309}
310
311function rewriteBase(token) {
312 // detect the origin of the script and rewrite the js/html to use it
313 return pull(
314 replace(token, '" + SSB_VIEWER_ORIGIN + "/'),
315 wrap(
316 "var SSB_VIEWER_ORIGIN = (function () {" +
317 'var scripts = document.getElementsByTagName("script")\n' +
318 "var script = scripts[scripts.length-1]\n" +
319 "if (!script) return location.origin\n" +
320 'return script.src.replace(/\\/%.*$/, "")\n' +
321 "}())\n",
322 ""
323 )
324 );
325}
326
327function join(delim) {
328 var first = true;
329 return pull.map(function(val) {
330 if (!first) return delim + String(val);
331 first = false;
332 return val;
333 });
334}
335
336function replace(re, rep) {
337 return pull.map(function(val) {
338 return String(val).replace(re, rep);
339 });
340}
341
342function docWrite(str) {
343 return "document.write(" + JSON.stringify(str) + ")\n";
344}
345
346function renderMsg(opts, id, msg) {
347 if (opts.renderPrivate == false && typeof(msg.value.content) == 'string') return ''
348 if (msg.author.publicWebHosting == 'false') return h('article', 'User has chosen not to be hosted publicly').outerHTML;
349 var c = msg.value.content || {};
350 var name = encodeURIComponent(msg.key);
351 return h('article#' + name,
352 h('header',
353 h('figure',
354 h('img', { alt: '',
355 src: opts.img_base + msg.author.image,
356 height: 50, width: 50 }),
357 h('figcaption',
358 h('a.ssb-avatar-name',
359 { href: opts.base + escape(msg.value.author) },
360 msg.author.name),
361 msgTimestamp(msg, opts.base + name)))),
362 render(opts, id, c)).outerHTML;
363}
364
365function renderRss(opts, msg) {
366 var c = msg.value.content || {};
367 var name = encodeURIComponent(msg.key);
368
369 let content = h('div', render(opts, c)).innerHTML;
370
371 if (!content) {
372 return null;
373 }
374
375 return (
376 '<item>' +
377 '<title>' + escape(c.type || 'private') + '</title>' +
378 '<author>' + escape(msg.author.name) + '</author>' +
379 '<description><![CDATA[' + content + ']]></description>' +
380 '<link>' + opts.base + escape(name) + '</link>' +
381 '<pubDate>' + new Date(msg.value.timestamp).toUTCString() + '</pubDate>' +
382 '<guid>' + msg.key + '</guid>' +
383 '</item>'
384 );
385}
386
387function msgTimestamp(msg, link) {
388 var date = new Date(msg.value.timestamp);
389 var isoStr = date.toISOString();
390 return h('time.ssb-timestamp',
391 { datetime: isoStr },
392 h('a',
393 { href: link,
394 title: isoStr },
395 formatDate(date)));
396}
397
398function formatDate(date) {
399 return htime(date);
400}
401
402function render(opts, id, c) {
403 var base = opts.base;
404 if (!c) return
405 if (c.type === "post") {
406 var channel = c.channel
407 ? h('div.top-right',
408 h('a',
409 { href: base + 'channel/' + c.channel },
410 '#' + c.channel))
411 : "";
412 return [channel, renderPost(opts, id, c)];
413 } else if (c.type == "vote" && c.vote.expression == "Dig") {
414 var channel = c.channel
415 ? [' in ',
416 h('a',
417 { href: base + 'channel/' + c.channel },
418 '#' + c.channel)]
419 : "";
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 ['Liked ',
425 h('a', { href: base + c.vote.link }, linkedText),
426 channel]);
427 } else if (c.type == "vote") {
428 var linkedText = "this";
429 if (typeof c.vote.linkedText != "undefined")
430 linkedText = c.vote.linkedText.substring(0, 75);
431 return h('span.status',
432 ['Voted ',
433 h('a', { href: base + c.vote.link }, linkedText)]);
434 } else if (c.type == "contact" && c.following) {
435 var name = c.contact;
436 if (typeof c.contactAbout != "undefined")
437 name = c.contactAbout.name;
438 return h('span.status',
439 ['Followed ',
440 h('a', { href: base + c.contact }, name)]);
441 } else if (c.type == "contact" && !c.following) {
442 var name = c.contact;
443 if (typeof c.contactAbout != "undefined")
444 name = c.contactAbout.name;
445 return h('span.status',
446 ['Unfollowed ',
447 h('a', { href: base + c.contact }, name)]);
448 } else if (typeof c == "string") {
449 return h('span.status', 'Wrote something private')
450 } else if (c.type == "chess_move") {
451 return h('span.status', 'Moved a chess piece')
452 } else if (c.type == "chess_invite") {
453 return h('span.status', 'Started a chess game')
454 }
455 else if (c.type == "about") {
456 return [h('span.status', 'Changed something in about'),
457 renderDefault(c)];
458 }
459 else if (c.type == "issue") {
460 return [h('span.status',
461 "Created a git issue" +
462 (c.repoName != undefined ? " in repo " + c.repoName : ""),
463 renderPost(opts, id, c))];
464 }
465 else if (c.type == "git-repo") {
466 return h('span.status',
467 "Created a git repo " + c.name);
468 }
469 else if (c.type == "git-update") {
470 var s = h('span.status');
471 s.innerHTML = "Did a git update " +
472 (c.repoName != undefined ? " in repo " + c.repoName : "") +
473 '<br>' +
474 (c.commits != undefined ?
475 c.commits.map(com => { return "-" +com.title; }).join('<br>') : "");
476 return s;
477 }
478 else if (c.type == "ssb-dns") {
479 return [h('span.status', 'Updated DNS'), renderDefault(c)];
480 }
481 else if (c.type == "pub") {
482 return h('span.status', 'Connected to the pub ' + c.address.host);
483 }
484 else if (c.type == "npm-packages") {
485 return [h('span.status', 'Pushed npm packages')];
486 }
487 else if (c.type == "channel" && c.subscribed)
488 return h('span.status',
489 'Subscribed to channel ',
490 h('a',
491 { href: base + 'channel/' + c.channel },
492 '#' + c.channel));
493 else if (c.type == "channel" && !c.subscribed)
494 return h('span.status',
495 'Unsubscribed from channel ',
496 h('a',
497 { href: base + 'channel/' + c.channel },
498 '#' + c.channel))
499 else return renderDefault(c);
500}
501
502function renderPost(opts, id, c) {
503 opts.mentions = {};
504 if (Array.isArray(c.mentions)) {
505 c.mentions.forEach(function (link) {
506 if (link && link.name && link.link)
507 opts.mentions[link.name] = link.link;
508 });
509 }
510 var s = h('section');
511 var content = '';
512 if (c.root && c.root != id)
513 content += 'Re: ' + h('a',
514 { href: '/' + encodeURIComponent(c.root) },
515 c.root.substring(0, 10)).outerHTML + '<br>';
516 s.innerHTML = content + marked(String(c.text), opts.marked);
517 return s;
518}
519
520function renderDefault(c) {
521 return h('pre', JSON.stringify(c, 0, 2));
522}
523
524function renderShowAll(showAll, url) {
525 if (showAll)
526 return '';
527 else
528 return '<br>' + h('a', { href : url + '?showAll' }, 'Show whole feed').outerHTML;
529}
530

Built with git-ssb-web