git ssb

9+

cel / ssb-viewer



Tree: 54c06b511566bde688d64e44d057778d19a45258

Files: 54c06b511566bde688d64e44d057778d19a45258 / render.js

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

Built with git-ssb-web