Files: f096230a58e0b7b3a85a61e839a6088a8fe434c2 / modules / message-name.js
1003 bytesRaw
1 | |
2 | var sbot_get = require('patchbay/plugs').first(exports.sbot_get = []) |
3 | |
4 | exports.message_name = function (id, cb) { |
5 | sbot_get(id, function (err, value) { |
6 | if (err && err.name === 'NotFoundError') { |
7 | return cb(null, id.substring(0, 10) + '...(missing)') |
8 | } else if (value.content.type === 'post' && typeof value.content.text === 'string') { |
9 | if (value.content.text.trim()) { |
10 | return cb(null, titleFromMarkdown(value.content.text, 40)) |
11 | } |
12 | } else if (typeof value.content.text === 'string') { |
13 | return cb(null, value.content.type + ': ' + titleFromMarkdown(value.content.text, 30)) |
14 | } |
15 | |
16 | return cb(null, id.substring(0, 10) + '...') |
17 | }) |
18 | } |
19 | |
20 | function titleFromMarkdown (text, max) { |
21 | text = text.trim().split('\n', 2).join('\n') |
22 | text = text.replace(/_|`|\*|\#|\[.*?\]|\(\S*?\)/g, '').trim() |
23 | text = text.replace(/\:$/, '') |
24 | text = text.trim().split('\n', 1)[0].trim() |
25 | if (text.length > max) { |
26 | text = text.substring(0, max - 2) + '...' |
27 | } |
28 | return text |
29 | } |
30 |
Built with git-ssb-web