Files: 618a38ee2f287072c80c2cddc56eba4536ffe253 / render.js
2692 bytesRaw
1 | var h = require('hyperscript') |
2 | var human = require('human-time') |
3 | var ref = require('ssb-ref') |
4 | var avatar = require('./avatar') |
5 | |
6 | var markdown = require('ssb-markdown') |
7 | var config = require('./config')() |
8 | |
9 | function rawJSON (obj) { |
10 | return JSON.stringify(obj, null, 2) |
11 | .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/) |
12 | .map(function (e) { |
13 | if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) { |
14 | return h('a', {href: '#' + e}, e) |
15 | } |
16 | return e |
17 | }) |
18 | } |
19 | |
20 | function header (msg) { |
21 | return h('div.header', |
22 | h('span.avatar', |
23 | h('a', {href: '#' + msg.value.author}, |
24 | h('span.avatar--small', avatar.image(msg.value.author)), |
25 | avatar.name(msg.value.author) |
26 | ) |
27 | ), |
28 | h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp)))), |
29 | ) |
30 | } |
31 | |
32 | function messageLink (msglink) { |
33 | var link = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 8) + '...')) |
34 | return link |
35 | } |
36 | |
37 | module.exports = function (msg) { |
38 | var message = h('div.message') |
39 | if (msg.value.content.type == 'post') { |
40 | message.appendChild(header(msg)) |
41 | if (msg.value.content.root) { |
42 | message.appendChild(h('span', 're: ', messageLink(msg.value.content.root))) |
43 | } |
44 | message.appendChild(h('div.message__body', |
45 | {innerHTML: markdown.block(msg.value.content.text, {toUrl: function (url, image) { |
46 | if(url[0] == '%' || url[0] == '@') return '#' + url |
47 | if(!image) return url |
48 | if(url[0] !== '&') return url |
49 | return config.blobsUrl + url |
50 | }})} |
51 | ) |
52 | ) |
53 | return message |
54 | } else if (msg.value.content.type == 'vote') { |
55 | message.appendChild(header(msg)) |
56 | var embed = msg.value.content.vote.link |
57 | |
58 | var embedded = h('div.embedded') |
59 | sbot.get(embed, function (err, msg) { |
60 | if (err) {console.log('could not find message locally, try ooo?') } |
61 | msg.value = msg |
62 | msg.key = embed |
63 | if (msg.value.content.text) { |
64 | //message.appendChild(h('img.emoji', {src: config.emojiUrl + 'star.png'})) |
65 | message.appendChild(embedded) |
66 | embedded.appendChild(header(msg)) |
67 | embedded.appendChild(h('div.message__body', |
68 | {innerHTML: markdown.block(msg.value.content.text.substring(0, 256) + '... ', {toUrl: function (url, image) { |
69 | if(url[0] == '@') return '#' + url |
70 | if(url[0] == '%') return '#' + url |
71 | if(!image) return url |
72 | if(url[0] !== '&') return url |
73 | return config.blobsUrl + url |
74 | }})}, messageLink(msg.key) |
75 | )) |
76 | } |
77 | }) |
78 | return message |
79 | } else { |
80 | //message.appendChild(h('pre', rawJSON(msg.value.content))) |
81 | //return message |
82 | return |
83 | } |
84 | } |
85 |
Built with git-ssb-web