aboutsummaryrefslogtreecommitdiff
path: root/vendor/ssb-mentions.js
blob: b01bf9a6e70b508fa63687328f89e168ece7b115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
ssb-mentions

Copyright (c) 2016 Dominic Tarr

Permission is hereby granted, free of charge, 
to any person obtaining a copy of this software and 
associated documentation files (the "Software"), to 
deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, 
merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom 
the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice 
shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var ref = require('ssb-ref')
var marked = require('./ssb-marked')
function noop(){}
var onLink = noop
var extractor = new marked.Renderer()

// prevent html from entering into mention labels.
// code taken from ssb-markdown
extractor.code = function(code, lang, escaped) { return escaped ? unquote(code) : code }
extractor.blockquote = function(quote) { return unquote(quote) }
extractor.html = function(html) { return false }
extractor.heading = function(text, level, raw) { return unquote(text)+' ' }
extractor.hr = function() { return ' --- ' }
extractor.br = function() { return ' ' }
extractor.list = function(body, ordered) { return unquote(body) }
extractor.listitem = function(text) { return '- '+unquote(text) }
extractor.paragraph = function(text) { return unquote(text)+' ' }
extractor.table = function(header, body) { return unquote(header + ' ' + body) }
extractor.tablerow = function(content) { return unquote(content) }
extractor.tablecell = function(content, flags) { return unquote(content) }
extractor.strong = function(text) { return unquote(text) }
extractor.em = function(text) { return unquote(text) }
extractor.codespan = function(text) { return unquote(text) }
extractor.del = function(text) { return unquote(text) }

function unquote (text) {
  return text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, '\'')
}

extractor.mention = function (_, id) {
  onLink({target: id})
}

extractor.emoji = function (name) {
  onLink({label: name, emoji: true})
}

extractor.hashtag = function (_, hashtag) {
  onLink({target: hashtag})
}

extractor.link = function (href, _, text) {
  onLink({label: text, target: href, embed: false})
}

extractor.image = function (href, _, text) {
  onLink({label: text, target: href, embed: true})
}

function links (s, _onLink) {
  if('string' !== typeof s) return
  onLink = _onLink
  try {
    marked(s, {renderer: extractor, emoji: extractor.emoji})
  } catch(err) {
    console.log(JSON.stringify(s))
    throw err
  }
  onLink = noop
}

module.exports = function (text, opts) {
  var bareFeedNames = opts && opts.bareFeedNames
  var emoji = opts && opts.emoji
  var a = []
  links(text, function (link) {
    var result = link.target && ref.parseLink(link.target)
    if (result) {
      result.name = link.label && link.label.replace(/^@/, '')
      a.push(result)
    } else if(bareFeedNames && link.target && link.target.startsWith('@')) {
      a.push({link: link.target[0], name: link.target.substr(1)})
    } else if(link.target && link.target.startsWith('#')) {
      a.push({link: link.target})
    } else if(emoji && link.emoji) {
      a.push({emoji: true, name: link.label})
    }
  })
  return a
}