Repository: shikijs/shiki
Pull request #1018 (2025-05-15 15:11:09)

feat(markdown-it): add langAlias option
<!-- 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

Does what https://github.com/shikijs/shiki/pull/1015 did for Rehype, for markdown-it. Supports the documented `langAlias` option in the markdown-it plugin. I've used the same fixture input/output as the Rehype test case did, and I've emulated the same settings (why is the `transformerMetaHighlight`  included in all the other tests for no reason by the way?).

### Linked Issues

### 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-next* processing.


|  Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | e8a9912293e5d997cdb4c0fc7dab7aba08ca9ae0 |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/projects/shiki-next/deploys/6826041042f7bd00081db237 |
uncenter: Error seems unrelated?

```
11:11:32 AM: Failed during stage 'preparing repo': Error checking out submodules: fatal: No url found for submodule path 'packages/core/vendor/vscode-textmate' in .gitmodules
: exit status 128: fatal: No url found for submodule path 'packages/core/vendor/vscode-textmate' in .gitmodules
: exit status 128
11:11:32 AM: Error checking out submodules: fatal: No url found for submodule path 'packages/core/vendor/vscode-textmate' in .gitmodules
: exit status 128
11:11:32 AM: Failing build: Failed to prepare repo
```

Patch:
diff --git a/packages/markdown-it/src/index.ts b/packages/markdown-it/src/index.ts
index 7dff40e1..41dd60bc 100644
--- a/packages/markdown-it/src/index.ts
+++ b/packages/markdown-it/src/index.ts
@@ -13,16 +13,25 @@ export type MarkdownItShikiOptions = MarkdownItShikiSetupOptions & {
    * @default Object.keys(bundledLanguages)
    */
   langs?: Array<LanguageInput | BuiltinLanguage>
+
+  /**
+   * Alias of languages
+   * @example { 'my-lang': 'javascript' }
+   */
+  langAlias?: Record<string, string>
 }
 
 export default async function markdownItShiki(options: MarkdownItShikiOptions) {
   const themeNames = ('themes' in options
     ? Object.values(options.themes)
     : [options.theme]).filter(Boolean) as BuiltinTheme[]
+  const langs = options.langs || Object.keys(bundledLanguages)
+  const langAlias = options.langAlias || {}
 
   const highlighter = await createHighlighter({
     themes: themeNames,
-    langs: options.langs || Object.keys(bundledLanguages) as BuiltinLanguage[],
+    langs,
+    langAlias,
   })
 
   return function (markdownit: MarkdownIt) {


Test patch:
diff --git a/packages/markdown-it/test/fixtures/lang-alias.md b/packages/markdown-it/test/fixtures/lang-alias.md
new file mode 100644
index 00000000..16895d6b
--- /dev/null
+++ b/packages/markdown-it/test/fixtures/lang-alias.md
@@ -0,0 +1,7 @@
+```mylang
+console.log("Hi")
+```
+
+```mylang2
+console.log("Hi")
+```
\ No newline at end of file
diff --git a/packages/markdown-it/test/fixtures/lang-alias.out.html b/packages/markdown-it/test/fixtures/lang-alias.out.html
new file mode 100644
index 00000000..37d541d7
--- /dev/null
+++ b/packages/markdown-it/test/fixtures/lang-alias.out.html
@@ -0,0 +1,2 @@
+<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code class="language-mylang"><span class="line"><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:#B5695977">"</span><span style="color:#B56959">Hi</span><span style="color:#B5695977">"</span><span style="color:#999999">)</span></span></code></pre>
+<pre class="shiki vitesse-light" style="background-color:#ffffff;color:#393a34" tabindex="0"><code class="language-mylang2"><span class="line"><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:#B5695977">"</span><span style="color:#B56959">Hi</span><span style="color:#B5695977">"</span><span style="color:#999999">)</span></span></code></pre>
diff --git a/packages/markdown-it/test/index.test.ts b/packages/markdown-it/test/index.test.ts
index 0dad57c6..92a28526 100644
--- a/packages/markdown-it/test/index.test.ts
+++ b/packages/markdown-it/test/index.test.ts
@@ -64,3 +64,19 @@ it('run for default language', { timeout: 10_000 }, async () => {
 
   await expect(result).toMatchFileSnapshot('./fixtures/c.out.html')
 })
+
+it('lang-alias', async () => {
+  const md = MarkdownIt()
+  md.use(await Shiki({
+    langs: ['js'],
+    theme: 'vitesse-light',
+    langAlias: {
+      mylang: 'javascript',
+      mylang2: 'js', // nested alias
+    },
+  }))
+
+  const result = md.render(await fs.readFile(new URL('./fixtures/lang-alias.md', import.meta.url), 'utf-8'))
+
+  await expect(result).toMatchFileSnapshot('./fixtures/lang-alias.out.html')
+})