git ssb

0+

cel / ssb-mentions



Tree: 17160691cbc6b73f0080c093dcf312a0c8ee5fa9

Files: 17160691cbc6b73f0080c093dcf312a0c8ee5fa9 / index.js

2788 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.emoji = function (name) {
35 onLink({label: name, emoji: true})
36}
37
38extractor.hashtag = function (_, hashtag) {
39 onLink({target: hashtag})
40}
41
42extractor.link = function (href, _, text) {
43 onLink({label: text, target: href, embed: false})
44}
45
46extractor.image = function (href, _, text) {
47 onLink({label: text, target: href, embed: true})
48}
49
50function links (s, _onLink) {
51 if('string' !== typeof s) return
52 onLink = _onLink
53 try {
54 marked(s, {renderer: extractor, emoji: extractor.emoji})
55 } catch(err) {
56 console.log(JSON.stringify(s))
57 throw err
58 }
59 onLink = noop
60}
61
62module.exports = function (text, opts) {
63 var bareFeedNames = opts && opts.bareFeedNames
64 var emoji = opts && opts.emoji
65 var a = []
66 links(text, function (link) {
67 if(ref.isFeed(link.target))
68 a.push({link: link.target, name: link.label && link.label.replace(/^@/, '')})
69 else if(ref.isBlob(link.target))
70 a.push({link: link.target, name: link.label})
71 else if(ref.isMsg(link.target))
72 a.push({link: link.target, name: link.label})
73 else if(bareFeedNames && link.target && link.target[0] === '@')
74 a.push({link: link.target[0], name: link.target.substr(1)})
75 else if(link.target && link.target[0] === '#')
76 a.push({link: link.target})
77 else if(emoji && link.emoji)
78 a.push({emoji: true, name: link.label})
79 })
80 return a
81}
82
83

Built with git-ssb-web