Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 7cb1e34

Browse files
committed
fix: Wrap modified tag scope
1 parent dfd603f commit 7cb1e34

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/style.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
background-color: rgba(255, 0, 0, .1);
6060

6161
span.modified {
62-
background-color: rgba(255, 0, 0, .2);
62+
background-color: rgba(255, 0, 0, .15);
6363
}
6464

6565
code::before {
@@ -71,7 +71,7 @@
7171
background-color: rgba(0, 255, 128, .1);
7272

7373
span.modified {
74-
background-color: rgba(0, 255, 128, .2);
74+
background-color: rgba(0, 255, 128, .15);
7575
}
7676

7777
code:before {

src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ const setHighlightCode = ({ highlightCode, language, code }: { highlightCode: Re
224224
let pureElement = document.createElement('div')
225225
pureElement.innerHTML = hljs.highlight(language, pureCode).value // Highlight DOM without modified tags
226226

227+
// Modified span is created per highlight operator and causes it to continue
228+
let innerModifiedTag = false
229+
227230
const diffElements = (node: HTMLElement) => {
228231
node.childNodes.forEach(child => {
229232
if (child.nodeType === 1) {
@@ -237,15 +240,21 @@ const setHighlightCode = ({ highlightCode, language, code }: { highlightCode: Re
237240
let oldContent = child.textContent
238241
let newContent = ''
239242

243+
if (innerModifiedTag) { // If it continues within the modified range
244+
newContent = newContent + MODIFIED_START_TAG
245+
}
246+
240247
while (oldContent.length) {
241248
if (originalCode.startsWith(MODIFIED_START_TAG)) { // Add modified start tag
242249
originalCode = originalCode.slice(MODIFIED_START_TAG.length)
243250
newContent = newContent + MODIFIED_START_TAG
251+
innerModifiedTag = true // Start modified
244252
continue
245253
}
246254
if (originalCode.startsWith(MODIFIED_CLOSE_TAG)) { // Add modified close tag
247255
originalCode = originalCode.slice(MODIFIED_CLOSE_TAG.length)
248256
newContent = newContent + MODIFIED_CLOSE_TAG
257+
innerModifiedTag = false // End modified
249258
continue
250259
}
251260

@@ -259,6 +268,10 @@ const setHighlightCode = ({ highlightCode, language, code }: { highlightCode: Re
259268
oldContent = oldContent.slice(nextDiffsLength)
260269
}
261270

271+
if (innerModifiedTag) { // If the loop is finished without a modified close, it is still within the modified range.
272+
newContent = newContent + MODIFIED_CLOSE_TAG
273+
}
274+
262275
child.textContent = newContent // put as entity code because change textContent
263276
}
264277
})

0 commit comments

Comments
 (0)