Commit 31c9fa39360b9c65460095352026a362be40c0ab
Render ssb links in markdown
Charles Lehner committed on 3/27/2016, 11:02:53 AMParent: 4158c83c87660fc6a59d13f7f5fb7fe7be4319d4
Files changed
index.js | changed |
static/styles.css | changed |
index.js | ||
---|---|---|
@@ -17,19 +17,33 @@ | ||
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 | +var blockRenderer = new marked.Renderer() | |
22 | +blockRenderer.urltransform = function (url) { | |
23 | + if (ref.isLink(url)) | |
24 | + return encodeLink(url) | |
25 | + return url | |
26 | +} | |
27 | + | |
21 | 28 | marked.setOptions({ |
22 | 29 | gfm: true, |
23 | 30 | mentions: true, |
24 | 31 | tables: true, |
25 | 32 | breaks: true, |
26 | 33 | pedantic: false, |
27 | 34 | sanitize: true, |
28 | 35 | smartLists: true, |
29 | - smartypants: false | |
36 | + smartypants: false, | |
37 | + renderer: blockRenderer | |
30 | 38 | }) |
31 | 39 | |
40 | +function markdown(text) { | |
41 | + if (!text) return '' | |
42 | + if (typeof text != 'string') text = String(text) | |
43 | + return marked(text) | |
44 | +} | |
45 | + | |
32 | 46 | function parseAddr(str, def) { |
33 | 47 | if (!str) return def |
34 | 48 | var i = str.lastIndexOf(':') |
35 | 49 | if (~i) return {host: str.substr(0, i), port: str.substr(i+1)} |
@@ -40,8 +54,12 @@ | ||
40 | 54 | function flattenPath(parts) { |
41 | 55 | return '/' + parts.map(encodeURIComponent).join('/') |
42 | 56 | } |
43 | 57 | |
58 | +function encodeLink(url) { | |
59 | + return '/' + encodeURIComponent(url) | |
60 | +} | |
61 | + | |
44 | 62 | function link(parts, text, raw) { |
45 | 63 | var href = flattenPath(parts) |
46 | 64 | if (text == null) text = parts[parts.length-1] |
47 | 65 | if (!raw) text = escapeHTML(text) |
@@ -357,9 +375,9 @@ | ||
357 | 375 | title: data.title, |
358 | 376 | text: data.text |
359 | 377 | }, function (err, issue) { |
360 | 378 | if (err) return cb(null, serveError(err)) |
361 | - cb(null, serveRedirect('/' + encodeURIComponent(issue.id))) | |
379 | + cb(null, serveRedirect(encodeLink(issue.id))) | |
362 | 380 | }) |
363 | 381 | |
364 | 382 | default: |
365 | 383 | cb(null, servePlainError(400, 'What are you trying to do?')) |
@@ -891,9 +909,9 @@ | ||
891 | 909 | readOnce(function (cb) { |
892 | 910 | pull(obj.read, pull.collect(function (err, bufs) { |
893 | 911 | if (err) return cb(err) |
894 | 912 | var buf = Buffer.concat(bufs, obj.length) |
895 | - cb(null, marked(buf.toString())) | |
913 | + cb(null, markdown(buf.toString())) | |
896 | 914 | })) |
897 | 915 | }) |
898 | 916 | : cat([ |
899 | 917 | pull.once('<pre>'), |
@@ -1117,11 +1135,10 @@ | ||
1117 | 1135 | pull( |
1118 | 1136 | issues.createFeedStream({ project: repo.id }), |
1119 | 1137 | pull.map(function (issue) { |
1120 | 1138 | numIssues++ |
1121 | - var issueHref = '/' + encodeURIComponent(issue.id) | |
1122 | 1139 | return '<section class="collapse">' + |
1123 | - '<a href="' + issueHref + '">' + | |
1140 | + '<a href="' + encodeLink(issue.id) + '">' + | |
1124 | 1141 | escapeHTML(issue.title) + |
1125 | 1142 | '<span class="issue-info">' + |
1126 | 1143 | new Date(issue.created_at).toLocaleString() + |
1127 | 1144 | '</span>' + |
@@ -1168,9 +1185,9 @@ | ||
1168 | 1185 | var authorLink = link([issue.author], authorName) |
1169 | 1186 | cb(null, |
1170 | 1187 | authorLink + ' opened this issue on ' + timestamp(issue.created_at) + |
1171 | 1188 | '<hr/>' + |
1172 | - (issue.text ? marked(issue.text) : '') + | |
1189 | + markdown(issue.text) + | |
1173 | 1190 | '</section>') |
1174 | 1191 | }) |
1175 | 1192 | }), |
1176 | 1193 | // render posts and edits |
@@ -1194,9 +1211,9 @@ | ||
1194 | 1211 | authorLink + |
1195 | 1212 | (changed == null ? '' : ' ' + ( |
1196 | 1213 | changed ? 'reopened this issue' : 'closed this issue')) + |
1197 | 1214 | ' · ' + msgTimeLink + |
1198 | - marked(c.text) + | |
1215 | + markdown(c.text) + | |
1199 | 1216 | '</section>' |
1200 | 1217 | } else { |
1201 | 1218 | var text = c.text || (c.type + ' ' + msg.key) |
1202 | 1219 | return '<section class="collapse mention-preview">' + |
Built with git-ssb-web