Remove trailing new line from diff/commit search results
Created by: attfarhan
There's currently a trailing new line added to the end of every diff and commit search result:
Instead of just replacing the content of the diff/commit, I use a regex to match the content and optionally any trailing new lines.
This is related to https://github.com/sourcegraph/sourcegraph/pull/2594, where changing the markdown renderer caused the rendered markdown output to change, throwing off some of the syntax highlighting logic we had.
@felixfbecker to address your comment on my fix yesterday (https://github.com/sourcegraph/sourcegraph/pull/2594#pullrequestreview-210969855), the change in the markdown renderer that we use means that the rendered markdown is now wrapped in a <div>
and <pre>
tag, instead of just a <pre>
tag as it was previously. This results in a trailing new line when we call body.innerText
since <div>
is a block element.
So, the syntax highlighting broke because what we got from body.innerText
was the code content + a trailing new line, which does not actually exist in the rendered markdown HTML, so replacing the raw content with the HTML failed. What exists in the rendered markdown HTML is just the code content.
The solution to use .trim()
works because it leaves us with just the code content. When we replace the contents wit HTML, if there is leading white space that matters, e.g. indentation, that will remain untouched since we just replace the trimmed code content. It could be a problem if the leading whitespace should be colored a certain way (e.g. red background color for a diff), but I can't think of a real world case where that's actually a possibility. Again, I think we all agree that https://github.com/sourcegraph/sourcegraph/issues/2144 would be the best solution, but this should hold up in the meantime.