Files: 3ee0398629a78832c2fbe78831ce756faeaeca74 / modules / helpers.js
2814 bytesRaw
1 | var h = require('hyperscript') |
2 | var human = require('human-time') |
3 | |
4 | module.exports.timestamp = function (msg) { |
5 | // this function renders a human-readable timestamp and updates it once in awhile |
6 | |
7 | function updateTimestampEl(el) { |
8 | el.firstChild.nodeValue = human(new Date(el.timestamp)) |
9 | return el |
10 | } |
11 | |
12 | setInterval(function () { |
13 | var els = [].slice.call(document.querySelectorAll('.timestamp')) |
14 | els.forEach(updateTimestampEl) |
15 | }, 60e3) |
16 | |
17 | var timestamp = updateTimestampEl(h('a.enter.timestamp', { |
18 | href: '#'+msg.key, |
19 | timestamp: msg.value.timestamp, |
20 | title: new Date(msg.value.timestamp) |
21 | }, '')) |
22 | |
23 | return timestamp |
24 | } |
25 | |
26 | var emojis = require('emoji-named-characters') |
27 | |
28 | module.exports.emojinames = function () { |
29 | var emojiNames = Object.keys(emojis) |
30 | return emojiNames |
31 | } |
32 | |
33 | var sbot = require('./scuttlebot.js') |
34 | |
35 | module.exports.message_name = function (id, cb) { |
36 | // gets the first few characters of a message, for message-link |
37 | function title (s) { |
38 | var m = /^\n*([^\n]{0,40})/.exec(s) |
39 | return m && (m[1].length == 40 ? m[1]+'...' : m[1]) |
40 | } |
41 | |
42 | sbot.get(id, function (err, value) { |
43 | if(err && err.name == 'NotFoundError') |
44 | return cb(null, id.substring(0, 10)+'...(missing)') |
45 | if(value.content.type === 'post' && 'string' === typeof value.content.text) |
46 | return cb(null, title(value.content.text)) |
47 | else if('string' === typeof value.content.text) |
48 | return cb(null, value.content.type + ':'+title(value.content.text)) |
49 | else |
50 | return cb(null, id.substring(0, 10)+'...') |
51 | }) |
52 | } |
53 | |
54 | var messageName = exports.message_name |
55 | var ref = require('ssb-ref') |
56 | |
57 | module.exports.message_link = function (id) { |
58 | // generates a link to a message that has been replied to |
59 | if('string' !== typeof id) |
60 | throw new Error('link must be to message id') |
61 | |
62 | var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...') |
63 | |
64 | if(ref.isMsg(id)) |
65 | messageName(id, function (err, name) { |
66 | if(err) console.error(err) |
67 | else link.textContent = name |
68 | }) |
69 | |
70 | return link |
71 | } |
72 | |
73 | var markdown = require('ssb-markdown') |
74 | |
75 | var config = require('../config')() |
76 | |
77 | module.exports.markdown = function (content) { |
78 | function renderEmoji(emoji) { |
79 | var url = config.emojiUrl + emoji |
80 | if (!url) return ':' + emoji + ':' |
81 | return '<img src="' + encodeURI(url) + '"' |
82 | + ' alt=":' + escape(emoji) + ':"' |
83 | + ' title=":' + escape(emoji) + ':"' |
84 | + ' class="emoji">' |
85 | } |
86 | |
87 | if('string' === typeof content) |
88 | content = {text: content} |
89 | //handle patchwork style mentions. |
90 | var mentions = {} |
91 | |
92 | var md = h('div.markdown') |
93 | md.innerHTML = markdown.block(content.text, { |
94 | emoji: renderEmoji, |
95 | toUrl: function (id) { |
96 | if(ref.isBlob(id)) return config.blobsUrl + id |
97 | return '#'+(mentions[id]?mentions[id]:id) |
98 | } |
99 | }) |
100 | |
101 | return md |
102 | } |
103 |
Built with git-ssb-web