git ssb

0+

Daan Patchwork / ssb-viewer



forked from cel / ssb-viewer

Tree: d6e3e6a468d96496d2bc2c9f4e092864723d88d2

Files: d6e3e6a468d96496d2bc2c9f4e092864723d88d2 / render.js

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

Built with git-ssb-web