Repository: shikijs/shiki
Pull request #808 (2024-10-12 15:03:33)

fix(rehype): support special languages
<!-- DO NOT IGNORE THE TEMPLATE!

Thank you for contributing!

Before submitting the PR, please make sure you do the following:

- Read the [Contributing Guide](https://github.com/shikijs/shiki/blob/main/CONTRIBUTING.md).
- Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
- Provide a description in this PR that addresses **what** the PR is solving, or reference the issue that it solves (e.g. `fixes #123`).
- Ideally, include relevant tests that fail without this PR but pass with it.

-->

### Description

Currently, special languages like `plaintext` and `txt` are treated as normal languages.

When `lazy` is not enabled, codeblocks with special languages will be ignored because `highlighter.getLoadedLanguages()` will never contain these languages.

This PR adds a check for `isSpecialLang` to allow styling codeblocks with special languages.

### Linked Issues

#805 

### Additional context

<!-- e.g. is there anything you'd like reviewers to focus on? -->


netlify[bot]: ### <span aria-hidden="true">👷</span> Deploy Preview for *shiki-matsu* processing.


|  Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | 32745e71dda8d62bcddd3691e2668627ef0e0a48 |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/sites/shiki-matsu/deploys/670a8fc74d213a00082186df |
netlify[bot]: ### <span aria-hidden="true">👷</span> Deploy Preview for *shiki-next* processing.


|  Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | 32745e71dda8d62bcddd3691e2668627ef0e0a48 |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/sites/shiki-next/deploys/670a8fc759ce9a0008bae3bb |

Patch:
diff --git a/packages/rehype/src/core.ts b/packages/rehype/src/core.ts
index 15c439b0..3a838a5d 100644
--- a/packages/rehype/src/core.ts
+++ b/packages/rehype/src/core.ts
@@ -6,6 +6,7 @@ import type { Root } from 'hast'
 import type { Transformer } from 'unified'
 import type { RehypeShikiHandler } from './handlers'
 import type { RehypeShikiCoreOptions } from './types'
+import { isSpecialLang } from 'shiki/core'
 import { visit } from 'unist-util-visit'
 import { InlineCodeHandlers, PreHandler } from './handlers'
 
@@ -92,7 +93,7 @@ function rehypeShikiFromHighlighter(
       if (!lang)
         return defaultLanguage
 
-      if (highlighter.getLoadedLanguages().includes(lang))
+      if (highlighter.getLoadedLanguages().includes(lang) || isSpecialLang(lang))
         return lang
 
       if (lazy) {


Test patch:
diff --git a/packages/rehype/test/fixtures/a.core.out.html b/packages/rehype/test/fixtures/a.core.out.html
index d3bde0c5..29dc0724 100644
--- a/packages/rehype/test/fixtures/a.core.out.html
+++ b/packages/rehype/test/fixtures/a.core.out.html
@@ -4,4 +4,5 @@
 <span class="line"></span>
 <span class="line highlighted"><span style="color:#AB5959">const</span><span style="color:#B07D48"> a</span><span style="color:#999999"> =</span><span style="color:#2F798A"> 1</span></span>
 <span class="line highlighted"><span style="color:#B07D48">console</span><span style="color:#999999">.</span><span style="color:#59873A">log</span><span style="color:#999999">(</span><span style="color:#B07D48">a</span><span style="color:#999999">)</span></span></code></pre>
-<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code><span class="line"><span>Should fallback to default language</span></span></code></pre>
\ No newline at end of file
+<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code><span class="line"><span>Should fallback to default language</span></span></code></pre>
+<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code><span class="line"><span>Special languages should be styled</span></span></code></pre>
\ No newline at end of file
diff --git a/packages/rehype/test/fixtures/a.md b/packages/rehype/test/fixtures/a.md
index b9894b04..244fc374 100644
--- a/packages/rehype/test/fixtures/a.md
+++ b/packages/rehype/test/fixtures/a.md
@@ -11,4 +11,8 @@ console.log(a)
 
 ```
 Should fallback to default language
+```
+
+```plaintext
+Special languages should be styled
 ```
\ No newline at end of file
diff --git a/packages/rehype/test/fixtures/a.out.html b/packages/rehype/test/fixtures/a.out.html
index 0484fac4..67530087 100644
--- a/packages/rehype/test/fixtures/a.out.html
+++ b/packages/rehype/test/fixtures/a.out.html
@@ -4,4 +4,5 @@
 <span class="line"></span>
 <span class="line highlighted"><span style="color:#AB5959">const</span><span style="color:#B07D48"> </span><span style="color:#B07D48" class="highlighted-word">a</span><span style="color:#999999"> =</span><span style="color:#2F798A"> 1</span></span>
 <span class="line highlighted"><span style="color:#B07D48">console</span><span style="color:#999999">.</span><span style="color:#59873A">log</span><span style="color:#999999">(</span><span style="color:#B07D48" class="highlighted-word">a</span><span style="color:#999999">)</span></span></code></pre>
-<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code><span class="line"><span>Should fallback to default language</span></span></code></pre>
\ No newline at end of file
+<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code><span class="line"><span>Should fallback to default language</span></span></code></pre>
+<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code><span class="line"><span>Special languages should be styled</span></span></code></pre>
\ No newline at end of file