Files: de7b121198e402b391580433b43572a993304c49 / index.js
1201 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.link = function (href, _, text) { |
12 | onLink({label: text, target: href, embed: false}) |
13 | } |
14 | |
15 | extractor.image = function (href, _, text) { |
16 | onLink({label: text, target: href, embed: true}) |
17 | } |
18 | |
19 | function links (s, _onLink) { |
20 | if('string' !== typeof s) return |
21 | onLink = _onLink |
22 | try { |
23 | marked(s, {renderer: extractor}) |
24 | } catch(err) { |
25 | console.log(JSON.stringify(s)) |
26 | throw err |
27 | } |
28 | onLink = noop |
29 | } |
30 | |
31 | module.exports = function (text, opts) { |
32 | var bareFeedNames = opts && opts.bareFeedNames |
33 | var a = [] |
34 | links(text, function (link) { |
35 | if(ref.isFeed(link.target)) |
36 | a.push({link: link.target, name: link.label && link.label.replace(/^@/, '')}) |
37 | else if(ref.isBlob(link.target)) |
38 | a.push({link: link.target, name: link.label}) |
39 | else if(ref.isMsg(link.target)) |
40 | a.push({link: link.target, name: link.label}) |
41 | else if(bareFeedNames && link.target && link.target[0] === '@') |
42 | a.push({link: link.target[0], name: link.target.substr(1)}) |
43 | }) |
44 | return a |
45 | } |
46 | |
47 |
Built with git-ssb-web