aboutsummaryrefslogtreecommitdiff
path: root/lib/serve.js
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2018-07-26 11:57:07 -1000
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2018-07-26 12:03:40 -1000
commitcab781470c10d358f770475c7b25d87e29f614bb (patch)
treed4b68301107ae9a21023cbefb94dbc8cf0d1e38f /lib/serve.js
parent09f70e5f94777bbe5a18d2980ca221360642a970 (diff)
downloadpatchfoo-cab781470c10d358f770475c7b25d87e29f614bb.tar.gz
patchfoo-cab781470c10d358f770475c7b25d87e29f614bb.zip
Render post edits and diffs
Diffstat (limited to 'lib/serve.js')
-rw-r--r--lib/serve.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/serve.js b/lib/serve.js
index db99b49..b0e1267 100644
--- a/lib/serve.js
+++ b/lib/serve.js
@@ -376,6 +376,7 @@ Serve.prototype.path = function (url) {
case '/npm-readme': return this.npmReadme(m[2])
case '/npm-registry': return this.npmRegistry(m[2])
case '/markdown': return this.markdown(m[2])
+ case '/edit-diff': return this.editDiff(m[2])
case '/zip': return this.zip(m[2])
case '/web': return this.web(m[2])
}
@@ -3466,3 +3467,79 @@ Serve.prototype.emojis = function (path) {
this.respondSink(200)
)
}
+
+Serve.prototype.editDiff = function (url) {
+ var self = this
+ var id
+ try {
+ id = decodeURIComponent(url.substr(1))
+ } catch(err) {
+ return ph('div', u.renderError(err).outerHTML)
+ }
+ return pull(
+ ph('section', {}, [
+ 'diff: ',
+ ph('a', {href: self.app.render.toUrl(id)}, id.substr(0, 8) + '…'),
+ u.readNext(function (cb) {
+ self.getMsgDecryptedMaybeOoo(id, function (err, msg) {
+ if (err) return cb(null, pull.once(u.renderError(err).outerHTML))
+ var c = msg.value.content || {}
+ self.getMsgDecryptedMaybeOoo(c.updated, function (err, oldMsg) {
+ if (err) return cb(null, pull.once(u.renderError(err).outerHTML))
+ cb(null, self.postEditDiffTable(oldMsg, msg))
+ })
+ })
+ })
+ ]),
+ self.wrapPage('diff: ' + id),
+ self.respondSink(200)
+ )
+}
+
+Serve.prototype.postEditDiffTable = function (oldMsg, newMsg) {
+ var oldC = oldMsg.value.content || {}
+ var newC = newMsg.value.content || {}
+ var diff = Diff.structuredPatch('', '', String(oldC.text), String(newC.text))
+ var self = this
+ return pull(
+ ph('table', [
+ pull(
+ pull.values(diff.hunks),
+ pull.map(function (hunk) {
+ var oldLine = hunk.oldStart
+ var newLine = hunk.newStart
+ return [
+ ph('tr', [
+ ph('td', {colspan: 2}),
+ ph('td', ph('pre',
+ '@@ -' + oldLine + ',' + hunk.oldLines + ' ' +
+ '+' + newLine + ',' + hunk.newLines + ' @@'))
+ ]),
+ pull(
+ pull.values(hunk.lines),
+ pull.map(function (line) {
+ var s = line[0]
+ if (s == '\\') return
+ var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++]
+ return [
+ ph('tr', {
+ class: s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : ''
+ }, [
+ lineNums.map(function (num, i) {
+ return ph('td', String(num))
+ }),
+ ph('td', [
+ ph('code', s),
+ u.unwrapP(self.app.render.markdown(line.substr(1),
+ s == '-' ? oldC.mentions : newC.mentions))
+ ])
+ ])
+ ]
+ })
+ )
+ ]
+ })
+ )
+ ])
+ )
+}