git ssb

0+

cel / ssb-mentions



Tree: 27b98ddcb4a61a15e2b1b237e0ead0de6cd87798

Files: 27b98ddcb4a61a15e2b1b237e0ead0de6cd87798 / index.js

2576 bytesRaw
1var ref = require('ssb-ref')
2var marked = require('ssb-marked')
3function noop(){}
4var onLink = noop
5var extractor = new marked.Renderer()
6
7// prevent html from entering into mention labels.
8// code taken from ssb-markdown
9extractor.code = function(code, lang, escaped) { return escaped ? unquote(code) : code }
10extractor.blockquote = function(quote) { return unquote(quote) }
11extractor.html = function(html) { return false }
12extractor.heading = function(text, level, raw) { return unquote(text)+' ' }
13extractor.hr = function() { return ' --- ' }
14extractor.br = function() { return ' ' }
15extractor.list = function(body, ordered) { return unquote(body) }
16extractor.listitem = function(text) { return '- '+unquote(text) }
17extractor.paragraph = function(text) { return unquote(text)+' ' }
18extractor.table = function(header, body) { return unquote(header + ' ' + body) }
19extractor.tablerow = function(content) { return unquote(content) }
20extractor.tablecell = function(content, flags) { return unquote(content) }
21extractor.strong = function(text) { return unquote(text) }
22extractor.em = function(text) { return unquote(text) }
23extractor.codespan = function(text) { return unquote(text) }
24extractor.del = function(text) { return unquote(text) }
25
26function unquote (text) {
27 return text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, '\'')
28}
29
30extractor.mention = function (_, id) {
31 onLink({target: id})
32}
33
34extractor.hashtag = function (_, hashtag) {
35 onLink({target: hashtag})
36}
37
38extractor.link = function (href, _, text) {
39 onLink({label: text, target: href, embed: false})
40}
41
42extractor.image = function (href, _, text) {
43 onLink({label: text, target: href, embed: true})
44}
45
46function links (s, _onLink) {
47 if('string' !== typeof s) return
48 onLink = _onLink
49 try {
50 marked(s, {renderer: extractor})
51 } catch(err) {
52 console.log(JSON.stringify(s))
53 throw err
54 }
55 onLink = noop
56}
57
58module.exports = function (text, opts) {
59 var bareFeedNames = opts && opts.bareFeedNames
60 var a = []
61 links(text, function (link) {
62 if(ref.isFeed(link.target))
63 a.push({link: link.target, name: link.label && link.label.replace(/^@/, '')})
64 else if(ref.isBlob(link.target))
65 a.push({link: link.target, name: link.label})
66 else if(ref.isMsg(link.target))
67 a.push({link: link.target, name: link.label})
68 else if(bareFeedNames && link.target && link.target[0] === '@')
69 a.push({link: link.target[0], name: link.target.substr(1)})
70 else if(link.target && link.target[0] === '#')
71 a.push({link: link.target})
72 })
73 return a
74}
75
76

Built with git-ssb-web