You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add support for custom regex engines with engine param (#100)
This PR adds support for custom regex engines in the highlighter factory functions. It:
- Adds an optional `engine` parameter to `createFullHighlighter` and `createWebHighlighter` functions
- Defaults to using the Oniguruma engine when no custom engine is provided
- Re-exports JavaScript regex engines from Shiki in the main bundles for convenience
- Adds the `engine` option to the `HighlighterOptions` type
These changes allow users to customize the regex engine used for syntax highlighting, providing more flexibility in different environments.
-**JavaScript Raw** - For [pre-compiled languages](https://shiki.style/guide/regex-engines#pre-compiled-languages), skips transpilation step for best performance
143
146
144
-
Unlike the Oniguruma engine, the JavaScript engine is [strict by default](https://shiki.style/guide/regex-engines#use-with-unsupported-languages). It will throw an error if it encounters an invalid Oniguruma pattern or a pattern that it cannot convert. If you want best-effort results for unsupported grammars, you can enable the forgiving option to suppress any conversion errors:
147
+
#### Using Engines with Full and Web Bundles
148
+
149
+
The full and web bundles use Oniguruma by default, but you can override this with the `engine` option:
// Component with JavaScript Raw engine (for pre-compiled languages)
164
+
// See https://shiki.style/guide/regex-engines#pre-compiled-languages
165
+
<ShikiHighlighter
166
+
language="typescript"
167
+
theme="github-dark"
168
+
engine={createJavaScriptRawEngine()}
169
+
>
170
+
{code}
171
+
</ShikiHighlighter>
172
+
```
173
+
174
+
#### Using Engines with Core Bundle
175
+
176
+
When using the core bundle, you must specify an engine:
177
+
178
+
```tsx
179
+
import {
180
+
createHighlighterCore,
181
+
createOnigurumaEngine,
182
+
createJavaScriptRegexEngine
183
+
} from'react-shiki/core';
184
+
185
+
const highlighter =awaitcreateHighlighterCore({
186
+
themes: [import('@shikijs/themes/nord')],
187
+
langs: [import('@shikijs/langs/typescript')],
188
+
engine: createJavaScriptRegexEngine() // or createOnigurumaEngine(import('shiki/wasm'))
189
+
});
190
+
```
191
+
192
+
#### Engine Options
193
+
194
+
The JavaScript RegExp engine is [strict by default](https://shiki.style/guide/regex-engines#use-with-unsupported-languages). For best-effort results with unsupported grammars, enable the `forgiving` option:
145
195
146
196
```tsx
147
197
createJavaScriptRegexEngine({ forgiving: true });
@@ -163,6 +213,7 @@ See [Shiki - RegExp Engines](https://shiki.style/guide/regex-engines) for more i
163
213
|`delay`|`number`|`0`| Delay between highlights (in milliseconds) |
164
214
|`customLanguages`|`array`|`[]`| Array of custom languages to preload |
165
215
|`langAlias`|`object`|`{}`| Map of language aliases |
216
+
|`engine`|`RegexEngine`| Oniguruma | RegExp engine for syntax highlighting (Oniguruma, JavaScript RegExp, or JavaScript Raw) |
166
217
|`showLineNumbers`|`boolean`|`false`| Display line numbers alongside code |
167
218
|`startingLineNumber`|`number`|`1`| Starting line number when line numbers are enabled |
168
219
|`transformers`|`array`|`[]`| Custom Shiki transformers for modifying the highlighting output |
0 commit comments