git ssb

30+

cel / git-ssb-web



Commit 89ce7442bb7d0a3e38e89aab3f493cba4140cba4

Transform links to git objects in markdown

Charles Lehner committed on 3/27/2016, 3:13:18 PM
Parent: 11d710dc59aeff08bd0093012217deb84b8e2f00

Files changed

index.jschanged
index.jsView
@@ -17,12 +17,15 @@
1717 var schemas = require('ssb-msg-schemas')
1818 var Issues = require('ssb-issues')
1919 var paramap = require('pull-paramap')
2020
21 +// render links to git objects and ssb objects
2122 var blockRenderer = new marked.Renderer()
2223 blockRenderer.urltransform = function (url) {
2324 if (ref.isLink(url))
2425 return encodeLink(url)
26 + if (/^[0-9a-f]{40}$/.test(url) && this.options.repo)
27 + return flattenPath([this.options.repo.id, 'tree', url])
2528 return url
2629 }
2730
2831 marked.setOptions({
@@ -36,12 +39,16 @@
3639 smartypants: false,
3740 renderer: blockRenderer
3841 })
3942
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) {
4148 if (!text) return ''
4249 if (typeof text != 'string') text = String(text)
43- return marked(text)
50 + return marked(text, {repo: repo}, cb)
4451 }
4552
4653 function parseAddr(str, def) {
4754 if (!str) return def
@@ -145,15 +152,16 @@
145152 ) +
146153 '</form>'
147154 }
148155
149-function renderPostForm(placeholder, rows) {
156 +function renderPostForm(repo, placeholder, rows) {
150157 return '<input type="radio" class="tab-radio" id="tab1" name="tab" checked="checked"/>' +
151158 '<input type="radio" class="tab-radio" id="tab2" name="tab"/>' +
152159 '<div class="tab-links">' +
153160 '<label for="tab1" id="write-tab-link" class="tab1-link">Write</label>' +
154161 '<label for="tab2" id="preview-tab-link" class="tab2-link">Preview</label>' +
155162 '</div>' +
163 + '<input type="hidden" id="repo-id" value="' + repo.id + '"/>' +
156164 '<div id="write-tab" class="tab1">' +
157165 '<textarea id="post-text" name="text" class="wide-input"' +
158166 ' rows="' + (rows||4) + '" cols="77"' +
159167 (placeholder ? ' placeholder="' + placeholder + '"' : '') +
@@ -264,9 +272,9 @@
264272 open('POST', '', true)
265273 onload = function() {
266274 $('preview-tab').innerHTML = responseText
267275 }
268- send('action=markdown&text=' +
276 + send('action=markdown&repo=' + $('repo-id').value + '&text=' +
269277 encodeURIComponent($('post-text').value))
270278 }
271279 }
272280 }.toString() + ')()'
@@ -423,9 +431,9 @@
423431 cb(null, serveRedirect(encodeLink(issue.id)))
424432 })
425433
426434 case 'markdown':
427- return cb(null, serveMarkdown(data.text))
435 + return cb(null, serveMarkdown(data.text, {id: data.repo}))
428436
429437 default:
430438 cb(null, servePlainError(400, 'What are you trying to do?'))
431439 }
@@ -500,10 +508,10 @@
500508 msg
501509 ])
502510 }
503511
504- function serveMarkdown(text) {
505- var html = markdown(text)
512 + function serveMarkdown(text, repo) {
513 + var html = markdown(text, repo)
506514 return pull.values([
507515 [200, {
508516 'Content-Length': Buffer.byteLength(html),
509517 'Content-Type': 'text/html; charset=utf-8'
@@ -972,9 +980,9 @@
972980 readOnce(function (cb) {
973981 pull(obj.read, pull.collect(function (err, bufs) {
974982 if (err) return cb(err)
975983 var buf = Buffer.concat(bufs, obj.length)
976- cb(null, markdown(buf.toString()))
984 + markdown(buf.toString(), repo, cb)
977985 }))
978986 })
979987 : cat([
980988 pull.once('<pre>'),
@@ -1104,9 +1112,15 @@
11041112 extension in imgMimes
11051113 ? pull.once('<img src="' + escapeHTML(flattenPath(rawFilePath)) +
11061114 '" alt="' + escapeHTML(filename) + '" />')
11071115 : 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 + })
11091123 : pull(object.read, escapeHTMLStream(), wrap('pre')),
11101124 pull.once('</section>')
11111125 ])))
11121126 })
@@ -1224,9 +1238,9 @@
12241238 '<h3>New Issue</h3>' +
12251239 '<section><form action="" method="post">' +
12261240 '<input type="hidden" name="action" value="new-issue">' +
12271241 '<p><input class="wide-input" name="title" placeholder="Issue Title" size="77" /></p>' +
1228- renderPostForm('Description', 8) +
1242 + renderPostForm(repo, 'Description', 8) +
12291243 '<button type="submit" class="btn">Create</button>' +
12301244 '</form></section>'))
12311245 }
12321246
@@ -1251,9 +1265,9 @@
12511265 var authorLink = link([issue.author], authorName)
12521266 cb(null,
12531267 authorLink + ' opened this issue on ' + timestamp(issue.created_at) +
12541268 '<hr/>' +
1255- markdown(issue.text) +
1269 + markdown(issue.text, repo) +
12561270 '</section>')
12571271 })
12581272 }),
12591273 // render posts and edits
@@ -1279,9 +1293,9 @@
12791293 authorLink +
12801294 (changed == null ? '' : ' ' + (
12811295 changed ? 'reopened this issue' : 'closed this issue')) +
12821296 ' &middot; ' + msgTimeLink +
1283- markdown(c.text) +
1297 + markdown(c.text, repo) +
12841298 '</section>'
12851299 } else {
12861300 var text = c.text || (c.type + ' ' + msg.key)
12871301 return '<section class="collapse mention-preview">' +
@@ -1312,9 +1326,9 @@
13121326 cb(null, '<section><form action="" method="post">' +
13131327 '<input type="hidden" name="action" value="comment">' +
13141328 '<input type="hidden" name="id" value="' + issue.id + '">' +
13151329 '<input type="hidden" name="branch" value="' + newestMsg.key + '">' +
1316- renderPostForm() +
1330 + renderPostForm(repo) +
13171331 '<input type="submit" class="btn open" value="Comment" />' +
13181332 (isAuthor ?
13191333 '<input type="submit" class="btn"' +
13201334 ' name="' + (issue.open ? 'close' : 'open') + '"' +

Built with git-ssb-web