git ssb

30+

cel / git-ssb-web



Tree: dd4e83623a7471b95f8faa0bd8c16453c6b9f6f5

Files: dd4e83623a7471b95f8faa0bd8c16453c6b9f6f5 / lib / markdown.js

2030 bytesRaw
1var path = require('path')
2var marked = require('ssb-marked')
3var ref = require('ssb-ref')
4var u = require('./util')
5
6// render links to git objects and ssb objects
7var blockRenderer = new marked.Renderer()
8blockRenderer.urltransform = function (url) {
9 if (ref.isLink(url))
10 return u.encodeLink(url)
11 if (/^[0-9a-f]{40}$/.test(url) && this.options.repo)
12 return u.encodeLink([this.options.repo.id, 'commit', url])
13 return url
14}
15
16blockRenderer.image = function (href, title, text) {
17 href = href.replace(/^&/, '&')
18 var url
19 if (ref.isBlobId(href))
20 url = u.encodeLink(href)
21 else if (/^https?:\/\//.test(href))
22 url = href
23 else if (this.options.repo && this.options.rev && this.options.path)
24 url = path.join('/', encodeURIComponent(this.options.repo.id),
25 'raw', this.options.rev, this.options.path.join('/'), href)
26 else
27 return text
28 return '<img src="' + u.escape(url) + '" alt="' + text + '"' +
29 (title ? ' title="' + title + '"' : '') + '/>'
30}
31
32blockRenderer.mention = function (preceding, id) {
33 // prevent broken name mention
34 if (id[0] == '@' && !ref.isFeed(id))
35 return (preceding||'') + u.escape(id)
36
37 return marked.Renderer.prototype.mention.call(this, preceding, id)
38}
39
40marked.setOptions({
41 gfm: true,
42 mentions: true,
43 tables: true,
44 breaks: true,
45 pedantic: false,
46 sanitize: true,
47 smartLists: true,
48 smartypants: false,
49 highlight: u.highlight,
50 renderer: blockRenderer
51})
52
53// hack to make git link mentions work
54var mdRules = new marked.InlineLexer(1, marked.defaults).rules
55mdRules.mention =
56 /^(\s)?([@%&][A-Za-z0-9\._\-+=\/]*[A-Za-z0-9_\-+=\/]|[0-9a-f]{40})/
57mdRules.text = /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n| [@%&]|[0-9a-f]{40}|$)/
58
59module.exports = function (text, options, cb) {
60 if (!text) return ''
61 if (typeof text != 'string') text = String(text)
62 if (!options) options = {}
63 else if (options.id) options = {repo: options}
64 if (!options.rev) options.rev = 'HEAD'
65 if (!options.path) options.path = []
66
67 return marked(text, options, cb)
68}
69

Built with git-ssb-web