diff options
author | cel <cel@lOUVT+Phkvai9a/cCS/RKo+S9hnPAQdVixms/7ldpPA=.ed25519> | 2020-11-27 12:07:39 -0500 |
---|---|---|
committer | cel <cel@lOUVT+Phkvai9a/cCS/RKo+S9hnPAQdVixms/7ldpPA=.ed25519> | 2020-11-27 12:09:48 -0500 |
commit | e6bb8bd53b7045b8bfcbeaa8936ab7a3ff1760e1 (patch) | |
tree | c9fcb81410a83602c5c8814ae7f6267b88bb70f2 /vendor | |
parent | fdb5ceeebc7b7e7ca182c7ac547723a321c7ffc5 (diff) | |
download | patchfoo-e6bb8bd53b7045b8bfcbeaa8936ab7a3ff1760e1.tar.gz patchfoo-e6bb8bd53b7045b8bfcbeaa8936ab7a3ff1760e1.zip |
Vendor ssb-mentions
Diffstat (limited to 'vendor')
-rw-r--r-- | vendor/ssb-mentions.js | 108 |
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 +} + |