Skip to content

Commit

Permalink
fix: collapsing lines with plaintext lang with grid true (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks authored Jan 16, 2024
1 parent 8d5fcab commit 20159e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { CharsHighlighterOptions } from './types';
import type { Highlighter, CodeToHastOptions } from 'shikiji';
import type { Transformer } from 'unified';
import { visit } from 'unist-util-visit';
import { toString as hastToString } from 'hast-util-to-string';
import rangeParser from 'parse-numeric-range';
import { getHighlighter as defaultGetHighlighter } from 'shikiji';
import { unified } from 'unified';
Expand Down Expand Up @@ -442,7 +443,7 @@ export default function rehypePrettyCode(
Array.isArray(element.properties?.className) &&
element.properties?.className?.[0] === 'line'
) {
if (grid && element.children.length === 0) {
if (grid && hastToString(element) === '') {
element.children = [{ type: 'text', value: ' ' }];
}

Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/unknownLanguage.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unknown language

```console
test
```abcdefg
line 1
line 2
```
6 changes: 4 additions & 2 deletions test/results/unknownLanguage.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ <h1>Unknown language</h1>
<pre
style="background-color: #24292e; color: #e1e4e8"
tabindex="0"
data-language="console"
data-language="abcdefg"
data-theme="github-dark"
><code data-language="console" data-theme="github-dark" style="display: grid;"><span data-line=""><span style="color:#79B8FF">test</span></span></code></pre>
><code data-language="abcdefg" data-theme="github-dark" style="display: grid;"><span data-line=""><span>line 1</span></span>
<span data-line=""> </span>
<span data-line=""><span>line 2</span></span></code></pre>
</figure>

0 comments on commit 20159e2

Please sign in to comment.