Files: 30816f2fc9f07da0bfa9b850793c83462b489489 / index.js
1361 bytesRaw
1 | var ref = require('ssb-ref') |
2 | var marked = require('ssb-marked') |
3 | function noop(){} |
4 | var onLink = noop |
5 | var extractor = new marked.Renderer() |
6 | |
7 | extractor.mention = function (_, id) { |
8 | onLink({target: id}) |
9 | } |
10 | |
11 | extractor.hashtag = function (_, hashtag) { |
12 | onLink({target: hashtag}) |
13 | } |
14 | |
15 | extractor.link = function (href, _, text) { |
16 | onLink({label: text, target: href, embed: false}) |
17 | } |
18 | |
19 | extractor.image = function (href, _, text) { |
20 | onLink({label: text, target: href, embed: true}) |
21 | } |
22 | |
23 | function links (s, _onLink) { |
24 | if('string' !== typeof s) return |
25 | onLink = _onLink |
26 | try { |
27 | marked(s, {renderer: extractor}) |
28 | } catch(err) { |
29 | console.log(JSON.stringify(s)) |
30 | throw err |
31 | } |
32 | onLink = noop |
33 | } |
34 | |
35 | module.exports = function (text, opts) { |
36 | var bareFeedNames = opts && opts.bareFeedNames |
37 | var a = [] |
38 | links(text, function (link) { |
39 | if(ref.isFeed(link.target)) |
40 | a.push({link: link.target, name: link.label && link.label.replace(/^@/, '')}) |
41 | else if(ref.isBlob(link.target)) |
42 | a.push({link: link.target, name: link.label}) |
43 | else if(ref.isMsg(link.target)) |
44 | a.push({link: link.target, name: link.label}) |
45 | else if(bareFeedNames && link.target && link.target[0] === '@') |
46 | a.push({link: link.target[0], name: link.target.substr(1)}) |
47 | else if(link.target && link.target[0] === '#') |
48 | a.push({link: link.target}) |
49 | }) |
50 | return a |
51 | } |
52 | |
53 |
Built with git-ssb-web