git ssb

30+

cel / git-ssb-web



Tree: 4b7fe8493fa89039404fac7dbeb5ff3a5326686b

Files: 4b7fe8493fa89039404fac7dbeb5ff3a5326686b / lib / markdown.js

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

Built with git-ssb-web