Files: d798033917baf76b190d90e02d3d8fc46a3572d7 / index.js
2814 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 | // prevent html from entering into mention labels. |
8 | // code taken from ssb-markdown |
9 | extractor.code = function(code, lang, escaped) { return escaped ? unquote(code) : code } |
10 | extractor.blockquote = function(quote) { return unquote(quote) } |
11 | extractor.html = function(html) { return false } |
12 | extractor.heading = function(text, level, raw) { return unquote(text)+' ' } |
13 | extractor.hr = function() { return ' --- ' } |
14 | extractor.br = function() { return ' ' } |
15 | extractor.list = function(body, ordered) { return unquote(body) } |
16 | extractor.listitem = function(text) { return '- '+unquote(text) } |
17 | extractor.paragraph = function(text) { return unquote(text)+' ' } |
18 | extractor.table = function(header, body) { return unquote(header + ' ' + body) } |
19 | extractor.tablerow = function(content) { return unquote(content) } |
20 | extractor.tablecell = function(content, flags) { return unquote(content) } |
21 | extractor.strong = function(text) { return unquote(text) } |
22 | extractor.em = function(text) { return unquote(text) } |
23 | extractor.codespan = function(text) { return unquote(text) } |
24 | extractor.del = function(text) { return unquote(text) } |
25 | |
26 | function unquote (text) { |
27 | return text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, '\'') |
28 | } |
29 | |
30 | extractor.mention = function (_, id) { |
31 | onLink({target: id}) |
32 | } |
33 | |
34 | extractor.emoji = function (name) { |
35 | onLink({label: name, emoji: true}) |
36 | return ':' + name + ':' |
37 | } |
38 | |
39 | extractor.hashtag = function (_, hashtag) { |
40 | onLink({target: hashtag}) |
41 | } |
42 | |
43 | extractor.link = function (href, _, text) { |
44 | onLink({label: text, target: href, embed: false}) |
45 | } |
46 | |
47 | extractor.image = function (href, _, text) { |
48 | onLink({label: text, target: href, embed: true}) |
49 | } |
50 | |
51 | function links (s, _onLink) { |
52 | if('string' !== typeof s) return |
53 | onLink = _onLink |
54 | try { |
55 | marked(s, {renderer: extractor, emoji: extractor.emoji}) |
56 | } catch(err) { |
57 | console.log(JSON.stringify(s)) |
58 | throw err |
59 | } |
60 | onLink = noop |
61 | } |
62 | |
63 | module.exports = function (text, opts) { |
64 | var bareFeedNames = opts && opts.bareFeedNames |
65 | var emoji = opts && opts.emoji |
66 | var a = [] |
67 | links(text, function (link) { |
68 | if(ref.isFeed(link.target)) |
69 | a.push({link: link.target, name: link.label && link.label.replace(/^@/, '')}) |
70 | else if(ref.isBlob(link.target)) |
71 | a.push({link: link.target, name: link.label}) |
72 | else if(ref.isMsg(link.target)) |
73 | a.push({link: link.target, name: link.label}) |
74 | else if(bareFeedNames && link.target && link.target[0] === '@') |
75 | a.push({link: link.target[0], name: link.target.substr(1)}) |
76 | else if(link.target && link.target[0] === '#') |
77 | a.push({link: link.target}) |
78 | else if(emoji && link.emoji) |
79 | a.push({emoji: true, name: link.label}) |
80 | }) |
81 | return a |
82 | } |
83 | |
84 |
Built with git-ssb-web