git ssb

0+

Daan Patchwork / ssb-viewer



forked from cel / ssb-viewer

Tree: c5cabd1c1390883a88e7ce23acc23075309f39bd

Files: c5cabd1c1390883a88e7ce23acc23075309f39bd / render.js

11862 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;
17
18function MdRenderer(opts) {
19 marked.Renderer.call(this, {});
20 this.opts = opts;
21}
22
23MdRenderer.prototype = new marked.Renderer();
24
25MdRenderer.prototype.urltransform = function(href) {
26 if (!href) return false;
27 switch (href[0]) {
28 case "#":
29 return this.opts.base + "channel/" + href.slice(1);
30 case "%":
31 return this.opts.msg_base + encodeURIComponent(href);
32 case "@":
33 href = this.opts.mentions[href.substr(1)] || href;
34 return this.opts.feed_base + encodeURIComponent(href);
35 case "&":
36 return this.opts.blob_base + encodeURIComponent(href);
37 }
38 if (href.indexOf("javascript:") === 0) return false;
39 return href;
40};
41
42MdRenderer.prototype.image = function(href, title, text) {
43 return h('img',
44 { src: this.opts.img_base + href,
45 alt: text,
46 title: title
47 }).outerHTML;
48};
49
50function renderEmoji(emoji) {
51 var opts = this.renderer.opts;
52 var mentions = opts.mentions;
53 var url = mentions[emoji]
54 ? opts.blob_base + encodeURIComponent(mentions[emoji])
55 : emoji in emojis && opts.emoji_base + escape(emoji) + '.png';
56 return url
57 ? h('img.ssb-emoji',
58 { src: url,
59 alt: ':' + escape(emoji) + ':',
60 title: ':' + escape(emoji) + ':',
61 height: 16, width: 16
62 }).outerHTML
63 : ":" + emoji + ":";
64}
65
66function escape(str) {
67 return String(str)
68 .replace(/&/g, "&")
69 .replace(/</g, "&lt;")
70 .replace(/>/g, "&gt;")
71 .replace(/"/g, "&quot;");
72}
73
74function formatMsgs(id, ext, opts) {
75 switch (ext || "html") {
76 case "html":
77 return pull(renderThread(opts), wrapPage(id));
78 case "js":
79 return pull(renderThread(opts), wrapJSEmbed(opts));
80 case "json":
81 return wrapJSON();
82 default:
83 return null;
84 }
85}
86
87function wrap(before, after) {
88 return function(read) {
89 return cat([pull.once(before), read, pull.once(after)]);
90 };
91}
92
93function callToAction() {
94 return h('a.call-to-action',
95 { href: 'https://www.scuttlebutt.nz' },
96 'Join Scuttlebutt now').outerHTML;
97}
98
99function toolTipTop() {
100 return h('span.top-tip',
101 'You are reading content from ',
102 h('a', { href: 'https://www.scuttlebutt.nz' },
103 'Scuttlebutt')).outerHTML;
104}
105
106function renderAbout(opts, about, showAllHTML = "") {
107 var figCaption = h('figcaption');
108 figCaption.innerHTML = 'Feed of ' + about.name + '<br>' +
109 (about.description != undefined ?
110 marked(about.description, opts.marked) : '');
111 return pull(
112 pull.map(renderMsg.bind(this, opts)),
113 wrap(toolTipTop() + '<main>' +
114 h('article',
115 h('header',
116 h('figure',
117 h('img',
118 { src: opts.img_base + about.image,
119 height: 200,
120 width: 200
121 }),
122 figCaption)
123 )).outerHTML,
124 showAllHTML + '</main>' + callToAction())
125 );
126}
127
128function renderThread(opts, showAllHTML = "") {
129 return pull(
130 pull.map(renderMsg.bind(this, opts)),
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, 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, 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, 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, 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, c))];
402 }
403 else if (c.type == "git-update") {
404 return h('span.status',
405 "Did a git update " +
406 (c.repoName != undefined ? " in repo " + c.repoName : "") +
407 '<br>' +
408 (c.commits != undefined ?
409 c.commits.map(com => { return "-" +com.title; }).join('<br>') : ""));
410 }
411 else if (c.type == "ssb-dns") {
412 return [h('span.status', 'Updated DNS'), renderDefault(c)];
413 }
414 else if (c.type == "pub") {
415 return h('span.status', 'Connected to the pub ' + c.address.host);
416 }
417 else if (c.type == "channel" && c.subscribed)
418 return h('span.status',
419 'Subscribed to channel ',
420 h('a',
421 { href: base + 'channel/' + c.channel },
422 '#' + c.channel));
423 else if (c.type == "channel" && !c.subscribed)
424 return h('span.status',
425 'Unsubscribed from channel ',
426 h('a',
427 { href: base + 'channel/' + c.channel },
428 '#' + c.channel))
429 else return renderDefault(c);
430}
431
432function renderPost(opts, c) {
433 opts.mentions = {};
434 if (Array.isArray(c.mentions)) {
435 c.mentions.forEach(function (link) {
436 if (link && link.name && link.link)
437 opts.mentions[link.name] = link.link;
438 });
439 }
440 var s = h('section');
441 s.innerHTML = marked(String(c.text), opts.marked);
442 return s;
443}
444
445function renderDefault(c) {
446 return h('pre', JSON.stringify(c, 0, 2));
447}
448

Built with git-ssb-web