react-codemirror-runmode
v2.1.0
Published
Syntax highlighting for react, utilizing CodeMirror's parser
Maintainers
Readme
react-codemirror-runmode
Syntax highlighter for React, using CodeMirror 6. It automatically loads the language metadata and dynamically loads language parser modules based on the specified language.
Installation
npm install --save react-codemirror-runmodeUsage
import { oneDarkHighlightStyle } from '@codemirror/theme-one-dark'
import { Highlighter } from 'react-codemirror-runmode'
import React from 'react'
import ReactDOM from 'react-dom/client'
const code = 'const x = 123'
ReactDOM.createRoot(document.getElementById('app')!).render(
<Highlighter lang="js" theme={oneDarkHighlightStyle}>
{code}
</Highlighter>
)Theming
You can apply custom themes using CodeMirror's theme system. This component uses HighlightStyle objects to apply styles, which you can customize or replace.
Reference
Highlighter
Props:
lang:string- The name of the languagetheme:Highlighter- The highlight stylechildren:string- The code to highlightfallbackLanguage:Language- Optional fallback language to use if the specified language isn't foundlanguages:LanguageDescription[]- Optional custom list of language descriptions
highlightCode<o>(languageName, input, highlightStyle, fallbackLanguage?, languages?, callback): Promise<Output[]>
Parameters:
languageName:string- The name of the languageinput:string- The code to highlighthighlighter:Highlighter- The highlight stylefallbackLanguage:Language- Optional fallback language to use if the specified language isn't foundlanguages:LanguageDescription[]- Optional custom list of language descriptionscallback:(text: string, style: string | null, from: number, to: number) => Output)- A callback function that converts the parsed tokens
getCodeParser(languageName, defaultLanguage?): Promise<Parser | null>
Parameters:
languageName: string- The name of the languagedefaultLanguage?: Language- A fallback language (Optional)
License
MIT License. Developed by Takuya Matsuyama [email protected]
