Files: ccc712bad78a439c6287c829554052b9cd943a4b / render.js
12594 bytesRaw
1 | var path = require('path'); |
2 | var pull = require("pull-stream"); |
3 | var marked = require("ssb-marked"); |
4 | var htime = require("human-time"); |
5 | var emojis = require("emoji-named-characters"); |
6 | var cat = require("pull-cat"); |
7 | var h = require('hyperscript'); |
8 | |
9 | var emojiDir = path.join(require.resolve("emoji-named-characters"), "../pngs"); |
10 | |
11 | exports.wrapPage = wrapPage; |
12 | exports.MdRenderer = MdRenderer; |
13 | exports.renderEmoji = renderEmoji; |
14 | exports.formatMsgs = formatMsgs; |
15 | exports.renderThread = renderThread; |
16 | exports.renderAbout = renderAbout; |
17 | exports.renderShowAll = renderShowAll; |
18 | |
19 | function MdRenderer(opts) { |
20 | marked.Renderer.call(this, {}); |
21 | this.opts = opts; |
22 | } |
23 | |
24 | MdRenderer.prototype = new marked.Renderer(); |
25 | |
26 | MdRenderer.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 | |
43 | MdRenderer.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 | |
57 | function 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 | |
73 | function escape(str) { |
74 | return String(str) |
75 | .replace(/&/g, "&") |
76 | .replace(/</g, "<") |
77 | .replace(/>/g, ">") |
78 | .replace(/"/g, """); |
79 | } |
80 | |
81 | function 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 | |
94 | function wrap(before, after) { |
95 | return function(read) { |
96 | return cat([pull.once(before), read, pull.once(after)]); |
97 | }; |
98 | } |
99 | |
100 | function callToAction() { |
101 | return h('a.call-to-action', |
102 | { href: 'https://www.scuttlebutt.nz' }, |
103 | 'Join Scuttlebutt now').outerHTML; |
104 | } |
105 | |
106 | function 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 | |
113 | function 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 | |
134 | function 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 | |
142 | function 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 | |
156 | var 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 | |
271 | function wrapJSON() { |
272 | var first = true; |
273 | return pull(pull.map(JSON.stringify), join(","), wrap("[", "]")); |
274 | } |
275 | |
276 | function 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 | |
284 | function 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 | |
300 | function 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 | |
309 | function replace(re, rep) { |
310 | return pull.map(function(val) { |
311 | return String(val).replace(re, rep); |
312 | }); |
313 | } |
314 | |
315 | function docWrite(str) { |
316 | return "document.write(" + JSON.stringify(str) + ")\n"; |
317 | } |
318 | |
319 | function 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 | |
336 | function 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 | |
347 | function formatDate(date) { |
348 | return htime(date); |
349 | } |
350 | |
351 | function 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 | } |
399 | else if (c.type == "about") { |
400 | return [h('span.status', 'Changed something in about'), |
401 | renderDefault(c)]; |
402 | } |
403 | else if (c.type == "issue") { |
404 | return [h('span.status', |
405 | "Created a git issue" + |
406 | (c.repoName != undefined ? " in repo " + c.repoName : ""), |
407 | renderPost(opts, id, c))]; |
408 | } |
409 | else if (c.type == "git-repo") { |
410 | return h('span.status', |
411 | "Created a git repo " + c.name); |
412 | } |
413 | else if (c.type == "git-update") { |
414 | var s = h('span.status'); |
415 | s.innerHTML = "Did a git update " + |
416 | (c.repoName != undefined ? " in repo " + c.repoName : "") + |
417 | '<br>' + |
418 | (c.commits != undefined ? |
419 | c.commits.map(com => { return "-" +com.title; }).join('<br>') : ""); |
420 | return s; |
421 | } |
422 | else if (c.type == "ssb-dns") { |
423 | return [h('span.status', 'Updated DNS'), renderDefault(c)]; |
424 | } |
425 | else if (c.type == "pub") { |
426 | return h('span.status', 'Connected to the pub ' + c.address.host); |
427 | } |
428 | else if (c.type == "channel" && c.subscribed) |
429 | return h('span.status', |
430 | 'Subscribed to channel ', |
431 | h('a', |
432 | { href: base + 'channel/' + c.channel }, |
433 | '#' + c.channel)); |
434 | else if (c.type == "channel" && !c.subscribed) |
435 | return h('span.status', |
436 | 'Unsubscribed from channel ', |
437 | h('a', |
438 | { href: base + 'channel/' + c.channel }, |
439 | '#' + c.channel)) |
440 | else return renderDefault(c); |
441 | } |
442 | |
443 | function renderPost(opts, id, c) { |
444 | opts.mentions = {}; |
445 | if (Array.isArray(c.mentions)) { |
446 | c.mentions.forEach(function (link) { |
447 | if (link && link.name && link.link) |
448 | opts.mentions[link.name] = link.link; |
449 | }); |
450 | } |
451 | var s = h('section'); |
452 | var content = ''; |
453 | if (c.root && c.root != id) |
454 | content += 'Re: ' + h('a', |
455 | { href: '/' + encodeURIComponent(c.root) }, |
456 | c.root.substring(0, 10)).outerHTML + '<br>'; |
457 | s.innerHTML = content + marked(String(c.text), opts.marked); |
458 | return s; |
459 | } |
460 | |
461 | function renderDefault(c) { |
462 | return h('pre', JSON.stringify(c, 0, 2)); |
463 | } |
464 | |
465 | function renderShowAll(showAll, url) { |
466 | if (showAll) |
467 | return ''; |
468 | else |
469 | return '<br>' + h('a', { href : url + '?showAll' }, 'Show whole feed').outerHTML; |
470 | } |
471 |
Built with git-ssb-web