git ssb

0+

cel / ssb-mentions



Tree: b4bcd3df93e1498eeb74cf22b94767d03f5dd739

Files: b4bcd3df93e1498eeb74cf22b94767d03f5dd739 / index.js

1012 bytesRaw
1var ref = require('ssb-ref')
2var marked = require('ssb-marked')
3function noop(){}
4var onLink = noop
5var extractor = new marked.Renderer()
6
7extractor.mention = function (_, id) {
8 onLink({target: id})
9}
10
11extractor.link = function (href, _, text) {
12 onLink({label: text, target: href, embed: false})
13}
14
15extractor.image = function (href, _, text) {
16 onLink({label: text, target: href, embed: true})
17}
18
19function links (s, _onLink) {
20 if('string' !== typeof s) return
21 onLink = _onLink
22 try {
23 marked(s, {renderer: extractor})
24 } catch(err) {
25 console.log(JSON.stringify(s))
26 throw err
27 }
28 onLink = noop
29}
30
31module.exports = function (text) {
32 var a = []
33 links(text, function (link) {
34 if(ref.isFeed(link.target))
35 a.push({link: link.target, name: link.label && link.label.replace(/^@/, '')})
36 else if(ref.isBlob(link.target))
37 a.push({link: link.target, name: link.label})
38 else if(ref.isMsg(link.target))
39 a.push({link: link.target, name: link.label})
40 })
41 return a
42}
43
44

Built with git-ssb-web