git ssb

30+

cel / git-ssb-web



Commit 31c9fa39360b9c65460095352026a362be40c0ab

Render ssb links in markdown

Charles Lehner committed on 3/27/2016, 11:02:53 AM
Parent: 4158c83c87660fc6a59d13f7f5fb7fe7be4319d4

Files changed

index.jschanged
static/styles.csschanged
index.jsView
@@ -17,19 +17,33 @@
1717 var schemas = require('ssb-msg-schemas')
1818 var Issues = require('ssb-issues')
1919 var paramap = require('pull-paramap')
2020
21+var blockRenderer = new marked.Renderer()
22+blockRenderer.urltransform = function (url) {
23+ if (ref.isLink(url))
24+ return encodeLink(url)
25+ return url
26+}
27+
2128 marked.setOptions({
2229 gfm: true,
2330 mentions: true,
2431 tables: true,
2532 breaks: true,
2633 pedantic: false,
2734 sanitize: true,
2835 smartLists: true,
29- smartypants: false
36+ smartypants: false,
37+ renderer: blockRenderer
3038 })
3139
40+function markdown(text) {
41+ if (!text) return ''
42+ if (typeof text != 'string') text = String(text)
43+ return marked(text)
44+}
45+
3246 function parseAddr(str, def) {
3347 if (!str) return def
3448 var i = str.lastIndexOf(':')
3549 if (~i) return {host: str.substr(0, i), port: str.substr(i+1)}
@@ -40,8 +54,12 @@
4054 function flattenPath(parts) {
4155 return '/' + parts.map(encodeURIComponent).join('/')
4256 }
4357
58+function encodeLink(url) {
59+ return '/' + encodeURIComponent(url)
60+}
61+
4462 function link(parts, text, raw) {
4563 var href = flattenPath(parts)
4664 if (text == null) text = parts[parts.length-1]
4765 if (!raw) text = escapeHTML(text)
@@ -357,9 +375,9 @@
357375 title: data.title,
358376 text: data.text
359377 }, function (err, issue) {
360378 if (err) return cb(null, serveError(err))
361- cb(null, serveRedirect('/' + encodeURIComponent(issue.id)))
379+ cb(null, serveRedirect(encodeLink(issue.id)))
362380 })
363381
364382 default:
365383 cb(null, servePlainError(400, 'What are you trying to do?'))
@@ -891,9 +909,9 @@
891909 readOnce(function (cb) {
892910 pull(obj.read, pull.collect(function (err, bufs) {
893911 if (err) return cb(err)
894912 var buf = Buffer.concat(bufs, obj.length)
895- cb(null, marked(buf.toString()))
913+ cb(null, markdown(buf.toString()))
896914 }))
897915 })
898916 : cat([
899917 pull.once('<pre>'),
@@ -1117,11 +1135,10 @@
11171135 pull(
11181136 issues.createFeedStream({ project: repo.id }),
11191137 pull.map(function (issue) {
11201138 numIssues++
1121- var issueHref = '/' + encodeURIComponent(issue.id)
11221139 return '<section class="collapse">' +
1123- '<a href="' + issueHref + '">' +
1140+ '<a href="' + encodeLink(issue.id) + '">' +
11241141 escapeHTML(issue.title) +
11251142 '<span class="issue-info">' +
11261143 new Date(issue.created_at).toLocaleString() +
11271144 '</span>' +
@@ -1168,9 +1185,9 @@
11681185 var authorLink = link([issue.author], authorName)
11691186 cb(null,
11701187 authorLink + ' opened this issue on ' + timestamp(issue.created_at) +
11711188 '<hr/>' +
1172- (issue.text ? marked(issue.text) : '') +
1189+ markdown(issue.text) +
11731190 '</section>')
11741191 })
11751192 }),
11761193 // render posts and edits
@@ -1194,9 +1211,9 @@
11941211 authorLink +
11951212 (changed == null ? '' : ' ' + (
11961213 changed ? 'reopened this issue' : 'closed this issue')) +
11971214 ' &middot; ' + msgTimeLink +
1198- marked(c.text) +
1215+ markdown(c.text) +
11991216 '</section>'
12001217 } else {
12011218 var text = c.text || (c.type + ' ' + msg.key)
12021219 return '<section class="collapse mention-preview">' +
static/styles.cssView
@@ -231,7 +231,7 @@
231231 }
232232
233233 .mention-preview {
234234 overflow: hidden;
235- white-space: pre;
235+ white-space: nowrap;
236236 text-overflow: ellipsis;
237237 }

Built with git-ssb-web