Commit 89ce7442bb7d0a3e38e89aab3f493cba4140cba4
Transform links to git objects in markdown
Charles Lehner committed on 3/27/2016, 3:13:18 PMParent: 11d710dc59aeff08bd0093012217deb84b8e2f00
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -17,12 +17,15 @@ | ||
17 | 17 … | var schemas = require('ssb-msg-schemas') |
18 | 18 … | var Issues = require('ssb-issues') |
19 | 19 … | var paramap = require('pull-paramap') |
20 | 20 … | |
21 … | +// render links to git objects and ssb objects | |
21 | 22 … | var blockRenderer = new marked.Renderer() |
22 | 23 … | blockRenderer.urltransform = function (url) { |
23 | 24 … | if (ref.isLink(url)) |
24 | 25 … | return encodeLink(url) |
26 … | + if (/^[0-9a-f]{40}$/.test(url) && this.options.repo) | |
27 … | + return flattenPath([this.options.repo.id, 'tree', url]) | |
25 | 28 … | return url |
26 | 29 … | } |
27 | 30 … | |
28 | 31 … | marked.setOptions({ |
@@ -36,12 +39,16 @@ | ||
36 | 39 … | smartypants: false, |
37 | 40 … | renderer: blockRenderer |
38 | 41 … | }) |
39 | 42 … | |
40 | -function markdown(text) { | |
43 … | +// hack to make git link mentions work | |
44 … | +new marked.InlineLexer(1, marked.defaults).rules.mention = | |
45 … | + /^(\s)?([@%&][A-Za-z0-9\._\-+=\/]*[A-Za-z0-9_\-+=\/]|[0-9a-f]{40})/ | |
46 … | + | |
47 … | +function markdown(text, repo, cb) { | |
41 | 48 … | if (!text) return '' |
42 | 49 … | if (typeof text != 'string') text = String(text) |
43 | - return marked(text) | |
50 … | + return marked(text, {repo: repo}, cb) | |
44 | 51 … | } |
45 | 52 … | |
46 | 53 … | function parseAddr(str, def) { |
47 | 54 … | if (!str) return def |
@@ -145,15 +152,16 @@ | ||
145 | 152 … | ) + |
146 | 153 … | '</form>' |
147 | 154 … | } |
148 | 155 … | |
149 | -function renderPostForm(placeholder, rows) { | |
156 … | +function renderPostForm(repo, placeholder, rows) { | |
150 | 157 … | return '<input type="radio" class="tab-radio" id="tab1" name="tab" checked="checked"/>' + |
151 | 158 … | '<input type="radio" class="tab-radio" id="tab2" name="tab"/>' + |
152 | 159 … | '<div class="tab-links">' + |
153 | 160 … | '<label for="tab1" id="write-tab-link" class="tab1-link">Write</label>' + |
154 | 161 … | '<label for="tab2" id="preview-tab-link" class="tab2-link">Preview</label>' + |
155 | 162 … | '</div>' + |
163 … | + '<input type="hidden" id="repo-id" value="' + repo.id + '"/>' + | |
156 | 164 … | '<div id="write-tab" class="tab1">' + |
157 | 165 … | '<textarea id="post-text" name="text" class="wide-input"' + |
158 | 166 … | ' rows="' + (rows||4) + '" cols="77"' + |
159 | 167 … | (placeholder ? ' placeholder="' + placeholder + '"' : '') + |
@@ -264,9 +272,9 @@ | ||
264 | 272 … | open('POST', '', true) |
265 | 273 … | onload = function() { |
266 | 274 … | $('preview-tab').innerHTML = responseText |
267 | 275 … | } |
268 | - send('action=markdown&text=' + | |
276 … | + send('action=markdown&repo=' + $('repo-id').value + '&text=' + | |
269 | 277 … | encodeURIComponent($('post-text').value)) |
270 | 278 … | } |
271 | 279 … | } |
272 | 280 … | }.toString() + ')()' |
@@ -423,9 +431,9 @@ | ||
423 | 431 … | cb(null, serveRedirect(encodeLink(issue.id))) |
424 | 432 … | }) |
425 | 433 … | |
426 | 434 … | case 'markdown': |
427 | - return cb(null, serveMarkdown(data.text)) | |
435 … | + return cb(null, serveMarkdown(data.text, {id: data.repo})) | |
428 | 436 … | |
429 | 437 … | default: |
430 | 438 … | cb(null, servePlainError(400, 'What are you trying to do?')) |
431 | 439 … | } |
@@ -500,10 +508,10 @@ | ||
500 | 508 … | msg |
501 | 509 … | ]) |
502 | 510 … | } |
503 | 511 … | |
504 | - function serveMarkdown(text) { | |
505 | - var html = markdown(text) | |
512 … | + function serveMarkdown(text, repo) { | |
513 … | + var html = markdown(text, repo) | |
506 | 514 … | return pull.values([ |
507 | 515 … | [200, { |
508 | 516 … | 'Content-Length': Buffer.byteLength(html), |
509 | 517 … | 'Content-Type': 'text/html; charset=utf-8' |
@@ -972,9 +980,9 @@ | ||
972 | 980 … | readOnce(function (cb) { |
973 | 981 … | pull(obj.read, pull.collect(function (err, bufs) { |
974 | 982 … | if (err) return cb(err) |
975 | 983 … | var buf = Buffer.concat(bufs, obj.length) |
976 | - cb(null, markdown(buf.toString())) | |
984 … | + markdown(buf.toString(), repo, cb) | |
977 | 985 … | })) |
978 | 986 … | }) |
979 | 987 … | : cat([ |
980 | 988 … | pull.once('<pre>'), |
@@ -1104,9 +1112,15 @@ | ||
1104 | 1112 … | extension in imgMimes |
1105 | 1113 … | ? pull.once('<img src="' + escapeHTML(flattenPath(rawFilePath)) + |
1106 | 1114 … | '" alt="' + escapeHTML(filename) + '" />') |
1107 | 1115 … | : markdownFilenameRegex.test(filename) |
1108 | - ? pull(object.read, escapeHTMLStream(), pull.map(markdown)) | |
1116 … | + ? readOnce(function (cb) { | |
1117 … | + pull(object.read, pull.collect(function (err, bufs) { | |
1118 … | + if (err) return cb(err) | |
1119 … | + var buf = Buffer.concat(bufs, object.length) | |
1120 … | + cb(null, markdown(buf.toString('utf8'), repo)) | |
1121 … | + })) | |
1122 … | + }) | |
1109 | 1123 … | : pull(object.read, escapeHTMLStream(), wrap('pre')), |
1110 | 1124 … | pull.once('</section>') |
1111 | 1125 … | ]))) |
1112 | 1126 … | }) |
@@ -1224,9 +1238,9 @@ | ||
1224 | 1238 … | '<h3>New Issue</h3>' + |
1225 | 1239 … | '<section><form action="" method="post">' + |
1226 | 1240 … | '<input type="hidden" name="action" value="new-issue">' + |
1227 | 1241 … | '<p><input class="wide-input" name="title" placeholder="Issue Title" size="77" /></p>' + |
1228 | - renderPostForm('Description', 8) + | |
1242 … | + renderPostForm(repo, 'Description', 8) + | |
1229 | 1243 … | '<button type="submit" class="btn">Create</button>' + |
1230 | 1244 … | '</form></section>')) |
1231 | 1245 … | } |
1232 | 1246 … | |
@@ -1251,9 +1265,9 @@ | ||
1251 | 1265 … | var authorLink = link([issue.author], authorName) |
1252 | 1266 … | cb(null, |
1253 | 1267 … | authorLink + ' opened this issue on ' + timestamp(issue.created_at) + |
1254 | 1268 … | '<hr/>' + |
1255 | - markdown(issue.text) + | |
1269 … | + markdown(issue.text, repo) + | |
1256 | 1270 … | '</section>') |
1257 | 1271 … | }) |
1258 | 1272 … | }), |
1259 | 1273 … | // render posts and edits |
@@ -1279,9 +1293,9 @@ | ||
1279 | 1293 … | authorLink + |
1280 | 1294 … | (changed == null ? '' : ' ' + ( |
1281 | 1295 … | changed ? 'reopened this issue' : 'closed this issue')) + |
1282 | 1296 … | ' · ' + msgTimeLink + |
1283 | - markdown(c.text) + | |
1297 … | + markdown(c.text, repo) + | |
1284 | 1298 … | '</section>' |
1285 | 1299 … | } else { |
1286 | 1300 … | var text = c.text || (c.type + ' ' + msg.key) |
1287 | 1301 … | return '<section class="collapse mention-preview">' + |
@@ -1312,9 +1326,9 @@ | ||
1312 | 1326 … | cb(null, '<section><form action="" method="post">' + |
1313 | 1327 … | '<input type="hidden" name="action" value="comment">' + |
1314 | 1328 … | '<input type="hidden" name="id" value="' + issue.id + '">' + |
1315 | 1329 … | '<input type="hidden" name="branch" value="' + newestMsg.key + '">' + |
1316 | - renderPostForm() + | |
1330 … | + renderPostForm(repo) + | |
1317 | 1331 … | '<input type="submit" class="btn open" value="Comment" />' + |
1318 | 1332 … | (isAuthor ? |
1319 | 1333 … | '<input type="submit" class="btn"' + |
1320 | 1334 … | ' name="' + (issue.open ? 'close' : 'open') + '"' + |
Built with git-ssb-web