Files: b4bcd3df93e1498eeb74cf22b94767d03f5dd739 / index.js
1012 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) { |
32 | var a = [] |
33 | links(text, function (link) { |
34 | if(ref.isFeed(link.target)) |
35 | a.push({link: link.target, name: link.label && link.label.replace(/^@/, '')}) |
36 | else if(ref.isBlob(link.target)) |
37 | a.push({link: link.target, name: link.label}) |
38 | else if(ref.isMsg(link.target)) |
39 | a.push({link: link.target, name: link.label}) |
40 | }) |
41 | return a |
42 | } |
43 | |
44 |
Built with git-ssb-web