|
| 1 | +--- |
| 2 | +title: Language Switcher |
| 3 | +--- |
| 4 | + |
| 5 | +# Language Switcher |
| 6 | + |
| 7 | +The Language Switcher plugin allows you to display both TypeScript and JavaScript versions of code blocks with a toggle button. |
| 8 | +It converts TypeScript and TSX code blocks to their JavaScript equivalents, making your documentation accessible to developers who prefer JavaScript. |
| 9 | + |
| 10 | +## Configuration |
| 11 | + |
| 12 | +The plugin is not enabled by default. |
| 13 | +To enable it, configure the `languageSwitcher` option in your SolidBase config: |
| 14 | + |
| 15 | +### Basic Setup |
| 16 | + |
| 17 | +```ts title="app.config.ts" |
| 18 | +import { defineConfig } from "@solidjs/start/config"; |
| 19 | +import { withSolidBase } from "@kobalte/solidbase/config"; |
| 20 | + |
| 21 | +export default defineConfig( |
| 22 | + withSolidBase( |
| 23 | + /* your SolidStart config */, |
| 24 | + { |
| 25 | + markdown: { |
| 26 | + expressiveCode: { |
| 27 | + languageSwitcher: true, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + ), |
| 32 | +); |
| 33 | +``` |
| 34 | + |
| 35 | +### Advanced Configuration |
| 36 | + |
| 37 | +For custom options, pass an object: |
| 38 | + |
| 39 | +```ts title="app.config.ts" |
| 40 | +import { defineConfig } from "@solidjs/start/config"; |
| 41 | +import { withSolidBase } from "@kobalte/solidbase/config"; |
| 42 | + |
| 43 | +export default defineConfig( |
| 44 | + withSolidBase( |
| 45 | + /* your SolidStart config */, |
| 46 | + { |
| 47 | + markdown: { |
| 48 | + expressiveCode: { |
| 49 | + languageSwitcher: { |
| 50 | + showToggleButton: true, |
| 51 | + formatter: async (jsCode, isJsx) => { |
| 52 | + // Custom formatting |
| 53 | + return code; |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + ), |
| 60 | +); |
| 61 | +``` |
| 62 | + |
| 63 | +### Supported Languages |
| 64 | + |
| 65 | +The plugin converts these TypeScript variants to JavaScript: |
| 66 | + |
| 67 | +- `typescript` / `ts` → `javascript` / `js` |
| 68 | +- `tsx` → `jsx` |
| 69 | + |
| 70 | +JavaScript code blocks (`js`, `javascript`, `jsx`) are not converted. |
| 71 | + |
| 72 | +### Meta Options |
| 73 | + |
| 74 | +You can control the plugin behavior using code block meta options: |
| 75 | + |
| 76 | +#### Disable for Specific Blocks |
| 77 | + |
| 78 | +````md |
| 79 | +```typescript withoutLanguageSwitcher |
| 80 | +// This block won't have the toggle |
| 81 | +``` |
| 82 | +```` |
| 83 | + |
| 84 | +#### Line Markers |
| 85 | + |
| 86 | +The plugin automatically preserves line markers. |
| 87 | + |
| 88 | +## Options |
| 89 | + |
| 90 | +### `showToggleButton` |
| 91 | + |
| 92 | +- **Type:** `boolean` |
| 93 | +- **Default:** `true` |
| 94 | +- **Description:** Whether to show the toggle button in the code block header. |
| 95 | + |
| 96 | +### `formatter` |
| 97 | + |
| 98 | +- **Type:** `(jsCode: string, isJsx; boolean) => string | Promise<string>` |
| 99 | +- **Default:** Formatting with Prettier with default options |
| 100 | +- **Description:** A function to format the generated JavaScript code. |
0 commit comments