diff options
Diffstat (limited to 'lib/render.js')
-rw-r--r-- | lib/render.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/render.js b/lib/render.js index 5d8fd66..7c2cf64 100644 --- a/lib/render.js +++ b/lib/render.js @@ -13,6 +13,7 @@ var multicb = require('multicb') var RenderMsg = require('./render-msg') var Highlight = require('highlight.js') var md = require('ssb-markdown') +var Diff = require('diff') module.exports = Render @@ -719,3 +720,46 @@ Render.prototype.friendsList = function (prefix) { } ) } + +Render.prototype.textEditDiffTable = function (oldMsg, newMsg) { + var oldC = oldMsg && oldMsg.value.content || {} + var newC = newMsg && newMsg.value.content || {} + var oldText = String(oldC.text || oldC.description || '') + var newText = String(newC.text || newC.description || '') + var diff = Diff.structuredPatch('', '', oldText, newText) + var self = this + return h('table', [ + diff.hunks.map(function (hunk) { + var oldLine = hunk.oldStart + var newLine = hunk.newStart + return [ + h('tr', [ + h('td', {colspan: 2}), + h('td', h('pre', + '@@ -' + oldLine + ',' + hunk.oldLines + ' ' + + '+' + newLine + ',' + hunk.newLines + ' @@')) + ]), + hunk.lines.map(function (line) { + var s = line[0] + if (s == '\\') return + var lineNums = [s == '+' ? '' : oldLine++, s == '-' ? '' : newLine++] + return [ + h('tr', { + class: s == '+' ? 'diff-new' : s == '-' ? 'diff-old' : '' + }, [ + lineNums.map(function (num, i) { + return h('td', String(num)) + }), + h('td', {innerHTML: + h('code', s).outerHTML + + u.unwrapP(self.markdown(line.substr(1), + s == '-' ? oldC.mentions : newC.mentions)) + }) + ]) + ] + }) + ] + }) + ]) +} + |