git ssb

30+

cel / git-ssb-web



Commit 24932ccb6a1e535b89e82f8f65f67957e0345f4d

Render repo images in markdown

Charles Lehner committed on 4/12/2016, 7:26:59 PM
Parent: 46b860bdb36df02cc3a99ceea9b69e95f9e6e389

Files changed

index.jschanged
index.jsView
@@ -35,8 +35,22 @@
3535 return encodeLink([this.options.repo.id, 'commit', url])
3636 return url
3737 }
3838
39+blockRenderer.image = function (href, title, text) {
40+ href = href.replace(/^&/, '&')
41+ var url
42+ if (ref.isBlobId(href))
43+ url = encodeLink(href)
44+ else if (this.options.repo && this.options.rev && this.options.path)
45+ url = path.join('/', encodeURIComponent(this.options.repo.id),
46+ 'raw', this.options.rev, this.options.path.join('/'), href)
47+ else
48+ return text
49+ return '<img src="' + escapeHTML(url) + '" alt="' + text + '"' +
50+ (title ? ' title="' + title + '"' : '') + '/>'
51+}
52+
3953 function getExtension(filename) {
4054 return (/\.([^.]+)$/.exec(filename) || [,filename])[1]
4155 }
4256
@@ -70,12 +84,17 @@
7084 mdRules.mention =
7185 /^(\s)?([@%&][A-Za-z0-9\._\-+=\/]*[A-Za-z0-9_\-+=\/]|[0-9a-f]{40})/
7286 mdRules.text = /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n| [@%&]|[0-9a-f]{40}|$)/
7387
74-function markdown(text, repo, cb) {
88+function markdown(text, options, cb) {
7589 if (!text) return ''
7690 if (typeof text != 'string') text = String(text)
77- return marked(text, {repo: repo}, cb)
91+ if (!options) options = {}
92+ else if (options.id) options = {repo: options}
93+ if (!options.rev) options.rev = 'HEAD'
94+ if (!options.path) options.path = []
95+
96+ return marked(text, options, cb)
7897 }
7998
8099 function parseAddr(str, def) {
81100 if (!str) return def
@@ -749,16 +768,16 @@
749768 serveTemplate(err.name, status || 500)
750769 )
751770 }
752771
753- function renderObjectData(obj, filename, repo) {
772+ function renderObjectData(obj, filename, repo, rev, path) {
754773 var ext = getExtension(filename)
755774 return readOnce(function (cb) {
756775 readObjectString(obj, function (err, buf) {
757776 buf = buf.toString('utf8')
758777 if (err) return cb(err)
759778 cb(null, (ext == 'md' || ext == 'markdown')
760- ? markdown(buf, repo)
779+ ? markdown(buf, {repo: repo, rev: rev, path: path})
761780 : renderCodeTable(buf, ext))
762781 })
763782 })
764783 }
@@ -1405,9 +1424,9 @@
14051424 if (err) return cb(err)
14061425 cb(null, cat([
14071426 pull.once('<section><h4><a name="readme">' +
14081427 escapeHTML(file.name) + '</a></h4><hr/>'),
1409- renderObjectData(obj, file.name, repo),
1428+ renderObjectData(obj, file.name, repo, branch, path),
14101429 pull.once('</section>')
14111430 ]))
14121431 })
14131432 })
@@ -1653,8 +1672,9 @@
16531672 var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch'
16541673 var pathLinks = path.length === 0 ? '' :
16551674 ': ' + linkPath([repo.id, 'tree'], [rev].concat(path))
16561675 var rawFilePath = [repo.id, 'raw', rev].concat(path)
1676+ var dirPath = path.slice(0, path.length-1)
16571677 var filename = path[path.length-1]
16581678 var extension = getExtension(filename)
16591679 cb(null, renderRepoPage(repo, 'code', rev, cat([
16601680 pull.once('<section><form action="" method="get">' +
@@ -1670,9 +1690,9 @@
16701690 '<section>'),
16711691 extension in imgMimes
16721692 ? pull.once('<img src="' + encodeLink(rawFilePath) +
16731693 '" alt="' + escapeHTML(filename) + '" />')
1674- : renderObjectData(object, filename, repo),
1694+ : renderObjectData(object, filename, repo, rev, dirPath),
16751695 pull.once('</section>')
16761696 ])))
16771697 })
16781698 })

Built with git-ssb-web