Fix the error with newlines getting inserted in the middle of a line

383520cabadcd413237e14e19fc8df2dd8396a4b

Tucker McKnight <tmcknight@instructure.com> | Sun Mar 22 2026

Fix the error with newlines getting inserted in the middle of a line

When keeping the before/after lines matched up, newlines are now only
inserted after an existing newline, or at the beginning of the diff.
They should not be inserted in the middle of a line anymore, which was
adding a newline where one does not actually existin the original text.
main.ts:137
Before
136
137
138
139
140
141

142

143


144


145
146

  eleventyConfig.addFilter("lineNumbers", (code: string) => {
    const lines = code.split('\n')
    console.log(lines)
    const numLines = lines.length
    const lineNumbers = []
⁣
    for (let i = 0; i < numLines - 1; i++) {
⁣
      const lineNum = lines[i].includes('\u{2063}') ? null : i
⁣
⁣
      lineNumbers.push(lineNum)
⁣
⁣
    }

    return lineNumbers
After
136
137
138

139
140
141
142
143
144
145
146
147
148
149
150
151

  eleventyConfig.addFilter("lineNumbers", (code: string) => {
    const lines = code.split('\n')
⁣
    const numLines = lines.length
    const lineNumbers = []
    let printedLineNumber = 0
    for (let i = 0; i < numLines - 1; i++) {
      if (lines[i].includes('\u{2063}')) {
        lineNumbers.push(null)
      }
      else {
        lineNumbers.push(printedLineNumber)
        printedLineNumber++
      }
    }

    return lineNumbers
src/helpers.ts:36
Before
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
      }
      const lastHunk = lines.slice(previousHunk, hunkEndIndex)

      // TODO: this change I've made doesn't quite work. Need to make it
      // skip lines in the other diff when adding lines to just one diff.

      lastHunk.forEach((line) => {
        if (line.startsWith(' ')) {
          line = line.replace(' ', '')
        }
        else if (line.startsWith('-')) {
          line = line.replace('-', '')
        }
        else if (line.startsWith('+')) {
          line = line.replace('+', '')
        }
      })

      let lastHunkBefore = lastHunk.filter((line) => {
        return line.startsWith("-") || line.startsWith(" ")
      }).map((str) => {
After
35
36
37















38
39
      }
      const lastHunk = lines.slice(previousHunk, hunkEndIndex)

⁣
⁣
⁣
⁣
⁣
⁣
⁣
⁣
⁣
⁣
⁣
⁣
⁣
⁣
⁣
      let lastHunkBefore = lastHunk.filter((line) => {
        return line.startsWith("-") || line.startsWith(" ")
      }).map((str) => {
src/helpers.ts:65
Before
64
65
66
67
68
69
        if (str.startsWith(' ')) { return str.replace(" ", "") }
      }).join("\n")

      const changeObject = Diff.diffLines(lastHunkBefore, lastHunkAfter)
      let beforeText = ""
      let afterText = ""
After
64
65
66
67
68
69
        if (str.startsWith(' ')) { return str.replace(" ", "") }
      }).join("\n")

      const changeObject = Diff.diffWordsWithSpace(lastHunkBefore, lastHunkAfter)
      let beforeText = ""
      let afterText = ""
src/helpers.ts:79
Before
78
79
80



81
82
83
84
85
86



87
88
89
        if (obj.added) {
          afterText = afterText + "<mark>" + escape(obj.value) + "</mark>"
          if (numNewLines > 0) {
⁣
⁣
⁣
            beforeText = beforeText + (new Array(numNewLines).fill('\u{2063}', 0).join('\n'))
          }
        }
        if (obj.removed) {
          beforeText = beforeText + "<mark>" + escape(obj.value) + "</mark>"
          if (numNewLines > 0) {
⁣
⁣
⁣
            afterText = afterText + (new Array(numNewLines).fill('\u{2063}', 0).join('\n'))
          }
        }
      })
After
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        if (obj.added) {
          afterText = afterText + "<mark>" + escape(obj.value) + "</mark>"
          if (numNewLines > 0) {
            let insertAt = beforeText.lastIndexOf("\n")
            insertAt = insertAt === -1 ? 0 : insertAt

            beforeText = beforeText.slice(0, insertAt + 1) + (new Array(numNewLines).fill('\u{2063}\n', 0).join('')) + beforeText.slice(insertAt + 1)
          }
        }
        if (obj.removed) {
          beforeText = beforeText + "<mark>" + escape(obj.value) + "</mark>"
          if (numNewLines > 0) {
            let insertAt = afterText.lastIndexOf("\n")
            insertAt = insertAt === -1 ? 0 : insertAt

            afterText = afterText.slice(0, insertAt + 1) + (new Array(numNewLines).fill('\u{2063}\n', 0).join('')) + afterText.slice(insertAt + 1)
          }
        }
      })