Files: b0af4a5b196f6a3c00d7b9e44537ccdf1248230e / tools.js
2030 bytesRaw
1 | var h = require('hyperscript') |
2 | var human = require('human-time') |
3 | var avatar = require('./avatar') |
4 | var ref = require('ssb-ref') |
5 | |
6 | var pull = require('pull-stream') |
7 | |
8 | var sbot = require('./scuttlebot') |
9 | |
10 | var config = require('./config')() |
11 | |
12 | function votes (msg) { |
13 | var votes = h('div.votes') |
14 | if (msg.key) { |
15 | pull( |
16 | sbot.links({dest: msg.key, rel: 'vote'}), |
17 | pull.drain(function (data) { |
18 | if (data) { |
19 | votes.appendChild(h('a', {href:'#' + data.key}, h('img.emoji', {src: config.emojiUrl + 'star.png'}))) |
20 | } else {console.log(data)} |
21 | }) |
22 | ) |
23 | } |
24 | return votes |
25 | } |
26 | |
27 | module.exports.timestamp = function (msg, edited) { |
28 | var timestamp |
29 | if (edited) |
30 | timestamp = h('span.timestamp', 'Edited: ', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp)))) |
31 | else |
32 | timestamp = h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp)))) |
33 | return timestamp |
34 | } |
35 | |
36 | module.exports.header = function (msg) { |
37 | return h('div.header', |
38 | h('span.avatar', |
39 | h('a', {href: '#' + msg.value.author}, |
40 | h('span.avatar--small', avatar.image(msg.value.author)), |
41 | avatar.name(msg.value.author) |
42 | ) |
43 | ), |
44 | exports.timestamp(msg), |
45 | votes(msg) |
46 | ) |
47 | } |
48 | |
49 | module.exports.messageLink = function (msglink) { |
50 | var link = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 8) + '...')) |
51 | return link |
52 | } |
53 | |
54 | module.exports.rawJSON = function (obj) { |
55 | return JSON.stringify(obj, null, 2) |
56 | .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/) |
57 | .map(function (e) { |
58 | if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) { |
59 | return h('a', {href: '#' + e}, e) |
60 | } |
61 | return e |
62 | }) |
63 | } |
64 | |
65 | var markdown = require('ssb-markdown') |
66 | var config = require('./config')() |
67 | |
68 | module.exports.markdown = function (msg, md) { |
69 | return {innerHTML: markdown.block(msg, {toUrl: function (url, image) { |
70 | if(url[0] == '%' || url[0] == '@') return '#' + url |
71 | if(!image) return url |
72 | if(url[0] !== '&') return url |
73 | return config.blobsUrl + url |
74 | }})} |
75 | } |
76 |
Built with git-ssb-web