Commit f856a88ddc79a7ba60cbc42fa9ea382d5c05efbb
Consolidate link functions
Charles Lehner committed on 3/27/2016, 4:36:44 PMParent: 1610838e2f74772cb14e43fe47ee593084a17c3c
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -23,9 +23,9 @@ | ||
23 | 23 | blockRenderer.urltransform = function (url) { |
24 | 24 | if (ref.isLink(url)) |
25 | 25 | return encodeLink(url) |
26 | 26 | if (/^[0-9a-f]{40}$/.test(url) && this.options.repo) |
27 | - return flattenPath([this.options.repo.id, 'tree', url]) | |
27 | + return encodeLink([this.options.repo.id, 'tree', url]) | |
28 | 28 | return url |
29 | 29 | } |
30 | 30 | |
31 | 31 | marked.setOptions({ |
@@ -57,21 +57,21 @@ | ||
57 | 57 | if (isNaN(str)) return {host: str, port: def.port} |
58 | 58 | return {host: def.host, port: str} |
59 | 59 | } |
60 | 60 | |
61 | -function flattenPath(parts) { | |
62 | - return '/' + parts.map(encodeURIComponent).join('/') | |
61 | +function isArray(arr) { | |
62 | + return Object.prototype.toString.call(arr) == '[object Array]' | |
63 | 63 | } |
64 | 64 | |
65 | 65 | function encodeLink(url) { |
66 | - return '/' + encodeURIComponent(url) | |
66 | + if (!isArray(url)) url = [url] | |
67 | + return '/' + url.map(encodeURIComponent).join('/') | |
67 | 68 | } |
68 | 69 | |
69 | 70 | function link(parts, text, raw, props) { |
70 | - var href = flattenPath(parts) | |
71 | 71 | if (text == null) text = parts[parts.length-1] |
72 | 72 | if (!raw) text = escapeHTML(text) |
73 | - return '<a href="' + escapeHTML(href) + '"' + | |
73 | + return '<a href="' + encodeLink(parts) + '"' + | |
74 | 74 | (props ? ' ' + props : '') + |
75 | 75 | '>' + text + '</a>' |
76 | 76 | } |
77 | 77 | |
@@ -742,9 +742,9 @@ | ||
742 | 742 | // Allow navigating revs using GET query param. |
743 | 743 | // Replace the branch in the path with the rev query value |
744 | 744 | path[0] = path[0] || 'tree' |
745 | 745 | path[1] = query.rev |
746 | - req._u.pathname = flattenPath([repo.id].concat(path)) | |
746 | + req._u.pathname = encodeLink([repo.id].concat(path)) | |
747 | 747 | delete req._u.query.rev |
748 | 748 | delete req._u.search |
749 | 749 | return serveRedirect(url.format(req._u)) |
750 | 750 | } |
@@ -1153,9 +1153,9 @@ | ||
1153 | 1153 | '<span class="raw-link">' + link(rawFilePath, 'Raw') + '</span>' + |
1154 | 1154 | '</div></section>' + |
1155 | 1155 | '<section>'), |
1156 | 1156 | extension in imgMimes |
1157 | - ? pull.once('<img src="' + escapeHTML(flattenPath(rawFilePath)) + | |
1157 | + ? pull.once('<img src="' + encodeLink(rawFilePath) + | |
1158 | 1158 | '" alt="' + escapeHTML(filename) + '" />') |
1159 | 1159 | : markdownFilenameRegex.test(filename) |
1160 | 1160 | ? readOnce(function (cb) { |
1161 | 1161 | pull(object.read, pull.collect(function (err, bufs) { |
Built with git-ssb-web