git ssb

9+

cel / ssb-viewer



Tree: 2dcdef60a24ea6a888eee961f4c90e89cc87ca94

Files: 2dcdef60a24ea6a888eee961f4c90e89cc87ca94 / render.js

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

Built with git-ssb-web