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 | |
parent | fdb5ceeebc7b7e7ca182c7ac547723a321c7ffc5 (diff) | |
download | patchfoo-e6bb8bd53b7045b8bfcbeaa8936ab7a3ff1760e1.tar.gz patchfoo-e6bb8bd53b7045b8bfcbeaa8936ab7a3ff1760e1.zip |
Vendor ssb-mentions
-rw-r--r-- | lib/serve.js | 2 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | vendor/ssb-mentions.js | 108 |
3 files changed, 109 insertions, 2 deletions
diff --git a/lib/serve.js b/lib/serve.js index 147c6ee..67b57c7 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -10,7 +10,7 @@ var u = require('./util') var cat = require('pull-cat') var h = require('hyperscript') var paginate = require('pull-paginate') -var ssbMentions = require('ssb-mentions') +var ssbMentions = require('../vendor/ssb-mentions') var multicb = require('multicb') var pkg = require('../package') var Busboy = require('busboy') diff --git a/package.json b/package.json index de3512b..218d168 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "ssb-client": "^4.7.8", "ssb-git": "^1.1.0", "ssb-markdown": "^6.0.0", - "ssb-mentions": "^0.5.0", "ssb-npm-registry": "^1.8.0", "ssb-sort": "^1.0.0", "ssb-web-resolver": "^1.0.2", 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 +} + |