Skip to content

Commit 8ee331a

Browse files
Add language switcher plugin (#112)
Co-authored-by: jer3m01 <jer3m01@jer3m01.com>
1 parent cfe8016 commit 8ee331a

File tree

19 files changed

+1311
-36
lines changed

19 files changed

+1311
-36
lines changed

dev/src/routes/ec.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ Hey I'm A
106106
:::tab[b.tsx]
107107
Hi this is B
108108
:::
109-
::::
109+
::::

docs/app.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export default defineConfig(
2727
"Fully featured, fully customisable static site generation for SolidStart",
2828
issueAutolink: "https://github.com/kobaltedev/solidbase/issues/:issue",
2929
lang: "en",
30+
markdown: {
31+
expressiveCode: {
32+
languageSwitcher: true,
33+
},
34+
},
3035
locales: {
3136
fr: {
3237
label: "Français",
@@ -128,6 +133,10 @@ export default defineConfig(
128133
title: "Markdown Extensions",
129134
link: "/markdown",
130135
},
136+
{
137+
title: "Language Switcher",
138+
link: "/language-switcher",
139+
},
131140
{
132141
title: "Internationalisation",
133142
link: "/i18n",
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.

docs/src/routes/guide/structure.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ The basic structure looks like:
5656
└─ app.config.ts
5757
```
5858

59-
Full documentation and reference can be found in the [SolidStart docs](https://docs.solidjs.com/solid-start/building-your-application/routing);
59+
Full documentation and reference can be found in the [SolidStart docs](https://docs.solidjs.com/solid-start/building-your-application/routing).

docs/src/routes/reference/runtime-api.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,13 @@ function getLocaleLink(locale: ResolvedLocale<any>): `/${string}`;
114114
```
115115

116116
Returns the root path for the provided locale.
117+
118+
## Preferred Language
119+
120+
### `usePreferredLanguage`
121+
122+
Manages the user's preferred language for TypeScript/JavaScript toggles in code blocks.
123+
124+
```ts
125+
function usePreferredLanguage(): [Accessor<"ts" | "js">, Setter<"ts" | "js">];
126+
```

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
"esbuild": "^0.25.2",
105105
"glob": "^11.0.1",
106106
"mdast": "^3.0.0",
107-
"prettier": "4.0.0-alpha.10",
108107
"resolve": "^1.22.10",
109108
"solid-js": "^1.9.5",
110109
"tsx": "^4.19.3",
@@ -144,15 +143,20 @@
144143
"@solidjs/meta": "^0.29.4",
145144
"@solidjs/router": "^0.15.3",
146145
"cross-spawn": "^7.0.3",
146+
"diff": "^8.0.2",
147147
"esast-util-from-js": "^2.0.1",
148148
"estree-util-value-to-estree": "^3.1.2",
149+
"expressive-code": "^0.41.3",
149150
"expressive-code-twoslash": "=0.4.0",
150151
"gray-matter": "^4.0.3",
151152
"hastscript": "^9.0.0",
153+
"magic-string": "^0.30.19",
152154
"mdast-util-find-and-replace": "^3.0.1",
153155
"mdast-util-mdx-jsx": "^3.2.0",
154156
"mdast-util-to-string": "^4.0.0",
155157
"mdast-util-toc": "^7.1.0",
158+
"parse-numeric-range": "^1.3.0",
159+
"prettier": "4.0.0-alpha.10",
156160
"rehype-autolink-headings": "^7.1.0",
157161
"rehype-expressive-code": "^0.40.2",
158162
"rehype-raw": "^7.0.0",
@@ -161,6 +165,7 @@
161165
"remark-frontmatter": "^5.0.0",
162166
"remark-gfm": "^4.0.0",
163167
"solid-mdx": "^0.0.7",
168+
"source-map": "^0.7.6",
164169
"toml": "^3.0.0",
165170
"typescript": "^5.6.3",
166171
"unified": "^11.0.5",

0 commit comments

Comments
 (0)