git ssb

9+

cel / ssb-viewer



Tree: 880f0401de03d40ddee364368a08983f02495fd6

Files: 880f0401de03d40ddee364368a08983f02495fd6 / render.js

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

Built with git-ssb-web