Files: a6940a3d687c1a19beaf95c047f36bf32fbb8f2e / post / html / subject.js
1149 bytesRaw
1 | const nest = require('depnest') |
2 | |
3 | exports.gives = nest('post.html.subject') |
4 | |
5 | exports.needs = nest({ |
6 | 'message.html.markdown': 'first' |
7 | }) |
8 | |
9 | exports.create = function (api) { |
10 | return nest('post.html.subject', subject) |
11 | |
12 | function subject (msg) { |
13 | const { subject, text } = msg.value.content |
14 | if(!(subject || text)) return |
15 | |
16 | return api.message.html.markdown(firstLine(subject|| text)) |
17 | } |
18 | |
19 | function firstLine (text) { |
20 | if(text.length < 80 && !~text.indexOf('\n')) return text |
21 | |
22 | //get the first non-empty line |
23 | // var line = text.trim().split('\n').shift().trim() |
24 | |
25 | var line = text.trim().replace(/\n+/g, ' // ').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