Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve copy/paste of diff code blocks #132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Renderers/CodeNodeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ public function render(): string
$highlightedCode = preg_replace('/^C:\\\&gt; /m', '<span class="hljs-prompt">C:\&gt; </span>', $highlightedCode);
}

if ('diff' === $language) {
// remove the '+' and '-' signs so they can be added with css
$highlightedCode = str_replace([
'<span class="hljs-deletion">-',
'<span class="hljs-addition">+',
], [
'<span class="hljs-deletion"> ',
'<span class="hljs-addition"> ',
], $highlightedCode);
}

$numOfLines = \count(preg_split('/\r\n|\r|\n/', $highlightedCode));
$lineNumbers = implode("\n", range(1, $numOfLines));

Expand Down
13 changes: 13 additions & 0 deletions src/Templates/rtd/assets/css/highlightjs.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ Visual Studio-like style based on original C# coloring by Jason Diamond <jason@d
.hljs-strong {
font-weight: bold;
}

.hljs-addition::before,
.hljs-deletion::before {
position: absolute;
}

.hljs-addition::before {
content: '+';
}

.hljs-deletion::before {
content: '-';
}
16 changes: 8 additions & 8 deletions tests/fixtures/expected/blocks/code-blocks/diff.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
5</pre>
<pre class="codeblock-code">
<code>
<span class="hljs-addition">+ Added line</span>
<span class="hljs-deletion">- Removed line</span>
<span class="hljs-addition"> Added line</span>
<span class="hljs-deletion"> Removed line</span>
Normal line
<span class="hljs-deletion">- Removed line</span>
<span class="hljs-addition">+ Added line</span>
<span class="hljs-deletion"> Removed line</span>
<span class="hljs-addition"> Added line</span>
</code>
</pre>
</div>
Expand All @@ -28,11 +28,11 @@
<pre class="codeblock-code">
<code>
Normal line
<span class="hljs-addition">+ Added line</span>
<span class="hljs-deletion">- Removed line</span>
<span class="hljs-addition"> Added line</span>
<span class="hljs-deletion"> Removed line</span>
Normal line
<span class="hljs-deletion">- Removed line</span>
<span class="hljs-addition">+ Added line</span>
<span class="hljs-deletion"> Removed line</span>
<span class="hljs-addition"> Added line</span>
</code>
</pre>
</div>
Expand Down