diff --git a/server/utils/__snapshots__/markdownDiff.test.js.snap b/server/utils/__snapshots__/markdownDiff.test.js.snap index c79df6ac74..746a9e01f0 100644 --- a/server/utils/__snapshots__/markdownDiff.test.js.snap +++ b/server/utils/__snapshots__/markdownDiff.test.js.snap @@ -1,7 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`should diff a complex document 1`] = ` -"
This is a second test paragraph. This is a second sentence.
+This is a another test paragraph. This is a another sentence.
+this is a codeblock
This is a new paragraph.
@@ -13,11 +17,10 @@ exports[`should diff a complex document 1`] = `This is a test paragraph
This is a second test paragraph. This is a second sentence.
This is a another test paragraph. This is a another sentence.
this is a codeblock
This is an info block
This is a placeholder
this is a highlight
same on both sides
same on both sides
same on both sides
" diff --git a/server/utils/markdownDiff.js b/server/utils/markdownDiff.js index 05f8d9ddf8..a6f8fe44a8 100644 --- a/server/utils/markdownDiff.js +++ b/server/utils/markdownDiff.js @@ -24,9 +24,14 @@ export default function markdownDiff( return ""; } - // split diff into individual lines and find the first and last changed tags - // so we can chop around those lines rather than return the entire document. - let lines = diffHtml.split("\n"); + // Split diff at paragraphs and find the first and last changed tags + // so we can chop around paragraphs rather than return the entire document. + // + // In an ideal world we'd use an AST here and parse that rather than be doing + // operations on strings. I hope this can be revisted in the future with an + // improved diffing library. + const newParagraph = /(?:^|\n)/; + let lines = diffHtml.split(newParagraph); const firstChangedLineIndex = findIndex( lines, @@ -37,10 +42,16 @@ export default function markdownDiff( (value) => value.includes("") || value.includes("") ); - return lines - .slice( - Math.max(0, firstChangedLineIndex - buffer), - Math.min(lines.length, lastChangedLineIndex + buffer) - ) - .join("\n"); + lines = lines.slice( + Math.max(0, firstChangedLineIndex - buffer), + Math.min(lines.length, lastChangedLineIndex + buffer) + ); + + if (!lines.length) { + return ""; + } + + return [firstChangedLineIndex > 0 ? "" : undefined, ...lines] + .filter((x) => x !== undefined) + .join("\n
"); } diff --git a/server/utils/markdownDiff.test.js b/server/utils/markdownDiff.test.js index 5f2f1338a9..789476d991 100644 --- a/server/utils/markdownDiff.test.js +++ b/server/utils/markdownDiff.test.js @@ -39,7 +39,10 @@ it("should return everything inserted when previously empty", async () => { expect(diff).toMatchSnapshot(); }); -it("should mark changed nodes", async () => { +it("should return empty for changed nodes", async () => { + // Note: This isn't ideal behavior, but it is current behavior. If the diffing + // library is improved then we could potentially render the old + new heading + // with ins/del tags as appropriate. const diff = markdownDiff("# Heading", "## Heading"); - expect(diff).toMatchSnapshot(); + expect(diff).toEqual(""); });