git ssb

9+

cel / ssb-viewer



Tree: 07ff4c602e229a458d6210ac0c19dc2b699b740f

Files: 07ff4c602e229a458d6210ac0c19dc2b699b740f / render.js

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

Built with git-ssb-web