aboutsummaryrefslogtreecommitdiff
path: root/vendor/ssb-mentions.js
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ssb-mentions.js')
-rw-r--r--vendor/ssb-mentions.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/vendor/ssb-mentions.js b/vendor/ssb-mentions.js
new file mode 100644
index 0000000..b01bf9a
--- /dev/null
+++ b/vendor/ssb-mentions.js
@@ -0,0 +1,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
+}
+