Files: bdbeb9c156a4c8b0e0ecd33e2613564c8491ee66 / lib / markdown.js
2867 bytesRaw
1 | var path = require('path') |
2 | var url = require('url') |
3 | var marked = require('ssb-marked') |
4 | var ref = require('ssb-ref') |
5 | var u = require('./util') |
6 | var emojiNamedCharacters = require('emoji-named-characters') |
7 | |
8 | // render links to git objects and ssb objects |
9 | var blockRenderer = new marked.Renderer() |
10 | blockRenderer.urltransform = function (href) { |
11 | if (ref.isLink(href)) |
12 | return u.encodeLink(href) |
13 | if (/^[0-9a-f]{40}$/.test(href) && this.options.repo) |
14 | return u.encodeLink([this.options.repo.id, 'commit', href]) |
15 | if (this.options.repo && this.options.rev && this.options.path |
16 | && !url.parse(href).host && href[0] !== '#') |
17 | return path.join('/', encodeURIComponent(this.options.repo.id), |
18 | 'blob', this.options.rev, this.options.path.join('/'), href) |
19 | return href |
20 | } |
21 | |
22 | blockRenderer.image = function (href, title, text) { |
23 | href = href.replace(/^&/, '&') |
24 | var url |
25 | if (ref.isBlobId(href)) |
26 | url = u.encodeLink(href) |
27 | else if (/^https?:\/\//.test(href)) |
28 | url = href |
29 | else if (this.options.repo && this.options.rev && this.options.path) |
30 | url = path.join('/', encodeURIComponent(this.options.repo.id), |
31 | 'raw', this.options.rev, this.options.path.join('/'), href) |
32 | else |
33 | return text |
34 | return '<img src="' + u.escape(url) + '" alt="' + text + '"' + |
35 | (title ? ' title="' + title + '"' : '') + '/>' |
36 | } |
37 | |
38 | blockRenderer.mention = function (preceding, id) { |
39 | // prevent broken name mention |
40 | if (id[0] == '@' && !ref.isFeed(id)) |
41 | return (preceding||'') + u.escape(id) |
42 | |
43 | return marked.Renderer.prototype.mention.call(this, preceding, id) |
44 | } |
45 | |
46 | blockRenderer.listitem = function (text, checked) { |
47 | return '<li>' + |
48 | (checked === undefined ? '' : '<i>' + (checked ? 'โ' : 'โ') + '</i> ') + |
49 | text + '</li>\n' |
50 | } |
51 | |
52 | function renderEmoji(emoji) { |
53 | return emoji in emojiNamedCharacters ? |
54 | '<img src="/emoji/' + encodeURI(emoji) + '.png"' |
55 | + ' alt=":' + escape(emoji) + ':"' |
56 | + ' title=":' + escape(emoji) + ':"' |
57 | + ' class="emoji" height="16" width="16">' |
58 | : ':' + emoji + ':' |
59 | } |
60 | |
61 | marked.setOptions({ |
62 | gfm: true, |
63 | mentions: true, |
64 | tables: true, |
65 | breaks: true, |
66 | pedantic: false, |
67 | sanitize: true, |
68 | smartLists: true, |
69 | smartypants: false, |
70 | emoji: renderEmoji, |
71 | highlight: u.highlight, |
72 | renderer: blockRenderer |
73 | }) |
74 | |
75 | // hack to make git link mentions work |
76 | var mdRules = new marked.InlineLexer(1, marked.defaults).rules |
77 | mdRules.mention = |
78 | /^(\s)?([@%&][A-Za-z0-9\._\-+=\/]*[A-Za-z0-9_\-+=\/]|[0-9a-f]{40})/ |
79 | mdRules.text = /^[\s\S]+?(?=[\\<!\[_*`:~]|https?:\/\/| {2,}\n| [@%&]|[0-9a-f]{40}|$)/ |
80 | |
81 | module.exports = function (text, options, cb) { |
82 | if (!text) return '' |
83 | if (typeof text != 'string') text = String(text) |
84 | if (!options) options = {} |
85 | else if (options.id) options = {repo: options} |
86 | if (!options.rev) options.rev = 'HEAD' |
87 | if (!options.path) options.path = [] |
88 | |
89 | return marked(text, options, cb) |
90 | } |
91 |
Built with git-ssb-web