From 5de776689a7e9d2862fbdda1af7b6cd7adff787b Mon Sep 17 00:00:00 2001 From: codebymikey <9484406+codebymikey@users.noreply.github.com> Date: Tue, 10 Sep 2024 08:40:05 +0000 Subject: [PATCH] Add structured patch support to gh-pages Co-authored-by: Mark Amery --- index.html | 33 ++++++++++++++++++++++++++++++++- style.css | 4 ++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 3f703e67c..9e2e4de66 100644 --- a/index.html +++ b/index.html @@ -12,6 +12,7 @@

Diff

+ github.com/kpdecker/jsdiff @@ -31,8 +32,34 @@

Diff

var result = document.getElementById('result'); function changed() { - var diff = Diff[window.diffType](a.textContent, b.textContent); var fragment = document.createDocumentFragment(); + var diff; + if (window.diffType === 'diffPatch') { + // We contort the patch into a similar data structure to that returned by diffChars, + // diffWords, etc so that the same rendering code below can work on both. + var pastHunkHeader = false; + diff = Diff.createTwoFilesPatch('a.txt', 'b.txt', a.textContent, b.textContent) + .split('\n') + .map(function(entry) { + const result = { + value: entry + '\n', + }; + if (entry.startsWith('@@')) { + result.chunkHeader = true; + pastHunkHeader = true; + } else if (pastHunkHeader) { + if (entry.startsWith('-')) { + result.removed = true; + } else if (entry.startsWith('+')) { + result.added = true; + } + } + return result; + }); + } else { + diff = Diff[window.diffType](a.textContent, b.textContent); + } + for (var i=0; i < diff.length; i++) { if (diff[i].added && diff[i + 1] && diff[i + 1].removed) { @@ -48,6 +75,10 @@

Diff

} else if (diff[i].added) { node = document.createElement('ins'); node.appendChild(document.createTextNode(diff[i].value)); + } else if (diff[i].chunkHeader) { + node = document.createElement('span'); + node.setAttribute('class', 'chunk-header'); + node.appendChild(document.createTextNode(diff[i].value)); } else { node = document.createTextNode(diff[i].value); } diff --git a/style.css b/style.css index 37710f21c..105354439 100644 --- a/style.css +++ b/style.css @@ -42,6 +42,10 @@ ins { color: #406619; text-decoration: none; } +.chunk-header { + color: #8a008b; + text-decoration: none; +} #result { white-space: pre-wrap;