Files: 0107e46b337f0846775565435aaf8a90f95b1955 / message / html / subject.js
1176 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, when, send, resolve, Value, computed } = require('mutant') |
3 | |
4 | exports.gives = nest('message.html.subject') |
5 | |
6 | exports.needs = nest({ |
7 | 'message.html.markdown': 'first', |
8 | }) |
9 | |
10 | exports.create = function (api) { |
11 | return nest('message.html.subject', subject) |
12 | |
13 | function subject (msg) { |
14 | const { subject, text } = msg.value.content |
15 | if(!(subject || text)) return |
16 | return api.message.html.markdown(firstLine(subject|| text)) |
17 | } |
18 | |
19 | // private |
20 | |
21 | function firstLine (text) { |
22 | if(text.length < 80 && !~text.indexOf('\n')) return text |
23 | |
24 | //get the first non-empty line |
25 | var line = text.trim().split('\n').shift().trim() |
26 | |
27 | //always break on a space, so that links are preserved. |
28 | const leadingMentionsLength = countLeadingMentions(line) |
29 | const i = line.indexOf(' ', leadingMentionsLength + 80) |
30 | var sample = line.substring(0, ~i ? i : line.length) |
31 | |
32 | const ellipsis = (sample.length < line.length) ? '...' : '' |
33 | return sample + ellipsis |
34 | } |
35 | |
36 | function countLeadingMentions (str) { |
37 | return str.match(/^(\s*\[@[^\)]+\)\s*)*/)[0].length |
38 | // matches any number of pattern " [@...) " from start of line |
39 | } |
40 | |
41 | } |
42 |
Built with git-ssb-web