Commit 24932ccb6a1e535b89e82f8f65f67957e0345f4d
Render repo images in markdown
Charles Lehner committed on 4/12/2016, 7:26:59 PMParent: 46b860bdb36df02cc3a99ceea9b69e95f9e6e389
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -35,8 +35,22 @@ | ||
35 | 35 | return encodeLink([this.options.repo.id, 'commit', url]) |
36 | 36 | return url |
37 | 37 | } |
38 | 38 | |
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 | + | |
39 | 53 | function getExtension(filename) { |
40 | 54 | return (/\.([^.]+)$/.exec(filename) || [,filename])[1] |
41 | 55 | } |
42 | 56 | |
@@ -70,12 +84,17 @@ | ||
70 | 84 | mdRules.mention = |
71 | 85 | /^(\s)?([@%&][A-Za-z0-9\._\-+=\/]*[A-Za-z0-9_\-+=\/]|[0-9a-f]{40})/ |
72 | 86 | mdRules.text = /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n| [@%&]|[0-9a-f]{40}|$)/ |
73 | 87 | |
74 | -function markdown(text, repo, cb) { | |
88 | +function markdown(text, options, cb) { | |
75 | 89 | if (!text) return '' |
76 | 90 | 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) | |
78 | 97 | } |
79 | 98 | |
80 | 99 | function parseAddr(str, def) { |
81 | 100 | if (!str) return def |
@@ -749,16 +768,16 @@ | ||
749 | 768 | serveTemplate(err.name, status || 500) |
750 | 769 | ) |
751 | 770 | } |
752 | 771 | |
753 | - function renderObjectData(obj, filename, repo) { | |
772 | + function renderObjectData(obj, filename, repo, rev, path) { | |
754 | 773 | var ext = getExtension(filename) |
755 | 774 | return readOnce(function (cb) { |
756 | 775 | readObjectString(obj, function (err, buf) { |
757 | 776 | buf = buf.toString('utf8') |
758 | 777 | if (err) return cb(err) |
759 | 778 | cb(null, (ext == 'md' || ext == 'markdown') |
760 | - ? markdown(buf, repo) | |
779 | + ? markdown(buf, {repo: repo, rev: rev, path: path}) | |
761 | 780 | : renderCodeTable(buf, ext)) |
762 | 781 | }) |
763 | 782 | }) |
764 | 783 | } |
@@ -1405,9 +1424,9 @@ | ||
1405 | 1424 | if (err) return cb(err) |
1406 | 1425 | cb(null, cat([ |
1407 | 1426 | pull.once('<section><h4><a name="readme">' + |
1408 | 1427 | escapeHTML(file.name) + '</a></h4><hr/>'), |
1409 | - renderObjectData(obj, file.name, repo), | |
1428 | + renderObjectData(obj, file.name, repo, branch, path), | |
1410 | 1429 | pull.once('</section>') |
1411 | 1430 | ])) |
1412 | 1431 | }) |
1413 | 1432 | }) |
@@ -1653,8 +1672,9 @@ | ||
1653 | 1672 | var type = repo.isCommitHash(rev) ? 'Tree' : 'Branch' |
1654 | 1673 | var pathLinks = path.length === 0 ? '' : |
1655 | 1674 | ': ' + linkPath([repo.id, 'tree'], [rev].concat(path)) |
1656 | 1675 | var rawFilePath = [repo.id, 'raw', rev].concat(path) |
1676 | + var dirPath = path.slice(0, path.length-1) | |
1657 | 1677 | var filename = path[path.length-1] |
1658 | 1678 | var extension = getExtension(filename) |
1659 | 1679 | cb(null, renderRepoPage(repo, 'code', rev, cat([ |
1660 | 1680 | pull.once('<section><form action="" method="get">' + |
@@ -1670,9 +1690,9 @@ | ||
1670 | 1690 | '<section>'), |
1671 | 1691 | extension in imgMimes |
1672 | 1692 | ? pull.once('<img src="' + encodeLink(rawFilePath) + |
1673 | 1693 | '" alt="' + escapeHTML(filename) + '" />') |
1674 | - : renderObjectData(object, filename, repo), | |
1694 | + : renderObjectData(object, filename, repo, rev, dirPath), | |
1675 | 1695 | pull.once('</section>') |
1676 | 1696 | ]))) |
1677 | 1697 | }) |
1678 | 1698 | }) |
Built with git-ssb-web